blob: d1b851548b7afc77fbfb75a523a21fbf558698a5 [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
Kinglong Meeb5971af2014-08-22 10:18:43 -0400219static inline struct nfs4_stateowner *
220nfs4_get_stateowner(struct nfs4_stateowner *sop)
221{
222 atomic_inc(&sop->so_count);
223 return sop;
224}
225
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400226static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400227same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400228{
229 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400230 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400231}
232
233static struct nfs4_openowner *
234find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400235 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400236{
237 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400238
Trond Myklebustd4f04892014-07-29 21:34:36 -0400239 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400240
Trond Myklebustd4f04892014-07-29 21:34:36 -0400241 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
242 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400243 if (!so->so_is_open_owner)
244 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400245 if (same_owner_str(so, &open->op_owner))
246 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400247 }
248 return NULL;
249}
250
251static struct nfs4_openowner *
252find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400253 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400254{
255 struct nfs4_openowner *oo;
256
Trond Myklebustd4f04892014-07-29 21:34:36 -0400257 spin_lock(&clp->cl_lock);
258 oo = find_openstateowner_str_locked(hashval, open, clp);
259 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400260 return oo;
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static inline u32
264opaque_hashval(const void *ptr, int nbytes)
265{
266 unsigned char *cptr = (unsigned char *) ptr;
267
268 u32 x = 0;
269 while (nbytes--) {
270 x *= 37;
271 x += *cptr++;
272 }
273 return x;
274}
275
J. Bruce Fields32513b42011-10-13 16:00:16 -0400276static void nfsd4_free_file(struct nfs4_file *f)
277{
278 kmem_cache_free(file_slab, f);
279}
280
NeilBrown13cd2182005-06-23 22:03:10 -0700281static inline void
282put_nfs4_file(struct nfs4_file *fi)
283{
Jeff Layton02e12152014-07-16 10:31:57 -0400284 might_lock(&state_lock);
285
Benny Halevycdc97502014-05-30 09:09:30 -0400286 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400287 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400288 spin_unlock(&state_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400289 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800290 }
NeilBrown13cd2182005-06-23 22:03:10 -0700291}
292
293static inline void
294get_nfs4_file(struct nfs4_file *fi)
295{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800296 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700297}
298
Trond Myklebustde186432014-07-10 14:07:26 -0400299static struct file *
300__nfs4_get_fd(struct nfs4_file *f, int oflag)
301{
302 if (f->fi_fds[oflag])
303 return get_file(f->fi_fds[oflag]);
304 return NULL;
305}
306
307static struct file *
308find_writeable_file_locked(struct nfs4_file *f)
309{
310 struct file *ret;
311
312 lockdep_assert_held(&f->fi_lock);
313
314 ret = __nfs4_get_fd(f, O_WRONLY);
315 if (!ret)
316 ret = __nfs4_get_fd(f, O_RDWR);
317 return ret;
318}
319
320static struct file *
321find_writeable_file(struct nfs4_file *f)
322{
323 struct file *ret;
324
325 spin_lock(&f->fi_lock);
326 ret = find_writeable_file_locked(f);
327 spin_unlock(&f->fi_lock);
328
329 return ret;
330}
331
332static struct file *find_readable_file_locked(struct nfs4_file *f)
333{
334 struct file *ret;
335
336 lockdep_assert_held(&f->fi_lock);
337
338 ret = __nfs4_get_fd(f, O_RDONLY);
339 if (!ret)
340 ret = __nfs4_get_fd(f, O_RDWR);
341 return ret;
342}
343
344static struct file *
345find_readable_file(struct nfs4_file *f)
346{
347 struct file *ret;
348
349 spin_lock(&f->fi_lock);
350 ret = find_readable_file_locked(f);
351 spin_unlock(&f->fi_lock);
352
353 return ret;
354}
355
356static struct file *
357find_any_file(struct nfs4_file *f)
358{
359 struct file *ret;
360
361 spin_lock(&f->fi_lock);
362 ret = __nfs4_get_fd(f, O_RDWR);
363 if (!ret) {
364 ret = __nfs4_get_fd(f, O_WRONLY);
365 if (!ret)
366 ret = __nfs4_get_fd(f, O_RDONLY);
367 }
368 spin_unlock(&f->fi_lock);
369 return ret;
370}
371
Trond Myklebust02a35082014-07-25 07:34:22 -0400372static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800373unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700374
375/*
376 * Open owner state (share locks)
377 */
378
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500379/* hash tables for lock and open owners */
380#define OWNER_HASH_BITS 8
381#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
382#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700383
Trond Myklebustd4f04892014-07-29 21:34:36 -0400384static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400385{
386 unsigned int ret;
387
388 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500389 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400390}
NeilBrownef0f3392006-04-10 22:55:41 -0700391
NeilBrownef0f3392006-04-10 22:55:41 -0700392/* hash table for nfs4_file */
393#define FILE_HASH_BITS 8
394#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800395
Trond Myklebustca943212014-07-23 16:17:39 -0400396static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400397{
Trond Myklebustca943212014-07-23 16:17:39 -0400398 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
399}
400
401static unsigned int file_hashval(struct knfsd_fh *fh)
402{
403 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
404}
405
406static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
407{
408 return fh1->fh_size == fh2->fh_size &&
409 !memcmp(fh1->fh_base.fh_pad,
410 fh2->fh_base.fh_pad,
411 fh1->fh_size);
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400412}
413
Jeff Layton89876f82013-04-02 09:01:59 -0400414static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700415
Jeff Layton12659652014-07-10 14:07:28 -0400416static void
417__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400418{
Jeff Layton7214e862014-07-10 14:07:33 -0400419 lockdep_assert_held(&fp->fi_lock);
420
Jeff Layton12659652014-07-10 14:07:28 -0400421 if (access & NFS4_SHARE_ACCESS_WRITE)
422 atomic_inc(&fp->fi_access[O_WRONLY]);
423 if (access & NFS4_SHARE_ACCESS_READ)
424 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400425}
426
Jeff Layton12659652014-07-10 14:07:28 -0400427static __be32
428nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400429{
Jeff Layton7214e862014-07-10 14:07:33 -0400430 lockdep_assert_held(&fp->fi_lock);
431
Jeff Layton12659652014-07-10 14:07:28 -0400432 /* Does this access mode make sense? */
433 if (access & ~NFS4_SHARE_ACCESS_BOTH)
434 return nfserr_inval;
435
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400436 /* Does it conflict with a deny mode already set? */
437 if ((access & fp->fi_share_deny) != 0)
438 return nfserr_share_denied;
439
Jeff Layton12659652014-07-10 14:07:28 -0400440 __nfs4_file_get_access(fp, access);
441 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400442}
443
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400444static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
445{
446 /* Common case is that there is no deny mode. */
447 if (deny) {
448 /* Does this deny mode make sense? */
449 if (deny & ~NFS4_SHARE_DENY_BOTH)
450 return nfserr_inval;
451
452 if ((deny & NFS4_SHARE_DENY_READ) &&
453 atomic_read(&fp->fi_access[O_RDONLY]))
454 return nfserr_share_denied;
455
456 if ((deny & NFS4_SHARE_DENY_WRITE) &&
457 atomic_read(&fp->fi_access[O_WRONLY]))
458 return nfserr_share_denied;
459 }
460 return nfs_ok;
461}
462
J. Bruce Fields998db522010-08-07 09:21:41 -0400463static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400464{
Trond Myklebustde186432014-07-10 14:07:26 -0400465 might_lock(&fp->fi_lock);
466
467 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
468 struct file *f1 = NULL;
469 struct file *f2 = NULL;
470
Jeff Layton6d338b52014-07-10 14:07:29 -0400471 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400472 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400473 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400474 spin_unlock(&fp->fi_lock);
475 if (f1)
476 fput(f1);
477 if (f2)
478 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400479 }
480}
481
Jeff Layton12659652014-07-10 14:07:28 -0400482static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400483{
Jeff Layton12659652014-07-10 14:07:28 -0400484 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
485
486 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400487 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400488 if (access & NFS4_SHARE_ACCESS_READ)
489 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400490}
491
Trond Myklebust60116952014-07-29 21:34:06 -0400492static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
493 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400494{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500495 struct nfs4_stid *stid;
496 int new_id;
497
Trond Myklebustf8338832014-07-25 07:34:19 -0400498 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500499 if (!stid)
500 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400501
Jeff Layton4770d722014-07-29 21:34:10 -0400502 idr_preload(GFP_KERNEL);
503 spin_lock(&cl->cl_lock);
504 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
505 spin_unlock(&cl->cl_lock);
506 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700507 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500508 goto out_free;
509 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500510 stid->sc_stateid.si_opaque.so_id = new_id;
511 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
512 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400513 atomic_set(&stid->sc_count, 1);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500514
J. Bruce Fields996e0932011-10-17 11:14:48 -0400515 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500516 * It shouldn't be a problem to reuse an opaque stateid value.
517 * I don't think it is for 4.1. But with 4.0 I worry that, for
518 * example, a stray write retransmission could be accepted by
519 * the server when it should have been rejected. Therefore,
520 * adopt a trick from the sctp code to attempt to maximize the
521 * amount of time until an id is reused, by ensuring they always
522 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400523 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500524 return stid;
525out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800526 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500527 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400528}
529
Jeff Laytonb49e0842014-07-29 21:34:11 -0400530static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400531{
Trond Myklebust60116952014-07-29 21:34:06 -0400532 struct nfs4_stid *stid;
533 struct nfs4_ol_stateid *stp;
534
535 stid = nfs4_alloc_stid(clp, stateid_slab);
536 if (!stid)
537 return NULL;
538
539 stp = openlockstateid(stid);
540 stp->st_stid.sc_free = nfs4_free_ol_stateid;
541 return stp;
542}
543
544static void nfs4_free_deleg(struct nfs4_stid *stid)
545{
Trond Myklebust60116952014-07-29 21:34:06 -0400546 kmem_cache_free(deleg_slab, stid);
547 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400548}
549
NeilBrown6282cd52014-06-04 17:39:26 +1000550/*
551 * When we recall a delegation, we should be careful not to hand it
552 * out again straight away.
553 * To ensure this we keep a pair of bloom filters ('new' and 'old')
554 * in which the filehandles of recalled delegations are "stored".
555 * If a filehandle appear in either filter, a delegation is blocked.
556 * When a delegation is recalled, the filehandle is stored in the "new"
557 * filter.
558 * Every 30 seconds we swap the filters and clear the "new" one,
559 * unless both are empty of course.
560 *
561 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
562 * low 3 bytes as hash-table indices.
563 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400564 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000565 * is used to manage concurrent access. Testing does not need the lock
566 * except when swapping the two filters.
567 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400568static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000569static struct bloom_pair {
570 int entries, old_entries;
571 time_t swap_time;
572 int new; /* index into 'set' */
573 DECLARE_BITMAP(set[2], 256);
574} blocked_delegations;
575
576static int delegation_blocked(struct knfsd_fh *fh)
577{
578 u32 hash;
579 struct bloom_pair *bd = &blocked_delegations;
580
581 if (bd->entries == 0)
582 return 0;
583 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400584 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000585 if (seconds_since_boot() - bd->swap_time > 30) {
586 bd->entries -= bd->old_entries;
587 bd->old_entries = bd->entries;
588 memset(bd->set[bd->new], 0,
589 sizeof(bd->set[0]));
590 bd->new = 1-bd->new;
591 bd->swap_time = seconds_since_boot();
592 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400593 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000594 }
595 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
596 if (test_bit(hash&255, bd->set[0]) &&
597 test_bit((hash>>8)&255, bd->set[0]) &&
598 test_bit((hash>>16)&255, bd->set[0]))
599 return 1;
600
601 if (test_bit(hash&255, bd->set[1]) &&
602 test_bit((hash>>8)&255, bd->set[1]) &&
603 test_bit((hash>>16)&255, bd->set[1]))
604 return 1;
605
606 return 0;
607}
608
609static void block_delegations(struct knfsd_fh *fh)
610{
611 u32 hash;
612 struct bloom_pair *bd = &blocked_delegations;
613
614 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
615
Jeff Laytonf54fe962014-07-25 07:34:26 -0400616 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000617 __set_bit(hash&255, bd->set[bd->new]);
618 __set_bit((hash>>8)&255, bd->set[bd->new]);
619 __set_bit((hash>>16)&255, bd->set[bd->new]);
620 if (bd->entries == 0)
621 bd->swap_time = seconds_since_boot();
622 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400623 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static struct nfs4_delegation *
Jeff Laytonf9416e22014-07-25 07:34:23 -0400627alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400630 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400633 n = atomic_long_inc_return(&num_delegations);
634 if (n < 0 || n > max_delegations)
635 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000636 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400637 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400638 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700639 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400640 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400641
642 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400643 /*
644 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400645 * meaning of seqid 0 isn't meaningful, really, but let's avoid
646 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400647 */
648 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700649 INIT_LIST_HEAD(&dp->dl_perfile);
650 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400652 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Jeff Layton02e12152014-07-16 10:31:57 -0400653 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400655out_dec:
656 atomic_long_dec(&num_delegations);
657 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660void
Trond Myklebust60116952014-07-29 21:34:06 -0400661nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Trond Myklebust11b91642014-07-29 21:34:08 -0400663 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400664 struct nfs4_client *clp = s->sc_client;
665
Jeff Layton4770d722014-07-29 21:34:10 -0400666 might_lock(&clp->cl_lock);
667
Jeff Laytonb401be222014-07-29 21:34:33 -0400668 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
669 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400670 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400671 }
Trond Myklebust60116952014-07-29 21:34:06 -0400672 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400673 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400674 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400675 if (fp)
676 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500679static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400681 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400682
Jeff Layton6bcc0342014-08-09 10:22:40 -0400683 spin_lock(&fp->fi_lock);
Jeff Layton0c637be2014-08-22 12:05:43 -0400684 if (fp->fi_deleg_file && atomic_dec_and_test(&fp->fi_delegees))
Jeff Layton6bcc0342014-08-09 10:22:40 -0400685 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400686 spin_unlock(&fp->fi_lock);
687
688 if (filp) {
Jeff Laytone6f5c782014-08-22 10:40:25 -0400689 vfs_setlease(filp, F_UNLCK, NULL, NULL);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400690 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400694static void unhash_stid(struct nfs4_stid *s)
695{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500696 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400697}
698
Benny Halevy931ee562014-05-30 09:09:27 -0400699static void
700hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
701{
Benny Halevycdc97502014-05-30 09:09:30 -0400702 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400703 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400704
Trond Myklebust67cb1272014-07-29 21:34:17 -0400705 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400706 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400707 list_add(&dp->dl_perfile, &fp->fi_delegations);
708 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static void
Jeff Layton42690672014-07-25 07:34:20 -0400712unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Trond Myklebust11b91642014-07-29 21:34:08 -0400714 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400715
Jeff Layton42690672014-07-25 07:34:20 -0400716 lockdep_assert_held(&state_lock);
717
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400718 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400719 /* Ensure that deleg break won't try to requeue it */
720 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400721 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400722 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400724 list_del_init(&dp->dl_perfile);
725 spin_unlock(&fp->fi_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400726}
727
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400728static void destroy_delegation(struct nfs4_delegation *dp)
729{
Jeff Layton42690672014-07-25 07:34:20 -0400730 spin_lock(&state_lock);
731 unhash_delegation_locked(dp);
732 spin_unlock(&state_lock);
Jeff Laytonafbda402014-08-09 10:22:41 -0400733 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -0400734 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400735}
736
737static void revoke_delegation(struct nfs4_delegation *dp)
738{
739 struct nfs4_client *clp = dp->dl_stid.sc_client;
740
Jeff Layton2d4a5322014-07-25 07:34:21 -0400741 WARN_ON(!list_empty(&dp->dl_recall_lru));
742
Jeff Laytonafbda402014-08-09 10:22:41 -0400743 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
744
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400745 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400746 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400747 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400748 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400749 spin_lock(&clp->cl_lock);
750 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
751 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400752 }
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*
756 * SETCLIENTID state
757 */
758
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400759static unsigned int clientid_hashval(u32 id)
760{
761 return id & CLIENT_HASH_MASK;
762}
763
764static unsigned int clientstr_hashval(const char *name)
765{
766 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400770 * We store the NONE, READ, WRITE, and BOTH bits separately in the
771 * st_{access,deny}_bmap field of the stateid, in order to track not
772 * only what share bits are currently in force, but also what
773 * combinations of share bits previous opens have used. This allows us
774 * to enforce the recommendation of rfc 3530 14.2.19 that the server
775 * return an error if the client attempt to downgrade to a combination
776 * of share bits not explicable by closing some of its previous opens.
777 *
778 * XXX: This enforcement is actually incomplete, since we don't keep
779 * track of access/deny bit combinations; so, e.g., we allow:
780 *
781 * OPEN allow read, deny write
782 * OPEN allow both, deny none
783 * DOWNGRADE allow read, deny none
784 *
785 * which we should reject.
786 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400787static unsigned int
788bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400789 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400790 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400791
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400792 for (i = 1; i < 4; i++) {
793 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400794 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400795 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400796 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400797}
798
Jeff Layton82c5ff12012-05-11 09:45:13 -0400799/* set share access for a given stateid */
800static inline void
801set_access(u32 access, struct nfs4_ol_stateid *stp)
802{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400803 unsigned char mask = 1 << access;
804
805 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
806 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400807}
808
809/* clear share access for a given stateid */
810static inline void
811clear_access(u32 access, struct nfs4_ol_stateid *stp)
812{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400813 unsigned char mask = 1 << access;
814
815 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
816 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -0400817}
818
819/* test whether a given stateid has access */
820static inline bool
821test_access(u32 access, struct nfs4_ol_stateid *stp)
822{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400823 unsigned char mask = 1 << access;
824
825 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400826}
827
Jeff Laytonce0fc432012-05-11 09:45:14 -0400828/* set share deny for a given stateid */
829static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400830set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400831{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400832 unsigned char mask = 1 << deny;
833
834 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
835 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400836}
837
838/* clear share deny for a given stateid */
839static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -0400840clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400841{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400842 unsigned char mask = 1 << deny;
843
844 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
845 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -0400846}
847
848/* test whether a given stateid is denying specific access */
849static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -0400850test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -0400851{
Jeff Laytonc11c5912014-07-10 14:07:30 -0400852 unsigned char mask = 1 << deny;
853
854 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400855}
856
857static int nfs4_access_to_omode(u32 access)
858{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400859 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400860 case NFS4_SHARE_ACCESS_READ:
861 return O_RDONLY;
862 case NFS4_SHARE_ACCESS_WRITE:
863 return O_WRONLY;
864 case NFS4_SHARE_ACCESS_BOTH:
865 return O_RDWR;
866 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500867 WARN_ON_ONCE(1);
868 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400869}
870
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400871/*
872 * A stateid that had a deny mode associated with it is being released
873 * or downgraded. Recalculate the deny mode on the file.
874 */
875static void
876recalculate_deny_mode(struct nfs4_file *fp)
877{
878 struct nfs4_ol_stateid *stp;
879
880 spin_lock(&fp->fi_lock);
881 fp->fi_share_deny = 0;
882 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
883 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
884 spin_unlock(&fp->fi_lock);
885}
886
887static void
888reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
889{
890 int i;
891 bool change = false;
892
893 for (i = 1; i < 4; i++) {
894 if ((i & deny) != i) {
895 change = true;
896 clear_deny(i, stp);
897 }
898 }
899
900 /* Recalculate per-file deny mode if there was a change */
901 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -0400902 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400903}
904
Jeff Layton82c5ff12012-05-11 09:45:13 -0400905/* release all access and file references for a given stateid */
906static void
907release_all_access(struct nfs4_ol_stateid *stp)
908{
909 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -0400910 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400911
912 if (fp && stp->st_deny_bmap != 0)
913 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400914
915 for (i = 1; i < 4; i++) {
916 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -0400917 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -0400918 clear_access(i, stp);
919 }
920}
921
Jeff Layton6b180f02014-07-29 21:34:26 -0400922static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
923{
Jeff Laytona819ecc2014-07-29 21:34:38 -0400924 struct nfs4_client *clp = sop->so_client;
925
926 might_lock(&clp->cl_lock);
927
928 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -0400929 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -0400930 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -0400931 spin_unlock(&clp->cl_lock);
Jeff Layton6b180f02014-07-29 21:34:26 -0400932 kfree(sop->so_owner.data);
933 sop->so_ops->so_free(sop);
934}
935
Jeff Layton4ae098d2014-07-29 21:34:43 -0400936static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500937{
Trond Myklebust11b91642014-07-29 21:34:08 -0400938 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -0400939
Jeff Layton1c755dc2014-07-29 21:34:12 -0400940 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
941
Trond Myklebust1d31a252014-07-10 14:07:25 -0400942 spin_lock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500943 list_del(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -0400944 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500945 list_del(&stp->st_perstateowner);
946}
947
Trond Myklebust60116952014-07-29 21:34:06 -0400948static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500949{
Trond Myklebust60116952014-07-29 21:34:06 -0400950 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400951
Trond Myklebust60116952014-07-29 21:34:06 -0400952 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -0400953 if (stp->st_stateowner)
954 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -0400955 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500956}
957
Jeff Laytonb49e0842014-07-29 21:34:11 -0400958static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500959{
Jeff Laytonb49e0842014-07-29 21:34:11 -0400960 struct nfs4_ol_stateid *stp = openlockstateid(stid);
961 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500962 struct file *file;
963
Jeff Laytonb49e0842014-07-29 21:34:11 -0400964 file = find_any_file(stp->st_stid.sc_file);
965 if (file)
966 filp_close(file, (fl_owner_t)lo);
967 nfs4_free_ol_stateid(stid);
968}
969
Jeff Layton2c41beb2014-07-29 21:34:41 -0400970/*
971 * Put the persistent reference to an already unhashed generic stateid, while
972 * holding the cl_lock. If it's the last reference, then put it onto the
973 * reaplist for later destruction.
974 */
975static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
976 struct list_head *reaplist)
977{
978 struct nfs4_stid *s = &stp->st_stid;
979 struct nfs4_client *clp = s->sc_client;
980
981 lockdep_assert_held(&clp->cl_lock);
982
983 WARN_ON_ONCE(!list_empty(&stp->st_locks));
984
985 if (!atomic_dec_and_test(&s->sc_count)) {
986 wake_up_all(&close_wq);
987 return;
988 }
989
990 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
991 list_add(&stp->st_locks, reaplist);
992}
993
Jeff Layton3c1c9952014-07-29 21:34:39 -0400994static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
995{
996 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
997
998 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
999
1000 list_del_init(&stp->st_locks);
Jeff Layton4ae098d2014-07-29 21:34:43 -04001001 unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001002 unhash_stid(&stp->st_stid);
1003}
1004
Jeff Layton5adfd882014-07-29 21:34:31 -04001005static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001006{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001007 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1008
1009 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001010 unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001011 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001012 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001013}
1014
Trond Myklebustc58c6612014-07-29 21:34:35 -04001015static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001016{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001017 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001018
Trond Myklebustd4f04892014-07-29 21:34:36 -04001019 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001020
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001021 list_del_init(&lo->lo_owner.so_strhash);
1022}
1023
Jeff Layton2c41beb2014-07-29 21:34:41 -04001024/*
1025 * Free a list of generic stateids that were collected earlier after being
1026 * fully unhashed.
1027 */
1028static void
1029free_ol_stateid_reaplist(struct list_head *reaplist)
1030{
1031 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001032 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001033
1034 might_sleep();
1035
1036 while (!list_empty(reaplist)) {
1037 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1038 st_locks);
1039 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001040 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001041 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001042 if (fp)
1043 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001044 }
1045}
1046
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001047static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001048{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001049 struct nfs4_client *clp = lo->lo_owner.so_client;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001050 struct nfs4_ol_stateid *stp;
1051 struct list_head reaplist;
1052
1053 INIT_LIST_HEAD(&reaplist);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001054
Trond Myklebustd4f04892014-07-29 21:34:36 -04001055 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001056 unhash_lockowner_locked(lo);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001057 while (!list_empty(&lo->lo_owner.so_stateids)) {
1058 stp = list_first_entry(&lo->lo_owner.so_stateids,
1059 struct nfs4_ol_stateid, st_perstateowner);
1060 unhash_lock_stateid(stp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001061 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001062 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001063 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001064 free_ol_stateid_reaplist(&reaplist);
Jeff Layton6b180f02014-07-29 21:34:26 -04001065 nfs4_put_stateowner(&lo->lo_owner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001066}
1067
Jeff Laytond83017f2014-07-29 21:34:42 -04001068static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1069 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001070{
1071 struct nfs4_ol_stateid *stp;
1072
1073 while (!list_empty(&open_stp->st_locks)) {
1074 stp = list_entry(open_stp->st_locks.next,
1075 struct nfs4_ol_stateid, st_locks);
Jeff Laytond83017f2014-07-29 21:34:42 -04001076 unhash_lock_stateid(stp);
1077 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001078 }
1079}
1080
Jeff Laytond83017f2014-07-29 21:34:42 -04001081static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1082 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001083{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001084 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1085
Jeff Layton4ae098d2014-07-29 21:34:43 -04001086 unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001087 release_open_stateid_locks(stp, reaplist);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001088}
1089
1090static void release_open_stateid(struct nfs4_ol_stateid *stp)
1091{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001092 LIST_HEAD(reaplist);
1093
1094 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04001095 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001096 put_ol_stateid_locked(stp, &reaplist);
1097 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1098 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001099}
1100
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001101static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001102{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001103 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001104
Trond Myklebustd4f04892014-07-29 21:34:36 -04001105 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001106
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001107 list_del_init(&oo->oo_owner.so_strhash);
1108 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001109}
1110
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001111static void release_last_closed_stateid(struct nfs4_openowner *oo)
1112{
Jeff Layton217526e2014-07-30 08:27:11 -04001113 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1114 nfsd_net_id);
1115 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001116
Jeff Layton217526e2014-07-30 08:27:11 -04001117 spin_lock(&nn->client_lock);
1118 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001119 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001120 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001121 oo->oo_last_closed_stid = NULL;
1122 }
Jeff Layton217526e2014-07-30 08:27:11 -04001123 spin_unlock(&nn->client_lock);
1124 if (s)
1125 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001126}
1127
Jeff Layton2c41beb2014-07-29 21:34:41 -04001128static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001129{
1130 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001131 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001132 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001133
Jeff Layton2c41beb2014-07-29 21:34:41 -04001134 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001135
Trond Myklebustd4f04892014-07-29 21:34:36 -04001136 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001137 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001138 while (!list_empty(&oo->oo_owner.so_stateids)) {
1139 stp = list_first_entry(&oo->oo_owner.so_stateids,
1140 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytond83017f2014-07-29 21:34:42 -04001141 unhash_open_stateid(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001142 put_ol_stateid_locked(stp, &reaplist);
1143 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001144 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001145 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001146 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001147 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001148}
1149
Marc Eshel5282fd72009-04-03 08:27:52 +03001150static inline int
1151hash_sessionid(struct nfs4_sessionid *sessionid)
1152{
1153 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1154
1155 return sid->sequence % SESSION_HASH_SIZE;
1156}
1157
Trond Myklebust8f199b82012-03-20 15:11:17 -04001158#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001159static inline void
1160dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1161{
1162 u32 *ptr = (u32 *)(&sessionid->data[0]);
1163 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1164}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001165#else
1166static inline void
1167dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1168{
1169}
1170#endif
1171
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001172/*
1173 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1174 * won't be used for replay.
1175 */
1176void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1177{
1178 struct nfs4_stateowner *so = cstate->replay_owner;
1179
1180 if (nfserr == nfserr_replay_me)
1181 return;
1182
1183 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001184 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001185 return;
1186 }
1187 if (!so)
1188 return;
1189 if (so->so_is_open_owner)
1190 release_last_closed_stateid(openowner(so));
1191 so->so_seqid++;
1192 return;
1193}
Marc Eshel5282fd72009-04-03 08:27:52 +03001194
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001195static void
1196gen_sessionid(struct nfsd4_session *ses)
1197{
1198 struct nfs4_client *clp = ses->se_client;
1199 struct nfsd4_sessionid *sid;
1200
1201 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1202 sid->clientid = clp->cl_clientid;
1203 sid->sequence = current_sessionid++;
1204 sid->reserved = 0;
1205}
1206
1207/*
Andy Adamsona6496372009-08-28 08:45:01 -04001208 * The protocol defines ca_maxresponssize_cached to include the size of
1209 * the rpc header, but all we need to cache is the data starting after
1210 * the end of the initial SEQUENCE operation--the rest we regenerate
1211 * each time. Therefore we can advertise a ca_maxresponssize_cached
1212 * value that is the number of bytes in our cache plus a few additional
1213 * bytes. In order to stay on the safe side, and not promise more than
1214 * we can cache, those additional bytes must be the minimum possible: 24
1215 * bytes of rpc header (xid through accept state, with AUTH_NULL
1216 * verifier), 12 for the compound header (with zero-length tag), and 44
1217 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001218 */
Andy Adamsona6496372009-08-28 08:45:01 -04001219#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1220
Andy Adamson557ce262009-08-28 08:45:04 -04001221static void
1222free_session_slots(struct nfsd4_session *ses)
1223{
1224 int i;
1225
1226 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1227 kfree(ses->se_slots[i]);
1228}
1229
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001230/*
1231 * We don't actually need to cache the rpc and session headers, so we
1232 * can allocate a little less for each slot:
1233 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001234static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001235{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001236 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001237
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001238 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1239 size = 0;
1240 else
1241 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1242 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001243}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001244
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001245/*
1246 * XXX: If we run out of reserved DRC memory we could (up to a point)
1247 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001248 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001249 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001250static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001251{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001252 u32 slotsize = slot_bytes(ca);
1253 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001254 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001255
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001256 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001257 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1258 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001259 num = min_t(int, num, avail / slotsize);
1260 nfsd_drc_mem_used += num * slotsize;
1261 spin_unlock(&nfsd_drc_lock);
1262
1263 return num;
1264}
1265
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001266static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001267{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001268 int slotsize = slot_bytes(ca);
1269
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001270 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001271 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001272 spin_unlock(&nfsd_drc_lock);
1273}
1274
Kinglong Mee60810e52014-01-01 00:35:47 +08001275static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1276 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001277{
Kinglong Mee60810e52014-01-01 00:35:47 +08001278 int numslots = fattrs->maxreqs;
1279 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001280 struct nfsd4_session *new;
1281 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001282
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001283 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001284 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1285 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001286
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001287 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001288 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001289 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001290 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001291 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001292 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001293 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001294 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001295 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001296
1297 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1298 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1299
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001300 return new;
1301out_free:
1302 while (i--)
1303 kfree(new->se_slots[i]);
1304 kfree(new);
1305 return NULL;
1306}
1307
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001308static void free_conn(struct nfsd4_conn *c)
1309{
1310 svc_xprt_put(c->cn_xprt);
1311 kfree(c);
1312}
1313
1314static void nfsd4_conn_lost(struct svc_xpt_user *u)
1315{
1316 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1317 struct nfs4_client *clp = c->cn_session->se_client;
1318
1319 spin_lock(&clp->cl_lock);
1320 if (!list_empty(&c->cn_persession)) {
1321 list_del(&c->cn_persession);
1322 free_conn(c);
1323 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001324 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001325 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001326}
1327
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001328static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001329{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001330 struct nfsd4_conn *conn;
1331
1332 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1333 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001334 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001335 svc_xprt_get(rqstp->rq_xprt);
1336 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001337 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001338 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1339 return conn;
1340}
1341
J. Bruce Fields328ead22010-09-29 16:11:06 -04001342static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1343{
1344 conn->cn_session = ses;
1345 list_add(&conn->cn_persession, &ses->se_conns);
1346}
1347
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001348static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1349{
1350 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001351
1352 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001353 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001354 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001355}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001356
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001357static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001358{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001359 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001360 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001361}
1362
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001363static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001364{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001365 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001366
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001367 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001368 ret = nfsd4_register_conn(conn);
1369 if (ret)
1370 /* oops; xprt is already down: */
1371 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001372 /* We may have gained or lost a callback channel: */
1373 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001374}
1375
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001376static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001377{
1378 u32 dir = NFS4_CDFC4_FORE;
1379
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001380 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001381 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001382 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001383}
1384
1385/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001386static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001387{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001388 struct nfs4_client *clp = s->se_client;
1389 struct nfsd4_conn *c;
1390
1391 spin_lock(&clp->cl_lock);
1392 while (!list_empty(&s->se_conns)) {
1393 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1394 list_del_init(&c->cn_persession);
1395 spin_unlock(&clp->cl_lock);
1396
1397 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1398 free_conn(c);
1399
1400 spin_lock(&clp->cl_lock);
1401 }
1402 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001403}
1404
J. Bruce Fields1377b692012-09-11 21:42:40 -04001405static void __free_session(struct nfsd4_session *ses)
1406{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001407 free_session_slots(ses);
1408 kfree(ses);
1409}
1410
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001411static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001412{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001413 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001414 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001415 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001416}
Andy Adamson557ce262009-08-28 08:45:04 -04001417
Fengguang Wu135ae822012-11-10 07:20:25 -05001418static 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 -04001419{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001420 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001421 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001422
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001423 new->se_client = clp;
1424 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001425
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001426 INIT_LIST_HEAD(&new->se_conns);
1427
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001428 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001429 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001430 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001431 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001432 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001433 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001434 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001435 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001436 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001437 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001438
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001439 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001440 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001441 /*
1442 * This is a little silly; with sessions there's no real
1443 * use for the callback address. Use the peer address
1444 * as a reasonable default for now, but consider fixing
1445 * the rpc client not to require an address in the
1446 * future:
1447 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001448 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1449 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001450 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001451}
1452
Benny Halevy9089f1b2010-05-12 00:12:26 +03001453/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001454static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001455__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001456{
1457 struct nfsd4_session *elem;
1458 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001459 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001460
Trond Myklebust0a880a22014-07-30 08:27:10 -04001461 lockdep_assert_held(&nn->client_lock);
1462
Marc Eshel5282fd72009-04-03 08:27:52 +03001463 dump_sessionid(__func__, sessionid);
1464 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001465 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001466 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001467 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1468 NFS4_MAX_SESSIONID_LEN)) {
1469 return elem;
1470 }
1471 }
1472
1473 dprintk("%s: session not found\n", __func__);
1474 return NULL;
1475}
1476
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001477static struct nfsd4_session *
1478find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1479 __be32 *ret)
1480{
1481 struct nfsd4_session *session;
1482 __be32 status = nfserr_badsession;
1483
1484 session = __find_in_sessionid_hashtbl(sessionid, net);
1485 if (!session)
1486 goto out;
1487 status = nfsd4_get_session_locked(session);
1488 if (status)
1489 session = NULL;
1490out:
1491 *ret = status;
1492 return session;
1493}
1494
Benny Halevy9089f1b2010-05-12 00:12:26 +03001495/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001496static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001497unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001498{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001499 struct nfs4_client *clp = ses->se_client;
1500 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1501
1502 lockdep_assert_held(&nn->client_lock);
1503
Andy Adamson7116ed62009-04-03 08:27:43 +03001504 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001505 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001506 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001507 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1511static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001512STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001514 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001516 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001517 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return 1;
1519}
1520
1521/*
1522 * XXX Should we use a slab cache ?
1523 * This type of memory management is somewhat inefficient, but we use it
1524 * anyway since SETCLIENTID is not a common operation.
1525 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001526static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001529 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001531 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1532 if (clp == NULL)
1533 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001534 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001535 if (clp->cl_name.data == NULL)
1536 goto err_no_name;
1537 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1538 OWNER_HASH_SIZE, GFP_KERNEL);
1539 if (!clp->cl_ownerstr_hashtbl)
1540 goto err_no_hashtbl;
1541 for (i = 0; i < OWNER_HASH_SIZE; i++)
1542 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001543 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001544 INIT_LIST_HEAD(&clp->cl_sessions);
1545 idr_init(&clp->cl_stateids);
1546 atomic_set(&clp->cl_refcount, 0);
1547 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1548 INIT_LIST_HEAD(&clp->cl_idhash);
1549 INIT_LIST_HEAD(&clp->cl_openowners);
1550 INIT_LIST_HEAD(&clp->cl_delegations);
1551 INIT_LIST_HEAD(&clp->cl_lru);
1552 INIT_LIST_HEAD(&clp->cl_callbacks);
1553 INIT_LIST_HEAD(&clp->cl_revoked);
1554 spin_lock_init(&clp->cl_lock);
1555 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001557err_no_hashtbl:
1558 kfree(clp->cl_name.data);
1559err_no_name:
1560 kfree(clp);
1561 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001564static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565free_client(struct nfs4_client *clp)
1566{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001567 while (!list_empty(&clp->cl_sessions)) {
1568 struct nfsd4_session *ses;
1569 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1570 se_perclnt);
1571 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001572 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1573 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001574 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001575 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001576 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001577 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001579 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 kfree(clp);
1581}
1582
Benny Halevy84d38ac2010-05-12 00:13:16 +03001583/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001584static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001585unhash_client_locked(struct nfs4_client *clp)
1586{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001587 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001588 struct nfsd4_session *ses;
1589
Trond Myklebust0a880a22014-07-30 08:27:10 -04001590 lockdep_assert_held(&nn->client_lock);
1591
Trond Myklebust4beb3452014-07-30 08:27:02 -04001592 /* Mark the client as expired! */
1593 clp->cl_time = 0;
1594 /* Make it invisible */
1595 if (!list_empty(&clp->cl_idhash)) {
1596 list_del_init(&clp->cl_idhash);
1597 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1598 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1599 else
1600 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1601 }
1602 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001603 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001604 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1605 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001606 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001607}
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001610unhash_client(struct nfs4_client *clp)
1611{
1612 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1613
1614 spin_lock(&nn->client_lock);
1615 unhash_client_locked(clp);
1616 spin_unlock(&nn->client_lock);
1617}
1618
Jeff Layton97403d92014-07-30 08:27:12 -04001619static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1620{
1621 if (atomic_read(&clp->cl_refcount))
1622 return nfserr_jukebox;
1623 unhash_client_locked(clp);
1624 return nfs_ok;
1625}
1626
Trond Myklebust4beb3452014-07-30 08:27:02 -04001627static void
1628__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001630 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct list_head reaplist;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001635 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001636 while (!list_empty(&clp->cl_delegations)) {
1637 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton42690672014-07-25 07:34:20 -04001638 unhash_delegation_locked(dp);
1639 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
Benny Halevycdc97502014-05-30 09:09:30 -04001641 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 while (!list_empty(&reaplist)) {
1643 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001644 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04001645 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001646 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001648 while (!list_empty(&clp->cl_revoked)) {
Benny Halevy956c4fe2013-10-29 11:39:12 +02001649 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001650 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001651 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001652 }
NeilBrownea1da632005-06-23 22:04:17 -07001653 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001654 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001655 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001656 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001658 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001659 if (clp->cl_cb_conn.cb_xprt)
1660 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001661 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
Trond Myklebust4beb3452014-07-30 08:27:02 -04001664static void
1665destroy_client(struct nfs4_client *clp)
1666{
1667 unhash_client(clp);
1668 __destroy_client(clp);
1669}
1670
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001671static void expire_client(struct nfs4_client *clp)
1672{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001673 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001674 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001675 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001676}
1677
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001678static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1679{
1680 memcpy(target->cl_verifier.data, source->data,
1681 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001684static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1685{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1687 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1688}
1689
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001690static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001691{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001692 if (source->cr_principal) {
1693 target->cr_principal =
1694 kstrdup(source->cr_principal, GFP_KERNEL);
1695 if (target->cr_principal == NULL)
1696 return -ENOMEM;
1697 } else
1698 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001699 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 target->cr_uid = source->cr_uid;
1701 target->cr_gid = source->cr_gid;
1702 target->cr_group_info = source->cr_group_info;
1703 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001704 target->cr_gss_mech = source->cr_gss_mech;
1705 if (source->cr_gss_mech)
1706 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001710static long long
1711compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1712{
1713 long long res;
1714
1715 res = o1->len - o2->len;
1716 if (res)
1717 return res;
1718 return (long long)memcmp(o1->data, o2->data, o1->len);
1719}
1720
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001721static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001722{
NeilBrowna55370a2005-06-23 22:03:52 -07001723 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001727same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1728{
1729 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001733same_clid(clientid_t *cl1, clientid_t *cl2)
1734{
1735 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001738static bool groups_equal(struct group_info *g1, struct group_info *g2)
1739{
1740 int i;
1741
1742 if (g1->ngroups != g2->ngroups)
1743 return false;
1744 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001745 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001746 return false;
1747 return true;
1748}
1749
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001750/*
1751 * RFC 3530 language requires clid_inuse be returned when the
1752 * "principal" associated with a requests differs from that previously
1753 * used. We use uid, gid's, and gss principal string as our best
1754 * approximation. We also don't want to allow non-gss use of a client
1755 * established using gss: in theory cr_principal should catch that
1756 * change, but in practice cr_principal can be null even in the gss case
1757 * since gssd doesn't always pass down a principal string.
1758 */
1759static bool is_gss_cred(struct svc_cred *cr)
1760{
1761 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1762 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1763}
1764
1765
Vivek Trivedi5559b502012-07-24 21:18:20 +05301766static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001767same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1768{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001769 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001770 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1771 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001772 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1773 return false;
1774 if (cr1->cr_principal == cr2->cr_principal)
1775 return true;
1776 if (!cr1->cr_principal || !cr2->cr_principal)
1777 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301778 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
J. Bruce Fields57266a62013-04-13 14:27:29 -04001781static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1782{
1783 struct svc_cred *cr = &rqstp->rq_cred;
1784 u32 service;
1785
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001786 if (!cr->cr_gss_mech)
1787 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001788 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1789 return service == RPC_GSS_SVC_INTEGRITY ||
1790 service == RPC_GSS_SVC_PRIVACY;
1791}
1792
1793static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1794{
1795 struct svc_cred *cr = &rqstp->rq_cred;
1796
1797 if (!cl->cl_mach_cred)
1798 return true;
1799 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1800 return false;
1801 if (!svc_rqst_integrity_protected(rqstp))
1802 return false;
1803 if (!cr->cr_principal)
1804 return false;
1805 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1806}
1807
Jeff Layton294ac322014-07-30 08:27:15 -04001808static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001809{
Chuck Leverab4684d2012-03-02 17:13:50 -05001810 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Jeff Laytonf4199922014-06-17 07:44:11 -04001812 /*
1813 * This is opaque to client, so no need to byte-swap. Use
1814 * __force to keep sparse happy
1815 */
1816 verf[0] = (__force __be32)get_seconds();
Jeff Layton294ac322014-07-30 08:27:15 -04001817 verf[1] = (__force __be32)nn->clientid_counter;
Chuck Leverab4684d2012-03-02 17:13:50 -05001818 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Jeff Layton294ac322014-07-30 08:27:15 -04001821static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1822{
1823 clp->cl_clientid.cl_boot = nn->boot_time;
1824 clp->cl_clientid.cl_id = nn->clientid_counter++;
1825 gen_confirm(clp, nn);
1826}
1827
Jeff Layton4770d722014-07-29 21:34:10 -04001828static struct nfs4_stid *
1829find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001830{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001831 struct nfs4_stid *ret;
1832
1833 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1834 if (!ret || !ret->sc_type)
1835 return NULL;
1836 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001837}
1838
Jeff Layton4770d722014-07-29 21:34:10 -04001839static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04001840find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001841{
1842 struct nfs4_stid *s;
1843
Jeff Layton4770d722014-07-29 21:34:10 -04001844 spin_lock(&cl->cl_lock);
1845 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04001846 if (s != NULL) {
1847 if (typemask & s->sc_type)
1848 atomic_inc(&s->sc_count);
1849 else
1850 s = NULL;
1851 }
Jeff Layton4770d722014-07-29 21:34:10 -04001852 spin_unlock(&cl->cl_lock);
1853 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001854}
1855
Jeff Layton2216d442012-11-12 15:00:57 -05001856static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001857 struct svc_rqst *rqstp, nfs4_verifier *verf)
1858{
1859 struct nfs4_client *clp;
1860 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001861 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001862 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001863
1864 clp = alloc_client(name);
1865 if (clp == NULL)
1866 return NULL;
1867
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001868 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1869 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001870 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001871 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001872 }
Jeff Layton02e12152014-07-16 10:31:57 -04001873 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001874 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001875 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001876 copy_verf(clp, verf);
1877 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001878 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001879 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001880 return clp;
1881}
1882
NeilBrownfd39ca92005-06-23 22:04:03 -07001883static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001884add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1885{
1886 struct rb_node **new = &(root->rb_node), *parent = NULL;
1887 struct nfs4_client *clp;
1888
1889 while (*new) {
1890 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1891 parent = *new;
1892
1893 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1894 new = &((*new)->rb_left);
1895 else
1896 new = &((*new)->rb_right);
1897 }
1898
1899 rb_link_node(&new_clp->cl_namenode, parent, new);
1900 rb_insert_color(&new_clp->cl_namenode, root);
1901}
1902
1903static struct nfs4_client *
1904find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1905{
1906 long long cmp;
1907 struct rb_node *node = root->rb_node;
1908 struct nfs4_client *clp;
1909
1910 while (node) {
1911 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1912 cmp = compare_blob(&clp->cl_name, name);
1913 if (cmp > 0)
1914 node = node->rb_left;
1915 else if (cmp < 0)
1916 node = node->rb_right;
1917 else
1918 return clp;
1919 }
1920 return NULL;
1921}
1922
1923static void
1924add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001927 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Trond Myklebust0a880a22014-07-30 08:27:10 -04001929 lockdep_assert_held(&nn->client_lock);
1930
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001931 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001932 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001934 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001935 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
NeilBrownfd39ca92005-06-23 22:04:03 -07001938static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939move_to_confirmed(struct nfs4_client *clp)
1940{
1941 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001942 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Trond Myklebust0a880a22014-07-30 08:27:10 -04001944 lockdep_assert_held(&nn->client_lock);
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001947 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001948 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001949 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001950 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001951 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
1954static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001955find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 struct nfs4_client *clp;
1958 unsigned int idhashval = clientid_hashval(clid->cl_id);
1959
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001960 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001961 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001962 if ((bool)clp->cl_minorversion != sessions)
1963 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04001964 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 return NULL;
1969}
1970
1971static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001972find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1973{
1974 struct list_head *tbl = nn->conf_id_hashtbl;
1975
Trond Myklebust0a880a22014-07-30 08:27:10 -04001976 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001977 return find_client_in_id_table(tbl, clid, sessions);
1978}
1979
1980static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001981find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001983 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Trond Myklebust0a880a22014-07-30 08:27:10 -04001985 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001986 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001989static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001990{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001991 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001992}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001993
NeilBrown28ce6052005-06-23 22:03:56 -07001994static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001995find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001996{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001997 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001998 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001999}
2000
2001static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002002find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002003{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002004 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002005 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002006}
2007
NeilBrownfd39ca92005-06-23 22:04:03 -07002008static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002009gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002011 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002012 struct sockaddr *sa = svc_addr(rqstp);
2013 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002014 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Jeff Layton7077ecb2009-08-14 12:57:58 -04002016 /* Currently, we only support tcp and tcp6 for the callback channel */
2017 if (se->se_callback_netid_len == 3 &&
2018 !memcmp(se->se_callback_netid_val, "tcp", 3))
2019 expected_family = AF_INET;
2020 else if (se->se_callback_netid_len == 4 &&
2021 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2022 expected_family = AF_INET6;
2023 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto out_err;
2025
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002026 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002027 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002028 (struct sockaddr *)&conn->cb_addr,
2029 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002030
J. Bruce Fields07263f12010-05-31 19:09:40 -04002031 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002033
J. Bruce Fields07263f12010-05-31 19:09:40 -04002034 if (conn->cb_addr.ss_family == AF_INET6)
2035 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002036
J. Bruce Fields07263f12010-05-31 19:09:40 -04002037 conn->cb_prog = se->se_callback_prog;
2038 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002039 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return;
2041out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002042 conn->cb_addr.ss_family = AF_UNSPEC;
2043 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002044 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 "will not receive delegations\n",
2046 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return;
2049}
2050
Andy Adamson074fe892009-04-03 08:28:15 +03002051/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002052 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002053 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002054static void
Andy Adamson074fe892009-04-03 08:28:15 +03002055nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2056{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002057 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002058 struct nfsd4_slot *slot = resp->cstate.slot;
2059 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002060
Andy Adamson557ce262009-08-28 08:45:04 -04002061 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002062
Andy Adamson557ce262009-08-28 08:45:04 -04002063 slot->sl_opcnt = resp->opcnt;
2064 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002065
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002066 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002067 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002068 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002069 return;
2070 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002071 base = resp->cstate.data_offset;
2072 slot->sl_datalen = buf->len - base;
2073 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04002074 WARN("%s: sessions DRC could not cache compound\n", __func__);
2075 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002076}
2077
2078/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002079 * Encode the replay sequence operation from the slot values.
2080 * If cachethis is FALSE encode the uncached rep error on the next
2081 * operation which sets resp->p and increments resp->opcnt for
2082 * nfs4svc_encode_compoundres.
2083 *
Andy Adamson074fe892009-04-03 08:28:15 +03002084 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002085static __be32
2086nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2087 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002088{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002089 struct nfsd4_op *op;
2090 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002091
Andy Adamsonabfabf82009-07-23 19:02:18 -04002092 /* Encode the replayed sequence operation */
2093 op = &args->ops[resp->opcnt - 1];
2094 nfsd4_encode_operation(resp, op);
2095
2096 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002097 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002098 op = &args->ops[resp->opcnt++];
2099 op->status = nfserr_retry_uncached_rep;
2100 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002101 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002102 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002103}
2104
2105/*
Andy Adamson557ce262009-08-28 08:45:04 -04002106 * The sequence operation is not cached because we can use the slot and
2107 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002108 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002109static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002110nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2111 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002112{
Andy Adamson557ce262009-08-28 08:45:04 -04002113 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002114 struct xdr_stream *xdr = &resp->xdr;
2115 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002116 __be32 status;
2117
Andy Adamson557ce262009-08-28 08:45:04 -04002118 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002119
Andy Adamsonabfabf82009-07-23 19:02:18 -04002120 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002121 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002122 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002123
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002124 p = xdr_reserve_space(xdr, slot->sl_datalen);
2125 if (!p) {
2126 WARN_ON_ONCE(1);
2127 return nfserr_serverfault;
2128 }
2129 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2130 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002131
Andy Adamson557ce262009-08-28 08:45:04 -04002132 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002133 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002134}
2135
Andy Adamson0733d212009-04-03 08:28:01 +03002136/*
2137 * Set the exchange_id flags returned by the server.
2138 */
2139static void
2140nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2141{
2142 /* pNFS is not supported */
2143 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2144
2145 /* Referrals are supported, Migration is not. */
2146 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2147
2148 /* set the wire flags to return to client. */
2149 clid->flags = new->cl_exchange_flags;
2150}
2151
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002152static bool client_has_state(struct nfs4_client *clp)
2153{
2154 /*
2155 * Note clp->cl_openowners check isn't quite right: there's no
2156 * need to count owners without stateid's.
2157 *
2158 * Also note we should probably be using this in 4.0 case too.
2159 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002160 return !list_empty(&clp->cl_openowners)
2161 || !list_empty(&clp->cl_delegations)
2162 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002163}
2164
Al Virob37ad282006-10-19 23:28:59 -07002165__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002166nfsd4_exchange_id(struct svc_rqst *rqstp,
2167 struct nfsd4_compound_state *cstate,
2168 struct nfsd4_exchange_id *exid)
2169{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002170 struct nfs4_client *conf, *new;
2171 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002172 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002173 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002174 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002175 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002176 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002177 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002178
Jeff Layton363168b2009-08-14 12:57:56 -04002179 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002180 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002181 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002182 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002183 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002184
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002185 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002186 return nfserr_inval;
2187
Andy Adamson0733d212009-04-03 08:28:01 +03002188 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002189 case SP4_MACH_CRED:
2190 if (!svc_rqst_integrity_protected(rqstp))
2191 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002192 case SP4_NONE:
2193 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002194 default: /* checked by xdr code */
2195 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002196 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04002197 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03002198 }
2199
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002200 new = create_client(exid->clname, rqstp, &verf);
2201 if (new == NULL)
2202 return nfserr_jukebox;
2203
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002204 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002205 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002206 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002207 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002208 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2209 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2210
J. Bruce Fields136e6582012-05-12 20:37:23 -04002211 if (update) {
2212 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002213 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002214 goto out;
2215 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002216 if (!mach_creds_match(conf, rqstp)) {
2217 status = nfserr_wrong_cred;
2218 goto out;
2219 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002220 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002221 status = nfserr_perm;
2222 goto out;
2223 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002224 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002225 status = nfserr_not_same;
2226 goto out;
2227 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002228 /* case 6 */
2229 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002230 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002231 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002232 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002233 if (client_has_state(conf)) {
2234 status = nfserr_clid_inuse;
2235 goto out;
2236 }
Andy Adamson0733d212009-04-03 08:28:01 +03002237 goto out_new;
2238 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002239 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002240 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002241 goto out_copy;
2242 }
2243 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002244 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002245 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002246 }
2247
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002248 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002249 status = nfserr_noent;
2250 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002251 }
2252
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002253 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002254 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002255 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002256
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002257 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002258out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002259 if (conf) {
2260 status = mark_client_expired_locked(conf);
2261 if (status)
2262 goto out;
2263 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002264 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002265 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03002266
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002267 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002268 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002269 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002270out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002271 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2272 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002273
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002274 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2275 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002276
2277 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002278 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002279 status = nfs_ok;
2280
2281out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002282 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002283 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002284 expire_client(new);
2285 if (unconf)
2286 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002287 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002288}
2289
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002290static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002291check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002292{
Andy Adamson88e588d2009-07-23 19:02:15 -04002293 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2294 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002295
2296 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002297 if (slot_inuse) {
2298 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002299 return nfserr_jukebox;
2300 else
2301 return nfserr_seq_misordered;
2302 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002303 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002304 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002305 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002306 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002307 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002308 return nfserr_seq_misordered;
2309}
2310
Andy Adamson49557cc2009-07-23 19:02:16 -04002311/*
2312 * Cache the create session result into the create session single DRC
2313 * slot cache by saving the xdr structure. sl_seqid has been set.
2314 * Do this for solo or embedded create session operations.
2315 */
2316static void
2317nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002318 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002319{
2320 slot->sl_status = nfserr;
2321 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2322}
2323
2324static __be32
2325nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2326 struct nfsd4_clid_slot *slot)
2327{
2328 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2329 return slot->sl_status;
2330}
2331
Mi Jinlong1b74c252011-07-14 14:50:17 +08002332#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2333 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2334 1 + /* MIN tag is length with zero, only length */ \
2335 3 + /* version, opcount, opcode */ \
2336 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2337 /* seqid, slotID, slotID, cache */ \
2338 4 ) * sizeof(__be32))
2339
2340#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2341 2 + /* verifier: AUTH_NULL, length 0 */\
2342 1 + /* status */ \
2343 1 + /* MIN tag is length with zero, only length */ \
2344 3 + /* opcount, opcode, opstatus*/ \
2345 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2346 /* seqid, slotID, slotID, slotID, status */ \
2347 5 ) * sizeof(__be32))
2348
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002349static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002350{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002351 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2352
J. Bruce Fields373cd402013-04-08 15:42:12 -04002353 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2354 return nfserr_toosmall;
2355 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2356 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002357 ca->headerpadsz = 0;
2358 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2359 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2360 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2361 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2362 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2363 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2364 /*
2365 * Note decreasing slot size below client's request may make it
2366 * difficult for client to function correctly, whereas
2367 * decreasing the number of slots will (just?) affect
2368 * performance. When short on memory we therefore prefer to
2369 * decrease number of slots instead of their size. Clients that
2370 * request larger slots than they need will get poor results:
2371 */
2372 ca->maxreqs = nfsd4_get_drc_mem(ca);
2373 if (!ca->maxreqs)
2374 return nfserr_jukebox;
2375
J. Bruce Fields373cd402013-04-08 15:42:12 -04002376 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002377}
2378
Kinglong Mee8a891632013-12-23 18:11:02 +08002379#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2380 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2381#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2382 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2383
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002384static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2385{
2386 ca->headerpadsz = 0;
2387
2388 /*
2389 * These RPC_MAX_HEADER macros are overkill, especially since we
2390 * don't even do gss on the backchannel yet. But this is still
2391 * less than 1k. Tighten up this estimate in the unlikely event
2392 * it turns out to be a problem for some client:
2393 */
Kinglong Mee8a891632013-12-23 18:11:02 +08002394 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002395 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002396 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002397 return nfserr_toosmall;
2398 ca->maxresp_cached = 0;
2399 if (ca->maxops < 2)
2400 return nfserr_toosmall;
2401
2402 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002403}
2404
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002405static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2406{
2407 switch (cbs->flavor) {
2408 case RPC_AUTH_NULL:
2409 case RPC_AUTH_UNIX:
2410 return nfs_ok;
2411 default:
2412 /*
2413 * GSS case: the spec doesn't allow us to return this
2414 * error. But it also doesn't allow us not to support
2415 * GSS.
2416 * I'd rather this fail hard than return some error the
2417 * client might think it can already handle:
2418 */
2419 return nfserr_encr_alg_unsupp;
2420 }
2421}
2422
Andy Adamson069b6ad2009-04-03 08:27:58 +03002423__be32
2424nfsd4_create_session(struct svc_rqst *rqstp,
2425 struct nfsd4_compound_state *cstate,
2426 struct nfsd4_create_session *cr_ses)
2427{
Jeff Layton363168b2009-08-14 12:57:56 -04002428 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002429 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002430 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002431 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002432 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002433 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002434 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002435 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002436
Mi Jinlonga62573d2011-03-23 17:57:07 +08002437 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2438 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002439 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2440 if (status)
2441 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002442 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002443 if (status)
2444 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002445 status = check_backchannel_attrs(&cr_ses->back_channel);
2446 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002447 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002448 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002449 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002450 if (!new)
2451 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002452 conn = alloc_conn_from_crses(rqstp, cr_ses);
2453 if (!conn)
2454 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002455
Jeff Laytond20c11d2014-07-30 08:27:07 -04002456 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002457 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002458 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002459 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002460
2461 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002462 status = nfserr_wrong_cred;
2463 if (!mach_creds_match(conf, rqstp))
2464 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002465 cs_slot = &conf->cl_cs_slot;
2466 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002467 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002468 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002469 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002470 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002471 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002472 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002473 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002474 } else if (unconf) {
2475 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002476 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002477 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002478 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002479 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002480 status = nfserr_wrong_cred;
2481 if (!mach_creds_match(unconf, rqstp))
2482 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002483 cs_slot = &unconf->cl_cs_slot;
2484 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002485 if (status) {
2486 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002487 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002488 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002489 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002490 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002491 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002492 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002493 if (status) {
2494 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002495 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002496 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002497 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002498 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002499 conf = unconf;
2500 } else {
2501 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002502 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002503 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002504 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002505 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002506 * We do not support RDMA or persistent sessions
2507 */
2508 cr_ses->flags &= ~SESSION4_PERSIST;
2509 cr_ses->flags &= ~SESSION4_RDMA;
2510
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002511 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002512 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002513
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002514 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002515 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002516 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002517 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002518
Jeff Laytond20c11d2014-07-30 08:27:07 -04002519 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002520 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002521 spin_unlock(&nn->client_lock);
2522 /* init connection and backchannel */
2523 nfsd4_init_conn(rqstp, conn, new);
2524 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002525 if (old)
2526 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002527 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002528out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002529 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002530 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002531 if (old)
2532 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002533out_free_session:
2534 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002535out_release_drc_mem:
2536 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002537 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002538}
2539
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002540static __be32 nfsd4_map_bcts_dir(u32 *dir)
2541{
2542 switch (*dir) {
2543 case NFS4_CDFC4_FORE:
2544 case NFS4_CDFC4_BACK:
2545 return nfs_ok;
2546 case NFS4_CDFC4_FORE_OR_BOTH:
2547 case NFS4_CDFC4_BACK_OR_BOTH:
2548 *dir = NFS4_CDFC4_BOTH;
2549 return nfs_ok;
2550 };
2551 return nfserr_inval;
2552}
2553
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002554__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2555{
2556 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002557 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002558 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002559
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002560 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2561 if (status)
2562 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002563 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002564 session->se_cb_prog = bc->bc_cb_program;
2565 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002566 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002567
2568 nfsd4_probe_callback(session->se_client);
2569
2570 return nfs_ok;
2571}
2572
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002573__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2574 struct nfsd4_compound_state *cstate,
2575 struct nfsd4_bind_conn_to_session *bcts)
2576{
2577 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002578 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002579 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002580 struct net *net = SVC_NET(rqstp);
2581 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002582
2583 if (!nfsd4_last_compound_op(rqstp))
2584 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002585 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002586 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002587 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002588 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002589 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002590 status = nfserr_wrong_cred;
2591 if (!mach_creds_match(session->se_client, rqstp))
2592 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002593 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002594 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002595 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002596 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002597 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002598 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002599 goto out;
2600 nfsd4_init_conn(rqstp, conn, session);
2601 status = nfs_ok;
2602out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002603 nfsd4_put_session(session);
2604out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002605 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002606}
2607
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002608static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2609{
2610 if (!session)
2611 return 0;
2612 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2613}
2614
Andy Adamson069b6ad2009-04-03 08:27:58 +03002615__be32
2616nfsd4_destroy_session(struct svc_rqst *r,
2617 struct nfsd4_compound_state *cstate,
2618 struct nfsd4_destroy_session *sessionid)
2619{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002620 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002621 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002622 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002623 struct net *net = SVC_NET(r);
2624 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002625
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002626 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002627 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002628 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002629 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002630 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002631 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002632 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002633 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002634 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002635 if (!ses)
2636 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002637 status = nfserr_wrong_cred;
2638 if (!mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002639 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002640 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002641 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002642 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002643 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002644 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002645
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002646 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002647
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002648 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002649 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002650out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002651 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002652out_client_lock:
2653 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002654out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002655 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002656}
2657
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002658static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002659{
2660 struct nfsd4_conn *c;
2661
2662 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002663 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002664 return c;
2665 }
2666 }
2667 return NULL;
2668}
2669
J. Bruce Fields57266a62013-04-13 14:27:29 -04002670static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002671{
2672 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002673 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002674 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002675 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002676
2677 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002678 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002679 if (c)
2680 goto out_free;
2681 status = nfserr_conn_not_bound_to_session;
2682 if (clp->cl_mach_cred)
2683 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002684 __nfsd4_hash_conn(new, ses);
2685 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002686 ret = nfsd4_register_conn(new);
2687 if (ret)
2688 /* oops; xprt is already down: */
2689 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002690 return nfs_ok;
2691out_free:
2692 spin_unlock(&clp->cl_lock);
2693 free_conn(new);
2694 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002695}
2696
Mi Jinlong868b89c2011-04-27 09:09:58 +08002697static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2698{
2699 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2700
2701 return args->opcnt > session->se_fchannel.maxops;
2702}
2703
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002704static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2705 struct nfsd4_session *session)
2706{
2707 struct xdr_buf *xb = &rqstp->rq_arg;
2708
2709 return xb->len > session->se_fchannel.maxreq_sz;
2710}
2711
Andy Adamson069b6ad2009-04-03 08:27:58 +03002712__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002713nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002714 struct nfsd4_compound_state *cstate,
2715 struct nfsd4_sequence *seq)
2716{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002717 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002718 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002719 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002720 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002721 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002722 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002723 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002724 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002725 struct net *net = SVC_NET(rqstp);
2726 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002727
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002728 if (resp->opcnt != 1)
2729 return nfserr_sequence_pos;
2730
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002731 /*
2732 * Will be either used or freed by nfsd4_sequence_check_conn
2733 * below.
2734 */
2735 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2736 if (!conn)
2737 return nfserr_jukebox;
2738
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002739 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002740 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002741 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002742 goto out_no_session;
2743 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002744
Mi Jinlong868b89c2011-04-27 09:09:58 +08002745 status = nfserr_too_many_ops;
2746 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002747 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002748
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002749 status = nfserr_req_too_big;
2750 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002751 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002752
Benny Halevyb85d4c02009-04-03 08:28:08 +03002753 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002754 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002755 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002756
Andy Adamson557ce262009-08-28 08:45:04 -04002757 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002758 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2759
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002760 /* We do not negotiate the number of slots yet, so set the
2761 * maxslots to the session maxreqs which is used to encode
2762 * sr_highest_slotid and the sr_target_slot id to maxslots */
2763 seq->maxslots = session->se_fchannel.maxreqs;
2764
J. Bruce Fields73e79482012-02-13 16:39:00 -05002765 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2766 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002767 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002768 status = nfserr_seq_misordered;
2769 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002770 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002771 cstate->slot = slot;
2772 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002773 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002774 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002775 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002776 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002777 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002778 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002779 }
2780 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002781 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002782
J. Bruce Fields57266a62013-04-13 14:27:29 -04002783 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002784 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002785 if (status)
2786 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002787
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002788 buflen = (seq->cachethis) ?
2789 session->se_fchannel.maxresp_cached :
2790 session->se_fchannel.maxresp_sz;
2791 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2792 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002793 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002794 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002795 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002796
2797 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002798 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002799 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002800 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002801 if (seq->cachethis)
2802 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002803 else
2804 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002805
2806 cstate->slot = slot;
2807 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04002808 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002809
Benny Halevyb85d4c02009-04-03 08:28:08 +03002810out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002811 switch (clp->cl_cb_state) {
2812 case NFSD4_CB_DOWN:
2813 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2814 break;
2815 case NFSD4_CB_FAULT:
2816 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2817 break;
2818 default:
2819 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002820 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002821 if (!list_empty(&clp->cl_revoked))
2822 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002823out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002824 if (conn)
2825 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002826 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002827 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002828out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002829 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002830 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002831}
2832
Trond Myklebustb6076642014-06-30 11:48:35 -04002833void
2834nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2835{
2836 struct nfsd4_compound_state *cs = &resp->cstate;
2837
2838 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04002839 if (cs->status != nfserr_replay_cache) {
2840 nfsd4_store_cache_entry(resp);
2841 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2842 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002843 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04002844 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04002845 } else if (cs->clp)
2846 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04002847}
2848
Mi Jinlong345c2842011-10-20 17:51:39 +08002849__be32
2850nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2851{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002852 struct nfs4_client *conf, *unconf;
2853 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002854 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002855 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002856
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002857 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002858 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002859 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002860 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002861
2862 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002863 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002864 status = nfserr_clientid_busy;
2865 goto out;
2866 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04002867 status = mark_client_expired_locked(conf);
2868 if (status)
2869 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002870 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08002871 } else if (unconf)
2872 clp = unconf;
2873 else {
2874 status = nfserr_stale_clientid;
2875 goto out;
2876 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002877 if (!mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002878 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002879 status = nfserr_wrong_cred;
2880 goto out;
2881 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002882 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002883out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002884 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04002885 if (clp)
2886 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08002887 return status;
2888}
2889
Andy Adamson069b6ad2009-04-03 08:27:58 +03002890__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002891nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2892{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002893 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002894
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002895 if (rc->rca_one_fs) {
2896 if (!cstate->current_fh.fh_dentry)
2897 return nfserr_nofilehandle;
2898 /*
2899 * We don't take advantage of the rca_one_fs case.
2900 * That's OK, it's optional, we can safely ignore it.
2901 */
2902 return nfs_ok;
2903 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002904
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002905 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002906 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2907 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002908 goto out;
2909
2910 status = nfserr_stale_clientid;
2911 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002912 /*
2913 * The following error isn't really legal.
2914 * But we only get here if the client just explicitly
2915 * destroyed the client. Surely it no longer cares what
2916 * error it gets back on an operation for the dead
2917 * client.
2918 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002919 goto out;
2920
2921 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002922 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002923out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002924 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002925}
2926
2927__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002928nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2929 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002931 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002933 struct nfs4_client *conf, *new;
2934 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07002935 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002936 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2937
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002938 new = create_client(clname, rqstp, &clverifier);
2939 if (new == NULL)
2940 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002941 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002942 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002943 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002944 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002945 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002947 if (clp_used_exchangeid(conf))
2948 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002949 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002950 char addr_str[INET6_ADDRSTRLEN];
2951 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2952 sizeof(addr_str));
2953 dprintk("NFSD: setclientid: string in use by client "
2954 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 goto out;
2956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002958 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002959 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002960 unhash_client_locked(unconf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002961 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002962 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002964 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002965 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002966 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002967 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002968 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2970 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2971 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002972 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 status = nfs_ok;
2974out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002975 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002976 if (new)
2977 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002978 if (unconf)
2979 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return status;
2981}
2982
2983
Al Virob37ad282006-10-19 23:28:59 -07002984__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002985nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2986 struct nfsd4_compound_state *cstate,
2987 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
NeilBrown21ab45a2005-06-23 22:04:14 -07002989 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002990 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2992 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002993 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002994 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002996 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07002998
Jeff Laytond20c11d2014-07-30 08:27:07 -04002999 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003000 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003001 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003002 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003003 * We try hard to give out unique clientid's, so if we get an
3004 * attempt to confirm the same clientid with a different cred,
3005 * there's a bug somewhere. Let's charitably assume it's our
3006 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003007 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04003008 status = nfserr_serverfault;
3009 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3010 goto out;
3011 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3012 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003013 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003014 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3015 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003017 else /* case 4: client hasn't noticed we rebooted yet? */
3018 status = nfserr_stale_clientid;
3019 goto out;
3020 }
3021 status = nfs_ok;
3022 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003023 old = unconf;
3024 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003025 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003026 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003027 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3028 if (old) {
3029 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003030 if (status) {
3031 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003032 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003033 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003034 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003035 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003036 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003037 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003038 get_client_locked(conf);
3039 spin_unlock(&nn->client_lock);
3040 nfsd4_probe_callback(conf);
3041 spin_lock(&nn->client_lock);
3042 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003044 spin_unlock(&nn->client_lock);
3045 if (old)
3046 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return status;
3048}
3049
J. Bruce Fields32513b42011-10-13 16:00:16 -04003050static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003052 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3053}
3054
3055/* OPEN Share state helper functions */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003056static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003057{
Trond Myklebustca943212014-07-23 16:17:39 -04003058 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Trond Myklebust950e0112014-06-30 11:48:31 -04003060 lockdep_assert_held(&state_lock);
3061
J. Bruce Fields32513b42011-10-13 16:00:16 -04003062 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003063 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003064 INIT_LIST_HEAD(&fp->fi_stateids);
3065 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003066 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003067 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003068 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003069 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003070 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3071 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04003072 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003075void
NeilBrowne60d4392005-06-23 22:03:01 -07003076nfsd4_free_slabs(void)
3077{
Christoph Hellwigabf11352014-05-21 07:43:03 -07003078 kmem_cache_destroy(openowner_slab);
3079 kmem_cache_destroy(lockowner_slab);
3080 kmem_cache_destroy(file_slab);
3081 kmem_cache_destroy(stateid_slab);
3082 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Bryan Schumaker72083392011-11-01 15:24:59 -04003085int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086nfsd4_init_slabs(void)
3087{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003088 openowner_slab = kmem_cache_create("nfsd4_openowners",
3089 sizeof(struct nfs4_openowner), 0, 0, NULL);
3090 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003091 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003092 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003093 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003094 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003095 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003096 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003097 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003098 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003099 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003100 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003101 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003102 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003103 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003104 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003105 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003106 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003107 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003109
3110out_free_stateid_slab:
3111 kmem_cache_destroy(stateid_slab);
3112out_free_file_slab:
3113 kmem_cache_destroy(file_slab);
3114out_free_lockowner_slab:
3115 kmem_cache_destroy(lockowner_slab);
3116out_free_openowner_slab:
3117 kmem_cache_destroy(openowner_slab);
3118out:
NeilBrowne60d4392005-06-23 22:03:01 -07003119 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3120 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003123static void init_nfs4_replay(struct nfs4_replay *rp)
3124{
3125 rp->rp_status = nfserr_serverfault;
3126 rp->rp_buflen = 0;
3127 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003128 mutex_init(&rp->rp_mutex);
3129}
3130
3131static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3132 struct nfs4_stateowner *so)
3133{
3134 if (!nfsd4_has_session(cstate)) {
3135 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003136 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003137 }
3138}
3139
3140void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3141{
3142 struct nfs4_stateowner *so = cstate->replay_owner;
3143
3144 if (so != NULL) {
3145 cstate->replay_owner = NULL;
3146 mutex_unlock(&so->so_replay.rp_mutex);
3147 nfs4_put_stateowner(so);
3148 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003149}
3150
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003151static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct nfs4_stateowner *sop;
3154
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003155 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003156 if (!sop)
3157 return NULL;
3158
3159 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3160 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003161 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003162 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003164 sop->so_owner.len = owner->len;
3165
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003166 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003167 sop->so_client = clp;
3168 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003169 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003170 return sop;
3171}
3172
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003173static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003174{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003175 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003176
Trond Myklebustd4f04892014-07-29 21:34:36 -04003177 list_add(&oo->oo_owner.so_strhash,
3178 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003179 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003182static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3183{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003184 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003185}
3186
Jeff Layton6b180f02014-07-29 21:34:26 -04003187static void nfs4_free_openowner(struct nfs4_stateowner *so)
3188{
3189 struct nfs4_openowner *oo = openowner(so);
3190
3191 kmem_cache_free(openowner_slab, oo);
3192}
3193
3194static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003195 .so_unhash = nfs4_unhash_openowner,
3196 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003197};
3198
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003199static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003200alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003201 struct nfsd4_compound_state *cstate)
3202{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003203 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003204 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003206 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3207 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003209 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003210 oo->oo_owner.so_is_open_owner = 1;
3211 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003212 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003213 if (nfsd4_has_session(cstate))
3214 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003215 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003216 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003217 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003218 spin_lock(&clp->cl_lock);
3219 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003220 if (ret == NULL) {
3221 hash_openowner(oo, clp, strhashval);
3222 ret = oo;
3223 } else
3224 nfs4_free_openowner(&oo->oo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003225 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003226 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
J. Bruce Fields996e0932011-10-17 11:14:48 -04003229static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003230 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003232 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003233 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003234 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003235 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003236 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003237 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 stp->st_access_bmap = 0;
3239 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003240 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003241 spin_lock(&oo->oo_owner.so_client->cl_lock);
3242 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003243 spin_lock(&fp->fi_lock);
3244 list_add(&stp->st_perfile, &fp->fi_stateids);
3245 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003246 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Jeff Laytond3134b12014-07-29 21:34:32 -04003249/*
3250 * In the 4.0 case we need to keep the owners around a little while to handle
3251 * CLOSE replay. We still do need to release any file access that is held by
3252 * them before returning however.
3253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003255move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Jeff Layton217526e2014-07-30 08:27:11 -04003257 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003258 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3259 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3260 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003261
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003262 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Jeff Laytonb401be222014-07-29 21:34:33 -04003264 /*
3265 * We know that we hold one reference via nfsd4_close, and another
3266 * "persistent" reference for the client. If the refcount is higher
3267 * than 2, then there are still calls in progress that are using this
3268 * stateid. We can't put the sc_file reference until they are finished.
3269 * Wait for the refcount to drop to 2. Since it has been unhashed,
3270 * there should be no danger of the refcount going back up again at
3271 * this point.
3272 */
3273 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3274
Jeff Laytond3134b12014-07-29 21:34:32 -04003275 release_all_access(s);
3276 if (s->st_stid.sc_file) {
3277 put_nfs4_file(s->st_stid.sc_file);
3278 s->st_stid.sc_file = NULL;
3279 }
Jeff Layton217526e2014-07-30 08:27:11 -04003280
3281 spin_lock(&nn->client_lock);
3282 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003283 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003284 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003285 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003286 spin_unlock(&nn->client_lock);
3287 if (last)
3288 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291/* search file_hashtbl[] for file */
3292static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003293find_file_locked(struct knfsd_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Trond Myklebustca943212014-07-23 16:17:39 -04003295 unsigned int hashval = file_hashval(fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 struct nfs4_file *fp;
3297
Trond Myklebust950e0112014-06-30 11:48:31 -04003298 lockdep_assert_held(&state_lock);
3299
Jeff Layton89876f82013-04-02 09:01:59 -04003300 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
Trond Myklebustca943212014-07-23 16:17:39 -04003301 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
NeilBrown13cd2182005-06-23 22:03:10 -07003302 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
3306 return NULL;
3307}
3308
Trond Myklebust950e0112014-06-30 11:48:31 -04003309static struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003310find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003311{
3312 struct nfs4_file *fp;
3313
3314 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003315 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003316 spin_unlock(&state_lock);
3317 return fp;
3318}
3319
3320static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003321find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003322{
3323 struct nfs4_file *fp;
3324
3325 spin_lock(&state_lock);
Trond Myklebustca943212014-07-23 16:17:39 -04003326 fp = find_file_locked(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003327 if (fp == NULL) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003328 nfsd4_init_file(new, fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003329 fp = new;
3330 }
3331 spin_unlock(&state_lock);
3332
3333 return fp;
3334}
3335
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 * Called to check deny when READ with all zero stateid or
3338 * WRITE with all zero or all one stateid
3339 */
Al Virob37ad282006-10-19 23:28:59 -07003340static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003344 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Trond Myklebustca943212014-07-23 16:17:39 -04003346 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003347 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003348 return ret;
3349 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003350 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003351 if (fp->fi_share_deny & deny_type)
3352 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003353 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003354 put_nfs4_file(fp);
3355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
3357
Jeff Layton02e12152014-07-16 10:31:57 -04003358void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Trond Myklebust11b91642014-07-29 21:34:08 -04003360 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3361 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003362
Trond Myklebust11b91642014-07-29 21:34:08 -04003363 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003364
Jeff Layton02e12152014-07-16 10:31:57 -04003365 /*
3366 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003367 * already holding inode->i_lock.
3368 *
Jeff Laytondff13992014-07-08 14:02:49 -04003369 * If the dl_time != 0, then we know that it has already been
3370 * queued for a lease break. Don't queue it again.
3371 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003372 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003373 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003374 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003375 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003376 }
Jeff Layton02e12152014-07-16 10:31:57 -04003377 spin_unlock(&state_lock);
3378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Jeff Layton02e12152014-07-16 10:31:57 -04003380static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3381{
3382 /*
3383 * We're assuming the state code never drops its reference
3384 * without first removing the lease. Since we're in this lease
3385 * callback (and since the lease code is serialized by the kernel
3386 * lock) we know the server hasn't removed the lease yet, we know
3387 * it's safe to take a reference.
3388 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003389 atomic_inc(&dp->dl_stid.sc_count);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003390 nfsd4_cb_recall(dp);
3391}
3392
Jeff Layton1c8c6012013-06-21 08:58:15 -04003393/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003394static bool
3395nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003396{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003397 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003398 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3399 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003400
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003401 if (!fp) {
3402 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003403 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003404 }
3405 if (fp->fi_had_conflict) {
3406 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003407 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003408 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003409 /*
3410 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003411 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003412 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003413 */
3414 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Jeff Layton02e12152014-07-16 10:31:57 -04003416 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003417 fp->fi_had_conflict = true;
3418 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003419 * If there are no delegations on the list, then return true
3420 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003421 */
3422 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003423 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003424 else
3425 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3426 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003427 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003428 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
Jeff Laytonc45198e2014-09-01 07:12:07 -04003431static int
3432nfsd_change_deleg_cb(struct file_lock **onlist, int arg, struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
3434 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003435 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 else
3437 return -EAGAIN;
3438}
3439
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003440static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003441 .lm_break = nfsd_break_deleg_cb,
3442 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443};
3444
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003445static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3446{
3447 if (nfsd4_has_session(cstate))
3448 return nfs_ok;
3449 if (seqid == so->so_seqid - 1)
3450 return nfserr_replay_me;
3451 if (seqid == so->so_seqid)
3452 return nfs_ok;
3453 return nfserr_bad_seqid;
3454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Jeff Layton4b24ca72014-06-30 11:48:44 -04003456static __be32 lookup_clientid(clientid_t *clid,
3457 struct nfsd4_compound_state *cstate,
3458 struct nfsd_net *nn)
3459{
3460 struct nfs4_client *found;
3461
3462 if (cstate->clp) {
3463 found = cstate->clp;
3464 if (!same_clid(&found->cl_clientid, clid))
3465 return nfserr_stale_clientid;
3466 return nfs_ok;
3467 }
3468
3469 if (STALE_CLIENTID(clid, nn))
3470 return nfserr_stale_clientid;
3471
3472 /*
3473 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3474 * cached already then we know this is for is for v4.0 and "sessions"
3475 * will be false.
3476 */
3477 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003478 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003479 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003480 if (!found) {
3481 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003482 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003483 }
3484 atomic_inc(&found->cl_refcount);
3485 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003486
3487 /* Cache the nfs4_client in cstate! */
3488 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003489 return nfs_ok;
3490}
3491
Al Virob37ad282006-10-19 23:28:59 -07003492__be32
Andy Adamson66689582009-04-03 08:28:45 +03003493nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003494 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 clientid_t *clientid = &open->op_clientid;
3497 struct nfs4_client *clp = NULL;
3498 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003499 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003500 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003502 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003504 /*
3505 * In case we need it later, after we've already created the
3506 * file and don't want to risk a further failure:
3507 */
3508 open->op_file = nfsd4_alloc_file();
3509 if (open->op_file == NULL)
3510 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Trond Myklebust2d91e892014-06-30 11:48:46 -04003512 status = lookup_clientid(clientid, cstate, nn);
3513 if (status)
3514 return status;
3515 clp = cstate->clp;
3516
Trond Myklebustd4f04892014-07-29 21:34:36 -04003517 strhashval = ownerstr_hashval(&open->op_owner);
3518 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003519 open->op_openowner = oo;
3520 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003521 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003523 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003524 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003525 release_openowner(oo);
3526 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003527 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003528 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003529 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3530 if (status)
3531 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003532 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003533new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003534 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003535 if (oo == NULL)
3536 return nfserr_jukebox;
3537 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003538alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003539 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003540 if (!open->op_stp)
3541 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003542 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
Al Virob37ad282006-10-19 23:28:59 -07003545static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003546nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3547{
3548 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3549 return nfserr_openmode;
3550 else
3551 return nfs_ok;
3552}
3553
Daniel Mackc47d8322011-05-16 16:38:14 +02003554static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003555{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003556 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3557}
3558
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003559static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003560{
3561 struct nfs4_stid *ret;
3562
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003563 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003564 if (!ret)
3565 return NULL;
3566 return delegstateid(ret);
3567}
3568
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003569static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3570{
3571 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3572 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3573}
3574
Al Virob37ad282006-10-19 23:28:59 -07003575static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003576nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003577 struct nfs4_delegation **dp)
3578{
3579 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003580 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003581 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003582
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003583 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3584 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003585 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003586 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003587 status = nfs4_check_delegmode(deleg, flags);
3588 if (status) {
3589 nfs4_put_stid(&deleg->dl_stid);
3590 goto out;
3591 }
3592 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003593out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003594 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003595 return nfs_ok;
3596 if (status)
3597 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003598 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003599 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003600}
3601
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003602static struct nfs4_ol_stateid *
3603nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003605 struct nfs4_ol_stateid *local, *ret = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003606 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Trond Myklebust1d31a252014-07-10 14:07:25 -04003608 spin_lock(&fp->fi_lock);
NeilBrown8beefa22005-06-23 22:03:08 -07003609 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /* ignore lock owners */
3611 if (local->st_stateowner->so_is_open_owner == 0)
3612 continue;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003613 if (local->st_stateowner == &oo->oo_owner) {
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003614 ret = local;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003615 atomic_inc(&ret->st_stid.sc_count);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003616 break;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
Trond Myklebust1d31a252014-07-10 14:07:25 -04003619 spin_unlock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04003620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003623static inline int nfs4_access_to_access(u32 nfs4_access)
3624{
3625 int flags = 0;
3626
3627 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3628 flags |= NFSD_MAY_READ;
3629 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3630 flags |= NFSD_MAY_WRITE;
3631 return flags;
3632}
3633
Al Virob37ad282006-10-19 23:28:59 -07003634static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3636 struct nfsd4_open *open)
3637{
3638 struct iattr iattr = {
3639 .ia_valid = ATTR_SIZE,
3640 .ia_size = 0,
3641 };
3642 if (!open->op_truncate)
3643 return 0;
3644 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003645 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3647}
3648
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003649static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003650 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3651 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003652{
Trond Myklebustde186432014-07-10 14:07:26 -04003653 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003654 __be32 status;
3655 int oflag = nfs4_access_to_omode(open->op_share_access);
3656 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003657 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003658
Trond Myklebustde186432014-07-10 14:07:26 -04003659 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003660
3661 /*
3662 * Are we trying to set a deny mode that would conflict with
3663 * current access?
3664 */
3665 status = nfs4_file_check_deny(fp, open->op_share_deny);
3666 if (status != nfs_ok) {
3667 spin_unlock(&fp->fi_lock);
3668 goto out;
3669 }
3670
3671 /* set access to the file */
3672 status = nfs4_file_get_access(fp, open->op_share_access);
3673 if (status != nfs_ok) {
3674 spin_unlock(&fp->fi_lock);
3675 goto out;
3676 }
3677
3678 /* Set access bits in stateid */
3679 old_access_bmap = stp->st_access_bmap;
3680 set_access(open->op_share_access, stp);
3681
3682 /* Set new deny mask */
3683 old_deny_bmap = stp->st_deny_bmap;
3684 set_deny(open->op_share_deny, stp);
3685 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3686
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003687 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04003688 spin_unlock(&fp->fi_lock);
3689 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003690 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003691 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04003692 spin_lock(&fp->fi_lock);
3693 if (!fp->fi_fds[oflag]) {
3694 fp->fi_fds[oflag] = filp;
3695 filp = NULL;
3696 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003697 }
Trond Myklebustde186432014-07-10 14:07:26 -04003698 spin_unlock(&fp->fi_lock);
3699 if (filp)
3700 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003701
3702 status = nfsd4_truncate(rqstp, cur_fh, open);
3703 if (status)
3704 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003705out:
3706 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003707out_put_access:
3708 stp->st_access_bmap = old_access_bmap;
3709 nfs4_file_put_access(fp, open->op_share_access);
3710 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3711 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003712}
3713
Al Virob37ad282006-10-19 23:28:59 -07003714static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003715nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716{
Al Virob37ad282006-10-19 23:28:59 -07003717 __be32 status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003718 unsigned char old_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04003720 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003721 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003722
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003723 /* test and set deny mode */
3724 spin_lock(&fp->fi_lock);
3725 status = nfs4_file_check_deny(fp, open->op_share_deny);
3726 if (status == nfs_ok) {
3727 old_deny_bmap = stp->st_deny_bmap;
3728 set_deny(open->op_share_deny, stp);
3729 fp->fi_share_deny |=
3730 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3731 }
3732 spin_unlock(&fp->fi_lock);
3733
3734 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003737 status = nfsd4_truncate(rqstp, cur_fh, open);
3738 if (status != nfs_ok)
3739 reset_union_bmap_deny(old_deny_bmap, stp);
3740 return status;
3741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003744nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003746 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003749/* Should we give out recallable state?: */
3750static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3751{
3752 if (clp->cl_cb_state == NFSD4_CB_UP)
3753 return true;
3754 /*
3755 * In the sessions case, since we don't have to establish a
3756 * separate connection for callbacks, we assume it's OK
3757 * until we hear otherwise:
3758 */
3759 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3760}
3761
Jeff Laytond564fbe2014-07-16 10:31:58 -04003762static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003763{
3764 struct file_lock *fl;
3765
3766 fl = locks_alloc_lock();
3767 if (!fl)
3768 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003769 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003770 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003771 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3772 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04003773 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003774 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003775 return fl;
3776}
3777
J. Bruce Fields99c41512013-05-21 16:21:25 -04003778static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003779{
Trond Myklebust11b91642014-07-29 21:34:08 -04003780 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton415b96c2014-08-22 12:26:36 -04003781 struct file_lock *fl, *ret;
Jeff Layton417c6622014-07-21 09:34:57 -04003782 struct file *filp;
3783 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003784
Jeff Laytond564fbe2014-07-16 10:31:58 -04003785 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003786 if (!fl)
3787 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04003788 filp = find_readable_file(fp);
3789 if (!filp) {
3790 /* We should always have a readable file here */
3791 WARN_ON_ONCE(1);
3792 return -EBADF;
3793 }
3794 fl->fl_file = filp;
Jeff Layton415b96c2014-08-22 12:26:36 -04003795 ret = fl;
Jeff Laytone6f5c782014-08-22 10:40:25 -04003796 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003797 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04003798 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04003799 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04003800 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04003801 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003802 spin_lock(&fp->fi_lock);
3803 /* Did the lease get broken before we took the lock? */
3804 status = -EAGAIN;
3805 if (fp->fi_had_conflict)
3806 goto out_unlock;
3807 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04003808 if (fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003809 status = 0;
3810 atomic_inc(&fp->fi_delegees);
3811 hash_delegation_locked(dp, fp);
3812 goto out_unlock;
3813 }
Jeff Layton417c6622014-07-21 09:34:57 -04003814 fp->fi_deleg_file = filp;
3815 atomic_set(&fp->fi_delegees, 1);
Benny Halevy931ee562014-05-30 09:09:27 -04003816 hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04003817 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003818 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003819 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003820out_unlock:
3821 spin_unlock(&fp->fi_lock);
3822 spin_unlock(&state_lock);
3823out_fput:
3824 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003825 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003826}
3827
Jeff Layton0b266932014-07-25 07:34:25 -04003828static struct nfs4_delegation *
3829nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3830 struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003831{
Jeff Layton0b266932014-07-25 07:34:25 -04003832 int status;
3833 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04003834
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003835 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04003836 return ERR_PTR(-EAGAIN);
3837
3838 dp = alloc_init_deleg(clp, fh);
3839 if (!dp)
3840 return ERR_PTR(-ENOMEM);
3841
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003842 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003843 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003844 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04003845 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04003846 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04003847 spin_unlock(&fp->fi_lock);
3848 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003849 status = nfs4_setlease(dp);
3850 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04003851 }
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003852 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003853 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04003854 status = -EAGAIN;
3855 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003856 }
Benny Halevy931ee562014-05-30 09:09:27 -04003857 hash_delegation_locked(dp, fp);
Jeff Layton0b266932014-07-25 07:34:25 -04003858 status = 0;
Jeff Layton417c6622014-07-21 09:34:57 -04003859out_unlock:
3860 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04003861 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04003862out:
3863 if (status) {
Trond Myklebust60116952014-07-29 21:34:06 -04003864 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04003865 return ERR_PTR(status);
3866 }
3867 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003868}
3869
Benny Halevy4aa89132012-02-21 14:16:44 -08003870static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3871{
3872 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3873 if (status == -EAGAIN)
3874 open->op_why_no_deleg = WND4_CONTENTION;
3875 else {
3876 open->op_why_no_deleg = WND4_RESOURCE;
3877 switch (open->op_deleg_want) {
3878 case NFS4_SHARE_WANT_READ_DELEG:
3879 case NFS4_SHARE_WANT_WRITE_DELEG:
3880 case NFS4_SHARE_WANT_ANY_DELEG:
3881 break;
3882 case NFS4_SHARE_WANT_CANCEL:
3883 open->op_why_no_deleg = WND4_CANCELLED;
3884 break;
3885 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003886 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003887 }
3888 }
3889}
3890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891/*
3892 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003893 *
3894 * Note we don't support write delegations, and won't until the vfs has
3895 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 */
3897static void
Jeff Layton4cf59222014-07-25 07:34:24 -04003898nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3899 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900{
3901 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04003902 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3903 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003904 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003905 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003907 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003908 open->op_recall = 0;
3909 switch (open->op_claim_type) {
3910 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003911 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003912 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003913 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3914 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003915 break;
3916 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003917 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003918 /*
3919 * Let's not give out any delegations till everyone's
3920 * had the chance to reclaim theirs....
3921 */
Jeff Layton4cf59222014-07-25 07:34:24 -04003922 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003923 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003924 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003925 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003926 /*
3927 * Also, if the file was opened for write or
3928 * create, there's a good chance the client's
3929 * about to write to it, resulting in an
3930 * immediate recall (since we don't support
3931 * write delegations):
3932 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003933 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003934 goto out_no_deleg;
3935 if (open->op_create == NFS4_OPEN_CREATE)
3936 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003937 break;
3938 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003939 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003940 }
Trond Myklebust11b91642014-07-29 21:34:08 -04003941 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
Jeff Layton0b266932014-07-25 07:34:25 -04003942 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003943 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003945 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003947 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003948 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003949 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04003950 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003951 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003952out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003953 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3954 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003955 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003956 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003957 open->op_recall = 1;
3958 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003959
3960 /* 4.1 client asking for a delegation? */
3961 if (open->op_deleg_want)
3962 nfsd4_open_deleg_none_ext(open, status);
3963 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
Benny Halevye27f49c2012-02-21 14:16:54 -08003966static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3967 struct nfs4_delegation *dp)
3968{
3969 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3970 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3971 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3972 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3973 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3974 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3975 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3976 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3977 }
3978 /* Otherwise the client must be confused wanting a delegation
3979 * it already has, therefore we don't return
3980 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3981 */
3982}
3983
Al Virob37ad282006-10-19 23:28:59 -07003984__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3986{
Andy Adamson66689582009-04-03 08:28:45 +03003987 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003988 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003990 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003991 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003992 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 /*
3995 * Lookup file; if found, lookup stateid and check open request,
3996 * and check for delegations in the process of being recalled.
3997 * If not found, create the nfs4_file struct
3998 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003999 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004000 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004001 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004002 if (status)
4003 goto out;
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004004 stp = nfsd4_find_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004006 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004007 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004008 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004009 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004010 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 }
4012
4013 /*
4014 * OPEN the file, or upgrade an existing OPEN.
4015 * If truncate fails, the OPEN fails.
4016 */
4017 if (stp) {
4018 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004019 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 if (status)
4021 goto out;
4022 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004023 stp = open->op_stp;
4024 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004025 init_open_stateid(stp, fp, open);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004026 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4027 if (status) {
4028 release_open_stateid(stp);
4029 goto out;
4030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004032 update_stateid(&stp->st_stid.sc_stateid);
4033 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Benny Halevyd24433c2012-02-16 20:57:17 +02004035 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004036 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4037 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4038 open->op_why_no_deleg = WND4_NOT_WANTED;
4039 goto nodeleg;
4040 }
4041 }
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 /*
4044 * Attempt to hand out a delegation. No error return, because the
4045 * OPEN succeeds even if we fail.
4046 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004047 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004048nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 status = nfs_ok;
4050
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004051 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004052 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004054 /* 4.1 client trying to upgrade/downgrade delegation? */
4055 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004056 open->op_deleg_want)
4057 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004058
NeilBrown13cd2182005-06-23 22:03:10 -07004059 if (fp)
4060 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004061 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05004062 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 /*
4064 * To finish the open response, we just need to set the rflags.
4065 */
4066 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004067 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004068 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004070 if (dp)
4071 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004072 if (stp)
4073 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 return status;
4076}
4077
Jeff Layton58fb12e2014-07-29 21:34:27 -04004078void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4079 struct nfsd4_open *open, __be32 status)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004080{
4081 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004082 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004083
Jeff Laytond3134b12014-07-29 21:34:32 -04004084 nfsd4_cstate_assign_replay(cstate, so);
4085 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004086 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004087 if (open->op_file)
4088 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004089 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004090 nfs4_put_stid(&open->op_stp->st_stid);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004091}
4092
Al Virob37ad282006-10-19 23:28:59 -07004093__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004094nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4095 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
4097 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004098 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004099 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 dprintk("process_renew(%08x/%08x): starting\n",
4102 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004103 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004104 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004106 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004108 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004109 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 goto out;
4111 status = nfs_ok;
4112out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 return status;
4114}
4115
NeilBrowna76b4312005-06-23 22:04:01 -07004116static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004117nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004118{
Jeff Layton33dcc482012-04-10 11:08:48 -04004119 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004120 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004121 return;
4122
NeilBrowna76b4312005-06-23 22:04:01 -07004123 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004124 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004125 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004126 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004127 /*
4128 * Now that every NFSv4 client has had the chance to recover and
4129 * to see the (possibly new, possibly shorter) lease time, we
4130 * can safely set the next grace time to the current lease time:
4131 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004132 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07004133}
4134
NeilBrownfd39ca92005-06-23 22:04:03 -07004135static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004136nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004139 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004141 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004143 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004144 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004147 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004148 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004149 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004150 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 clp = list_entry(pos, struct nfs4_client, cl_lru);
4152 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4153 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004154 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 break;
4156 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004157 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004158 dprintk("NFSD: client in use (clientid %08x)\n",
4159 clp->cl_clientid.cl_id);
4160 continue;
4161 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004162 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004163 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004164 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004165 list_for_each_safe(pos, next, &reaplist) {
4166 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 dprintk("NFSD: purging unused client (clientid %08x)\n",
4168 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004169 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 expire_client(clp);
4171 }
Benny Halevycdc97502014-05-30 09:09:30 -04004172 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004173 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004175 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4176 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004178 t = dp->dl_time - cutoff;
4179 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 break;
4181 }
Jeff Layton42690672014-07-25 07:34:20 -04004182 unhash_delegation_locked(dp);
4183 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 }
Benny Halevycdc97502014-05-30 09:09:30 -04004185 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004186 while (!list_empty(&reaplist)) {
4187 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4188 dl_recall_lru);
4189 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004190 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 }
Jeff Layton217526e2014-07-30 08:27:11 -04004192
4193 spin_lock(&nn->client_lock);
4194 while (!list_empty(&nn->close_lru)) {
4195 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4196 oo_close_lru);
4197 if (time_after((unsigned long)oo->oo_time,
4198 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004199 t = oo->oo_time - cutoff;
4200 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 break;
4202 }
Jeff Layton217526e2014-07-30 08:27:11 -04004203 list_del_init(&oo->oo_close_lru);
4204 stp = oo->oo_last_closed_stid;
4205 oo->oo_last_closed_stid = NULL;
4206 spin_unlock(&nn->client_lock);
4207 nfs4_put_stid(&stp->st_stid);
4208 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
Jeff Layton217526e2014-07-30 08:27:11 -04004210 spin_unlock(&nn->client_lock);
4211
Jeff Laytona832e7a2014-05-30 09:09:26 -04004212 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004213 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
Harvey Harrisona254b242008-02-20 12:49:00 -08004216static struct workqueue_struct *laundry_wq;
4217static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004218
4219static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004220laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
4222 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004223 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4224 work);
4225 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4226 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004228 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004230 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004233static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004234{
Trond Myklebust11b91642014-07-29 21:34:08 -04004235 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004236 return nfserr_bad_stateid;
4237 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004241access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004243 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4244 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4245 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246}
4247
4248static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004249access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004251 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4252 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253}
4254
4255static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004256__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
Al Virob37ad282006-10-19 23:28:59 -07004258 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
J. Bruce Fields02921912010-07-29 15:16:59 -04004260 /* For lock stateid's, we test the parent open, not the lock: */
4261 if (stp->st_openstp)
4262 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004263 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004265 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 goto out;
4267 status = nfs_ok;
4268out:
4269 return status;
4270}
4271
Al Virob37ad282006-10-19 23:28:59 -07004272static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004273check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004275 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004277 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004278 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 * conflicting state; so we must wait out the grace period. */
4280 return nfserr_grace;
4281 } else if (flags & WR_STATE)
4282 return nfs4_share_conflict(current_fh,
4283 NFS4_SHARE_DENY_WRITE);
4284 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4285 return nfs4_share_conflict(current_fh,
4286 NFS4_SHARE_DENY_READ);
4287}
4288
4289/*
4290 * Allow READ/WRITE during grace period on recovered state only for files
4291 * that are not able to provide mandatory locking.
4292 */
4293static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004294grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004296 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
J. Bruce Fields81b82962011-08-23 11:03:29 -04004299/* Returns true iff a is later than b: */
4300static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4301{
Jim Rees1a9357f2013-05-17 17:33:00 -04004302 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04004303}
4304
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004305static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004306{
Andy Adamson66689582009-04-03 08:28:45 +03004307 /*
4308 * When sessions are used the stateid generation number is ignored
4309 * when it is zero.
4310 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004311 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004312 return nfs_ok;
4313
4314 if (in->si_generation == ref->si_generation)
4315 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004316
J. Bruce Fields0836f582008-01-26 19:08:12 -05004317 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004318 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004319 return nfserr_bad_stateid;
4320 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004321 * However, we could see a stateid from the past, even from a
4322 * non-buggy client. For example, if the client sends a lock
4323 * while some IO is outstanding, the lock may bump si_generation
4324 * while the IO is still in flight. The client could avoid that
4325 * situation by waiting for responses on all the IO requests,
4326 * but better performance may result in retrying IO that
4327 * receives an old_stateid error if requests are rarely
4328 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004329 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004330 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004331}
4332
Chuck Lever7df302f2012-05-29 13:56:37 -04004333static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004334{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004335 struct nfs4_stid *s;
4336 struct nfs4_ol_stateid *ols;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004337 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004338
Chuck Lever7df302f2012-05-29 13:56:37 -04004339 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004340 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004341 /* Client debugging aid. */
4342 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4343 char addr_str[INET6_ADDRSTRLEN];
4344 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4345 sizeof(addr_str));
4346 pr_warn_ratelimited("NFSD: client %s testing state ID "
4347 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004348 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004349 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004350 spin_lock(&cl->cl_lock);
4351 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004352 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004353 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004354 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004355 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004356 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004357 switch (s->sc_type) {
4358 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004359 status = nfs_ok;
4360 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004361 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004362 status = nfserr_deleg_revoked;
4363 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004364 case NFS4_OPEN_STID:
4365 case NFS4_LOCK_STID:
4366 ols = openlockstateid(s);
4367 if (ols->st_stateowner->so_is_open_owner
4368 && !(openowner(ols->st_stateowner)->oo_flags
4369 & NFS4_OO_CONFIRMED))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004370 status = nfserr_bad_stateid;
4371 else
4372 status = nfs_ok;
4373 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004374 default:
4375 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004376 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004377 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004378 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004379 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004380 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004381out_unlock:
4382 spin_unlock(&cl->cl_lock);
4383 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004384}
4385
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004386static __be32
4387nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4388 stateid_t *stateid, unsigned char typemask,
4389 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004390{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004391 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004392
4393 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4394 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004395 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004396 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004397 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004398 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004399 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004400 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004401 if (status)
4402 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004403 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004404 if (!*s)
4405 return nfserr_bad_stateid;
4406 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004407}
4408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409/*
4410* Checks for stateid operations
4411*/
Al Virob37ad282006-10-19 23:28:59 -07004412__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004413nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03004414 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
J. Bruce Fields69064a22011-09-09 11:54:57 -04004416 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004417 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03004419 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04004422 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004423 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (filpp)
4426 *filpp = NULL;
4427
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004428 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 return nfserr_grace;
4430
4431 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004432 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004434 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004435 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004436 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004437 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004438 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04004439 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4440 if (status)
4441 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004442 switch (s->sc_type) {
4443 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004444 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08004445 status = nfs4_check_delegmode(dp, flags);
4446 if (status)
4447 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02004448 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004449 file = dp->dl_stid.sc_file->fi_deleg_file;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004450 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004451 WARN_ON_ONCE(1);
4452 status = nfserr_serverfault;
4453 goto out;
4454 }
Trond Myklebustde186432014-07-10 14:07:26 -04004455 get_file(file);
Dan Carpenter43b01782010-10-27 23:19:04 +02004456 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004457 break;
4458 case NFS4_OPEN_STID:
4459 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04004460 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004461 status = nfs4_check_fh(current_fh, stp);
4462 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004464 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004465 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08004467 status = nfs4_check_openmode(stp, flags);
4468 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004470 if (filpp) {
Trond Myklebust11b91642014-07-29 21:34:08 -04004471 struct nfs4_file *fp = stp->st_stid.sc_file;
4472
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004473 if (flags & RD_STATE)
Trond Myklebust11b91642014-07-29 21:34:08 -04004474 file = find_readable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004475 else
Trond Myklebust11b91642014-07-29 21:34:08 -04004476 file = find_writeable_file(fp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004477 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004478 break;
4479 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004480 status = nfserr_bad_stateid;
4481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 }
4483 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04004484 if (file)
Trond Myklebustde186432014-07-10 14:07:26 -04004485 *filpp = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486out:
Trond Myklebustfd911012014-07-29 21:34:24 -04004487 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return status;
4489}
4490
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004491/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004492 * Test if the stateid is valid
4493 */
4494__be32
4495nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4496 struct nfsd4_test_stateid *test_stateid)
4497{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004498 struct nfsd4_test_stateid_id *stateid;
4499 struct nfs4_client *cl = cstate->session->se_client;
4500
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004501 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004502 stateid->ts_id_status =
4503 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004504
Bryan Schumaker17456802011-07-13 10:50:48 -04004505 return nfs_ok;
4506}
4507
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004508__be32
4509nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4510 struct nfsd4_free_stateid *free_stateid)
4511{
4512 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004513 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004514 struct nfs4_delegation *dp;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004515 struct nfs4_ol_stateid *stp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004516 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004517 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004518
Jeff Layton1af71cc2014-07-29 21:34:14 -04004519 spin_lock(&cl->cl_lock);
4520 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004521 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004522 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004523 switch (s->sc_type) {
4524 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004525 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004526 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004527 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004528 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4529 if (ret)
4530 break;
4531 ret = nfserr_locks_held;
4532 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04004533 case NFS4_LOCK_STID:
4534 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4535 if (ret)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004536 break;
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004537 stp = openlockstateid(s);
4538 ret = nfserr_locks_held;
4539 if (check_for_locks(stp->st_stid.sc_file,
4540 lockowner(stp->st_stateowner)))
4541 break;
4542 unhash_lock_stateid(stp);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004543 spin_unlock(&cl->cl_lock);
Jeff Laytonfc5a96c2014-07-29 21:34:40 -04004544 nfs4_put_stid(s);
4545 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004546 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004547 case NFS4_REVOKED_DELEG_STID:
4548 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004549 list_del_init(&dp->dl_recall_lru);
4550 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04004551 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004552 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004553 goto out;
4554 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004555 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004556out_unlock:
4557 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004558out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004559 return ret;
4560}
4561
NeilBrown4c4cd222005-07-07 17:59:27 -07004562static inline int
4563setlkflg (int type)
4564{
4565 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4566 RD_STATE : WR_STATE;
4567}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004569static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004570{
4571 struct svc_fh *current_fh = &cstate->current_fh;
4572 struct nfs4_stateowner *sop = stp->st_stateowner;
4573 __be32 status;
4574
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004575 status = nfsd4_check_seqid(cstate, sop, seqid);
4576 if (status)
4577 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004578 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4579 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004580 /*
4581 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004582 * nfserr_replay_me from the previous step, and
4583 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004584 */
4585 return nfserr_bad_stateid;
4586 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4587 if (status)
4588 return status;
4589 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004590}
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592/*
4593 * Checks for sequence id mutating operations.
4594 */
Al Virob37ad282006-10-19 23:28:59 -07004595static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03004596nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004597 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004598 struct nfs4_ol_stateid **stpp,
4599 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
J. Bruce Fields0836f582008-01-26 19:08:12 -05004601 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004602 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004603 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004605 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4606 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07004607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004609 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004610 if (status)
4611 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004612 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004613 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004615 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004616 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004617 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04004618 else
4619 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04004620 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004621}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05004622
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004623static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4624 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004625{
4626 __be32 status;
4627 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004628 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004630 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004631 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004632 if (status)
4633 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004634 oo = openowner(stp->st_stateowner);
4635 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4636 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07004637 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04004638 }
4639 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004640 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Al Virob37ad282006-10-19 23:28:59 -07004643__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004644nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004645 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Al Virob37ad282006-10-19 23:28:59 -07004647 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004648 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004649 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004650 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Al Viroa6a9f182013-09-16 10:57:01 -04004652 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4653 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004655 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004656 if (status)
4657 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004659 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004660 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004661 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004662 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004663 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004664 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004665 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004666 if (oo->oo_flags & NFS4_OO_CONFIRMED)
Trond Myklebust2585fc72014-07-29 21:34:21 -04004667 goto put_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004668 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004669 update_stateid(&stp->st_stid.sc_stateid);
4670 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004671 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004672 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004673
Jeff Layton2a4317c2012-03-21 16:42:43 -04004674 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004675 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04004676put_stateid:
4677 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004679 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return status;
4681}
4682
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004683static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004685 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004686 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04004687 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004688 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004689}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004690
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004691static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4692{
4693 switch (to_access) {
4694 case NFS4_SHARE_ACCESS_READ:
4695 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4696 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4697 break;
4698 case NFS4_SHARE_ACCESS_WRITE:
4699 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4700 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4701 break;
4702 case NFS4_SHARE_ACCESS_BOTH:
4703 break;
4704 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004705 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707}
4708
Al Virob37ad282006-10-19 23:28:59 -07004709__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004710nfsd4_open_downgrade(struct svc_rqst *rqstp,
4711 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004712 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713{
Al Virob37ad282006-10-19 23:28:59 -07004714 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004715 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004716 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Al Viroa6a9f182013-09-16 10:57:01 -04004718 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4719 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004721 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004722 if (od->od_deleg_want)
4723 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4724 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004726 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004727 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004728 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004731 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004732 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004734 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004736 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04004737 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004739 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004741 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
Jeff Laytonce0fc432012-05-11 09:45:14 -04004743 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004745 update_stateid(&stp->st_stid.sc_stateid);
4746 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04004748put_stateid:
4749 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004751 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 return status;
4753}
4754
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004755static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4756{
Trond Myklebustacf92952014-06-30 11:48:37 -04004757 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytond83017f2014-07-29 21:34:42 -04004758 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004759
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004760 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04004761 spin_lock(&clp->cl_lock);
Jeff Laytond83017f2014-07-29 21:34:42 -04004762 unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04004763
Jeff Laytond83017f2014-07-29 21:34:42 -04004764 if (clp->cl_minorversion) {
4765 put_ol_stateid_locked(s, &reaplist);
4766 spin_unlock(&clp->cl_lock);
4767 free_ol_stateid_reaplist(&reaplist);
4768 } else {
4769 spin_unlock(&clp->cl_lock);
4770 free_ol_stateid_reaplist(&reaplist);
Jeff Laytond3134b12014-07-29 21:34:32 -04004771 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04004772 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004773}
4774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775/*
4776 * nfs4_unlock_state() called after encode
4777 */
Al Virob37ad282006-10-19 23:28:59 -07004778__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004779nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004780 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Al Virob37ad282006-10-19 23:28:59 -07004782 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004783 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004784 struct net *net = SVC_NET(rqstp);
4785 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Al Viroa6a9f182013-09-16 10:57:01 -04004787 dprintk("NFSD: nfsd4_close on file %pd\n",
4788 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004790 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4791 &close->cl_stateid,
4792 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004793 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004794 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004795 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004797 update_stateid(&stp->st_stid.sc_stateid);
4798 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004800 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04004801
4802 /* put reference from nfs4_preprocess_seqid_op */
4803 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 return status;
4806}
4807
Al Virob37ad282006-10-19 23:28:59 -07004808__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004809nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4810 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004812 struct nfs4_delegation *dp;
4813 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004814 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004815 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004816 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004818 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004819 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004821 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004822 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004823 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004824 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004825 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004826 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04004827 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004828
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004829 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04004830put_stateid:
4831 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832out:
4833 return status;
4834}
4835
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Benny Halevy87df4de2008-12-15 19:42:03 +02004839static inline u64
4840end_offset(u64 start, u64 len)
4841{
4842 u64 end;
4843
4844 end = start + len;
4845 return end >= start ? end: NFS4_MAX_UINT64;
4846}
4847
4848/* last octet in a range */
4849static inline u64
4850last_byte_offset(u64 start, u64 len)
4851{
4852 u64 end;
4853
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004854 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004855 end = start + len;
4856 return end > start ? end - 1: NFS4_MAX_UINT64;
4857}
4858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859/*
4860 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4861 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4862 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4863 * locking, this prevents us from being completely protocol-compliant. The
4864 * real solution to this problem is to start using unsigned file offsets in
4865 * the VFS, but this is a very deep change!
4866 */
4867static inline void
4868nfs4_transform_lock_offset(struct file_lock *lock)
4869{
4870 if (lock->fl_start < 0)
4871 lock->fl_start = OFFSET_MAX;
4872 if (lock->fl_end < 0)
4873 lock->fl_end = OFFSET_MAX;
4874}
4875
Kinglong Meeaef95832014-08-22 10:18:44 -04004876static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4877{
4878 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4879 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4880}
4881
4882static void nfsd4_fl_put_owner(struct file_lock *fl)
4883{
4884 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4885
4886 if (lo) {
4887 nfs4_put_stateowner(&lo->lo_owner);
4888 fl->fl_owner = NULL;
4889 }
4890}
4891
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004892static const struct lock_manager_operations nfsd_posix_mng_ops = {
Kinglong Meeaef95832014-08-22 10:18:44 -04004893 .lm_get_owner = nfsd4_fl_get_owner,
4894 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07004895};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896
4897static inline void
4898nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4899{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004900 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
NeilBrownd5b90262006-04-10 22:55:22 -07004902 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004903 lo = (struct nfs4_lockowner *) fl->fl_owner;
4904 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4905 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004906 if (!deny->ld_owner.data)
4907 /* We just don't care that much */
4908 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004909 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4910 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004911 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004912nevermind:
4913 deny->ld_owner.len = 0;
4914 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004915 deny->ld_clientid.cl_boot = 0;
4916 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 }
4918 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004919 deny->ld_length = NFS4_MAX_UINT64;
4920 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4922 deny->ld_type = NFS4_READ_LT;
4923 if (fl->fl_type != F_RDLCK)
4924 deny->ld_type = NFS4_WRITE_LT;
4925}
4926
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004927static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004928find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004929 struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004931 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004932 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Trond Myklebust0a880a22014-07-30 08:27:10 -04004934 lockdep_assert_held(&clp->cl_lock);
4935
Trond Myklebustd4f04892014-07-29 21:34:36 -04004936 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4937 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004938 if (so->so_is_open_owner)
4939 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04004940 if (same_owner_str(so, owner))
4941 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943 return NULL;
4944}
4945
Trond Myklebustc58c6612014-07-29 21:34:35 -04004946static struct nfs4_lockowner *
4947find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004948 struct nfs4_client *clp)
Trond Myklebustc58c6612014-07-29 21:34:35 -04004949{
4950 struct nfs4_lockowner *lo;
4951
Trond Myklebustd4f04892014-07-29 21:34:36 -04004952 spin_lock(&clp->cl_lock);
4953 lo = find_lockowner_str_locked(clid, owner, clp);
4954 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004955 return lo;
4956}
4957
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004958static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4959{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004960 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004961}
4962
Jeff Layton6b180f02014-07-29 21:34:26 -04004963static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4964{
4965 struct nfs4_lockowner *lo = lockowner(sop);
4966
4967 kmem_cache_free(lockowner_slab, lo);
4968}
4969
4970static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004971 .so_unhash = nfs4_unhash_lockowner,
4972 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004973};
4974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975/*
4976 * Alloc a lock owner structure.
4977 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004978 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004980 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004982static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04004983alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4984 struct nfs4_ol_stateid *open_stp,
4985 struct nfsd4_lock *lock)
4986{
Trond Myklebustc58c6612014-07-29 21:34:35 -04004987 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004989 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4990 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004992 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4993 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04004994 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04004995 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04004996 spin_lock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004997 ret = find_lockowner_str_locked(&clp->cl_clientid,
Trond Myklebustd4f04892014-07-29 21:34:36 -04004998 &lock->lk_new_owner, clp);
Trond Myklebustc58c6612014-07-29 21:34:35 -04004999 if (ret == NULL) {
5000 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005001 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005002 ret = lo;
5003 } else
5004 nfs4_free_lockowner(&lo->lo_owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005005 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005006 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007}
5008
Jeff Layton356a95e2014-07-29 21:34:13 -04005009static void
5010init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5011 struct nfs4_file *fp, struct inode *inode,
5012 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005014 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Jeff Layton356a95e2014-07-29 21:34:13 -04005016 lockdep_assert_held(&clp->cl_lock);
5017
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005018 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005019 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005020 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005021 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005022 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005023 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005024 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005026 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005027 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005028 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005029 spin_lock(&fp->fi_lock);
5030 list_add(&stp->st_perfile, &fp->fi_stateids);
5031 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032}
5033
Jeff Laytonc53530d2014-06-30 11:48:39 -04005034static struct nfs4_ol_stateid *
5035find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5036{
5037 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005038 struct nfs4_client *clp = lo->lo_owner.so_client;
5039
5040 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005041
5042 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005043 if (lst->st_stid.sc_file == fp) {
5044 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005045 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005046 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005047 }
5048 return NULL;
5049}
5050
Jeff Layton356a95e2014-07-29 21:34:13 -04005051static struct nfs4_ol_stateid *
5052find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5053 struct inode *inode, struct nfs4_ol_stateid *ost,
5054 bool *new)
5055{
5056 struct nfs4_stid *ns = NULL;
5057 struct nfs4_ol_stateid *lst;
5058 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5059 struct nfs4_client *clp = oo->oo_owner.so_client;
5060
5061 spin_lock(&clp->cl_lock);
5062 lst = find_lock_stateid(lo, fi);
5063 if (lst == NULL) {
5064 spin_unlock(&clp->cl_lock);
5065 ns = nfs4_alloc_stid(clp, stateid_slab);
5066 if (ns == NULL)
5067 return NULL;
5068
5069 spin_lock(&clp->cl_lock);
5070 lst = find_lock_stateid(lo, fi);
5071 if (likely(!lst)) {
5072 lst = openlockstateid(ns);
5073 init_lock_stateid(lst, lo, fi, inode, ost);
5074 ns = NULL;
5075 *new = true;
5076 }
5077 }
5078 spin_unlock(&clp->cl_lock);
5079 if (ns)
5080 nfs4_put_stid(ns);
5081 return lst;
5082}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005083
NeilBrownfd39ca92005-06-23 22:04:03 -07005084static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085check_lock_length(u64 offset, u64 length)
5086{
Benny Halevy87df4de2008-12-15 19:42:03 +02005087 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 LOFF_OVERFLOW(offset, length)));
5089}
5090
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005091static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005092{
Trond Myklebust11b91642014-07-29 21:34:08 -04005093 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005094
Jeff Layton7214e862014-07-10 14:07:33 -04005095 lockdep_assert_held(&fp->fi_lock);
5096
Jeff Layton82c5ff12012-05-11 09:45:13 -04005097 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005098 return;
Jeff Layton12659652014-07-10 14:07:28 -04005099 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005100 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005101}
5102
Jeff Layton356a95e2014-07-29 21:34:13 -04005103static __be32
5104lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5105 struct nfs4_ol_stateid *ost,
5106 struct nfsd4_lock *lock,
5107 struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005108{
Jeff Layton5db1c032014-07-29 21:34:28 -04005109 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005110 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005111 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5112 struct nfs4_client *cl = oo->oo_owner.so_client;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005113 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005114 struct nfs4_lockowner *lo;
5115 unsigned int strhashval;
5116
Trond Myklebustd4f04892014-07-29 21:34:36 -04005117 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005118 if (!lo) {
Trond Myklebustd4f04892014-07-29 21:34:36 -04005119 strhashval = ownerstr_hashval(&lock->v.new.owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005120 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5121 if (lo == NULL)
5122 return nfserr_jukebox;
5123 } else {
5124 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005125 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005126 if (!cstate->minorversion &&
5127 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005128 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005129 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005130
Jeff Layton356a95e2014-07-29 21:34:13 -04005131 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005132 if (*lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005133 status = nfserr_jukebox;
5134 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005135 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005136 status = nfs_ok;
5137out:
5138 nfs4_put_stateowner(&lo->lo_owner);
5139 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005140}
5141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142/*
5143 * LOCK operation
5144 */
Al Virob37ad282006-10-19 23:28:59 -07005145__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005146nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005147 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005149 struct nfs4_openowner *open_sop = NULL;
5150 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005151 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005152 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005153 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005154 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005155 struct file_lock *file_lock = NULL;
5156 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005157 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005158 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005159 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005160 bool new = false;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005161 struct net *net = SVC_NET(rqstp);
5162 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
5164 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5165 (long long) lock->lk_offset,
5166 (long long) lock->lk_length);
5167
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 if (check_lock_length(lock->lk_offset, lock->lk_length))
5169 return nfserr_inval;
5170
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005171 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005172 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005173 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5174 return status;
5175 }
5176
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005178 if (nfsd4_has_session(cstate))
5179 /* See rfc 5661 18.10.3: given clientid is ignored: */
5180 memcpy(&lock->v.new.clientid,
5181 &cstate->session->se_client->cl_clientid,
5182 sizeof(clientid_t));
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005185 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005189 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 lock->lk_new_open_seqid,
5191 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005192 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005193 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005195 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005196 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005197 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005198 &lock->v.new.clientid))
5199 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005200 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005201 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005202 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005203 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005204 lock->lk_old_lock_seqid,
5205 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005206 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005207 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005208 if (status)
5209 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005210 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005212 lkflg = setlkflg(lock->lk_type);
5213 status = nfs4_check_openmode(lock_stp, lkflg);
5214 if (status)
5215 goto out;
5216
NeilBrown0dd395d2005-07-07 17:59:15 -07005217 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005218 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005219 goto out;
5220 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005221 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005222 goto out;
5223
Jeff Layton21179d82012-08-21 08:03:32 -04005224 file_lock = locks_alloc_lock();
5225 if (!file_lock) {
5226 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5227 status = nfserr_jukebox;
5228 goto out;
5229 }
5230
Trond Myklebust11b91642014-07-29 21:34:08 -04005231 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 switch (lock->lk_type) {
5233 case NFS4_READ_LT:
5234 case NFS4_READW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005235 spin_lock(&fp->fi_lock);
5236 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005237 if (filp)
5238 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005239 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005240 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 case NFS4_WRITE_LT:
5243 case NFS4_WRITEW_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005244 spin_lock(&fp->fi_lock);
5245 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005246 if (filp)
5247 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005248 spin_unlock(&fp->fi_lock);
Jeff Layton21179d82012-08-21 08:03:32 -04005249 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 default:
5252 status = nfserr_inval;
5253 goto out;
5254 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005255 if (!filp) {
5256 status = nfserr_openmode;
5257 goto out;
5258 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005259
5260 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005261 file_lock->fl_pid = current->tgid;
5262 file_lock->fl_file = filp;
5263 file_lock->fl_flags = FL_POSIX;
5264 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5265 file_lock->fl_start = lock->lk_offset;
5266 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5267 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Jeff Layton21179d82012-08-21 08:03:32 -04005269 conflock = locks_alloc_lock();
5270 if (!conflock) {
5271 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5272 status = nfserr_jukebox;
5273 goto out;
5274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Jeff Layton21179d82012-08-21 08:03:32 -04005276 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07005277 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005279 update_stateid(&lock_stp->st_stid.sc_stateid);
5280 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07005282 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005283 break;
5284 case (EAGAIN): /* conflock holds conflicting lock */
5285 status = nfserr_denied;
5286 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005287 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 case (EDEADLK):
5290 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005291 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005292 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005293 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005294 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297out:
Trond Myklebustde186432014-07-10 14:07:26 -04005298 if (filp)
5299 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005300 if (lock_stp) {
5301 /* Bump seqid manually if the 4.0 replay owner is openowner */
5302 if (cstate->replay_owner &&
5303 cstate->replay_owner != &lock_sop->lo_owner &&
5304 seqid_mutating_err(ntohl(status)))
5305 lock_sop->lo_owner.so_seqid++;
5306
5307 /*
5308 * If this is a new, never-before-used stateid, and we are
5309 * returning an error, then just go ahead and release it.
5310 */
5311 if (status && new)
5312 release_lock_stateid(lock_stp);
5313
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005314 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005315 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005316 if (open_stp)
5317 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005318 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005319 if (file_lock)
5320 locks_free_lock(file_lock);
5321 if (conflock)
5322 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 return status;
5324}
5325
5326/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005327 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5328 * so we do a temporary open here just to get an open file to pass to
5329 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5330 * inode operation.)
5331 */
Al Viro04da6e92012-04-13 00:00:04 -04005332static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005333{
5334 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005335 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5336 if (!err) {
5337 err = nfserrno(vfs_test_lock(file, lock));
5338 nfsd_close(file);
5339 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005340 return err;
5341}
5342
5343/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 * LOCKT operation
5345 */
Al Virob37ad282006-10-19 23:28:59 -07005346__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005347nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5348 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
Jeff Layton21179d82012-08-21 08:03:32 -04005350 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005351 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005352 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005353 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005355 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 return nfserr_grace;
5357
5358 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5359 return nfserr_inval;
5360
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005361 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005362 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005363 if (status)
5364 goto out;
5365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005367 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Jeff Layton21179d82012-08-21 08:03:32 -04005370 file_lock = locks_alloc_lock();
5371 if (!file_lock) {
5372 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5373 status = nfserr_jukebox;
5374 goto out;
5375 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 switch (lockt->lt_type) {
5378 case NFS4_READ_LT:
5379 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005380 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 break;
5382 case NFS4_WRITE_LT:
5383 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005384 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 break;
5386 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005387 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 status = nfserr_inval;
5389 goto out;
5390 }
5391
Trond Myklebustd4f04892014-07-29 21:34:36 -04005392 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5393 cstate->clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005394 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005395 file_lock->fl_owner = (fl_owner_t)lo;
5396 file_lock->fl_pid = current->tgid;
5397 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Jeff Layton21179d82012-08-21 08:03:32 -04005399 file_lock->fl_start = lockt->lt_offset;
5400 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Jeff Layton21179d82012-08-21 08:03:32 -04005402 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
Jeff Layton21179d82012-08-21 08:03:32 -04005404 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005405 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005406 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005407
Jeff Layton21179d82012-08-21 08:03:32 -04005408 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005410 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 }
5412out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005413 if (lo)
5414 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005415 if (file_lock)
5416 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return status;
5418}
5419
Al Virob37ad282006-10-19 23:28:59 -07005420__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005421nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005422 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005424 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005426 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005427 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005428 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005429 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5432 (long long) locku->lu_offset,
5433 (long long) locku->lu_length);
5434
5435 if (check_lock_length(locku->lu_offset, locku->lu_length))
5436 return nfserr_inval;
5437
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005438 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005439 &locku->lu_stateid, NFS4_LOCK_STID,
5440 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005441 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04005443 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005444 if (!filp) {
5445 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04005446 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005447 }
Jeff Layton21179d82012-08-21 08:03:32 -04005448 file_lock = locks_alloc_lock();
5449 if (!file_lock) {
5450 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5451 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04005452 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04005453 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005454
Jeff Layton21179d82012-08-21 08:03:32 -04005455 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04005456 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04005457 file_lock->fl_pid = current->tgid;
5458 file_lock->fl_file = filp;
5459 file_lock->fl_flags = FL_POSIX;
5460 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5461 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Jeff Layton21179d82012-08-21 08:03:32 -04005463 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5464 locku->lu_length);
5465 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Jeff Layton21179d82012-08-21 08:03:32 -04005467 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07005468 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05005469 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 goto out_nfserr;
5471 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005472 update_stateid(&stp->st_stid.sc_stateid);
5473 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Trond Myklebustde186432014-07-10 14:07:26 -04005474fput:
5475 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04005476put_stateid:
5477 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005479 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005480 if (file_lock)
5481 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 return status;
5483
5484out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07005485 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04005486 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487}
5488
5489/*
5490 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005491 * true: locks held by lockowner
5492 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005494static bool
5495check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
5497 struct file_lock **flpp;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005498 int status = false;
5499 struct file *filp = find_any_file(fp);
5500 struct inode *inode;
5501
5502 if (!filp) {
5503 /* Any valid lock stateid should have some sort of access */
5504 WARN_ON_ONCE(1);
5505 return status;
5506 }
5507
5508 inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
Jeff Layton1c8c6012013-06-21 08:58:15 -04005510 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005512 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005513 status = true;
5514 break;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08005515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
Jeff Layton1c8c6012013-06-21 08:58:15 -04005517 spin_unlock(&inode->i_lock);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005518 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return status;
5520}
5521
Al Virob37ad282006-10-19 23:28:59 -07005522__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005523nfsd4_release_lockowner(struct svc_rqst *rqstp,
5524 struct nfsd4_compound_state *cstate,
5525 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526{
5527 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04005528 struct nfs4_stateowner *sop;
5529 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005530 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005532 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07005533 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005534 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005535 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5538 clid->cl_boot, clid->cl_id);
5539
Jeff Layton4b24ca72014-06-30 11:48:44 -04005540 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005541 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04005542 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005543
Trond Myklebustd4f04892014-07-29 21:34:36 -04005544 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04005545 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04005546 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005547 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04005548 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04005549
5550 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005551 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04005552
Jeff Layton882e9d22014-07-29 21:34:37 -04005553 /* see if there are still any locks associated with it */
5554 lo = lockowner(sop);
5555 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5556 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5557 status = nfserr_locks_held;
5558 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04005559 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04005560 }
Jeff Layton5adfd882014-07-29 21:34:31 -04005561 }
Jeff Layton882e9d22014-07-29 21:34:37 -04005562
Kinglong Meeb5971af2014-08-22 10:18:43 -04005563 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04005564 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04005565 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04005566 spin_unlock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04005567 if (lo)
5568 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 return status;
5570}
5571
5572static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07005573alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
NeilBrowna55370a2005-06-23 22:03:52 -07005575 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576}
5577
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005578bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005579nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07005580{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005581 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07005582
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005583 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005584 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07005585}
5586
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587/*
5588 * failure => all reset bets are off, nfserr_no_grace...
5589 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05005590struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005591nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592{
5593 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005594 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
NeilBrowna55370a2005-06-23 22:03:52 -07005596 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5597 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05005598 if (crp) {
5599 strhashval = clientstr_hashval(name);
5600 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005601 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05005602 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05005603 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005604 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05005605 }
5606 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
Jeff Layton2a4317c2012-03-21 16:42:43 -04005609void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005610nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05005611{
5612 list_del(&crp->cr_strhash);
5613 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005614 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05005615}
5616
5617void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005618nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619{
5620 struct nfs4_client_reclaim *crp = NULL;
5621 int i;
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005624 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5625 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005627 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
5629 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005630 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631}
5632
5633/*
5634 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04005635struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005636nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
5638 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 struct nfs4_client_reclaim *crp = NULL;
5640
Jeff Layton278c9312012-11-12 15:00:52 -05005641 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Jeff Layton278c9312012-11-12 15:00:52 -05005643 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03005644 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05005645 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 return crp;
5647 }
5648 }
5649 return NULL;
5650}
5651
5652/*
5653* Called from OPEN. Look for clientid in reclaim list.
5654*/
Al Virob37ad282006-10-19 23:28:59 -07005655__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005656nfs4_check_open_reclaim(clientid_t *clid,
5657 struct nfsd4_compound_state *cstate,
5658 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005660 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04005661
5662 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005663 status = lookup_clientid(clid, cstate, nn);
5664 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04005665 return nfserr_reclaim_bad;
5666
Trond Myklebust0fe492d2014-06-30 11:48:47 -04005667 if (nfsd4_client_record_check(cstate->clp))
5668 return nfserr_reclaim_bad;
5669
5670 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671}
5672
Bryan Schumaker65178db2011-11-01 13:35:21 -04005673#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04005674static inline void
5675put_client(struct nfs4_client *clp)
5676{
5677 atomic_dec(&clp->cl_refcount);
5678}
5679
Jeff Layton285abde2014-07-30 08:27:24 -04005680static struct nfs4_client *
5681nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5682{
5683 struct nfs4_client *clp;
5684 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5685 nfsd_net_id);
5686
5687 if (!nfsd_netns_ready(nn))
5688 return NULL;
5689
5690 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5691 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5692 return clp;
5693 }
5694 return NULL;
5695}
5696
Jeff Layton7ec0e362014-07-30 08:27:17 -04005697u64
Jeff Layton285abde2014-07-30 08:27:24 -04005698nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04005699{
5700 struct nfs4_client *clp;
5701 u64 count = 0;
5702 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5703 nfsd_net_id);
5704 char buf[INET6_ADDRSTRLEN];
5705
5706 if (!nfsd_netns_ready(nn))
5707 return 0;
5708
5709 spin_lock(&nn->client_lock);
5710 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5711 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5712 pr_info("NFS Client: %s\n", buf);
5713 ++count;
5714 }
5715 spin_unlock(&nn->client_lock);
5716
5717 return count;
5718}
Bryan Schumaker65178db2011-11-01 13:35:21 -04005719
Jeff Laytona0926d12014-07-30 08:27:18 -04005720u64
Jeff Layton285abde2014-07-30 08:27:24 -04005721nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04005722{
5723 u64 count = 0;
5724 struct nfs4_client *clp;
5725 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5726 nfsd_net_id);
5727
5728 if (!nfsd_netns_ready(nn))
5729 return count;
5730
5731 spin_lock(&nn->client_lock);
5732 clp = nfsd_find_client(addr, addr_size);
5733 if (clp) {
5734 if (mark_client_expired_locked(clp) == nfs_ok)
5735 ++count;
5736 else
5737 clp = NULL;
5738 }
5739 spin_unlock(&nn->client_lock);
5740
5741 if (clp)
5742 expire_client(clp);
5743
5744 return count;
5745}
5746
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005747u64
Jeff Layton285abde2014-07-30 08:27:24 -04005748nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04005749{
5750 u64 count = 0;
5751 struct nfs4_client *clp, *next;
5752 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5753 nfsd_net_id);
5754 LIST_HEAD(reaplist);
5755
5756 if (!nfsd_netns_ready(nn))
5757 return count;
5758
5759 spin_lock(&nn->client_lock);
5760 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5761 if (mark_client_expired_locked(clp) == nfs_ok) {
5762 list_add(&clp->cl_lru, &reaplist);
5763 if (max != 0 && ++count >= max)
5764 break;
5765 }
5766 }
5767 spin_unlock(&nn->client_lock);
5768
5769 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5770 expire_client(clp);
5771
5772 return count;
5773}
5774
Bryan Schumaker184c1842012-11-29 11:40:44 -05005775static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5776 const char *type)
5777{
5778 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05005779 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05005780 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5781}
5782
Jeff Layton016200c2014-07-30 08:27:21 -04005783static void
5784nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5785 struct list_head *collect)
5786{
5787 struct nfs4_client *clp = lst->st_stid.sc_client;
5788 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5789 nfsd_net_id);
5790
5791 if (!collect)
5792 return;
5793
5794 lockdep_assert_held(&nn->client_lock);
5795 atomic_inc(&clp->cl_refcount);
5796 list_add(&lst->st_locks, collect);
5797}
5798
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005799static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04005800 struct list_head *collect,
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005801 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05005802{
5803 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005804 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005805 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005806 u64 count = 0;
5807
Jeff Layton016200c2014-07-30 08:27:21 -04005808 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005809 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005810 list_for_each_entry_safe(stp, st_next,
5811 &oop->oo_owner.so_stateids, st_perstateowner) {
5812 list_for_each_entry_safe(lst, lst_next,
5813 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04005814 if (func) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005815 func(lst);
Jeff Layton016200c2014-07-30 08:27:21 -04005816 nfsd_inject_add_lock_to_list(lst,
5817 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04005818 }
Jeff Layton016200c2014-07-30 08:27:21 -04005819 ++count;
5820 /*
5821 * Despite the fact that these functions deal
5822 * with 64-bit integers for "count", we must
5823 * ensure that it doesn't blow up the
5824 * clp->cl_refcount. Throw a warning if we
5825 * start to approach INT_MAX here.
5826 */
5827 WARN_ON_ONCE(count == (INT_MAX / 2));
5828 if (count == max)
5829 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05005830 }
5831 }
5832 }
Jeff Layton016200c2014-07-30 08:27:21 -04005833out:
5834 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005835
5836 return count;
5837}
5838
Jeff Layton016200c2014-07-30 08:27:21 -04005839static u64
5840nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5841 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05005842{
Jeff Layton016200c2014-07-30 08:27:21 -04005843 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05005844}
5845
Jeff Layton016200c2014-07-30 08:27:21 -04005846static u64
5847nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005848{
Jeff Layton016200c2014-07-30 08:27:21 -04005849 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005850 nfsd_print_count(clp, count, "locked files");
5851 return count;
5852}
5853
Jeff Layton016200c2014-07-30 08:27:21 -04005854u64
Jeff Layton285abde2014-07-30 08:27:24 -04005855nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04005856{
5857 struct nfs4_client *clp;
5858 u64 count = 0;
5859 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5860 nfsd_net_id);
5861
5862 if (!nfsd_netns_ready(nn))
5863 return 0;
5864
5865 spin_lock(&nn->client_lock);
5866 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5867 count += nfsd_print_client_locks(clp);
5868 spin_unlock(&nn->client_lock);
5869
5870 return count;
5871}
5872
5873static void
5874nfsd_reap_locks(struct list_head *reaplist)
5875{
5876 struct nfs4_client *clp;
5877 struct nfs4_ol_stateid *stp, *next;
5878
5879 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5880 list_del_init(&stp->st_locks);
5881 clp = stp->st_stid.sc_client;
5882 nfs4_put_stid(&stp->st_stid);
5883 put_client(clp);
5884 }
5885}
5886
5887u64
Jeff Layton285abde2014-07-30 08:27:24 -04005888nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04005889{
5890 unsigned int count = 0;
5891 struct nfs4_client *clp;
5892 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5893 nfsd_net_id);
5894 LIST_HEAD(reaplist);
5895
5896 if (!nfsd_netns_ready(nn))
5897 return count;
5898
5899 spin_lock(&nn->client_lock);
5900 clp = nfsd_find_client(addr, addr_size);
5901 if (clp)
5902 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5903 spin_unlock(&nn->client_lock);
5904 nfsd_reap_locks(&reaplist);
5905 return count;
5906}
5907
5908u64
Jeff Layton285abde2014-07-30 08:27:24 -04005909nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04005910{
5911 u64 count = 0;
5912 struct nfs4_client *clp;
5913 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5914 nfsd_net_id);
5915 LIST_HEAD(reaplist);
5916
5917 if (!nfsd_netns_ready(nn))
5918 return count;
5919
5920 spin_lock(&nn->client_lock);
5921 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5922 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5923 if (max != 0 && count >= max)
5924 break;
5925 }
5926 spin_unlock(&nn->client_lock);
5927 nfsd_reap_locks(&reaplist);
5928 return count;
5929}
5930
Jeff Layton82e05ef2014-07-30 08:27:22 -04005931static u64
5932nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5933 struct list_head *collect,
5934 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005935{
5936 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04005937 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5938 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005939 u64 count = 0;
5940
Jeff Layton82e05ef2014-07-30 08:27:22 -04005941 lockdep_assert_held(&nn->client_lock);
5942
5943 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005944 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04005945 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005946 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04005947 if (collect) {
5948 atomic_inc(&clp->cl_refcount);
5949 list_add(&oop->oo_perclient, collect);
5950 }
5951 }
5952 ++count;
5953 /*
5954 * Despite the fact that these functions deal with
5955 * 64-bit integers for "count", we must ensure that
5956 * it doesn't blow up the clp->cl_refcount. Throw a
5957 * warning if we start to approach INT_MAX here.
5958 */
5959 WARN_ON_ONCE(count == (INT_MAX / 2));
5960 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005961 break;
5962 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04005963 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005964
5965 return count;
5966}
5967
Jeff Layton82e05ef2014-07-30 08:27:22 -04005968static u64
5969nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005970{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005971 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5972
5973 nfsd_print_count(clp, count, "openowners");
5974 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005975}
5976
Jeff Layton82e05ef2014-07-30 08:27:22 -04005977static u64
5978nfsd_collect_client_openowners(struct nfs4_client *clp,
5979 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05005980{
Jeff Layton82e05ef2014-07-30 08:27:22 -04005981 return nfsd_foreach_client_openowner(clp, max, collect,
5982 unhash_openowner_locked);
5983}
5984
5985u64
Jeff Layton285abde2014-07-30 08:27:24 -04005986nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04005987{
5988 struct nfs4_client *clp;
5989 u64 count = 0;
5990 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5991 nfsd_net_id);
5992
5993 if (!nfsd_netns_ready(nn))
5994 return 0;
5995
5996 spin_lock(&nn->client_lock);
5997 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5998 count += nfsd_print_client_openowners(clp);
5999 spin_unlock(&nn->client_lock);
6000
6001 return count;
6002}
6003
6004static void
6005nfsd_reap_openowners(struct list_head *reaplist)
6006{
6007 struct nfs4_client *clp;
6008 struct nfs4_openowner *oop, *next;
6009
6010 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6011 list_del_init(&oop->oo_perclient);
6012 clp = oop->oo_owner.so_client;
6013 release_openowner(oop);
6014 put_client(clp);
6015 }
6016}
6017
6018u64
Jeff Layton285abde2014-07-30 08:27:24 -04006019nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6020 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006021{
6022 unsigned int count = 0;
6023 struct nfs4_client *clp;
6024 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6025 nfsd_net_id);
6026 LIST_HEAD(reaplist);
6027
6028 if (!nfsd_netns_ready(nn))
6029 return count;
6030
6031 spin_lock(&nn->client_lock);
6032 clp = nfsd_find_client(addr, addr_size);
6033 if (clp)
6034 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6035 spin_unlock(&nn->client_lock);
6036 nfsd_reap_openowners(&reaplist);
6037 return count;
6038}
6039
6040u64
Jeff Layton285abde2014-07-30 08:27:24 -04006041nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006042{
6043 u64 count = 0;
6044 struct nfs4_client *clp;
6045 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6046 nfsd_net_id);
6047 LIST_HEAD(reaplist);
6048
6049 if (!nfsd_netns_ready(nn))
6050 return count;
6051
6052 spin_lock(&nn->client_lock);
6053 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6054 count += nfsd_collect_client_openowners(clp, &reaplist,
6055 max - count);
6056 if (max != 0 && count >= max)
6057 break;
6058 }
6059 spin_unlock(&nn->client_lock);
6060 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006061 return count;
6062}
6063
Bryan Schumaker269de302012-11-29 11:40:42 -05006064static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6065 struct list_head *victims)
6066{
6067 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006068 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6069 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006070 u64 count = 0;
6071
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006072 lockdep_assert_held(&nn->client_lock);
6073
6074 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006075 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006076 if (victims) {
6077 /*
6078 * It's not safe to mess with delegations that have a
6079 * non-zero dl_time. They might have already been broken
6080 * and could be processed by the laundromat outside of
6081 * the state_lock. Just leave them be.
6082 */
6083 if (dp->dl_time != 0)
6084 continue;
6085
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006086 atomic_inc(&clp->cl_refcount);
Jeff Layton42690672014-07-25 07:34:20 -04006087 unhash_delegation_locked(dp);
6088 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006089 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006090 ++count;
6091 /*
6092 * Despite the fact that these functions deal with
6093 * 64-bit integers for "count", we must ensure that
6094 * it doesn't blow up the clp->cl_refcount. Throw a
6095 * warning if we start to approach INT_MAX here.
6096 */
6097 WARN_ON_ONCE(count == (INT_MAX / 2));
6098 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006099 break;
6100 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006101 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006102 return count;
6103}
6104
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006105static u64
6106nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006107{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006108 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006109
6110 nfsd_print_count(clp, count, "delegations");
6111 return count;
6112}
6113
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006114u64
Jeff Layton285abde2014-07-30 08:27:24 -04006115nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006116{
6117 struct nfs4_client *clp;
6118 u64 count = 0;
6119 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6120 nfsd_net_id);
6121
6122 if (!nfsd_netns_ready(nn))
6123 return 0;
6124
6125 spin_lock(&nn->client_lock);
6126 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6127 count += nfsd_print_client_delegations(clp);
6128 spin_unlock(&nn->client_lock);
6129
6130 return count;
6131}
6132
6133static void
6134nfsd_forget_delegations(struct list_head *reaplist)
6135{
6136 struct nfs4_client *clp;
6137 struct nfs4_delegation *dp, *next;
6138
6139 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6140 list_del_init(&dp->dl_recall_lru);
6141 clp = dp->dl_stid.sc_client;
6142 revoke_delegation(dp);
6143 put_client(clp);
6144 }
6145}
6146
6147u64
Jeff Layton285abde2014-07-30 08:27:24 -04006148nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6149 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006150{
6151 u64 count = 0;
6152 struct nfs4_client *clp;
6153 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6154 nfsd_net_id);
6155 LIST_HEAD(reaplist);
6156
6157 if (!nfsd_netns_ready(nn))
6158 return count;
6159
6160 spin_lock(&nn->client_lock);
6161 clp = nfsd_find_client(addr, addr_size);
6162 if (clp)
6163 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6164 spin_unlock(&nn->client_lock);
6165
6166 nfsd_forget_delegations(&reaplist);
6167 return count;
6168}
6169
6170u64
Jeff Layton285abde2014-07-30 08:27:24 -04006171nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006172{
6173 u64 count = 0;
6174 struct nfs4_client *clp;
6175 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6176 nfsd_net_id);
6177 LIST_HEAD(reaplist);
6178
6179 if (!nfsd_netns_ready(nn))
6180 return count;
6181
6182 spin_lock(&nn->client_lock);
6183 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6184 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6185 if (max != 0 && count >= max)
6186 break;
6187 }
6188 spin_unlock(&nn->client_lock);
6189 nfsd_forget_delegations(&reaplist);
6190 return count;
6191}
6192
6193static void
6194nfsd_recall_delegations(struct list_head *reaplist)
6195{
6196 struct nfs4_client *clp;
6197 struct nfs4_delegation *dp, *next;
6198
6199 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6200 list_del_init(&dp->dl_recall_lru);
6201 clp = dp->dl_stid.sc_client;
6202 /*
6203 * We skipped all entries that had a zero dl_time before,
6204 * so we can now reset the dl_time back to 0. If a delegation
6205 * break comes in now, then it won't make any difference since
6206 * we're recalling it either way.
6207 */
6208 spin_lock(&state_lock);
6209 dp->dl_time = 0;
6210 spin_unlock(&state_lock);
6211 nfsd_break_one_deleg(dp);
6212 put_client(clp);
6213 }
6214}
6215
6216u64
Jeff Layton285abde2014-07-30 08:27:24 -04006217nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006218 size_t addr_size)
6219{
6220 u64 count = 0;
6221 struct nfs4_client *clp;
6222 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6223 nfsd_net_id);
6224 LIST_HEAD(reaplist);
6225
6226 if (!nfsd_netns_ready(nn))
6227 return count;
6228
6229 spin_lock(&nn->client_lock);
6230 clp = nfsd_find_client(addr, addr_size);
6231 if (clp)
6232 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6233 spin_unlock(&nn->client_lock);
6234
6235 nfsd_recall_delegations(&reaplist);
6236 return count;
6237}
6238
6239u64
Jeff Layton285abde2014-07-30 08:27:24 -04006240nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006241{
6242 u64 count = 0;
6243 struct nfs4_client *clp, *next;
6244 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6245 nfsd_net_id);
6246 LIST_HEAD(reaplist);
6247
6248 if (!nfsd_netns_ready(nn))
6249 return count;
6250
6251 spin_lock(&nn->client_lock);
6252 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6253 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6254 if (max != 0 && ++count >= max)
6255 break;
6256 }
6257 spin_unlock(&nn->client_lock);
6258 nfsd_recall_delegations(&reaplist);
6259 return count;
6260}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006261#endif /* CONFIG_NFSD_FAULT_INJECTION */
6262
Meelap Shahc2f1a552007-07-17 04:04:39 -07006263/*
6264 * Since the lifetime of a delegation isn't limited to that of an open, a
6265 * client may quite reasonably hang on to a delegation as long as it has
6266 * the inode cached. This becomes an obvious problem the first time a
6267 * client's inode cache approaches the size of the server's total memory.
6268 *
6269 * For now we avoid this problem by imposing a hard limit on the number
6270 * of delegations, which varies according to the server's memory size.
6271 */
6272static void
6273set_max_delegations(void)
6274{
6275 /*
6276 * Allow at most 4 delegations per megabyte of RAM. Quick
6277 * estimates suggest that in the worst case (where every delegation
6278 * is for a different inode), a delegation could take about 1.5K,
6279 * giving a worst case usage of about 6% of memory.
6280 */
6281 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6282}
6283
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006284static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006285{
6286 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6287 int i;
6288
6289 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6290 CLIENT_HASH_SIZE, GFP_KERNEL);
6291 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006292 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006293 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6294 CLIENT_HASH_SIZE, GFP_KERNEL);
6295 if (!nn->unconf_id_hashtbl)
6296 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006297 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6298 SESSION_HASH_SIZE, GFP_KERNEL);
6299 if (!nn->sessionid_hashtbl)
6300 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006301
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006302 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006303 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006304 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006305 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006306 for (i = 0; i < SESSION_HASH_SIZE; i++)
6307 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006308 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006309 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006310 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006311 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006312 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006313 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006314
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006315 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006316 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006317
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006318 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006319
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006320err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006321 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006322err_unconf_id:
6323 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006324err:
6325 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006326}
6327
6328static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006329nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006330{
6331 int i;
6332 struct nfs4_client *clp = NULL;
6333 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6334
6335 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6336 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6337 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6338 destroy_client(clp);
6339 }
6340 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006341
Kinglong Mee2b905632014-03-26 22:09:30 +08006342 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6343 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6344 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6345 destroy_client(clp);
6346 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006347 }
6348
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006349 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006350 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006351 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006352 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006353}
6354
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006355int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006356nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006357{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006358 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006359 int ret;
6360
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006361 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006362 if (ret)
6363 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006364 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006365 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006366 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006367 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006368 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006369 nn->nfsd4_grace, net);
6370 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006371 return 0;
6372}
6373
6374/* initialization to perform when the nfsd service is started: */
6375
6376int
6377nfs4_state_start(void)
6378{
6379 int ret;
6380
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006381 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006382 if (ret)
6383 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07006384 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006385 if (laundry_wq == NULL) {
6386 ret = -ENOMEM;
6387 goto out_recovery;
6388 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006389 ret = nfsd4_create_callback_queue();
6390 if (ret)
6391 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006392
Meelap Shahc2f1a552007-07-17 04:04:39 -07006393 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006394
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006395 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006396
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006397out_free_laundry:
6398 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006399out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401}
6402
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006403void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006404nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006408 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006410 cancel_delayed_work_sync(&nn->laundromat_work);
6411 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05006412
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04006414 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006415 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006417 unhash_delegation_locked(dp);
6418 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 }
Benny Halevycdc97502014-05-30 09:09:30 -04006420 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 list_for_each_safe(pos, next, &reaplist) {
6422 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04006423 list_del_init(&dp->dl_recall_lru);
Jeff Laytonafbda402014-08-09 10:22:41 -04006424 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04006425 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 }
6427
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006428 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006429 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430}
6431
6432void
6433nfs4_state_shutdown(void)
6434{
NeilBrown5e8d5c22006-04-10 22:55:37 -07006435 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04006436 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006438
6439static void
6440get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6441{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006442 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6443 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006444}
6445
6446static void
6447put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6448{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01006449 if (cstate->minorversion) {
6450 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6451 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6452 }
6453}
6454
6455void
6456clear_current_stateid(struct nfsd4_compound_state *cstate)
6457{
6458 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006459}
6460
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006461/*
6462 * functions to set current state id
6463 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006464void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006465nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6466{
6467 put_stateid(cstate, &odp->od_stateid);
6468}
6469
6470void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006471nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6472{
6473 put_stateid(cstate, &open->op_stateid);
6474}
6475
6476void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006477nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6478{
6479 put_stateid(cstate, &close->cl_stateid);
6480}
6481
6482void
6483nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6484{
6485 put_stateid(cstate, &lock->lk_resp_stateid);
6486}
6487
6488/*
6489 * functions to consume current state id
6490 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006491
6492void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01006493nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6494{
6495 get_stateid(cstate, &odp->od_stateid);
6496}
6497
6498void
6499nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6500{
6501 get_stateid(cstate, &drp->dr_stateid);
6502}
6503
6504void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01006505nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6506{
6507 get_stateid(cstate, &fsp->fr_stateid);
6508}
6509
6510void
6511nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6512{
6513 get_stateid(cstate, &setattr->sa_stateid);
6514}
6515
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006516void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006517nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6518{
6519 get_stateid(cstate, &close->cl_stateid);
6520}
6521
6522void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006523nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006524{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01006525 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01006526}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01006527
6528void
6529nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6530{
6531 get_stateid(cstate, &read->rd_stateid);
6532}
6533
6534void
6535nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6536{
6537 get_stateid(cstate, &write->wr_stateid);
6538}