blob: a0ead0c572683c46722c147314b4d5af1c8899ac [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;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200648 dp->dl_retries = 1;
649 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
650 NFSPROC4_CLNT_CB_RECALL);
Jeff Layton02e12152014-07-16 10:31:57 -0400651 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400653out_dec:
654 atomic_long_dec(&num_delegations);
655 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658void
Trond Myklebust60116952014-07-29 21:34:06 -0400659nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Trond Myklebust11b91642014-07-29 21:34:08 -0400661 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400662 struct nfs4_client *clp = s->sc_client;
663
Jeff Layton4770d722014-07-29 21:34:10 -0400664 might_lock(&clp->cl_lock);
665
Jeff Laytonb401be222014-07-29 21:34:33 -0400666 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
667 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400668 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400669 }
Trond Myklebust60116952014-07-29 21:34:06 -0400670 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400671 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400672 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400673 if (fp)
674 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500677static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400679 struct file *filp = NULL;
680 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -0400681
Jeff Layton6bcc0342014-08-09 10:22:40 -0400682 spin_lock(&fp->fi_lock);
683 if (fp->fi_lease && atomic_dec_and_test(&fp->fi_delegees)) {
684 swap(filp, fp->fi_deleg_file);
685 fl = fp->fi_lease;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500686 fp->fi_lease = NULL;
Jeff Layton6bcc0342014-08-09 10:22:40 -0400687 }
688 spin_unlock(&fp->fi_lock);
689
690 if (filp) {
691 vfs_setlease(filp, F_UNLCK, &fl);
692 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400696static void unhash_stid(struct nfs4_stid *s)
697{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500698 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400699}
700
Benny Halevy931ee562014-05-30 09:09:27 -0400701static void
702hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
703{
Benny Halevycdc97502014-05-30 09:09:30 -0400704 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400705 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400706
Trond Myklebust67cb1272014-07-29 21:34:17 -0400707 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400708 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400709 list_add(&dp->dl_perfile, &fp->fi_delegations);
710 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713static void
Jeff Layton42690672014-07-25 07:34:20 -0400714unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Trond Myklebust11b91642014-07-29 21:34:08 -0400716 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400717
Jeff Layton42690672014-07-25 07:34:20 -0400718 lockdep_assert_held(&state_lock);
719
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400720 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400721 /* Ensure that deleg break won't try to requeue it */
722 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400723 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400724 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400726 list_del_init(&dp->dl_perfile);
727 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400728}
729
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400730static void destroy_delegation(struct nfs4_delegation *dp)
731{
Jeff Layton42690672014-07-25 07:34:20 -0400732 spin_lock(&state_lock);
733 unhash_delegation_locked(dp);
734 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400735 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400736 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400737}
738
739static void revoke_delegation(struct nfs4_delegation *dp)
740{
741 struct nfs4_client *clp = dp->dl_stid.sc_client;
742
Jeff Layton2d4a5322014-07-25 07:34:21 -0400743 WARN_ON(!list_empty(&dp->dl_recall_lru));
744
Jeff Laytonafbda402014-08-09 10:22:41 -0400745 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
746
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400747 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400748 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400749 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400750 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400751 spin_lock(&clp->cl_lock);
752 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
753 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400754 }
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
758 * SETCLIENTID state
759 */
760
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400761static unsigned int clientid_hashval(u32 id)
762{
763 return id & CLIENT_HASH_MASK;
764}
765
766static unsigned int clientstr_hashval(const char *name)
767{
768 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400772 * We store the NONE, READ, WRITE, and BOTH bits separately in the
773 * st_{access,deny}_bmap field of the stateid, in order to track not
774 * only what share bits are currently in force, but also what
775 * combinations of share bits previous opens have used. This allows us
776 * to enforce the recommendation of rfc 3530 14.2.19 that the server
777 * return an error if the client attempt to downgrade to a combination
778 * of share bits not explicable by closing some of its previous opens.
779 *
780 * XXX: This enforcement is actually incomplete, since we don't keep
781 * track of access/deny bit combinations; so, e.g., we allow:
782 *
783 * OPEN allow read, deny write
784 * OPEN allow both, deny none
785 * DOWNGRADE allow read, deny none
786 *
787 * which we should reject.
788 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400789static unsigned int
790bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400791 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400792 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400793
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400794 for (i = 1; i < 4; i++) {
795 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400796 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400797 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400798 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400799}
800
Jeff Layton82c5ff12012-05-11 09:45:13 -0400801/* set share access for a given stateid */
802static inline void
803set_access(u32 access, struct nfs4_ol_stateid *stp)
804{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400805 unsigned char mask = 1 << access;
806
807 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
808 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400809}
810
811/* clear share access for a given stateid */
812static inline void
813clear_access(u32 access, struct nfs4_ol_stateid *stp)
814{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400815 unsigned char mask = 1 << access;
816
817 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
818 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400819}
820
821/* test whether a given stateid has access */
822static inline bool
823test_access(u32 access, struct nfs4_ol_stateid *stp)
824{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400825 unsigned char mask = 1 << access;
826
827 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400828}
829
Jeff Laytonce0fc432012-05-11 09:45:14 -0400830/* set share deny for a given stateid */
831static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400832set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400833{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400834 unsigned char mask = 1 << deny;
835
836 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
837 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400838}
839
840/* clear share deny for a given stateid */
841static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400842clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400843{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400844 unsigned char mask = 1 << deny;
845
846 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
847 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400848}
849
850/* test whether a given stateid is denying specific access */
851static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400852test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400853{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400854 unsigned char mask = 1 << deny;
855
856 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400857}
858
859static int nfs4_access_to_omode(u32 access)
860{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400861 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400862 case NFS4_SHARE_ACCESS_READ:
863 return O_RDONLY;
864 case NFS4_SHARE_ACCESS_WRITE:
865 return O_WRONLY;
866 case NFS4_SHARE_ACCESS_BOTH:
867 return O_RDWR;
868 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500869 WARN_ON_ONCE(1);
870 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400871}
872
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400873/*
874 * A stateid that had a deny mode associated with it is being released
875 * or downgraded. Recalculate the deny mode on the file.
876 */
877static void
878recalculate_deny_mode(struct nfs4_file *fp)
879{
880 struct nfs4_ol_stateid *stp;
881
882 spin_lock(&fp->fi_lock);
883 fp->fi_share_deny = 0;
884 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
885 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
886 spin_unlock(&fp->fi_lock);
887}
888
889static void
890reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
891{
892 int i;
893 bool change = false;
894
895 for (i = 1; i < 4; i++) {
896 if ((i & deny) != i) {
897 change = true;
898 clear_deny(i, stp);
899 }
900 }
901
902 /* Recalculate per-file deny mode if there was a change */
903 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400904 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400905}
906
Jeff Layton82c5ff12012-05-11 09:45:13 -0400907/* release all access and file references for a given stateid */
908static void
909release_all_access(struct nfs4_ol_stateid *stp)
910{
911 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400912 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400913
914 if (fp && stp->st_deny_bmap != 0)
915 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400916
917 for (i = 1; i < 4; i++) {
918 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400919 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400920 clear_access(i, stp);
921 }
922}
923
Jeff Layton6b180f02014-07-29 21:34:26 -0400924static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
925{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400926 struct nfs4_client *clp = sop->so_client;
927
928 might_lock(&clp->cl_lock);
929
930 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400931 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400932 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400933 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400934 kfree(sop->so_owner.data);
935 sop->so_ops->so_free(sop);
936}
937
Jeff Layton4ae098d2014-07-29 21:34:43 -0400938static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500939{
Trond Myklebust11b91642014-07-29 21:34:08 -0400940 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400941
Jeff Layton1c755dc2014-07-29 21:34:12 -0400942 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
943
Trond Myklebust1d31a252014-07-10 14:07:25 -0400944 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500945 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400946 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500947 list_del(&stp->st_perstateowner);
948}
949
Trond Myklebust60116952014-07-29 21:34:06 -0400950static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500951{
Trond Myklebust60116952014-07-29 21:34:06 -0400952 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400953
Trond Myklebust60116952014-07-29 21:34:06 -0400954 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400955 if (stp->st_stateowner)
956 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400957 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500958}
959
Jeff Laytonb49e0842014-07-29 21:34:11 -0400960static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500961{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400962 struct nfs4_ol_stateid *stp = openlockstateid(stid);
963 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500964 struct file *file;
965
Jeff Laytonb49e0842014-07-29 21:34:11 -0400966 file = find_any_file(stp->st_stid.sc_file);
967 if (file)
968 filp_close(file, (fl_owner_t)lo);
969 nfs4_free_ol_stateid(stid);
970}
971
Jeff Layton2c41beb2014-07-29 21:34:41 -0400972/*
973 * Put the persistent reference to an already unhashed generic stateid, while
974 * holding the cl_lock. If it's the last reference, then put it onto the
975 * reaplist for later destruction.
976 */
977static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
978 struct list_head *reaplist)
979{
980 struct nfs4_stid *s = &stp->st_stid;
981 struct nfs4_client *clp = s->sc_client;
982
983 lockdep_assert_held(&clp->cl_lock);
984
985 WARN_ON_ONCE(!list_empty(&stp->st_locks));
986
987 if (!atomic_dec_and_test(&s->sc_count)) {
988 wake_up_all(&close_wq);
989 return;
990 }
991
992 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
993 list_add(&stp->st_locks, reaplist);
994}
995
Jeff Layton3c1c9952014-07-29 21:34:39 -0400996static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
997{
998 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
999
1000 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1001
1002 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001003 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001004 unhash_stid(&stp->st_stid);
1005}
1006
Jeff Layton5adfd882014-07-29 21:34:31 -04001007static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001008{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001009 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1010
1011 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001012 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001013 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001014 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001015}
1016
Trond Myklebustc58c6612014-07-29 21:34:35 -04001017static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001018{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001019 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001020
Trond Myklebustd4f04892014-07-29 21:34:36 -04001021 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001022
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001023 list_del_init(&lo->lo_owner.so_strhash);
1024}
1025
Jeff Layton2c41beb2014-07-29 21:34:41 -04001026/*
1027 * Free a list of generic stateids that were collected earlier after being
1028 * fully unhashed.
1029 */
1030static void
1031free_ol_stateid_reaplist(struct list_head *reaplist)
1032{
1033 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001034 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001035
1036 might_sleep();
1037
1038 while (!list_empty(reaplist)) {
1039 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1040 st_locks);
1041 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001042 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001043 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001044 if (fp)
1045 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001046 }
1047}
1048
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001049static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001050{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001051 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001052 struct nfs4_ol_stateid *stp;
1053 struct list_head reaplist;
1054
1055 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001056
Trond Myklebustd4f04892014-07-29 21:34:36 -04001057 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001058 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001059 while (!list_empty(&lo->lo_owner.so_stateids)) {
1060 stp = list_first_entry(&lo->lo_owner.so_stateids,
1061 struct nfs4_ol_stateid, st_perstateowner);
1062 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001063 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001064 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001065 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001066 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001067 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001068}
1069
Jeff Laytond83017f2014-07-29 21:34:42 -04001070static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1071 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001072{
1073 struct nfs4_ol_stateid *stp;
1074
1075 while (!list_empty(&open_stp->st_locks)) {
1076 stp = list_entry(open_stp->st_locks.next,
1077 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001078 unhash_lock_stateid(stp);
1079 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001080 }
1081}
1082
Jeff Laytond83017f2014-07-29 21:34:42 -04001083static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1084 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001085{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001086 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1087
Jeff Layton4ae098d2014-07-29 21:34:43 -04001088 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001089 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001090}
1091
1092static void release_open_stateid(struct nfs4_ol_stateid *stp)
1093{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001094 LIST_HEAD(reaplist);
1095
1096 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001097 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001098 put_ol_stateid_locked(stp, &reaplist);
1099 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1100 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001101}
1102
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001103static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001104{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001105 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001106
Trond Myklebustd4f04892014-07-29 21:34:36 -04001107 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001108
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001109 list_del_init(&oo->oo_owner.so_strhash);
1110 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001111}
1112
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001113static void release_last_closed_stateid(struct nfs4_openowner *oo)
1114{
Jeff Layton217526e2014-07-30 08:27:11 -04001115 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1116 nfsd_net_id);
1117 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001118
Jeff Layton217526e2014-07-30 08:27:11 -04001119 spin_lock(&nn->client_lock);
1120 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001121 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001122 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001123 oo->oo_last_closed_stid = NULL;
1124 }
Jeff Layton217526e2014-07-30 08:27:11 -04001125 spin_unlock(&nn->client_lock);
1126 if (s)
1127 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001128}
1129
Jeff Layton2c41beb2014-07-29 21:34:41 -04001130static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001131{
1132 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001133 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001134 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001135
Jeff Layton2c41beb2014-07-29 21:34:41 -04001136 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001137
Trond Myklebustd4f04892014-07-29 21:34:36 -04001138 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001139 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001140 while (!list_empty(&oo->oo_owner.so_stateids)) {
1141 stp = list_first_entry(&oo->oo_owner.so_stateids,
1142 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001143 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001144 put_ol_stateid_locked(stp, &reaplist);
1145 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001146 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001147 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001148 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001149 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001150}
1151
Marc Eshel5282fd72009-04-03 08:27:52 +03001152static inline int
1153hash_sessionid(struct nfs4_sessionid *sessionid)
1154{
1155 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1156
1157 return sid->sequence % SESSION_HASH_SIZE;
1158}
1159
Trond Myklebust8f199b82012-03-20 15:11:17 -04001160#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001161static inline void
1162dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1163{
1164 u32 *ptr = (u32 *)(&sessionid->data[0]);
1165 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1166}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001167#else
1168static inline void
1169dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1170{
1171}
1172#endif
1173
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001174/*
1175 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1176 * won't be used for replay.
1177 */
1178void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1179{
1180 struct nfs4_stateowner *so = cstate->replay_owner;
1181
1182 if (nfserr == nfserr_replay_me)
1183 return;
1184
1185 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001186 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001187 return;
1188 }
1189 if (!so)
1190 return;
1191 if (so->so_is_open_owner)
1192 release_last_closed_stateid(openowner(so));
1193 so->so_seqid++;
1194 return;
1195}
Marc Eshel5282fd72009-04-03 08:27:52 +03001196
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001197static void
1198gen_sessionid(struct nfsd4_session *ses)
1199{
1200 struct nfs4_client *clp = ses->se_client;
1201 struct nfsd4_sessionid *sid;
1202
1203 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1204 sid->clientid = clp->cl_clientid;
1205 sid->sequence = current_sessionid++;
1206 sid->reserved = 0;
1207}
1208
1209/*
Andy Adamsona6496372009-08-28 08:45:01 -04001210 * The protocol defines ca_maxresponssize_cached to include the size of
1211 * the rpc header, but all we need to cache is the data starting after
1212 * the end of the initial SEQUENCE operation--the rest we regenerate
1213 * each time. Therefore we can advertise a ca_maxresponssize_cached
1214 * value that is the number of bytes in our cache plus a few additional
1215 * bytes. In order to stay on the safe side, and not promise more than
1216 * we can cache, those additional bytes must be the minimum possible: 24
1217 * bytes of rpc header (xid through accept state, with AUTH_NULL
1218 * verifier), 12 for the compound header (with zero-length tag), and 44
1219 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001220 */
Andy Adamsona6496372009-08-28 08:45:01 -04001221#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1222
Andy Adamson557ce262009-08-28 08:45:04 -04001223static void
1224free_session_slots(struct nfsd4_session *ses)
1225{
1226 int i;
1227
1228 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1229 kfree(ses->se_slots[i]);
1230}
1231
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001232/*
1233 * We don't actually need to cache the rpc and session headers, so we
1234 * can allocate a little less for each slot:
1235 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001236static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001237{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001238 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001239
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001240 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1241 size = 0;
1242 else
1243 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1244 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001245}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001246
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001247/*
1248 * XXX: If we run out of reserved DRC memory we could (up to a point)
1249 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001250 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001252static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001253{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001254 u32 slotsize = slot_bytes(ca);
1255 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001256 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001257
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001258 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001259 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1260 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001261 num = min_t(int, num, avail / slotsize);
1262 nfsd_drc_mem_used += num * slotsize;
1263 spin_unlock(&nfsd_drc_lock);
1264
1265 return num;
1266}
1267
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001268static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001269{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001270 int slotsize = slot_bytes(ca);
1271
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001272 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001273 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001274 spin_unlock(&nfsd_drc_lock);
1275}
1276
Kinglong Mee60810e52014-01-01 00:35:47 +08001277static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1278 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001279{
Kinglong Mee60810e52014-01-01 00:35:47 +08001280 int numslots = fattrs->maxreqs;
1281 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001282 struct nfsd4_session *new;
1283 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001284
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001285 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001286 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1287 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001288
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001289 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001290 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001291 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001292 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001293 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001294 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001295 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001296 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001297 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001298
1299 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1300 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1301
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001302 return new;
1303out_free:
1304 while (i--)
1305 kfree(new->se_slots[i]);
1306 kfree(new);
1307 return NULL;
1308}
1309
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001310static void free_conn(struct nfsd4_conn *c)
1311{
1312 svc_xprt_put(c->cn_xprt);
1313 kfree(c);
1314}
1315
1316static void nfsd4_conn_lost(struct svc_xpt_user *u)
1317{
1318 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1319 struct nfs4_client *clp = c->cn_session->se_client;
1320
1321 spin_lock(&clp->cl_lock);
1322 if (!list_empty(&c->cn_persession)) {
1323 list_del(&c->cn_persession);
1324 free_conn(c);
1325 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001326 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001327 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001328}
1329
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001330static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001331{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001332 struct nfsd4_conn *conn;
1333
1334 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1335 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001336 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001337 svc_xprt_get(rqstp->rq_xprt);
1338 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001339 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001340 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1341 return conn;
1342}
1343
J. Bruce Fields328ead22010-09-29 16:11:06 -04001344static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1345{
1346 conn->cn_session = ses;
1347 list_add(&conn->cn_persession, &ses->se_conns);
1348}
1349
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001350static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1351{
1352 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001353
1354 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001355 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001356 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001357}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001358
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001359static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001360{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001361 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001362 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001363}
1364
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001365static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001366{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001367 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001368
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001369 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001370 ret = nfsd4_register_conn(conn);
1371 if (ret)
1372 /* oops; xprt is already down: */
1373 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001374 /* We may have gained or lost a callback channel: */
1375 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001376}
1377
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001378static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001379{
1380 u32 dir = NFS4_CDFC4_FORE;
1381
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001382 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001383 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001384 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001385}
1386
1387/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001388static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001389{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001390 struct nfs4_client *clp = s->se_client;
1391 struct nfsd4_conn *c;
1392
1393 spin_lock(&clp->cl_lock);
1394 while (!list_empty(&s->se_conns)) {
1395 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1396 list_del_init(&c->cn_persession);
1397 spin_unlock(&clp->cl_lock);
1398
1399 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1400 free_conn(c);
1401
1402 spin_lock(&clp->cl_lock);
1403 }
1404 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001405}
1406
J. Bruce Fields1377b692012-09-11 21:42:40 -04001407static void __free_session(struct nfsd4_session *ses)
1408{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001409 free_session_slots(ses);
1410 kfree(ses);
1411}
1412
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001413static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001414{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001415 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001416 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001417 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001418}
Andy Adamson557ce262009-08-28 08:45:04 -04001419
Fengguang Wu135ae822012-11-10 07:20:25 -05001420static 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 -04001421{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001422 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001423 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001424
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001425 new->se_client = clp;
1426 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001427
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001428 INIT_LIST_HEAD(&new->se_conns);
1429
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001430 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001431 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001432 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001433 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001434 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001435 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001436 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001437 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001438 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001439 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001440
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001441 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001442 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001443 /*
1444 * This is a little silly; with sessions there's no real
1445 * use for the callback address. Use the peer address
1446 * as a reasonable default for now, but consider fixing
1447 * the rpc client not to require an address in the
1448 * future:
1449 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001450 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1451 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001452 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001453}
1454
Benny Halevy9089f1b2010-05-12 00:12:26 +03001455/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001456static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001457__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001458{
1459 struct nfsd4_session *elem;
1460 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001461 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001462
Trond Myklebust0a880a22014-07-30 08:27:10 -04001463 lockdep_assert_held(&nn->client_lock);
1464
Marc Eshel5282fd72009-04-03 08:27:52 +03001465 dump_sessionid(__func__, sessionid);
1466 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001467 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001468 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001469 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1470 NFS4_MAX_SESSIONID_LEN)) {
1471 return elem;
1472 }
1473 }
1474
1475 dprintk("%s: session not found\n", __func__);
1476 return NULL;
1477}
1478
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001479static struct nfsd4_session *
1480find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1481 __be32 *ret)
1482{
1483 struct nfsd4_session *session;
1484 __be32 status = nfserr_badsession;
1485
1486 session = __find_in_sessionid_hashtbl(sessionid, net);
1487 if (!session)
1488 goto out;
1489 status = nfsd4_get_session_locked(session);
1490 if (status)
1491 session = NULL;
1492out:
1493 *ret = status;
1494 return session;
1495}
1496
Benny Halevy9089f1b2010-05-12 00:12:26 +03001497/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001498static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001499unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001500{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001501 struct nfs4_client *clp = ses->se_client;
1502 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1503
1504 lockdep_assert_held(&nn->client_lock);
1505
Andy Adamson7116ed62009-04-03 08:27:43 +03001506 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001507 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001508 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001509 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1513static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001514STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001516 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001518 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001519 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return 1;
1521}
1522
1523/*
1524 * XXX Should we use a slab cache ?
1525 * This type of memory management is somewhat inefficient, but we use it
1526 * anyway since SETCLIENTID is not a common operation.
1527 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001528static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001531 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001533 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1534 if (clp == NULL)
1535 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001536 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001537 if (clp->cl_name.data == NULL)
1538 goto err_no_name;
1539 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1540 OWNER_HASH_SIZE, GFP_KERNEL);
1541 if (!clp->cl_ownerstr_hashtbl)
1542 goto err_no_hashtbl;
1543 for (i = 0; i < OWNER_HASH_SIZE; i++)
1544 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001545 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001546 INIT_LIST_HEAD(&clp->cl_sessions);
1547 idr_init(&clp->cl_stateids);
1548 atomic_set(&clp->cl_refcount, 0);
1549 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1550 INIT_LIST_HEAD(&clp->cl_idhash);
1551 INIT_LIST_HEAD(&clp->cl_openowners);
1552 INIT_LIST_HEAD(&clp->cl_delegations);
1553 INIT_LIST_HEAD(&clp->cl_lru);
1554 INIT_LIST_HEAD(&clp->cl_callbacks);
1555 INIT_LIST_HEAD(&clp->cl_revoked);
1556 spin_lock_init(&clp->cl_lock);
1557 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001559err_no_hashtbl:
1560 kfree(clp->cl_name.data);
1561err_no_name:
1562 kfree(clp);
1563 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001566static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567free_client(struct nfs4_client *clp)
1568{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001569 while (!list_empty(&clp->cl_sessions)) {
1570 struct nfsd4_session *ses;
1571 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1572 se_perclnt);
1573 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001574 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1575 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001576 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001577 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001578 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001579 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001581 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 kfree(clp);
1583}
1584
Benny Halevy84d38ac2010-05-12 00:13:16 +03001585/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001586static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001587unhash_client_locked(struct nfs4_client *clp)
1588{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001589 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001590 struct nfsd4_session *ses;
1591
Trond Myklebust0a880a22014-07-30 08:27:10 -04001592 lockdep_assert_held(&nn->client_lock);
1593
Trond Myklebust4beb3452014-07-30 08:27:02 -04001594 /* Mark the client as expired! */
1595 clp->cl_time = 0;
1596 /* Make it invisible */
1597 if (!list_empty(&clp->cl_idhash)) {
1598 list_del_init(&clp->cl_idhash);
1599 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1600 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1601 else
1602 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1603 }
1604 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001605 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001606 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1607 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001608 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001609}
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001612unhash_client(struct nfs4_client *clp)
1613{
1614 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1615
1616 spin_lock(&nn->client_lock);
1617 unhash_client_locked(clp);
1618 spin_unlock(&nn->client_lock);
1619}
1620
Jeff Layton97403d92014-07-30 08:27:12 -04001621static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1622{
1623 if (atomic_read(&clp->cl_refcount))
1624 return nfserr_jukebox;
1625 unhash_client_locked(clp);
1626 return nfs_ok;
1627}
1628
Trond Myklebust4beb3452014-07-30 08:27:02 -04001629static void
1630__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001632 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct list_head reaplist;
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001637 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001638 while (!list_empty(&clp->cl_delegations)) {
1639 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001640 unhash_delegation_locked(dp);
1641 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Benny Halevycdc97502014-05-30 09:09:30 -04001643 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 while (!list_empty(&reaplist)) {
1645 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001646 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001647 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001648 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001650 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001651 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001652 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001653 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001654 }
NeilBrownea1da632005-06-23 22:04:17 -07001655 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001656 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Jeff Laytond3134b12014-07-29 21:34:32 -04001657 atomic_inc(&oo->oo_owner.so_count);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001658 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001660 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001661 if (clp->cl_cb_conn.cb_xprt)
1662 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001663 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Trond Myklebust4beb3452014-07-30 08:27:02 -04001666static void
1667destroy_client(struct nfs4_client *clp)
1668{
1669 unhash_client(clp);
1670 __destroy_client(clp);
1671}
1672
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001673static void expire_client(struct nfs4_client *clp)
1674{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001675 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001676 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001677 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001678}
1679
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001680static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1681{
1682 memcpy(target->cl_verifier.data, source->data,
1683 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001686static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1687{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1689 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1690}
1691
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001692static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001693{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001694 if (source->cr_principal) {
1695 target->cr_principal =
1696 kstrdup(source->cr_principal, GFP_KERNEL);
1697 if (target->cr_principal == NULL)
1698 return -ENOMEM;
1699 } else
1700 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001701 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 target->cr_uid = source->cr_uid;
1703 target->cr_gid = source->cr_gid;
1704 target->cr_group_info = source->cr_group_info;
1705 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001706 target->cr_gss_mech = source->cr_gss_mech;
1707 if (source->cr_gss_mech)
1708 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001712static long long
1713compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1714{
1715 long long res;
1716
1717 res = o1->len - o2->len;
1718 if (res)
1719 return res;
1720 return (long long)memcmp(o1->data, o2->data, o1->len);
1721}
1722
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001723static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001724{
NeilBrowna55370a2005-06-23 22:03:52 -07001725 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001729same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1730{
1731 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001735same_clid(clientid_t *cl1, clientid_t *cl2)
1736{
1737 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001740static bool groups_equal(struct group_info *g1, struct group_info *g2)
1741{
1742 int i;
1743
1744 if (g1->ngroups != g2->ngroups)
1745 return false;
1746 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001747 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001748 return false;
1749 return true;
1750}
1751
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001752/*
1753 * RFC 3530 language requires clid_inuse be returned when the
1754 * "principal" associated with a requests differs from that previously
1755 * used. We use uid, gid's, and gss principal string as our best
1756 * approximation. We also don't want to allow non-gss use of a client
1757 * established using gss: in theory cr_principal should catch that
1758 * change, but in practice cr_principal can be null even in the gss case
1759 * since gssd doesn't always pass down a principal string.
1760 */
1761static bool is_gss_cred(struct svc_cred *cr)
1762{
1763 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1764 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1765}
1766
1767
Vivek Trivedi5559b502012-07-24 21:18:20 +05301768static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001769same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1770{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001771 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001772 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1773 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001774 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1775 return false;
1776 if (cr1->cr_principal == cr2->cr_principal)
1777 return true;
1778 if (!cr1->cr_principal || !cr2->cr_principal)
1779 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301780 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
J. Bruce Fields57266a62013-04-13 14:27:29 -04001783static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1784{
1785 struct svc_cred *cr = &rqstp->rq_cred;
1786 u32 service;
1787
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001788 if (!cr->cr_gss_mech)
1789 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001790 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1791 return service == RPC_GSS_SVC_INTEGRITY ||
1792 service == RPC_GSS_SVC_PRIVACY;
1793}
1794
1795static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1796{
1797 struct svc_cred *cr = &rqstp->rq_cred;
1798
1799 if (!cl->cl_mach_cred)
1800 return true;
1801 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1802 return false;
1803 if (!svc_rqst_integrity_protected(rqstp))
1804 return false;
1805 if (!cr->cr_principal)
1806 return false;
1807 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1808}
1809
Jeff Layton294ac322014-07-30 08:27:15 -04001810static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001811{
Chuck Leverab4684d2012-03-02 17:13:50 -05001812 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Jeff Laytonf4199922014-06-17 07:44:11 -04001814 /*
1815 * This is opaque to client, so no need to byte-swap. Use
1816 * __force to keep sparse happy
1817 */
1818 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001819 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001820 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Jeff Layton294ac322014-07-30 08:27:15 -04001823static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1824{
1825 clp->cl_clientid.cl_boot = nn->boot_time;
1826 clp->cl_clientid.cl_id = nn->clientid_counter++;
1827 gen_confirm(clp, nn);
1828}
1829
Jeff Layton4770d722014-07-29 21:34:10 -04001830static struct nfs4_stid *
1831find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001832{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001833 struct nfs4_stid *ret;
1834
1835 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1836 if (!ret || !ret->sc_type)
1837 return NULL;
1838 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001839}
1840
Jeff Layton4770d722014-07-29 21:34:10 -04001841static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001842find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001843{
1844 struct nfs4_stid *s;
1845
Jeff Layton4770d722014-07-29 21:34:10 -04001846 spin_lock(&cl->cl_lock);
1847 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001848 if (s != NULL) {
1849 if (typemask & s->sc_type)
1850 atomic_inc(&s->sc_count);
1851 else
1852 s = NULL;
1853 }
Jeff Layton4770d722014-07-29 21:34:10 -04001854 spin_unlock(&cl->cl_lock);
1855 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001856}
1857
Jeff Layton2216d442012-11-12 15:00:57 -05001858static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001859 struct svc_rqst *rqstp, nfs4_verifier *verf)
1860{
1861 struct nfs4_client *clp;
1862 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001863 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001864 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001865
1866 clp = alloc_client(name);
1867 if (clp == NULL)
1868 return NULL;
1869
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001870 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1871 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001872 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001873 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001874 }
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02001875 nfsd4_init_cb(&clp->cl_cb_null, clp, NFSPROC4_CLNT_CB_NULL);
Jeff Layton02e12152014-07-16 10:31:57 -04001876 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001877 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001878 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001879 copy_verf(clp, verf);
1880 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001881 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001882 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001883 return clp;
1884}
1885
NeilBrownfd39ca92005-06-23 22:04:03 -07001886static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001887add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1888{
1889 struct rb_node **new = &(root->rb_node), *parent = NULL;
1890 struct nfs4_client *clp;
1891
1892 while (*new) {
1893 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1894 parent = *new;
1895
1896 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1897 new = &((*new)->rb_left);
1898 else
1899 new = &((*new)->rb_right);
1900 }
1901
1902 rb_link_node(&new_clp->cl_namenode, parent, new);
1903 rb_insert_color(&new_clp->cl_namenode, root);
1904}
1905
1906static struct nfs4_client *
1907find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1908{
1909 long long cmp;
1910 struct rb_node *node = root->rb_node;
1911 struct nfs4_client *clp;
1912
1913 while (node) {
1914 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1915 cmp = compare_blob(&clp->cl_name, name);
1916 if (cmp > 0)
1917 node = node->rb_left;
1918 else if (cmp < 0)
1919 node = node->rb_right;
1920 else
1921 return clp;
1922 }
1923 return NULL;
1924}
1925
1926static void
1927add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001930 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Trond Myklebust0a880a22014-07-30 08:27:10 -04001932 lockdep_assert_held(&nn->client_lock);
1933
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001934 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001935 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001937 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001938 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
NeilBrownfd39ca92005-06-23 22:04:03 -07001941static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942move_to_confirmed(struct nfs4_client *clp)
1943{
1944 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001945 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Trond Myklebust0a880a22014-07-30 08:27:10 -04001947 lockdep_assert_held(&nn->client_lock);
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001950 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001951 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001952 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001953 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001954 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
1957static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001958find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 struct nfs4_client *clp;
1961 unsigned int idhashval = clientid_hashval(clid->cl_id);
1962
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001963 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001964 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001965 if ((bool)clp->cl_minorversion != sessions)
1966 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001967 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 return NULL;
1972}
1973
1974static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001975find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1976{
1977 struct list_head *tbl = nn->conf_id_hashtbl;
1978
Trond Myklebust0a880a22014-07-30 08:27:10 -04001979 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001980 return find_client_in_id_table(tbl, clid, sessions);
1981}
1982
1983static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001984find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001986 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Trond Myklebust0a880a22014-07-30 08:27:10 -04001988 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001989 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001992static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001993{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001994 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001995}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001996
NeilBrown28ce6052005-06-23 22:03:56 -07001997static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001998find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001999{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002000 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002001 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002002}
2003
2004static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002005find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002006{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002007 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002008 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002009}
2010
NeilBrownfd39ca92005-06-23 22:04:03 -07002011static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002012gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002014 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002015 struct sockaddr *sa = svc_addr(rqstp);
2016 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002017 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Jeff Layton7077ecb2009-08-14 12:57:58 -04002019 /* Currently, we only support tcp and tcp6 for the callback channel */
2020 if (se->se_callback_netid_len == 3 &&
2021 !memcmp(se->se_callback_netid_val, "tcp", 3))
2022 expected_family = AF_INET;
2023 else if (se->se_callback_netid_len == 4 &&
2024 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2025 expected_family = AF_INET6;
2026 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto out_err;
2028
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002029 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002030 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002031 (struct sockaddr *)&conn->cb_addr,
2032 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002033
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002036
J. Bruce Fields07263f12010-05-31 19:09:40 -04002037 if (conn->cb_addr.ss_family == AF_INET6)
2038 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002039
J. Bruce Fields07263f12010-05-31 19:09:40 -04002040 conn->cb_prog = se->se_callback_prog;
2041 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002042 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return;
2044out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002045 conn->cb_addr.ss_family = AF_UNSPEC;
2046 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002047 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 "will not receive delegations\n",
2049 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return;
2052}
2053
Andy Adamson074fe892009-04-03 08:28:15 +03002054/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002055 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002056 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002057static void
Andy Adamson074fe892009-04-03 08:28:15 +03002058nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2059{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002060 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002061 struct nfsd4_slot *slot = resp->cstate.slot;
2062 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002063
Andy Adamson557ce262009-08-28 08:45:04 -04002064 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002065
Andy Adamson557ce262009-08-28 08:45:04 -04002066 slot->sl_opcnt = resp->opcnt;
2067 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002068
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002069 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002070 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002071 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002072 return;
2073 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002074 base = resp->cstate.data_offset;
2075 slot->sl_datalen = buf->len - base;
2076 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002077 WARN("%s: sessions DRC could not cache compound\n", __func__);
2078 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002079}
2080
2081/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002082 * Encode the replay sequence operation from the slot values.
2083 * If cachethis is FALSE encode the uncached rep error on the next
2084 * operation which sets resp->p and increments resp->opcnt for
2085 * nfs4svc_encode_compoundres.
2086 *
Andy Adamson074fe892009-04-03 08:28:15 +03002087 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002088static __be32
2089nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2090 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002091{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002092 struct nfsd4_op *op;
2093 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002094
Andy Adamsonabfabf82009-07-23 19:02:18 -04002095 /* Encode the replayed sequence operation */
2096 op = &args->ops[resp->opcnt - 1];
2097 nfsd4_encode_operation(resp, op);
2098
2099 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002100 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002101 op = &args->ops[resp->opcnt++];
2102 op->status = nfserr_retry_uncached_rep;
2103 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002104 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002105 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002106}
2107
2108/*
Andy Adamson557ce262009-08-28 08:45:04 -04002109 * The sequence operation is not cached because we can use the slot and
2110 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002111 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002112static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002113nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2114 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002115{
Andy Adamson557ce262009-08-28 08:45:04 -04002116 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002117 struct xdr_stream *xdr = &resp->xdr;
2118 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002119 __be32 status;
2120
Andy Adamson557ce262009-08-28 08:45:04 -04002121 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002122
Andy Adamsonabfabf82009-07-23 19:02:18 -04002123 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002124 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002125 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002126
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002127 p = xdr_reserve_space(xdr, slot->sl_datalen);
2128 if (!p) {
2129 WARN_ON_ONCE(1);
2130 return nfserr_serverfault;
2131 }
2132 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2133 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002134
Andy Adamson557ce262009-08-28 08:45:04 -04002135 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002136 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002137}
2138
Andy Adamson0733d212009-04-03 08:28:01 +03002139/*
2140 * Set the exchange_id flags returned by the server.
2141 */
2142static void
2143nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2144{
2145 /* pNFS is not supported */
2146 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2147
2148 /* Referrals are supported, Migration is not. */
2149 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2150
2151 /* set the wire flags to return to client. */
2152 clid->flags = new->cl_exchange_flags;
2153}
2154
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002155static bool client_has_state(struct nfs4_client *clp)
2156{
2157 /*
2158 * Note clp->cl_openowners check isn't quite right: there's no
2159 * need to count owners without stateid's.
2160 *
2161 * Also note we should probably be using this in 4.0 case too.
2162 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002163 return !list_empty(&clp->cl_openowners)
2164 || !list_empty(&clp->cl_delegations)
2165 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002166}
2167
Al Virob37ad282006-10-19 23:28:59 -07002168__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002169nfsd4_exchange_id(struct svc_rqst *rqstp,
2170 struct nfsd4_compound_state *cstate,
2171 struct nfsd4_exchange_id *exid)
2172{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002173 struct nfs4_client *conf, *new;
2174 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002175 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002176 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002177 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002178 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002179 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002180 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002181
Jeff Layton363168b2009-08-14 12:57:56 -04002182 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002183 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002184 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002185 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002186 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002187
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002188 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002189 return nfserr_inval;
2190
Andy Adamson0733d212009-04-03 08:28:01 +03002191 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002192 case SP4_MACH_CRED:
2193 if (!svc_rqst_integrity_protected(rqstp))
2194 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002195 case SP4_NONE:
2196 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002197 default: /* checked by xdr code */
2198 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002199 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002200 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002201 }
2202
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002203 new = create_client(exid->clname, rqstp, &verf);
2204 if (new == NULL)
2205 return nfserr_jukebox;
2206
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002207 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002208 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002209 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002210 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002211 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2212 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2213
J. Bruce Fields136e6582012-05-12 20:37:23 -04002214 if (update) {
2215 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002216 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002217 goto out;
2218 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002219 if (!mach_creds_match(conf, rqstp)) {
2220 status = nfserr_wrong_cred;
2221 goto out;
2222 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002223 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002224 status = nfserr_perm;
2225 goto out;
2226 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002227 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002228 status = nfserr_not_same;
2229 goto out;
2230 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002231 /* case 6 */
2232 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002233 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002234 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002235 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002236 if (client_has_state(conf)) {
2237 status = nfserr_clid_inuse;
2238 goto out;
2239 }
Andy Adamson0733d212009-04-03 08:28:01 +03002240 goto out_new;
2241 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002242 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002243 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002244 goto out_copy;
2245 }
2246 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002247 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002248 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002249 }
2250
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002251 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002252 status = nfserr_noent;
2253 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002254 }
2255
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002256 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002257 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002258 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002259
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002260 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002261out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002262 if (conf) {
2263 status = mark_client_expired_locked(conf);
2264 if (status)
2265 goto out;
2266 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002267 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002268 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002269
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002270 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002271 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002272 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002273out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002274 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2275 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002276
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002277 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2278 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002279
2280 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002281 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002282 status = nfs_ok;
2283
2284out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002285 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002286 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002287 expire_client(new);
2288 if (unconf)
2289 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002290 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002291}
2292
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002293static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002294check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002295{
Andy Adamson88e588d2009-07-23 19:02:15 -04002296 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2297 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002298
2299 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002300 if (slot_inuse) {
2301 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002302 return nfserr_jukebox;
2303 else
2304 return nfserr_seq_misordered;
2305 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002306 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002307 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002308 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002309 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002310 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002311 return nfserr_seq_misordered;
2312}
2313
Andy Adamson49557cc2009-07-23 19:02:16 -04002314/*
2315 * Cache the create session result into the create session single DRC
2316 * slot cache by saving the xdr structure. sl_seqid has been set.
2317 * Do this for solo or embedded create session operations.
2318 */
2319static void
2320nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002321 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002322{
2323 slot->sl_status = nfserr;
2324 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2325}
2326
2327static __be32
2328nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2329 struct nfsd4_clid_slot *slot)
2330{
2331 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2332 return slot->sl_status;
2333}
2334
Mi Jinlong1b74c252011-07-14 14:50:17 +08002335#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2336 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2337 1 + /* MIN tag is length with zero, only length */ \
2338 3 + /* version, opcount, opcode */ \
2339 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2340 /* seqid, slotID, slotID, cache */ \
2341 4 ) * sizeof(__be32))
2342
2343#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2344 2 + /* verifier: AUTH_NULL, length 0 */\
2345 1 + /* status */ \
2346 1 + /* MIN tag is length with zero, only length */ \
2347 3 + /* opcount, opcode, opstatus*/ \
2348 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2349 /* seqid, slotID, slotID, slotID, status */ \
2350 5 ) * sizeof(__be32))
2351
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002352static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002353{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002354 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2355
J. Bruce Fields373cd402013-04-08 15:42:12 -04002356 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2357 return nfserr_toosmall;
2358 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2359 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002360 ca->headerpadsz = 0;
2361 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2362 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2363 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2364 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2365 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2366 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2367 /*
2368 * Note decreasing slot size below client's request may make it
2369 * difficult for client to function correctly, whereas
2370 * decreasing the number of slots will (just?) affect
2371 * performance. When short on memory we therefore prefer to
2372 * decrease number of slots instead of their size. Clients that
2373 * request larger slots than they need will get poor results:
2374 */
2375 ca->maxreqs = nfsd4_get_drc_mem(ca);
2376 if (!ca->maxreqs)
2377 return nfserr_jukebox;
2378
J. Bruce Fields373cd402013-04-08 15:42:12 -04002379 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002380}
2381
Kinglong Mee8a891632013-12-23 18:11:02 +08002382#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2383 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2384#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2385 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2386
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002387static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2388{
2389 ca->headerpadsz = 0;
2390
2391 /*
2392 * These RPC_MAX_HEADER macros are overkill, especially since we
2393 * don't even do gss on the backchannel yet. But this is still
2394 * less than 1k. Tighten up this estimate in the unlikely event
2395 * it turns out to be a problem for some client:
2396 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002397 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002398 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002399 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002400 return nfserr_toosmall;
2401 ca->maxresp_cached = 0;
2402 if (ca->maxops < 2)
2403 return nfserr_toosmall;
2404
2405 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002406}
2407
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002408static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2409{
2410 switch (cbs->flavor) {
2411 case RPC_AUTH_NULL:
2412 case RPC_AUTH_UNIX:
2413 return nfs_ok;
2414 default:
2415 /*
2416 * GSS case: the spec doesn't allow us to return this
2417 * error. But it also doesn't allow us not to support
2418 * GSS.
2419 * I'd rather this fail hard than return some error the
2420 * client might think it can already handle:
2421 */
2422 return nfserr_encr_alg_unsupp;
2423 }
2424}
2425
Andy Adamson069b6ad2009-04-03 08:27:58 +03002426__be32
2427nfsd4_create_session(struct svc_rqst *rqstp,
2428 struct nfsd4_compound_state *cstate,
2429 struct nfsd4_create_session *cr_ses)
2430{
Jeff Layton363168b2009-08-14 12:57:56 -04002431 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002432 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002433 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002434 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002435 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002436 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002437 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002438 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002439
Mi Jinlonga62573d2011-03-23 17:57:07 +08002440 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2441 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002442 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2443 if (status)
2444 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002445 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002446 if (status)
2447 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002448 status = check_backchannel_attrs(&cr_ses->back_channel);
2449 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002450 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002451 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002452 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002453 if (!new)
2454 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002455 conn = alloc_conn_from_crses(rqstp, cr_ses);
2456 if (!conn)
2457 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002458
Jeff Laytond20c11d2014-07-30 08:27:07 -04002459 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002460 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002461 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002462 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002463
2464 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002465 status = nfserr_wrong_cred;
2466 if (!mach_creds_match(conf, rqstp))
2467 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002468 cs_slot = &conf->cl_cs_slot;
2469 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002470 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002471 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002472 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002473 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002474 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002475 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002476 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 } else if (unconf) {
2478 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002479 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002480 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002481 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002482 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002483 status = nfserr_wrong_cred;
2484 if (!mach_creds_match(unconf, rqstp))
2485 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002486 cs_slot = &unconf->cl_cs_slot;
2487 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002488 if (status) {
2489 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002490 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002491 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002492 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002493 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002494 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002495 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002496 if (status) {
2497 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002498 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002499 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002500 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002501 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002502 conf = unconf;
2503 } else {
2504 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002505 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002506 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002507 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002508 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002509 * We do not support RDMA or persistent sessions
2510 */
2511 cr_ses->flags &= ~SESSION4_PERSIST;
2512 cr_ses->flags &= ~SESSION4_RDMA;
2513
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002514 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002515 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002516
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002517 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002518 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002519 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002520 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002521
Jeff Laytond20c11d2014-07-30 08:27:07 -04002522 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002523 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002524 spin_unlock(&nn->client_lock);
2525 /* init connection and backchannel */
2526 nfsd4_init_conn(rqstp, conn, new);
2527 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002528 if (old)
2529 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002530 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002531out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002532 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002533 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002534 if (old)
2535 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002536out_free_session:
2537 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002538out_release_drc_mem:
2539 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002540 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002541}
2542
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002543static __be32 nfsd4_map_bcts_dir(u32 *dir)
2544{
2545 switch (*dir) {
2546 case NFS4_CDFC4_FORE:
2547 case NFS4_CDFC4_BACK:
2548 return nfs_ok;
2549 case NFS4_CDFC4_FORE_OR_BOTH:
2550 case NFS4_CDFC4_BACK_OR_BOTH:
2551 *dir = NFS4_CDFC4_BOTH;
2552 return nfs_ok;
2553 };
2554 return nfserr_inval;
2555}
2556
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002557__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2558{
2559 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002560 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002561 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002562
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002563 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2564 if (status)
2565 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002566 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002567 session->se_cb_prog = bc->bc_cb_program;
2568 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002569 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002570
2571 nfsd4_probe_callback(session->se_client);
2572
2573 return nfs_ok;
2574}
2575
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002576__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2577 struct nfsd4_compound_state *cstate,
2578 struct nfsd4_bind_conn_to_session *bcts)
2579{
2580 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002581 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002582 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002583 struct net *net = SVC_NET(rqstp);
2584 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002585
2586 if (!nfsd4_last_compound_op(rqstp))
2587 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002588 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002589 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002590 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002591 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002592 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002593 status = nfserr_wrong_cred;
2594 if (!mach_creds_match(session->se_client, rqstp))
2595 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002596 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002597 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002598 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002599 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002600 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002601 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002602 goto out;
2603 nfsd4_init_conn(rqstp, conn, session);
2604 status = nfs_ok;
2605out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002606 nfsd4_put_session(session);
2607out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002608 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002609}
2610
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002611static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2612{
2613 if (!session)
2614 return 0;
2615 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2616}
2617
Andy Adamson069b6ad2009-04-03 08:27:58 +03002618__be32
2619nfsd4_destroy_session(struct svc_rqst *r,
2620 struct nfsd4_compound_state *cstate,
2621 struct nfsd4_destroy_session *sessionid)
2622{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002623 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002624 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002625 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002626 struct net *net = SVC_NET(r);
2627 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002628
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002629 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002630 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002631 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002632 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002633 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002634 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002635 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002636 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002637 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002638 if (!ses)
2639 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002640 status = nfserr_wrong_cred;
2641 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002642 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002643 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002644 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002645 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002646 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002647 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002648
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002649 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002650
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002651 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002652 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002653out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002654 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002655out_client_lock:
2656 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002657out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002658 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002659}
2660
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002661static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002662{
2663 struct nfsd4_conn *c;
2664
2665 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002666 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002667 return c;
2668 }
2669 }
2670 return NULL;
2671}
2672
J. Bruce Fields57266a62013-04-13 14:27:29 -04002673static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002674{
2675 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002676 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002677 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002678 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002679
2680 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002681 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002682 if (c)
2683 goto out_free;
2684 status = nfserr_conn_not_bound_to_session;
2685 if (clp->cl_mach_cred)
2686 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002687 __nfsd4_hash_conn(new, ses);
2688 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002689 ret = nfsd4_register_conn(new);
2690 if (ret)
2691 /* oops; xprt is already down: */
2692 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002693 return nfs_ok;
2694out_free:
2695 spin_unlock(&clp->cl_lock);
2696 free_conn(new);
2697 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002698}
2699
Mi Jinlong868b89c2011-04-27 09:09:58 +08002700static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2701{
2702 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2703
2704 return args->opcnt > session->se_fchannel.maxops;
2705}
2706
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002707static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2708 struct nfsd4_session *session)
2709{
2710 struct xdr_buf *xb = &rqstp->rq_arg;
2711
2712 return xb->len > session->se_fchannel.maxreq_sz;
2713}
2714
Andy Adamson069b6ad2009-04-03 08:27:58 +03002715__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002716nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002717 struct nfsd4_compound_state *cstate,
2718 struct nfsd4_sequence *seq)
2719{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002720 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002721 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002722 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002723 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002724 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002725 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002726 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002727 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002728 struct net *net = SVC_NET(rqstp);
2729 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002730
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002731 if (resp->opcnt != 1)
2732 return nfserr_sequence_pos;
2733
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002734 /*
2735 * Will be either used or freed by nfsd4_sequence_check_conn
2736 * below.
2737 */
2738 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2739 if (!conn)
2740 return nfserr_jukebox;
2741
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002742 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002743 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002744 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002745 goto out_no_session;
2746 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002747
Mi Jinlong868b89c2011-04-27 09:09:58 +08002748 status = nfserr_too_many_ops;
2749 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002750 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002751
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002752 status = nfserr_req_too_big;
2753 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002754 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002755
Benny Halevyb85d4c02009-04-03 08:28:08 +03002756 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002757 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002758 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002759
Andy Adamson557ce262009-08-28 08:45:04 -04002760 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002761 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2762
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002763 /* We do not negotiate the number of slots yet, so set the
2764 * maxslots to the session maxreqs which is used to encode
2765 * sr_highest_slotid and the sr_target_slot id to maxslots */
2766 seq->maxslots = session->se_fchannel.maxreqs;
2767
J. Bruce Fields73e79482012-02-13 16:39:00 -05002768 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2769 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002770 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002771 status = nfserr_seq_misordered;
2772 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002773 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002774 cstate->slot = slot;
2775 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002776 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002777 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002778 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002779 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002780 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002781 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002782 }
2783 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002784 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002785
J. Bruce Fields57266a62013-04-13 14:27:29 -04002786 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002787 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002788 if (status)
2789 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002790
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002791 buflen = (seq->cachethis) ?
2792 session->se_fchannel.maxresp_cached :
2793 session->se_fchannel.maxresp_sz;
2794 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2795 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002796 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002797 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002798 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002799
2800 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002801 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002802 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002803 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002804 if (seq->cachethis)
2805 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002806 else
2807 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002808
2809 cstate->slot = slot;
2810 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002811 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002812
Benny Halevyb85d4c02009-04-03 08:28:08 +03002813out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002814 switch (clp->cl_cb_state) {
2815 case NFSD4_CB_DOWN:
2816 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2817 break;
2818 case NFSD4_CB_FAULT:
2819 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2820 break;
2821 default:
2822 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002823 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002824 if (!list_empty(&clp->cl_revoked))
2825 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002826out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002827 if (conn)
2828 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002829 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002830 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002831out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002832 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002833 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002834}
2835
Trond Myklebustb6076642014-06-30 11:48:35 -04002836void
2837nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2838{
2839 struct nfsd4_compound_state *cs = &resp->cstate;
2840
2841 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002842 if (cs->status != nfserr_replay_cache) {
2843 nfsd4_store_cache_entry(resp);
2844 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2845 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002846 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002847 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002848 } else if (cs->clp)
2849 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002850}
2851
Mi Jinlong345c2842011-10-20 17:51:39 +08002852__be32
2853nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2854{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002855 struct nfs4_client *conf, *unconf;
2856 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002857 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002859
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002860 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002861 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002862 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002863 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002864
2865 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002866 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002867 status = nfserr_clientid_busy;
2868 goto out;
2869 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002870 status = mark_client_expired_locked(conf);
2871 if (status)
2872 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002873 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002874 } else if (unconf)
2875 clp = unconf;
2876 else {
2877 status = nfserr_stale_clientid;
2878 goto out;
2879 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002880 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002881 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002882 status = nfserr_wrong_cred;
2883 goto out;
2884 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002885 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002886out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002887 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002888 if (clp)
2889 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002890 return status;
2891}
2892
Andy Adamson069b6ad2009-04-03 08:27:58 +03002893__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002894nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2895{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002896 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002897
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002898 if (rc->rca_one_fs) {
2899 if (!cstate->current_fh.fh_dentry)
2900 return nfserr_nofilehandle;
2901 /*
2902 * We don't take advantage of the rca_one_fs case.
2903 * That's OK, it's optional, we can safely ignore it.
2904 */
2905 return nfs_ok;
2906 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002907
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002908 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002909 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2910 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002911 goto out;
2912
2913 status = nfserr_stale_clientid;
2914 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002915 /*
2916 * The following error isn't really legal.
2917 * But we only get here if the client just explicitly
2918 * destroyed the client. Surely it no longer cares what
2919 * error it gets back on an operation for the dead
2920 * client.
2921 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002922 goto out;
2923
2924 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002925 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002926out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002927 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002928}
2929
2930__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002931nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2932 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002934 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002936 struct nfs4_client *conf, *new;
2937 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002938 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002939 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2940
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002941 new = create_client(clname, rqstp, &clverifier);
2942 if (new == NULL)
2943 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002944 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002945 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002946 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002947 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002948 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002950 if (clp_used_exchangeid(conf))
2951 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002952 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002953 char addr_str[INET6_ADDRSTRLEN];
2954 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2955 sizeof(addr_str));
2956 dprintk("NFSD: setclientid: string in use by client "
2957 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 goto out;
2959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002961 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002962 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002963 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002964 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002965 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002967 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002968 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002969 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002970 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002971 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2973 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2974 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002975 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 status = nfs_ok;
2977out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002978 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002979 if (new)
2980 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002981 if (unconf)
2982 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return status;
2984}
2985
2986
Al Virob37ad282006-10-19 23:28:59 -07002987__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002988nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2989 struct nfsd4_compound_state *cstate,
2990 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
NeilBrown21ab45a2005-06-23 22:04:14 -07002992 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002993 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2995 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002996 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002997 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002999 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003001
Jeff Laytond20c11d2014-07-30 08:27:07 -04003002 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003003 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003004 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003005 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003006 * We try hard to give out unique clientid's, so if we get an
3007 * attempt to confirm the same clientid with a different cred,
3008 * there's a bug somewhere. Let's charitably assume it's our
3009 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003010 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003011 status = nfserr_serverfault;
3012 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3013 goto out;
3014 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3015 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003016 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003017 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3018 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003020 else /* case 4: client hasn't noticed we rebooted yet? */
3021 status = nfserr_stale_clientid;
3022 goto out;
3023 }
3024 status = nfs_ok;
3025 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003026 old = unconf;
3027 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003028 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003029 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003030 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3031 if (old) {
3032 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003033 if (status) {
3034 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003035 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003036 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003037 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003038 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003039 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003040 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003041 get_client_locked(conf);
3042 spin_unlock(&nn->client_lock);
3043 nfsd4_probe_callback(conf);
3044 spin_lock(&nn->client_lock);
3045 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003047 spin_unlock(&nn->client_lock);
3048 if (old)
3049 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return status;
3051}
3052
J. Bruce Fields32513b42011-10-13 16:00:16 -04003053static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003055 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3056}
3057
3058/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003059static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003060{
Trond Myklebustca943212014-07-23 16:17:39 -04003061 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Trond Myklebust950e0112014-06-30 11:48:31 -04003063 lockdep_assert_held(&state_lock);
3064
J. Bruce Fields32513b42011-10-13 16:00:16 -04003065 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003066 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003067 INIT_LIST_HEAD(&fp->fi_stateids);
3068 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003069 fh_copy_shallow(&fp->fi_fhandle, fh);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003070 fp->fi_had_conflict = false;
3071 fp->fi_lease = NULL;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003072 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003073 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3074 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003075 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003078void
NeilBrowne60d4392005-06-23 22:03:01 -07003079nfsd4_free_slabs(void)
3080{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003081 kmem_cache_destroy(openowner_slab);
3082 kmem_cache_destroy(lockowner_slab);
3083 kmem_cache_destroy(file_slab);
3084 kmem_cache_destroy(stateid_slab);
3085 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Bryan Schumaker72083392011-11-01 15:24:59 -04003088int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089nfsd4_init_slabs(void)
3090{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003091 openowner_slab = kmem_cache_create("nfsd4_openowners",
3092 sizeof(struct nfs4_openowner), 0, 0, NULL);
3093 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003094 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003095 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003096 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003097 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003098 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003099 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003100 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003101 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003102 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003103 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003104 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003105 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003106 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003107 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003108 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003109 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003110 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003112
3113out_free_stateid_slab:
3114 kmem_cache_destroy(stateid_slab);
3115out_free_file_slab:
3116 kmem_cache_destroy(file_slab);
3117out_free_lockowner_slab:
3118 kmem_cache_destroy(lockowner_slab);
3119out_free_openowner_slab:
3120 kmem_cache_destroy(openowner_slab);
3121out:
NeilBrowne60d4392005-06-23 22:03:01 -07003122 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3123 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003126static void init_nfs4_replay(struct nfs4_replay *rp)
3127{
3128 rp->rp_status = nfserr_serverfault;
3129 rp->rp_buflen = 0;
3130 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003131 mutex_init(&rp->rp_mutex);
3132}
3133
3134static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3135 struct nfs4_stateowner *so)
3136{
3137 if (!nfsd4_has_session(cstate)) {
3138 mutex_lock(&so->so_replay.rp_mutex);
3139 cstate->replay_owner = so;
3140 atomic_inc(&so->so_count);
3141 }
3142}
3143
3144void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3145{
3146 struct nfs4_stateowner *so = cstate->replay_owner;
3147
3148 if (so != NULL) {
3149 cstate->replay_owner = NULL;
3150 mutex_unlock(&so->so_replay.rp_mutex);
3151 nfs4_put_stateowner(so);
3152 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003153}
3154
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003155static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
3157 struct nfs4_stateowner *sop;
3158
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003159 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003160 if (!sop)
3161 return NULL;
3162
3163 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3164 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003165 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003166 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003168 sop->so_owner.len = owner->len;
3169
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003170 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003171 sop->so_client = clp;
3172 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003173 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003174 return sop;
3175}
3176
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003177static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003178{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003179 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003180
Trond Myklebustd4f04892014-07-29 21:34:36 -04003181 list_add(&oo->oo_owner.so_strhash,
3182 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003183 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003186static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3187{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003188 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003189}
3190
Jeff Layton6b180f02014-07-29 21:34:26 -04003191static void nfs4_free_openowner(struct nfs4_stateowner *so)
3192{
3193 struct nfs4_openowner *oo = openowner(so);
3194
3195 kmem_cache_free(openowner_slab, oo);
3196}
3197
3198static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003199 .so_unhash = nfs4_unhash_openowner,
3200 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003201};
3202
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003203static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003204alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003205 struct nfsd4_compound_state *cstate)
3206{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003207 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003208 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003210 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3211 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003213 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003214 oo->oo_owner.so_is_open_owner = 1;
3215 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003216 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003217 if (nfsd4_has_session(cstate))
3218 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003219 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003220 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003221 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003222 spin_lock(&clp->cl_lock);
3223 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003224 if (ret == NULL) {
3225 hash_openowner(oo, clp, strhashval);
3226 ret = oo;
3227 } else
3228 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003229 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003230 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
J. Bruce Fields996e0932011-10-17 11:14:48 -04003233static 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 -04003234 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003236 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003237 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003238 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003239 stp->st_stateowner = &oo->oo_owner;
Jeff Laytond3134b12014-07-29 21:34:32 -04003240 atomic_inc(&stp->st_stateowner->so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07003241 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003242 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 stp->st_access_bmap = 0;
3244 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003245 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003246 spin_lock(&oo->oo_owner.so_client->cl_lock);
3247 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003248 spin_lock(&fp->fi_lock);
3249 list_add(&stp->st_perfile, &fp->fi_stateids);
3250 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003251 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Jeff Laytond3134b12014-07-29 21:34:32 -04003254/*
3255 * In the 4.0 case we need to keep the owners around a little while to handle
3256 * CLOSE replay. We still do need to release any file access that is held by
3257 * them before returning however.
3258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003260move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261{
Jeff Layton217526e2014-07-30 08:27:11 -04003262 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003263 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3264 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3265 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003266
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003267 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Jeff Laytonb401be222014-07-29 21:34:33 -04003269 /*
3270 * We know that we hold one reference via nfsd4_close, and another
3271 * "persistent" reference for the client. If the refcount is higher
3272 * than 2, then there are still calls in progress that are using this
3273 * stateid. We can't put the sc_file reference until they are finished.
3274 * Wait for the refcount to drop to 2. Since it has been unhashed,
3275 * there should be no danger of the refcount going back up again at
3276 * this point.
3277 */
3278 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3279
Jeff Laytond3134b12014-07-29 21:34:32 -04003280 release_all_access(s);
3281 if (s->st_stid.sc_file) {
3282 put_nfs4_file(s->st_stid.sc_file);
3283 s->st_stid.sc_file = NULL;
3284 }
Jeff Layton217526e2014-07-30 08:27:11 -04003285
3286 spin_lock(&nn->client_lock);
3287 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003288 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003289 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003290 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003291 spin_unlock(&nn->client_lock);
3292 if (last)
3293 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296/* search file_hashtbl[] for file */
3297static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003298find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Trond Myklebustca943212014-07-23 16:17:39 -04003300 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 struct nfs4_file *fp;
3302
Trond Myklebust950e0112014-06-30 11:48:31 -04003303 lockdep_assert_held(&state_lock);
3304
Jeff Layton89876f82013-04-02 09:01:59 -04003305 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003306 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003307 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
3311 return NULL;
3312}
3313
Trond Myklebust950e0112014-06-30 11:48:31 -04003314static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003315find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003316{
3317 struct nfs4_file *fp;
3318
3319 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003320 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003321 spin_unlock(&state_lock);
3322 return fp;
3323}
3324
3325static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003326find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003327{
3328 struct nfs4_file *fp;
3329
3330 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003331 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003332 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003333 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003334 fp = new;
3335 }
3336 spin_unlock(&state_lock);
3337
3338 return fp;
3339}
3340
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003341/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 * Called to check deny when READ with all zero stateid or
3343 * WRITE with all zero or all one stateid
3344 */
Al Virob37ad282006-10-19 23:28:59 -07003345static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003349 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Trond Myklebustca943212014-07-23 16:17:39 -04003351 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003352 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003353 return ret;
3354 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003355 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003356 if (fp->fi_share_deny & deny_type)
3357 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003358 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003359 put_nfs4_file(fp);
3360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Jeff Layton02e12152014-07-16 10:31:57 -04003363void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Trond Myklebust11b91642014-07-29 21:34:08 -04003365 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3366 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003367
Trond Myklebust11b91642014-07-29 21:34:08 -04003368 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003369
Jeff Layton02e12152014-07-16 10:31:57 -04003370 /*
3371 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003372 * already holding inode->i_lock.
3373 *
Jeff Laytondff13992014-07-08 14:02:49 -04003374 * If the dl_time != 0, then we know that it has already been
3375 * queued for a lease break. Don't queue it again.
3376 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003377 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003378 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003379 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003380 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003381 }
Jeff Layton02e12152014-07-16 10:31:57 -04003382 spin_unlock(&state_lock);
3383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Jeff Layton02e12152014-07-16 10:31:57 -04003385static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3386{
3387 /*
3388 * We're assuming the state code never drops its reference
3389 * without first removing the lease. Since we're in this lease
3390 * callback (and since the lease code is serialized by the kernel
3391 * lock) we know the server hasn't removed the lease yet, we know
3392 * it's safe to take a reference.
3393 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003394 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003395 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003396}
3397
Jeff Layton1c8c6012013-06-21 08:58:15 -04003398/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003399static void nfsd_break_deleg_cb(struct file_lock *fl)
3400{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003401 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3402 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003403
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003404 if (!fp) {
3405 WARN(1, "(%p)->fl_owner NULL\n", fl);
3406 return;
3407 }
3408 if (fp->fi_had_conflict) {
3409 WARN(1, "duplicate break on %p\n", fp);
3410 return;
3411 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003412 /*
3413 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003414 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003415 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003416 */
3417 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Jeff Layton02e12152014-07-16 10:31:57 -04003419 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003420 fp->fi_had_conflict = true;
3421 /*
3422 * If there are no delegations on the list, then we can't count on this
3423 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3424 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3425 * true should keep any new delegations from being hashed.
3426 */
3427 if (list_empty(&fp->fi_delegations))
3428 fl->fl_break_time = jiffies;
3429 else
3430 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3431 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003432 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433}
3434
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435static
3436int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3437{
3438 if (arg & F_UNLCK)
3439 return lease_modify(onlist, arg);
3440 else
3441 return -EAGAIN;
3442}
3443
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003444static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003445 .lm_break = nfsd_break_deleg_cb,
3446 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447};
3448
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003449static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3450{
3451 if (nfsd4_has_session(cstate))
3452 return nfs_ok;
3453 if (seqid == so->so_seqid - 1)
3454 return nfserr_replay_me;
3455 if (seqid == so->so_seqid)
3456 return nfs_ok;
3457 return nfserr_bad_seqid;
3458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Jeff Layton4b24ca72014-06-30 11:48:44 -04003460static __be32 lookup_clientid(clientid_t *clid,
3461 struct nfsd4_compound_state *cstate,
3462 struct nfsd_net *nn)
3463{
3464 struct nfs4_client *found;
3465
3466 if (cstate->clp) {
3467 found = cstate->clp;
3468 if (!same_clid(&found->cl_clientid, clid))
3469 return nfserr_stale_clientid;
3470 return nfs_ok;
3471 }
3472
3473 if (STALE_CLIENTID(clid, nn))
3474 return nfserr_stale_clientid;
3475
3476 /*
3477 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3478 * cached already then we know this is for is for v4.0 and "sessions"
3479 * will be false.
3480 */
3481 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003482 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003483 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003484 if (!found) {
3485 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003486 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003487 }
3488 atomic_inc(&found->cl_refcount);
3489 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003490
3491 /* Cache the nfs4_client in cstate! */
3492 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003493 return nfs_ok;
3494}
3495
Al Virob37ad282006-10-19 23:28:59 -07003496__be32
Andy Adamson66689582009-04-03 08:28:45 +03003497nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003498 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 clientid_t *clientid = &open->op_clientid;
3501 struct nfs4_client *clp = NULL;
3502 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003503 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003504 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003506 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003508 /*
3509 * In case we need it later, after we've already created the
3510 * file and don't want to risk a further failure:
3511 */
3512 open->op_file = nfsd4_alloc_file();
3513 if (open->op_file == NULL)
3514 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Trond Myklebust2d91e892014-06-30 11:48:46 -04003516 status = lookup_clientid(clientid, cstate, nn);
3517 if (status)
3518 return status;
3519 clp = cstate->clp;
3520
Trond Myklebustd4f04892014-07-29 21:34:36 -04003521 strhashval = ownerstr_hashval(&open->op_owner);
3522 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003523 open->op_openowner = oo;
3524 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003525 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003527 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003528 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003529 release_openowner(oo);
3530 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003531 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003532 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003533 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3534 if (status)
3535 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003536 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003537new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003538 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003539 if (oo == NULL)
3540 return nfserr_jukebox;
3541 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003542alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003543 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003544 if (!open->op_stp)
3545 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003546 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Al Virob37ad282006-10-19 23:28:59 -07003549static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003550nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3551{
3552 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3553 return nfserr_openmode;
3554 else
3555 return nfs_ok;
3556}
3557
Daniel Mackc47d8322011-05-16 16:38:14 +02003558static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003559{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003560 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3561}
3562
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003563static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003564{
3565 struct nfs4_stid *ret;
3566
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003567 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003568 if (!ret)
3569 return NULL;
3570 return delegstateid(ret);
3571}
3572
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003573static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3574{
3575 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3576 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3577}
3578
Al Virob37ad282006-10-19 23:28:59 -07003579static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003580nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003581 struct nfs4_delegation **dp)
3582{
3583 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003584 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003585 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003586
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003587 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3588 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003589 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003590 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003591 status = nfs4_check_delegmode(deleg, flags);
3592 if (status) {
3593 nfs4_put_stid(&deleg->dl_stid);
3594 goto out;
3595 }
3596 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003597out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003598 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003599 return nfs_ok;
3600 if (status)
3601 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003602 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003603 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003604}
3605
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003606static struct nfs4_ol_stateid *
3607nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003609 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003610 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Trond Myklebust1d31a252014-07-10 14:07:25 -04003612 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003613 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 /* ignore lock owners */
3615 if (local->st_stateowner->so_is_open_owner == 0)
3616 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003617 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003618 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003619 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003620 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003623 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003624 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003627static inline int nfs4_access_to_access(u32 nfs4_access)
3628{
3629 int flags = 0;
3630
3631 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3632 flags |= NFSD_MAY_READ;
3633 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3634 flags |= NFSD_MAY_WRITE;
3635 return flags;
3636}
3637
Al Virob37ad282006-10-19 23:28:59 -07003638static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3640 struct nfsd4_open *open)
3641{
3642 struct iattr iattr = {
3643 .ia_valid = ATTR_SIZE,
3644 .ia_size = 0,
3645 };
3646 if (!open->op_truncate)
3647 return 0;
3648 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003649 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3651}
3652
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003653static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003654 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3655 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003656{
Trond Myklebustde186432014-07-10 14:07:26 -04003657 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003658 __be32 status;
3659 int oflag = nfs4_access_to_omode(open->op_share_access);
3660 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003661 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003662
Trond Myklebustde186432014-07-10 14:07:26 -04003663 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003664
3665 /*
3666 * Are we trying to set a deny mode that would conflict with
3667 * current access?
3668 */
3669 status = nfs4_file_check_deny(fp, open->op_share_deny);
3670 if (status != nfs_ok) {
3671 spin_unlock(&fp->fi_lock);
3672 goto out;
3673 }
3674
3675 /* set access to the file */
3676 status = nfs4_file_get_access(fp, open->op_share_access);
3677 if (status != nfs_ok) {
3678 spin_unlock(&fp->fi_lock);
3679 goto out;
3680 }
3681
3682 /* Set access bits in stateid */
3683 old_access_bmap = stp->st_access_bmap;
3684 set_access(open->op_share_access, stp);
3685
3686 /* Set new deny mask */
3687 old_deny_bmap = stp->st_deny_bmap;
3688 set_deny(open->op_share_deny, stp);
3689 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3690
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003691 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003692 spin_unlock(&fp->fi_lock);
3693 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003694 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003695 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003696 spin_lock(&fp->fi_lock);
3697 if (!fp->fi_fds[oflag]) {
3698 fp->fi_fds[oflag] = filp;
3699 filp = NULL;
3700 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003701 }
Trond Myklebustde186432014-07-10 14:07:26 -04003702 spin_unlock(&fp->fi_lock);
3703 if (filp)
3704 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003705
3706 status = nfsd4_truncate(rqstp, cur_fh, open);
3707 if (status)
3708 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003709out:
3710 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003711out_put_access:
3712 stp->st_access_bmap = old_access_bmap;
3713 nfs4_file_put_access(fp, open->op_share_access);
3714 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3715 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003716}
3717
Al Virob37ad282006-10-19 23:28:59 -07003718static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003719nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
Al Virob37ad282006-10-19 23:28:59 -07003721 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003722 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003724 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003725 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003726
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003727 /* test and set deny mode */
3728 spin_lock(&fp->fi_lock);
3729 status = nfs4_file_check_deny(fp, open->op_share_deny);
3730 if (status == nfs_ok) {
3731 old_deny_bmap = stp->st_deny_bmap;
3732 set_deny(open->op_share_deny, stp);
3733 fp->fi_share_deny |=
3734 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3735 }
3736 spin_unlock(&fp->fi_lock);
3737
3738 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003741 status = nfsd4_truncate(rqstp, cur_fh, open);
3742 if (status != nfs_ok)
3743 reset_union_bmap_deny(old_deny_bmap, stp);
3744 return status;
3745}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003748nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003750 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003753/* Should we give out recallable state?: */
3754static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3755{
3756 if (clp->cl_cb_state == NFSD4_CB_UP)
3757 return true;
3758 /*
3759 * In the sessions case, since we don't have to establish a
3760 * separate connection for callbacks, we assume it's OK
3761 * until we hear otherwise:
3762 */
3763 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3764}
3765
Jeff Laytond564fbe2014-07-16 10:31:58 -04003766static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003767{
3768 struct file_lock *fl;
3769
3770 fl = locks_alloc_lock();
3771 if (!fl)
3772 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003773 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003774 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003775 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3776 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003777 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003778 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003779 return fl;
3780}
3781
J. Bruce Fields99c41512013-05-21 16:21:25 -04003782static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003783{
Trond Myklebust11b91642014-07-29 21:34:08 -04003784 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003785 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04003786 struct file *filp;
3787 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003788
Jeff Laytond564fbe2014-07-16 10:31:58 -04003789 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003790 if (!fl)
3791 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003792 filp = find_readable_file(fp);
3793 if (!filp) {
3794 /* We should always have a readable file here */
3795 WARN_ON_ONCE(1);
3796 return -EBADF;
3797 }
3798 fl->fl_file = filp;
3799 status = vfs_setlease(filp, fl->fl_type, &fl);
3800 if (status) {
3801 locks_free_lock(fl);
3802 goto out_fput;
3803 }
Benny Halevycdc97502014-05-30 09:09:30 -04003804 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003805 spin_lock(&fp->fi_lock);
3806 /* Did the lease get broken before we took the lock? */
3807 status = -EAGAIN;
3808 if (fp->fi_had_conflict)
3809 goto out_unlock;
3810 /* Race breaker */
3811 if (fp->fi_lease) {
3812 status = 0;
3813 atomic_inc(&fp->fi_delegees);
3814 hash_delegation_locked(dp, fp);
3815 goto out_unlock;
3816 }
3817 fp->fi_lease = fl;
3818 fp->fi_deleg_file = filp;
3819 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003820 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003821 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003822 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003823 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003824out_unlock:
3825 spin_unlock(&fp->fi_lock);
3826 spin_unlock(&state_lock);
3827out_fput:
3828 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003829 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003830}
3831
Jeff Layton0b266932014-07-25 07:34:25 -04003832static struct nfs4_delegation *
3833nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3834 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003835{
Jeff Layton0b266932014-07-25 07:34:25 -04003836 int status;
3837 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003838
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003839 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003840 return ERR_PTR(-EAGAIN);
3841
3842 dp = alloc_init_deleg(clp, fh);
3843 if (!dp)
3844 return ERR_PTR(-ENOMEM);
3845
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003846 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003847 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003848 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003849 dp->dl_stid.sc_file = fp;
Jeff Layton417c6622014-07-21 09:34:57 -04003850 if (!fp->fi_lease) {
3851 spin_unlock(&fp->fi_lock);
3852 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003853 status = nfs4_setlease(dp);
3854 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003855 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003856 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003857 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003858 status = -EAGAIN;
3859 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003860 }
Benny Halevy931ee562014-05-30 09:09:27 -04003861 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003862 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003863out_unlock:
3864 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003865 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003866out:
3867 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003868 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003869 return ERR_PTR(status);
3870 }
3871 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003872}
3873
Benny Halevy4aa89132012-02-21 14:16:44 -08003874static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3875{
3876 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3877 if (status == -EAGAIN)
3878 open->op_why_no_deleg = WND4_CONTENTION;
3879 else {
3880 open->op_why_no_deleg = WND4_RESOURCE;
3881 switch (open->op_deleg_want) {
3882 case NFS4_SHARE_WANT_READ_DELEG:
3883 case NFS4_SHARE_WANT_WRITE_DELEG:
3884 case NFS4_SHARE_WANT_ANY_DELEG:
3885 break;
3886 case NFS4_SHARE_WANT_CANCEL:
3887 open->op_why_no_deleg = WND4_CANCELLED;
3888 break;
3889 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003890 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003891 }
3892 }
3893}
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895/*
3896 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003897 *
3898 * Note we don't support write delegations, and won't until the vfs has
3899 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 */
3901static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003902nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3903 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904{
3905 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003906 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3907 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003908 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003909 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003911 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003912 open->op_recall = 0;
3913 switch (open->op_claim_type) {
3914 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003915 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003916 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003917 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3918 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003919 break;
3920 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003921 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003922 /*
3923 * Let's not give out any delegations till everyone's
3924 * had the chance to reclaim theirs....
3925 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003926 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003927 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003928 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003929 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003930 /*
3931 * Also, if the file was opened for write or
3932 * create, there's a good chance the client's
3933 * about to write to it, resulting in an
3934 * immediate recall (since we don't support
3935 * write delegations):
3936 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003937 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003938 goto out_no_deleg;
3939 if (open->op_create == NFS4_OPEN_CREATE)
3940 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003941 break;
3942 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003943 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003944 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003945 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003946 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003947 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003949 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003951 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003952 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003953 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003954 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003955 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003956out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003957 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3958 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003959 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003960 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003961 open->op_recall = 1;
3962 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003963
3964 /* 4.1 client asking for a delegation? */
3965 if (open->op_deleg_want)
3966 nfsd4_open_deleg_none_ext(open, status);
3967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968}
3969
Benny Halevye27f49c2012-02-21 14:16:54 -08003970static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3971 struct nfs4_delegation *dp)
3972{
3973 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_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_DOWNGRADE;
3977 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3978 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3979 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3980 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3981 }
3982 /* Otherwise the client must be confused wanting a delegation
3983 * it already has, therefore we don't return
3984 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3985 */
3986}
3987
Al Virob37ad282006-10-19 23:28:59 -07003988__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3990{
Andy Adamson66689582009-04-03 08:28:45 +03003991 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003992 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003994 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003995 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003996 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 /*
3999 * Lookup file; if found, lookup stateid and check open request,
4000 * and check for delegations in the process of being recalled.
4001 * If not found, create the nfs4_file struct
4002 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004003 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004004 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004005 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004006 if (status)
4007 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004008 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004010 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004011 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004012 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004013 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004014 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 }
4016
4017 /*
4018 * OPEN the file, or upgrade an existing OPEN.
4019 * If truncate fails, the OPEN fails.
4020 */
4021 if (stp) {
4022 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004023 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (status)
4025 goto out;
4026 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004027 stp = open->op_stp;
4028 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004029 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004030 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4031 if (status) {
4032 release_open_stateid(stp);
4033 goto out;
4034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004036 update_stateid(&stp->st_stid.sc_stateid);
4037 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Benny Halevyd24433c2012-02-16 20:57:17 +02004039 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004040 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4041 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4042 open->op_why_no_deleg = WND4_NOT_WANTED;
4043 goto nodeleg;
4044 }
4045 }
4046
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 /*
4048 * Attempt to hand out a delegation. No error return, because the
4049 * OPEN succeeds even if we fail.
4050 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004051 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004052nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 status = nfs_ok;
4054
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004055 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004056 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004058 /* 4.1 client trying to upgrade/downgrade delegation? */
4059 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004060 open->op_deleg_want)
4061 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004062
NeilBrown13cd2182005-06-23 22:03:10 -07004063 if (fp)
4064 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004065 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004066 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 /*
4068 * To finish the open response, we just need to set the rflags.
4069 */
4070 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004071 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004072 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004074 if (dp)
4075 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004076 if (stp)
4077 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 return status;
4080}
4081
Jeff Layton58fb12e2014-07-29 21:34:27 -04004082void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4083 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004084{
4085 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004086 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004087
Jeff Laytond3134b12014-07-29 21:34:32 -04004088 nfsd4_cstate_assign_replay(cstate, so);
4089 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004090 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004091 if (open->op_file)
4092 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004093 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004094 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004095}
4096
Al Virob37ad282006-10-19 23:28:59 -07004097__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004098nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4099 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100{
4101 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004102 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004103 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 dprintk("process_renew(%08x/%08x): starting\n",
4106 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004107 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004108 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004110 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004112 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004113 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 goto out;
4115 status = nfs_ok;
4116out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 return status;
4118}
4119
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004120void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004121nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004122{
Jeff Layton33dcc482012-04-10 11:08:48 -04004123 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004124 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004125 return;
4126
NeilBrowna76b4312005-06-23 22:04:01 -07004127 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004128 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004129 /*
4130 * If the server goes down again right now, an NFSv4
4131 * client will still be allowed to reclaim after it comes back up,
4132 * even if it hasn't yet had a chance to reclaim state this time.
4133 *
4134 */
Jeff Layton919b8042014-09-12 16:40:20 -04004135 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004136 /*
4137 * At this point, NFSv4 clients can still reclaim. But if the
4138 * server crashes, any that have not yet reclaimed will be out
4139 * of luck on the next boot.
4140 *
4141 * (NFSv4.1+ clients are considered to have reclaimed once they
4142 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4143 * have reclaimed after their first OPEN.)
4144 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004145 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004146 /*
4147 * At this point, and once lockd and/or any other containers
4148 * exit their grace period, further reclaims will fail and
4149 * regular locking can resume.
4150 */
NeilBrowna76b4312005-06-23 22:04:01 -07004151}
4152
NeilBrownfd39ca92005-06-23 22:04:03 -07004153static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004154nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
4156 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004157 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004159 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004161 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004162 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004165 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004166 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004167 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004168 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 clp = list_entry(pos, struct nfs4_client, cl_lru);
4170 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4171 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004172 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 break;
4174 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004175 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004176 dprintk("NFSD: client in use (clientid %08x)\n",
4177 clp->cl_clientid.cl_id);
4178 continue;
4179 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004180 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004181 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004182 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004183 list_for_each_safe(pos, next, &reaplist) {
4184 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 dprintk("NFSD: purging unused client (clientid %08x)\n",
4186 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004187 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 expire_client(clp);
4189 }
Benny Halevycdc97502014-05-30 09:09:30 -04004190 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004191 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004193 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4194 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004196 t = dp->dl_time - cutoff;
4197 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 break;
4199 }
Jeff Layton42690672014-07-25 07:34:20 -04004200 unhash_delegation_locked(dp);
4201 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
Benny Halevycdc97502014-05-30 09:09:30 -04004203 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004204 while (!list_empty(&reaplist)) {
4205 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4206 dl_recall_lru);
4207 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004208 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
Jeff Layton217526e2014-07-30 08:27:11 -04004210
4211 spin_lock(&nn->client_lock);
4212 while (!list_empty(&nn->close_lru)) {
4213 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4214 oo_close_lru);
4215 if (time_after((unsigned long)oo->oo_time,
4216 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004217 t = oo->oo_time - cutoff;
4218 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 break;
4220 }
Jeff Layton217526e2014-07-30 08:27:11 -04004221 list_del_init(&oo->oo_close_lru);
4222 stp = oo->oo_last_closed_stid;
4223 oo->oo_last_closed_stid = NULL;
4224 spin_unlock(&nn->client_lock);
4225 nfs4_put_stid(&stp->st_stid);
4226 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 }
Jeff Layton217526e2014-07-30 08:27:11 -04004228 spin_unlock(&nn->client_lock);
4229
Jeff Laytona832e7a2014-05-30 09:09:26 -04004230 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004231 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232}
4233
Harvey Harrisona254b242008-02-20 12:49:00 -08004234static struct workqueue_struct *laundry_wq;
4235static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004236
4237static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004238laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239{
4240 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004241 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4242 work);
4243 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4244 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004246 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004248 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004251static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004252{
Trond Myklebust11b91642014-07-29 21:34:08 -04004253 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004254 return nfserr_bad_stateid;
4255 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256}
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004259access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004261 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4262 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4263 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264}
4265
4266static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004267access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004269 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4270 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
4273static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004274__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
Al Virob37ad282006-10-19 23:28:59 -07004276 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
J. Bruce Fields02921912010-07-29 15:16:59 -04004278 /* For lock stateid's, we test the parent open, not the lock: */
4279 if (stp->st_openstp)
4280 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004281 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004283 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 goto out;
4285 status = nfs_ok;
4286out:
4287 return status;
4288}
4289
Al Virob37ad282006-10-19 23:28:59 -07004290static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004291check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004293 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004295 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004296 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 * conflicting state; so we must wait out the grace period. */
4298 return nfserr_grace;
4299 } else if (flags & WR_STATE)
4300 return nfs4_share_conflict(current_fh,
4301 NFS4_SHARE_DENY_WRITE);
4302 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4303 return nfs4_share_conflict(current_fh,
4304 NFS4_SHARE_DENY_READ);
4305}
4306
4307/*
4308 * Allow READ/WRITE during grace period on recovered state only for files
4309 * that are not able to provide mandatory locking.
4310 */
4311static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004312grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004314 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315}
4316
J. Bruce Fields81b82962011-08-23 11:03:29 -04004317/* Returns true iff a is later than b: */
4318static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4319{
Jim Rees1a9357f2013-05-17 17:33:00 -04004320 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004321}
4322
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004323static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004324{
Andy Adamson66689582009-04-03 08:28:45 +03004325 /*
4326 * When sessions are used the stateid generation number is ignored
4327 * when it is zero.
4328 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004329 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004330 return nfs_ok;
4331
4332 if (in->si_generation == ref->si_generation)
4333 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004334
J. Bruce Fields0836f582008-01-26 19:08:12 -05004335 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004336 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004337 return nfserr_bad_stateid;
4338 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004339 * However, we could see a stateid from the past, even from a
4340 * non-buggy client. For example, if the client sends a lock
4341 * while some IO is outstanding, the lock may bump si_generation
4342 * while the IO is still in flight. The client could avoid that
4343 * situation by waiting for responses on all the IO requests,
4344 * but better performance may result in retrying IO that
4345 * receives an old_stateid error if requests are rarely
4346 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004347 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004348 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004349}
4350
Chuck Lever7df302f2012-05-29 13:56:37 -04004351static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004352{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004353 struct nfs4_stid *s;
4354 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004355 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004356
Chuck Lever7df302f2012-05-29 13:56:37 -04004357 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004358 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004359 /* Client debugging aid. */
4360 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4361 char addr_str[INET6_ADDRSTRLEN];
4362 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4363 sizeof(addr_str));
4364 pr_warn_ratelimited("NFSD: client %s testing state ID "
4365 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004366 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004367 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004368 spin_lock(&cl->cl_lock);
4369 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004370 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004371 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004372 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004373 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004374 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004375 switch (s->sc_type) {
4376 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004377 status = nfs_ok;
4378 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004379 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004380 status = nfserr_deleg_revoked;
4381 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004382 case NFS4_OPEN_STID:
4383 case NFS4_LOCK_STID:
4384 ols = openlockstateid(s);
4385 if (ols->st_stateowner->so_is_open_owner
4386 && !(openowner(ols->st_stateowner)->oo_flags
4387 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004388 status = nfserr_bad_stateid;
4389 else
4390 status = nfs_ok;
4391 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004392 default:
4393 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004394 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004395 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004396 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004397 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004398 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004399out_unlock:
4400 spin_unlock(&cl->cl_lock);
4401 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004402}
4403
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004404static __be32
4405nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4406 stateid_t *stateid, unsigned char typemask,
4407 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004408{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004409 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004410
4411 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4412 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004413 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004414 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004415 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004416 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004417 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004418 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004419 if (status)
4420 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004421 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004422 if (!*s)
4423 return nfserr_bad_stateid;
4424 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004425}
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427/*
4428* Checks for stateid operations
4429*/
Al Virob37ad282006-10-19 23:28:59 -07004430__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004431nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004432 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004434 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004435 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004437 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004439 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004440 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004441 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 if (filpp)
4444 *filpp = NULL;
4445
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004446 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 return nfserr_grace;
4448
4449 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004450 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004452 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004453 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004454 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004455 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004456 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004457 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4458 if (status)
4459 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004460 switch (s->sc_type) {
4461 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004462 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004463 status = nfs4_check_delegmode(dp, flags);
4464 if (status)
4465 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004466 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004467 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004468 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004469 WARN_ON_ONCE(1);
4470 status = nfserr_serverfault;
4471 goto out;
4472 }
Trond Myklebustde186432014-07-10 14:07:26 -04004473 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004474 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004475 break;
4476 case NFS4_OPEN_STID:
4477 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004478 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004479 status = nfs4_check_fh(current_fh, stp);
4480 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004482 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004483 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004485 status = nfs4_check_openmode(stp, flags);
4486 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004488 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004489 struct nfs4_file *fp = stp->st_stid.sc_file;
4490
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004491 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004492 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004493 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004494 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004495 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004496 break;
4497 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004498 status = nfserr_bad_stateid;
4499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
4501 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004502 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004503 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004505 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 return status;
4507}
4508
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004509/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004510 * Test if the stateid is valid
4511 */
4512__be32
4513nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4514 struct nfsd4_test_stateid *test_stateid)
4515{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004516 struct nfsd4_test_stateid_id *stateid;
4517 struct nfs4_client *cl = cstate->session->se_client;
4518
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004519 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004520 stateid->ts_id_status =
4521 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004522
Bryan Schumaker17456802011-07-13 10:50:48 -04004523 return nfs_ok;
4524}
4525
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004526__be32
4527nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4528 struct nfsd4_free_stateid *free_stateid)
4529{
4530 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004531 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004532 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004533 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004534 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004535 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004536
Jeff Layton1af71cc2014-07-29 21:34:14 -04004537 spin_lock(&cl->cl_lock);
4538 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004539 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004540 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004541 switch (s->sc_type) {
4542 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004543 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004544 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004545 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004546 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4547 if (ret)
4548 break;
4549 ret = nfserr_locks_held;
4550 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004551 case NFS4_LOCK_STID:
4552 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4553 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004554 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004555 stp = openlockstateid(s);
4556 ret = nfserr_locks_held;
4557 if (check_for_locks(stp->st_stid.sc_file,
4558 lockowner(stp->st_stateowner)))
4559 break;
4560 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004561 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004562 nfs4_put_stid(s);
4563 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004564 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004565 case NFS4_REVOKED_DELEG_STID:
4566 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004567 list_del_init(&dp->dl_recall_lru);
4568 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004569 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004570 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004571 goto out;
4572 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004573 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004574out_unlock:
4575 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004576out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004577 return ret;
4578}
4579
NeilBrown4c4cd222005-07-07 17:59:27 -07004580static inline int
4581setlkflg (int type)
4582{
4583 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4584 RD_STATE : WR_STATE;
4585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004587static __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 -04004588{
4589 struct svc_fh *current_fh = &cstate->current_fh;
4590 struct nfs4_stateowner *sop = stp->st_stateowner;
4591 __be32 status;
4592
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004593 status = nfsd4_check_seqid(cstate, sop, seqid);
4594 if (status)
4595 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004596 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4597 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004598 /*
4599 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004600 * nfserr_replay_me from the previous step, and
4601 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004602 */
4603 return nfserr_bad_stateid;
4604 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4605 if (status)
4606 return status;
4607 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004608}
4609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610/*
4611 * Checks for sequence id mutating operations.
4612 */
Al Virob37ad282006-10-19 23:28:59 -07004613static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004614nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004615 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004616 struct nfs4_ol_stateid **stpp,
4617 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004619 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004620 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004621 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004623 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4624 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004627 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004628 if (status)
4629 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004630 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004631 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004633 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004634 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004635 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004636 else
4637 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004638 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004639}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004640
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004641static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4642 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004643{
4644 __be32 status;
4645 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004646 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004648 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004649 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004650 if (status)
4651 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004652 oo = openowner(stp->st_stateowner);
4653 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4654 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004655 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004656 }
4657 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004658 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
Al Virob37ad282006-10-19 23:28:59 -07004661__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004662nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004663 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Al Virob37ad282006-10-19 23:28:59 -07004665 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004666 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004667 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004668 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Al Viroa6a9f182013-09-16 10:57:01 -04004670 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4671 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004673 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004674 if (status)
4675 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004677 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004678 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004679 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004680 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004681 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004682 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004683 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004684 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004685 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004686 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004687 update_stateid(&stp->st_stid.sc_stateid);
4688 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004689 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004690 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004691
Jeff Layton2a4317c2012-03-21 16:42:43 -04004692 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004693 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004694put_stateid:
4695 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004697 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 return status;
4699}
4700
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004701static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004703 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004704 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004705 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004706 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004707}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004708
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004709static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4710{
4711 switch (to_access) {
4712 case NFS4_SHARE_ACCESS_READ:
4713 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4714 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4715 break;
4716 case NFS4_SHARE_ACCESS_WRITE:
4717 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4718 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4719 break;
4720 case NFS4_SHARE_ACCESS_BOTH:
4721 break;
4722 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004723 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 }
4725}
4726
Al Virob37ad282006-10-19 23:28:59 -07004727__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004728nfsd4_open_downgrade(struct svc_rqst *rqstp,
4729 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004730 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Al Virob37ad282006-10-19 23:28:59 -07004732 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004733 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004734 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
Al Viroa6a9f182013-09-16 10:57:01 -04004736 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4737 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004739 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004740 if (od->od_deleg_want)
4741 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4742 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004744 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004745 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004746 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004749 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004750 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004752 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004754 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004755 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004757 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004759 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Jeff Laytonce0fc432012-05-11 09:45:14 -04004761 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004763 update_stateid(&stp->st_stid.sc_stateid);
4764 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004766put_stateid:
4767 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004769 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 return status;
4771}
4772
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004773static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4774{
Trond Myklebustacf92952014-06-30 11:48:37 -04004775 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004776 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004777
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004778 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004779 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004780 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004781
Jeff Laytond83017f2014-07-29 21:34:42 -04004782 if (clp->cl_minorversion) {
4783 put_ol_stateid_locked(s, &reaplist);
4784 spin_unlock(&clp->cl_lock);
4785 free_ol_stateid_reaplist(&reaplist);
4786 } else {
4787 spin_unlock(&clp->cl_lock);
4788 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004789 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004790 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004791}
4792
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793/*
4794 * nfs4_unlock_state() called after encode
4795 */
Al Virob37ad282006-10-19 23:28:59 -07004796__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004797nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004798 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
Al Virob37ad282006-10-19 23:28:59 -07004800 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004801 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004802 struct net *net = SVC_NET(rqstp);
4803 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Al Viroa6a9f182013-09-16 10:57:01 -04004805 dprintk("NFSD: nfsd4_close on file %pd\n",
4806 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004808 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4809 &close->cl_stateid,
4810 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004811 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004812 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004813 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004815 update_stateid(&stp->st_stid.sc_stateid);
4816 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004818 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004819
4820 /* put reference from nfs4_preprocess_seqid_op */
4821 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 return status;
4824}
4825
Al Virob37ad282006-10-19 23:28:59 -07004826__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004827nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4828 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004830 struct nfs4_delegation *dp;
4831 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004832 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004833 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004834 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004836 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004837 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004839 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004840 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004841 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004842 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004843 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004844 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004845 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004846
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004847 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004848put_stateid:
4849 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850out:
4851 return status;
4852}
4853
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Benny Halevy87df4de2008-12-15 19:42:03 +02004857static inline u64
4858end_offset(u64 start, u64 len)
4859{
4860 u64 end;
4861
4862 end = start + len;
4863 return end >= start ? end: NFS4_MAX_UINT64;
4864}
4865
4866/* last octet in a range */
4867static inline u64
4868last_byte_offset(u64 start, u64 len)
4869{
4870 u64 end;
4871
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004872 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004873 end = start + len;
4874 return end > start ? end - 1: NFS4_MAX_UINT64;
4875}
4876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877/*
4878 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4879 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4880 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4881 * locking, this prevents us from being completely protocol-compliant. The
4882 * real solution to this problem is to start using unsigned file offsets in
4883 * the VFS, but this is a very deep change!
4884 */
4885static inline void
4886nfs4_transform_lock_offset(struct file_lock *lock)
4887{
4888 if (lock->fl_start < 0)
4889 lock->fl_start = OFFSET_MAX;
4890 if (lock->fl_end < 0)
4891 lock->fl_end = OFFSET_MAX;
4892}
4893
NeilBrownd5b90262006-04-10 22:55:22 -07004894/* Hack!: For now, we're defining this just so we can use a pointer to it
4895 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004896static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004897};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
4899static inline void
4900nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4901{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004902 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
NeilBrownd5b90262006-04-10 22:55:22 -07004904 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004905 lo = (struct nfs4_lockowner *) fl->fl_owner;
4906 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4907 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004908 if (!deny->ld_owner.data)
4909 /* We just don't care that much */
4910 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004911 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4912 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004913 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004914nevermind:
4915 deny->ld_owner.len = 0;
4916 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004917 deny->ld_clientid.cl_boot = 0;
4918 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004921 deny->ld_length = NFS4_MAX_UINT64;
4922 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4924 deny->ld_type = NFS4_READ_LT;
4925 if (fl->fl_type != F_RDLCK)
4926 deny->ld_type = NFS4_WRITE_LT;
4927}
4928
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004929static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004930find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004931 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004933 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004934 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
Trond Myklebust0a880a22014-07-30 08:27:10 -04004936 lockdep_assert_held(&clp->cl_lock);
4937
Trond Myklebustd4f04892014-07-29 21:34:36 -04004938 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4939 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004940 if (so->so_is_open_owner)
4941 continue;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004942 if (!same_owner_str(so, owner))
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004943 continue;
Jeff Layton5db1c032014-07-29 21:34:28 -04004944 atomic_inc(&so->so_count);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004945 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
4947 return NULL;
4948}
4949
Trond Myklebustc58c6612014-07-29 21:34:35 -04004950static struct nfs4_lockowner *
4951find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004952 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004953{
4954 struct nfs4_lockowner *lo;
4955
Trond Myklebustd4f04892014-07-29 21:34:36 -04004956 spin_lock(&clp->cl_lock);
4957 lo = find_lockowner_str_locked(clid, owner, clp);
4958 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004959 return lo;
4960}
4961
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004962static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4963{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004964 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004965}
4966
Jeff Layton6b180f02014-07-29 21:34:26 -04004967static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4968{
4969 struct nfs4_lockowner *lo = lockowner(sop);
4970
4971 kmem_cache_free(lockowner_slab, lo);
4972}
4973
4974static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004975 .so_unhash = nfs4_unhash_lockowner,
4976 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004977};
4978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979/*
4980 * Alloc a lock owner structure.
4981 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004982 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004984 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004986static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004987alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4988 struct nfs4_ol_stateid *open_stp,
4989 struct nfsd4_lock *lock)
4990{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004991 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004993 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4994 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004996 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4997 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004998 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004999 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005000 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005001 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005002 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005003 if (ret == NULL) {
5004 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005005 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005006 ret = lo;
5007 } else
5008 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005009 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005010 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011}
5012
Jeff Layton356a95e2014-07-29 21:34:13 -04005013static void
5014init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5015 struct nfs4_file *fp, struct inode *inode,
5016 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005018 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
Jeff Layton356a95e2014-07-29 21:34:13 -04005020 lockdep_assert_held(&clp->cl_lock);
5021
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005022 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005023 stp->st_stid.sc_type = NFS4_LOCK_STID;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005024 stp->st_stateowner = &lo->lo_owner;
Trond Myklebuste4f1dd72014-07-29 21:34:30 -04005025 atomic_inc(&lo->lo_owner.so_count);
NeilBrown13cd2182005-06-23 22:03:10 -07005026 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005027 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005028 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005029 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005031 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005032 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005033 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005034 spin_lock(&fp->fi_lock);
5035 list_add(&stp->st_perfile, &fp->fi_stateids);
5036 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
Jeff Laytonc53530d2014-06-30 11:48:39 -04005039static struct nfs4_ol_stateid *
5040find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5041{
5042 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005043 struct nfs4_client *clp = lo->lo_owner.so_client;
5044
5045 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005046
5047 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005048 if (lst->st_stid.sc_file == fp) {
5049 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005050 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005051 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005052 }
5053 return NULL;
5054}
5055
Jeff Layton356a95e2014-07-29 21:34:13 -04005056static struct nfs4_ol_stateid *
5057find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5058 struct inode *inode, struct nfs4_ol_stateid *ost,
5059 bool *new)
5060{
5061 struct nfs4_stid *ns = NULL;
5062 struct nfs4_ol_stateid *lst;
5063 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5064 struct nfs4_client *clp = oo->oo_owner.so_client;
5065
5066 spin_lock(&clp->cl_lock);
5067 lst = find_lock_stateid(lo, fi);
5068 if (lst == NULL) {
5069 spin_unlock(&clp->cl_lock);
5070 ns = nfs4_alloc_stid(clp, stateid_slab);
5071 if (ns == NULL)
5072 return NULL;
5073
5074 spin_lock(&clp->cl_lock);
5075 lst = find_lock_stateid(lo, fi);
5076 if (likely(!lst)) {
5077 lst = openlockstateid(ns);
5078 init_lock_stateid(lst, lo, fi, inode, ost);
5079 ns = NULL;
5080 *new = true;
5081 }
5082 }
5083 spin_unlock(&clp->cl_lock);
5084 if (ns)
5085 nfs4_put_stid(ns);
5086 return lst;
5087}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005088
NeilBrownfd39ca92005-06-23 22:04:03 -07005089static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090check_lock_length(u64 offset, u64 length)
5091{
Benny Halevy87df4de2008-12-15 19:42:03 +02005092 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 LOFF_OVERFLOW(offset, length)));
5094}
5095
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005096static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005097{
Trond Myklebust11b91642014-07-29 21:34:08 -04005098 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005099
Jeff Layton7214e862014-07-10 14:07:33 -04005100 lockdep_assert_held(&fp->fi_lock);
5101
Jeff Layton82c5ff12012-05-11 09:45:13 -04005102 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005103 return;
Jeff Layton12659652014-07-10 14:07:28 -04005104 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005105 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005106}
5107
Jeff Layton356a95e2014-07-29 21:34:13 -04005108static __be32
5109lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5110 struct nfs4_ol_stateid *ost,
5111 struct nfsd4_lock *lock,
5112 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005113{
Jeff Layton5db1c032014-07-29 21:34:28 -04005114 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005115 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005116 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5117 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005118 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005119 struct nfs4_lockowner *lo;
5120 unsigned int strhashval;
5121
Trond Myklebustd4f04892014-07-29 21:34:36 -04005122 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005123 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005124 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005125 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5126 if (lo == NULL)
5127 return nfserr_jukebox;
5128 } else {
5129 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005130 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005131 if (!cstate->minorversion &&
5132 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005133 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005134 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005135
Jeff Layton356a95e2014-07-29 21:34:13 -04005136 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005137 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005138 status = nfserr_jukebox;
5139 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005140 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005141 status = nfs_ok;
5142out:
5143 nfs4_put_stateowner(&lo->lo_owner);
5144 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005145}
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147/*
5148 * LOCK operation
5149 */
Al Virob37ad282006-10-19 23:28:59 -07005150__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005151nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005152 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005154 struct nfs4_openowner *open_sop = NULL;
5155 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005156 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005157 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005158 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005159 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005160 struct file_lock *file_lock = NULL;
5161 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005162 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005163 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005164 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005165 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005166 struct net *net = SVC_NET(rqstp);
5167 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
5169 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5170 (long long) lock->lk_offset,
5171 (long long) lock->lk_length);
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 if (check_lock_length(lock->lk_offset, lock->lk_length))
5174 return nfserr_inval;
5175
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005176 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005177 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005178 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5179 return status;
5180 }
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005183 if (nfsd4_has_session(cstate))
5184 /* See rfc 5661 18.10.3: given clientid is ignored: */
5185 memcpy(&lock->v.new.clientid,
5186 &cstate->session->se_client->cl_clientid,
5187 sizeof(clientid_t));
5188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005190 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005194 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 lock->lk_new_open_seqid,
5196 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005197 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005198 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005200 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005201 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005202 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005203 &lock->v.new.clientid))
5204 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005205 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005206 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005207 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005208 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005209 lock->lk_old_lock_seqid,
5210 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005211 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005212 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005213 if (status)
5214 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005215 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005217 lkflg = setlkflg(lock->lk_type);
5218 status = nfs4_check_openmode(lock_stp, lkflg);
5219 if (status)
5220 goto out;
5221
NeilBrown0dd395d2005-07-07 17:59:15 -07005222 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005223 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005224 goto out;
5225 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005226 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005227 goto out;
5228
Jeff Layton21179d82012-08-21 08:03:32 -04005229 file_lock = locks_alloc_lock();
5230 if (!file_lock) {
5231 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5232 status = nfserr_jukebox;
5233 goto out;
5234 }
5235
Trond Myklebust11b91642014-07-29 21:34:08 -04005236 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 switch (lock->lk_type) {
5238 case NFS4_READ_LT:
5239 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005240 spin_lock(&fp->fi_lock);
5241 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005242 if (filp)
5243 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005244 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005245 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 case NFS4_WRITE_LT:
5248 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005249 spin_lock(&fp->fi_lock);
5250 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005251 if (filp)
5252 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005253 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005254 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 default:
5257 status = nfserr_inval;
5258 goto out;
5259 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005260 if (!filp) {
5261 status = nfserr_openmode;
5262 goto out;
5263 }
Jeff Layton21179d82012-08-21 08:03:32 -04005264 file_lock->fl_owner = (fl_owner_t)lock_sop;
5265 file_lock->fl_pid = current->tgid;
5266 file_lock->fl_file = filp;
5267 file_lock->fl_flags = FL_POSIX;
5268 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5269 file_lock->fl_start = lock->lk_offset;
5270 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5271 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
Jeff Layton21179d82012-08-21 08:03:32 -04005273 conflock = locks_alloc_lock();
5274 if (!conflock) {
5275 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5276 status = nfserr_jukebox;
5277 goto out;
5278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Jeff Layton21179d82012-08-21 08:03:32 -04005280 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005281 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005283 update_stateid(&lock_stp->st_stid.sc_stateid);
5284 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005286 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005287 break;
5288 case (EAGAIN): /* conflock holds conflicting lock */
5289 status = nfserr_denied;
5290 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005291 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 case (EDEADLK):
5294 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005295 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005296 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005297 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005298 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301out:
Trond Myklebustde186432014-07-10 14:07:26 -04005302 if (filp)
5303 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005304 if (lock_stp) {
5305 /* Bump seqid manually if the 4.0 replay owner is openowner */
5306 if (cstate->replay_owner &&
5307 cstate->replay_owner != &lock_sop->lo_owner &&
5308 seqid_mutating_err(ntohl(status)))
5309 lock_sop->lo_owner.so_seqid++;
5310
5311 /*
5312 * If this is a new, never-before-used stateid, and we are
5313 * returning an error, then just go ahead and release it.
5314 */
5315 if (status && new)
5316 release_lock_stateid(lock_stp);
5317
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005318 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005319 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005320 if (open_stp)
5321 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005322 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005323 if (file_lock)
5324 locks_free_lock(file_lock);
5325 if (conflock)
5326 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 return status;
5328}
5329
5330/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005331 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5332 * so we do a temporary open here just to get an open file to pass to
5333 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5334 * inode operation.)
5335 */
Al Viro04da6e92012-04-13 00:00:04 -04005336static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005337{
5338 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005339 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5340 if (!err) {
5341 err = nfserrno(vfs_test_lock(file, lock));
5342 nfsd_close(file);
5343 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005344 return err;
5345}
5346
5347/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 * LOCKT operation
5349 */
Al Virob37ad282006-10-19 23:28:59 -07005350__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005351nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5352 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
Jeff Layton21179d82012-08-21 08:03:32 -04005354 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005355 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005356 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005357 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005359 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 return nfserr_grace;
5361
5362 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5363 return nfserr_inval;
5364
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005365 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005366 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005367 if (status)
5368 goto out;
5369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005371 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
Jeff Layton21179d82012-08-21 08:03:32 -04005374 file_lock = locks_alloc_lock();
5375 if (!file_lock) {
5376 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5377 status = nfserr_jukebox;
5378 goto out;
5379 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005380
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 switch (lockt->lt_type) {
5382 case NFS4_READ_LT:
5383 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005384 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 break;
5386 case NFS4_WRITE_LT:
5387 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005388 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 break;
5390 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005391 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 status = nfserr_inval;
5393 goto out;
5394 }
5395
Trond Myklebustd4f04892014-07-29 21:34:36 -04005396 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5397 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005398 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005399 file_lock->fl_owner = (fl_owner_t)lo;
5400 file_lock->fl_pid = current->tgid;
5401 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
Jeff Layton21179d82012-08-21 08:03:32 -04005403 file_lock->fl_start = lockt->lt_offset;
5404 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
Jeff Layton21179d82012-08-21 08:03:32 -04005406 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
Jeff Layton21179d82012-08-21 08:03:32 -04005408 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005409 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005410 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005411
Jeff Layton21179d82012-08-21 08:03:32 -04005412 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005414 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 }
5416out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005417 if (lo)
5418 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005419 if (file_lock)
5420 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 return status;
5422}
5423
Al Virob37ad282006-10-19 23:28:59 -07005424__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005425nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005426 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005428 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005430 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005431 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005432 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005433 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5434
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5436 (long long) locku->lu_offset,
5437 (long long) locku->lu_length);
5438
5439 if (check_lock_length(locku->lu_offset, locku->lu_length))
5440 return nfserr_inval;
5441
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005442 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005443 &locku->lu_stateid, NFS4_LOCK_STID,
5444 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005445 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005447 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005448 if (!filp) {
5449 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005450 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005451 }
Jeff Layton21179d82012-08-21 08:03:32 -04005452 file_lock = locks_alloc_lock();
5453 if (!file_lock) {
5454 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5455 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005456 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005457 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005458
Jeff Layton21179d82012-08-21 08:03:32 -04005459 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04005460 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04005461 file_lock->fl_pid = current->tgid;
5462 file_lock->fl_file = filp;
5463 file_lock->fl_flags = FL_POSIX;
5464 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5465 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Jeff Layton21179d82012-08-21 08:03:32 -04005467 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5468 locku->lu_length);
5469 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
Jeff Layton21179d82012-08-21 08:03:32 -04005471 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005472 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005473 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 goto out_nfserr;
5475 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005476 update_stateid(&stp->st_stid.sc_stateid);
5477 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005478fput:
5479 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005480put_stateid:
5481 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005483 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005484 if (file_lock)
5485 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 return status;
5487
5488out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005489 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005490 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491}
5492
5493/*
5494 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005495 * true: locks held by lockowner
5496 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005498static bool
5499check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500{
5501 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005502 int status = false;
5503 struct file *filp = find_any_file(fp);
5504 struct inode *inode;
5505
5506 if (!filp) {
5507 /* Any valid lock stateid should have some sort of access */
5508 WARN_ON_ONCE(1);
5509 return status;
5510 }
5511
5512 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Jeff Layton1c8c6012013-06-21 08:58:15 -04005514 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005516 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005517 status = true;
5518 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005521 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005522 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 return status;
5524}
5525
Al Virob37ad282006-10-19 23:28:59 -07005526__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005527nfsd4_release_lockowner(struct svc_rqst *rqstp,
5528 struct nfsd4_compound_state *cstate,
5529 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
5531 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005532 struct nfs4_stateowner *sop;
5533 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005534 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005536 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005537 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005538 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005539 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
5541 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5542 clid->cl_boot, clid->cl_id);
5543
Jeff Layton4b24ca72014-06-30 11:48:44 -04005544 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005545 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005546 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005547
Trond Myklebustd4f04892014-07-29 21:34:36 -04005548 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005549 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005550 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005551 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005552 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005553
5554 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005555 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005556
Jeff Layton882e9d22014-07-29 21:34:37 -04005557 /* see if there are still any locks associated with it */
5558 lo = lockowner(sop);
5559 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5560 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5561 status = nfserr_locks_held;
5562 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005563 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005564 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005565 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005566
5567 atomic_inc(&sop->so_count);
5568 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005569 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005570 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005571 if (lo)
5572 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 return status;
5574}
5575
5576static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005577alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578{
NeilBrowna55370a2005-06-23 22:03:52 -07005579 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580}
5581
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005582bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005583nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005584{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005585 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005586
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005587 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005588 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005589}
5590
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591/*
5592 * failure => all reset bets are off, nfserr_no_grace...
5593 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005594struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005595nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596{
5597 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005598 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
NeilBrowna55370a2005-06-23 22:03:52 -07005600 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5601 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005602 if (crp) {
5603 strhashval = clientstr_hashval(name);
5604 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005605 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005606 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005607 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005608 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005609 }
5610 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611}
5612
Jeff Layton2a4317c2012-03-21 16:42:43 -04005613void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005614nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005615{
5616 list_del(&crp->cr_strhash);
5617 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005618 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005619}
5620
5621void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005622nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623{
5624 struct nfs4_client_reclaim *crp = NULL;
5625 int i;
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005628 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5629 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005631 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 }
5633 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005634 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635}
5636
5637/*
5638 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005639struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005640nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
5642 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 struct nfs4_client_reclaim *crp = NULL;
5644
Jeff Layton278c9312012-11-12 15:00:52 -05005645 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Jeff Layton278c9312012-11-12 15:00:52 -05005647 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005648 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005649 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 return crp;
5651 }
5652 }
5653 return NULL;
5654}
5655
5656/*
5657* Called from OPEN. Look for clientid in reclaim list.
5658*/
Al Virob37ad282006-10-19 23:28:59 -07005659__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005660nfs4_check_open_reclaim(clientid_t *clid,
5661 struct nfsd4_compound_state *cstate,
5662 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005664 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005665
5666 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005667 status = lookup_clientid(clid, cstate, nn);
5668 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005669 return nfserr_reclaim_bad;
5670
Jeff Layton3b3e7b72014-09-12 16:40:22 -04005671 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5672 return nfserr_no_grace;
5673
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005674 if (nfsd4_client_record_check(cstate->clp))
5675 return nfserr_reclaim_bad;
5676
5677 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678}
5679
Bryan Schumaker65178db2011-11-01 13:35:21 -04005680#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005681static inline void
5682put_client(struct nfs4_client *clp)
5683{
5684 atomic_dec(&clp->cl_refcount);
5685}
5686
Jeff Layton285abde2014-07-30 08:27:24 -04005687static struct nfs4_client *
5688nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5689{
5690 struct nfs4_client *clp;
5691 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5692 nfsd_net_id);
5693
5694 if (!nfsd_netns_ready(nn))
5695 return NULL;
5696
5697 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5698 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5699 return clp;
5700 }
5701 return NULL;
5702}
5703
Jeff Layton7ec0e362014-07-30 08:27:17 -04005704u64
Jeff Layton285abde2014-07-30 08:27:24 -04005705nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005706{
5707 struct nfs4_client *clp;
5708 u64 count = 0;
5709 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5710 nfsd_net_id);
5711 char buf[INET6_ADDRSTRLEN];
5712
5713 if (!nfsd_netns_ready(nn))
5714 return 0;
5715
5716 spin_lock(&nn->client_lock);
5717 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5718 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5719 pr_info("NFS Client: %s\n", buf);
5720 ++count;
5721 }
5722 spin_unlock(&nn->client_lock);
5723
5724 return count;
5725}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005726
Jeff Laytona0926d12014-07-30 08:27:18 -04005727u64
Jeff Layton285abde2014-07-30 08:27:24 -04005728nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005729{
5730 u64 count = 0;
5731 struct nfs4_client *clp;
5732 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5733 nfsd_net_id);
5734
5735 if (!nfsd_netns_ready(nn))
5736 return count;
5737
5738 spin_lock(&nn->client_lock);
5739 clp = nfsd_find_client(addr, addr_size);
5740 if (clp) {
5741 if (mark_client_expired_locked(clp) == nfs_ok)
5742 ++count;
5743 else
5744 clp = NULL;
5745 }
5746 spin_unlock(&nn->client_lock);
5747
5748 if (clp)
5749 expire_client(clp);
5750
5751 return count;
5752}
5753
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005754u64
Jeff Layton285abde2014-07-30 08:27:24 -04005755nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005756{
5757 u64 count = 0;
5758 struct nfs4_client *clp, *next;
5759 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5760 nfsd_net_id);
5761 LIST_HEAD(reaplist);
5762
5763 if (!nfsd_netns_ready(nn))
5764 return count;
5765
5766 spin_lock(&nn->client_lock);
5767 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5768 if (mark_client_expired_locked(clp) == nfs_ok) {
5769 list_add(&clp->cl_lru, &reaplist);
5770 if (max != 0 && ++count >= max)
5771 break;
5772 }
5773 }
5774 spin_unlock(&nn->client_lock);
5775
5776 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5777 expire_client(clp);
5778
5779 return count;
5780}
5781
Bryan Schumaker184c1842012-11-29 11:40:44 -05005782static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5783 const char *type)
5784{
5785 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005786 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005787 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5788}
5789
Jeff Layton016200c2014-07-30 08:27:21 -04005790static void
5791nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5792 struct list_head *collect)
5793{
5794 struct nfs4_client *clp = lst->st_stid.sc_client;
5795 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5796 nfsd_net_id);
5797
5798 if (!collect)
5799 return;
5800
5801 lockdep_assert_held(&nn->client_lock);
5802 atomic_inc(&clp->cl_refcount);
5803 list_add(&lst->st_locks, collect);
5804}
5805
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005806static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005807 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005808 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005809{
5810 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005811 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005812 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005813 u64 count = 0;
5814
Jeff Layton016200c2014-07-30 08:27:21 -04005815 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005816 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005817 list_for_each_entry_safe(stp, st_next,
5818 &oop->oo_owner.so_stateids, st_perstateowner) {
5819 list_for_each_entry_safe(lst, lst_next,
5820 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005821 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005822 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005823 nfsd_inject_add_lock_to_list(lst,
5824 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005825 }
Jeff Layton016200c2014-07-30 08:27:21 -04005826 ++count;
5827 /*
5828 * Despite the fact that these functions deal
5829 * with 64-bit integers for "count", we must
5830 * ensure that it doesn't blow up the
5831 * clp->cl_refcount. Throw a warning if we
5832 * start to approach INT_MAX here.
5833 */
5834 WARN_ON_ONCE(count == (INT_MAX / 2));
5835 if (count == max)
5836 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005837 }
5838 }
5839 }
Jeff Layton016200c2014-07-30 08:27:21 -04005840out:
5841 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005842
5843 return count;
5844}
5845
Jeff Layton016200c2014-07-30 08:27:21 -04005846static u64
5847nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5848 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005849{
Jeff Layton016200c2014-07-30 08:27:21 -04005850 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005851}
5852
Jeff Layton016200c2014-07-30 08:27:21 -04005853static u64
5854nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005855{
Jeff Layton016200c2014-07-30 08:27:21 -04005856 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005857 nfsd_print_count(clp, count, "locked files");
5858 return count;
5859}
5860
Jeff Layton016200c2014-07-30 08:27:21 -04005861u64
Jeff Layton285abde2014-07-30 08:27:24 -04005862nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005863{
5864 struct nfs4_client *clp;
5865 u64 count = 0;
5866 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5867 nfsd_net_id);
5868
5869 if (!nfsd_netns_ready(nn))
5870 return 0;
5871
5872 spin_lock(&nn->client_lock);
5873 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5874 count += nfsd_print_client_locks(clp);
5875 spin_unlock(&nn->client_lock);
5876
5877 return count;
5878}
5879
5880static void
5881nfsd_reap_locks(struct list_head *reaplist)
5882{
5883 struct nfs4_client *clp;
5884 struct nfs4_ol_stateid *stp, *next;
5885
5886 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5887 list_del_init(&stp->st_locks);
5888 clp = stp->st_stid.sc_client;
5889 nfs4_put_stid(&stp->st_stid);
5890 put_client(clp);
5891 }
5892}
5893
5894u64
Jeff Layton285abde2014-07-30 08:27:24 -04005895nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005896{
5897 unsigned int count = 0;
5898 struct nfs4_client *clp;
5899 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5900 nfsd_net_id);
5901 LIST_HEAD(reaplist);
5902
5903 if (!nfsd_netns_ready(nn))
5904 return count;
5905
5906 spin_lock(&nn->client_lock);
5907 clp = nfsd_find_client(addr, addr_size);
5908 if (clp)
5909 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5910 spin_unlock(&nn->client_lock);
5911 nfsd_reap_locks(&reaplist);
5912 return count;
5913}
5914
5915u64
Jeff Layton285abde2014-07-30 08:27:24 -04005916nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005917{
5918 u64 count = 0;
5919 struct nfs4_client *clp;
5920 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5921 nfsd_net_id);
5922 LIST_HEAD(reaplist);
5923
5924 if (!nfsd_netns_ready(nn))
5925 return count;
5926
5927 spin_lock(&nn->client_lock);
5928 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5929 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5930 if (max != 0 && count >= max)
5931 break;
5932 }
5933 spin_unlock(&nn->client_lock);
5934 nfsd_reap_locks(&reaplist);
5935 return count;
5936}
5937
Jeff Layton82e05ef2014-07-30 08:27:22 -04005938static u64
5939nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5940 struct list_head *collect,
5941 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005942{
5943 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005944 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5945 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005946 u64 count = 0;
5947
Jeff Layton82e05ef2014-07-30 08:27:22 -04005948 lockdep_assert_held(&nn->client_lock);
5949
5950 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005951 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04005952 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005953 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04005954 if (collect) {
5955 atomic_inc(&clp->cl_refcount);
5956 list_add(&oop->oo_perclient, collect);
5957 }
5958 }
5959 ++count;
5960 /*
5961 * Despite the fact that these functions deal with
5962 * 64-bit integers for "count", we must ensure that
5963 * it doesn't blow up the clp->cl_refcount. Throw a
5964 * warning if we start to approach INT_MAX here.
5965 */
5966 WARN_ON_ONCE(count == (INT_MAX / 2));
5967 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005968 break;
5969 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04005970 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005971
5972 return count;
5973}
5974
Jeff Layton82e05ef2014-07-30 08:27:22 -04005975static u64
5976nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005977{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005978 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5979
5980 nfsd_print_count(clp, count, "openowners");
5981 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005982}
5983
Jeff Layton82e05ef2014-07-30 08:27:22 -04005984static u64
5985nfsd_collect_client_openowners(struct nfs4_client *clp,
5986 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005987{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005988 return nfsd_foreach_client_openowner(clp, max, collect,
5989 unhash_openowner_locked);
5990}
5991
5992u64
Jeff Layton285abde2014-07-30 08:27:24 -04005993nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04005994{
5995 struct nfs4_client *clp;
5996 u64 count = 0;
5997 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5998 nfsd_net_id);
5999
6000 if (!nfsd_netns_ready(nn))
6001 return 0;
6002
6003 spin_lock(&nn->client_lock);
6004 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6005 count += nfsd_print_client_openowners(clp);
6006 spin_unlock(&nn->client_lock);
6007
6008 return count;
6009}
6010
6011static void
6012nfsd_reap_openowners(struct list_head *reaplist)
6013{
6014 struct nfs4_client *clp;
6015 struct nfs4_openowner *oop, *next;
6016
6017 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6018 list_del_init(&oop->oo_perclient);
6019 clp = oop->oo_owner.so_client;
6020 release_openowner(oop);
6021 put_client(clp);
6022 }
6023}
6024
6025u64
Jeff Layton285abde2014-07-30 08:27:24 -04006026nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6027 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006028{
6029 unsigned int count = 0;
6030 struct nfs4_client *clp;
6031 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6032 nfsd_net_id);
6033 LIST_HEAD(reaplist);
6034
6035 if (!nfsd_netns_ready(nn))
6036 return count;
6037
6038 spin_lock(&nn->client_lock);
6039 clp = nfsd_find_client(addr, addr_size);
6040 if (clp)
6041 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6042 spin_unlock(&nn->client_lock);
6043 nfsd_reap_openowners(&reaplist);
6044 return count;
6045}
6046
6047u64
Jeff Layton285abde2014-07-30 08:27:24 -04006048nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006049{
6050 u64 count = 0;
6051 struct nfs4_client *clp;
6052 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6053 nfsd_net_id);
6054 LIST_HEAD(reaplist);
6055
6056 if (!nfsd_netns_ready(nn))
6057 return count;
6058
6059 spin_lock(&nn->client_lock);
6060 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6061 count += nfsd_collect_client_openowners(clp, &reaplist,
6062 max - count);
6063 if (max != 0 && count >= max)
6064 break;
6065 }
6066 spin_unlock(&nn->client_lock);
6067 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006068 return count;
6069}
6070
Bryan Schumaker269de302012-11-29 11:40:42 -05006071static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6072 struct list_head *victims)
6073{
6074 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006075 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6076 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006077 u64 count = 0;
6078
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006079 lockdep_assert_held(&nn->client_lock);
6080
6081 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006082 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006083 if (victims) {
6084 /*
6085 * It's not safe to mess with delegations that have a
6086 * non-zero dl_time. They might have already been broken
6087 * and could be processed by the laundromat outside of
6088 * the state_lock. Just leave them be.
6089 */
6090 if (dp->dl_time != 0)
6091 continue;
6092
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006093 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006094 unhash_delegation_locked(dp);
6095 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006096 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006097 ++count;
6098 /*
6099 * Despite the fact that these functions deal with
6100 * 64-bit integers for "count", we must ensure that
6101 * it doesn't blow up the clp->cl_refcount. Throw a
6102 * warning if we start to approach INT_MAX here.
6103 */
6104 WARN_ON_ONCE(count == (INT_MAX / 2));
6105 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006106 break;
6107 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006108 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006109 return count;
6110}
6111
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006112static u64
6113nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006114{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006115 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006116
6117 nfsd_print_count(clp, count, "delegations");
6118 return count;
6119}
6120
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006121u64
Jeff Layton285abde2014-07-30 08:27:24 -04006122nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006123{
6124 struct nfs4_client *clp;
6125 u64 count = 0;
6126 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6127 nfsd_net_id);
6128
6129 if (!nfsd_netns_ready(nn))
6130 return 0;
6131
6132 spin_lock(&nn->client_lock);
6133 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6134 count += nfsd_print_client_delegations(clp);
6135 spin_unlock(&nn->client_lock);
6136
6137 return count;
6138}
6139
6140static void
6141nfsd_forget_delegations(struct list_head *reaplist)
6142{
6143 struct nfs4_client *clp;
6144 struct nfs4_delegation *dp, *next;
6145
6146 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6147 list_del_init(&dp->dl_recall_lru);
6148 clp = dp->dl_stid.sc_client;
6149 revoke_delegation(dp);
6150 put_client(clp);
6151 }
6152}
6153
6154u64
Jeff Layton285abde2014-07-30 08:27:24 -04006155nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6156 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006157{
6158 u64 count = 0;
6159 struct nfs4_client *clp;
6160 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6161 nfsd_net_id);
6162 LIST_HEAD(reaplist);
6163
6164 if (!nfsd_netns_ready(nn))
6165 return count;
6166
6167 spin_lock(&nn->client_lock);
6168 clp = nfsd_find_client(addr, addr_size);
6169 if (clp)
6170 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6171 spin_unlock(&nn->client_lock);
6172
6173 nfsd_forget_delegations(&reaplist);
6174 return count;
6175}
6176
6177u64
Jeff Layton285abde2014-07-30 08:27:24 -04006178nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006179{
6180 u64 count = 0;
6181 struct nfs4_client *clp;
6182 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6183 nfsd_net_id);
6184 LIST_HEAD(reaplist);
6185
6186 if (!nfsd_netns_ready(nn))
6187 return count;
6188
6189 spin_lock(&nn->client_lock);
6190 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6191 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6192 if (max != 0 && count >= max)
6193 break;
6194 }
6195 spin_unlock(&nn->client_lock);
6196 nfsd_forget_delegations(&reaplist);
6197 return count;
6198}
6199
6200static void
6201nfsd_recall_delegations(struct list_head *reaplist)
6202{
6203 struct nfs4_client *clp;
6204 struct nfs4_delegation *dp, *next;
6205
6206 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6207 list_del_init(&dp->dl_recall_lru);
6208 clp = dp->dl_stid.sc_client;
6209 /*
6210 * We skipped all entries that had a zero dl_time before,
6211 * so we can now reset the dl_time back to 0. If a delegation
6212 * break comes in now, then it won't make any difference since
6213 * we're recalling it either way.
6214 */
6215 spin_lock(&state_lock);
6216 dp->dl_time = 0;
6217 spin_unlock(&state_lock);
6218 nfsd_break_one_deleg(dp);
6219 put_client(clp);
6220 }
6221}
6222
6223u64
Jeff Layton285abde2014-07-30 08:27:24 -04006224nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006225 size_t addr_size)
6226{
6227 u64 count = 0;
6228 struct nfs4_client *clp;
6229 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6230 nfsd_net_id);
6231 LIST_HEAD(reaplist);
6232
6233 if (!nfsd_netns_ready(nn))
6234 return count;
6235
6236 spin_lock(&nn->client_lock);
6237 clp = nfsd_find_client(addr, addr_size);
6238 if (clp)
6239 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6240 spin_unlock(&nn->client_lock);
6241
6242 nfsd_recall_delegations(&reaplist);
6243 return count;
6244}
6245
6246u64
Jeff Layton285abde2014-07-30 08:27:24 -04006247nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006248{
6249 u64 count = 0;
6250 struct nfs4_client *clp, *next;
6251 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6252 nfsd_net_id);
6253 LIST_HEAD(reaplist);
6254
6255 if (!nfsd_netns_ready(nn))
6256 return count;
6257
6258 spin_lock(&nn->client_lock);
6259 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6260 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6261 if (max != 0 && ++count >= max)
6262 break;
6263 }
6264 spin_unlock(&nn->client_lock);
6265 nfsd_recall_delegations(&reaplist);
6266 return count;
6267}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006268#endif /* CONFIG_NFSD_FAULT_INJECTION */
6269
Meelap Shahc2f1a552007-07-17 04:04:39 -07006270/*
6271 * Since the lifetime of a delegation isn't limited to that of an open, a
6272 * client may quite reasonably hang on to a delegation as long as it has
6273 * the inode cached. This becomes an obvious problem the first time a
6274 * client's inode cache approaches the size of the server's total memory.
6275 *
6276 * For now we avoid this problem by imposing a hard limit on the number
6277 * of delegations, which varies according to the server's memory size.
6278 */
6279static void
6280set_max_delegations(void)
6281{
6282 /*
6283 * Allow at most 4 delegations per megabyte of RAM. Quick
6284 * estimates suggest that in the worst case (where every delegation
6285 * is for a different inode), a delegation could take about 1.5K,
6286 * giving a worst case usage of about 6% of memory.
6287 */
6288 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6289}
6290
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006291static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006292{
6293 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6294 int i;
6295
6296 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6297 CLIENT_HASH_SIZE, GFP_KERNEL);
6298 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006299 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006300 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6301 CLIENT_HASH_SIZE, GFP_KERNEL);
6302 if (!nn->unconf_id_hashtbl)
6303 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006304 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6305 SESSION_HASH_SIZE, GFP_KERNEL);
6306 if (!nn->sessionid_hashtbl)
6307 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006308
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006309 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006310 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006311 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006312 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006313 for (i = 0; i < SESSION_HASH_SIZE; i++)
6314 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006315 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006316 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006317 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006318 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006319 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006320 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006321
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006322 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006323 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006324
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006325 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006326
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006327err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006328 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006329err_unconf_id:
6330 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006331err:
6332 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006333}
6334
6335static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006336nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006337{
6338 int i;
6339 struct nfs4_client *clp = NULL;
6340 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6341
6342 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6343 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6344 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6345 destroy_client(clp);
6346 }
6347 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006348
Kinglong Mee2b905632014-03-26 22:09:30 +08006349 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6350 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6351 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6352 destroy_client(clp);
6353 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006354 }
6355
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006356 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006357 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006358 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006359 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006360}
6361
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006362int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006363nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006364{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006365 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006366 int ret;
6367
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006368 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006369 if (ret)
6370 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006371 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006372 nn->grace_ended = false;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006373 locks_start_grace(net, &nn->nfsd4_manager);
6374 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006375 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006376 nn->nfsd4_grace, net);
6377 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006378 return 0;
6379}
6380
6381/* initialization to perform when the nfsd service is started: */
6382
6383int
6384nfs4_state_start(void)
6385{
6386 int ret;
6387
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006388 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006389 if (ret)
6390 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006391 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006392 if (laundry_wq == NULL) {
6393 ret = -ENOMEM;
6394 goto out_recovery;
6395 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006396 ret = nfsd4_create_callback_queue();
6397 if (ret)
6398 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006399
Meelap Shahc2f1a552007-07-17 04:04:39 -07006400 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006401
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006402 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006403
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006404out_free_laundry:
6405 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006406out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408}
6409
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006410void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006411nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006415 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006417 cancel_delayed_work_sync(&nn->laundromat_work);
6418 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006419
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006421 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006422 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006424 unhash_delegation_locked(dp);
6425 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 }
Benny Halevycdc97502014-05-30 09:09:30 -04006427 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 list_for_each_safe(pos, next, &reaplist) {
6429 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006430 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006431 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006432 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 }
6434
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006435 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006436 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437}
6438
6439void
6440nfs4_state_shutdown(void)
6441{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006442 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006443 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006445
6446static void
6447get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6448{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006449 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6450 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006451}
6452
6453static void
6454put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6455{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006456 if (cstate->minorversion) {
6457 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6458 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6459 }
6460}
6461
6462void
6463clear_current_stateid(struct nfsd4_compound_state *cstate)
6464{
6465 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006466}
6467
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006468/*
6469 * functions to set current state id
6470 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006471void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006472nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6473{
6474 put_stateid(cstate, &odp->od_stateid);
6475}
6476
6477void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006478nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6479{
6480 put_stateid(cstate, &open->op_stateid);
6481}
6482
6483void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006484nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6485{
6486 put_stateid(cstate, &close->cl_stateid);
6487}
6488
6489void
6490nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6491{
6492 put_stateid(cstate, &lock->lk_resp_stateid);
6493}
6494
6495/*
6496 * functions to consume current state id
6497 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006498
6499void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006500nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6501{
6502 get_stateid(cstate, &odp->od_stateid);
6503}
6504
6505void
6506nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6507{
6508 get_stateid(cstate, &drp->dr_stateid);
6509}
6510
6511void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006512nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6513{
6514 get_stateid(cstate, &fsp->fr_stateid);
6515}
6516
6517void
6518nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6519{
6520 get_stateid(cstate, &setattr->sa_stateid);
6521}
6522
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006523void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006524nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6525{
6526 get_stateid(cstate, &close->cl_stateid);
6527}
6528
6529void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006530nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006531{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006532 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006533}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006534
6535void
6536nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6537{
6538 get_stateid(cstate, &read->rd_stateid);
6539}
6540
6541void
6542nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6543{
6544 get_stateid(cstate, &write->wr_stateid);
6545}