blob: a7954913b33258686011c0d26f9c5fe7f1bd1cfe [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>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020044#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040045#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050046#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040047#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040049#include "netns.h"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define NFSDDBG_FACILITY NFSDDBG_PROC
52
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050053#define all_ones {{~0,~0},~0}
54static const stateid_t one_stateid = {
55 .si_generation = ~0,
56 .si_opaque = all_ones,
57};
58static const stateid_t zero_stateid = {
59 /* all fields zero */
60};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010061static const stateid_t currentstateid = {
62 .si_generation = 1,
63};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050064
Andy Adamsonec6b5d72009-04-03 08:28:28 +030065static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070066
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050067#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
68#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010069#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040072static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
J. Bruce Fields8b671b82009-02-22 14:51:34 -080074/* Locking: */
75
76/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080077static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
J. Bruce Fields8b671b82009-02-22 14:51:34 -080079/*
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
83 */
84static DEFINE_SPINLOCK(recall_lock);
85
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040086static struct kmem_cache *openowner_slab = NULL;
87static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080088static struct kmem_cache *file_slab = NULL;
89static struct kmem_cache *stateid_slab = NULL;
90static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092void
93nfs4_lock_state(void)
94{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080095 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040098static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080099
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400100void nfsd4_put_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800101{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400102 atomic_dec(&ses->se_ref);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800103}
104
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400105static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800106{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400107 return ses->se_flags & NFS4_SESSION_DEAD;
108}
109
110static __be32 mark_session_dead_locked(struct nfsd4_session *ses)
111{
112 if (atomic_read(&ses->se_ref))
113 return nfserr_jukebox;
114 ses->se_flags |= NFS4_SESSION_DEAD;
115 return nfs_ok;
116}
117
118static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
119{
120 if (is_session_dead(ses))
121 return nfserr_badsession;
122 atomic_inc(&ses->se_ref);
123 return nfs_ok;
Benny Halevy508dc6e2012-02-23 17:40:52 -0800124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126void
127nfs4_unlock_state(void)
128{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800129 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
J. Bruce Fields221a6872013-04-01 22:23:49 -0400132static bool is_client_expired(struct nfs4_client *clp)
133{
134 return clp->cl_time == 0;
135}
136
137static __be32 mark_client_expired_locked(struct nfs4_client *clp)
138{
139 if (atomic_read(&clp->cl_refcount))
140 return nfserr_jukebox;
141 clp->cl_time = 0;
142 return nfs_ok;
143}
144
145static __be32 mark_client_expired(struct nfs4_client *clp)
146{
147 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
148 __be32 ret;
149
150 spin_lock(&nn->client_lock);
151 ret = mark_client_expired_locked(clp);
152 spin_unlock(&nn->client_lock);
153 return ret;
154}
155
156static __be32 get_client_locked(struct nfs4_client *clp)
157{
158 if (is_client_expired(clp))
159 return nfserr_expired;
160 atomic_inc(&clp->cl_refcount);
161 return nfs_ok;
162}
163
164/* must be called under the client_lock */
165static inline void
166renew_client_locked(struct nfs4_client *clp)
167{
168 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
169
170 if (is_client_expired(clp)) {
171 WARN_ON(1);
172 printk("%s: client (clientid %08x/%08x) already expired\n",
173 __func__,
174 clp->cl_clientid.cl_boot,
175 clp->cl_clientid.cl_id);
176 return;
177 }
178
179 dprintk("renewing client (clientid %08x/%08x)\n",
180 clp->cl_clientid.cl_boot,
181 clp->cl_clientid.cl_id);
182 list_move_tail(&clp->cl_lru, &nn->client_lru);
183 clp->cl_time = get_seconds();
184}
185
186static inline void
187renew_client(struct nfs4_client *clp)
188{
189 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
190
191 spin_lock(&nn->client_lock);
192 renew_client_locked(clp);
193 spin_unlock(&nn->client_lock);
194}
195
196void put_client_renew_locked(struct nfs4_client *clp)
197{
198 if (!atomic_dec_and_test(&clp->cl_refcount))
199 return;
200 if (!is_client_expired(clp))
201 renew_client_locked(clp);
202}
203
204void put_client_renew(struct nfs4_client *clp)
205{
206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207
208 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
209 return;
210 if (!is_client_expired(clp))
211 renew_client_locked(clp);
212 spin_unlock(&nn->client_lock);
213}
214
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static inline u32
217opaque_hashval(const void *ptr, int nbytes)
218{
219 unsigned char *cptr = (unsigned char *) ptr;
220
221 u32 x = 0;
222 while (nbytes--) {
223 x *= 37;
224 x += *cptr++;
225 }
226 return x;
227}
228
J. Bruce Fields32513b42011-10-13 16:00:16 -0400229static void nfsd4_free_file(struct nfs4_file *f)
230{
231 kmem_cache_free(file_slab, f);
232}
233
NeilBrown13cd2182005-06-23 22:03:10 -0700234static inline void
235put_nfs4_file(struct nfs4_file *fi)
236{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800237 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400238 hlist_del(&fi->fi_hash);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800239 spin_unlock(&recall_lock);
240 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400241 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800242 }
NeilBrown13cd2182005-06-23 22:03:10 -0700243}
244
245static inline void
246get_nfs4_file(struct nfs4_file *fi)
247{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800248 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700249}
250
NeilBrownef0f3392006-04-10 22:55:41 -0700251static int num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800252unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700253
254/*
255 * Open owner state (share locks)
256 */
257
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500258/* hash tables for lock and open owners */
259#define OWNER_HASH_BITS 8
260#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
261#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700262
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500263static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400264{
265 unsigned int ret;
266
267 ret = opaque_hashval(ownername->data, ownername->len);
268 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500269 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400270}
NeilBrownef0f3392006-04-10 22:55:41 -0700271
NeilBrownef0f3392006-04-10 22:55:41 -0700272/* hash table for nfs4_file */
273#define FILE_HASH_BITS 8
274#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800275
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400276static unsigned int file_hashval(struct inode *ino)
277{
278 /* XXX: why are we hashing on inode pointer, anyway? */
279 return hash_ptr(ino, FILE_HASH_BITS);
280}
281
Jeff Layton89876f82013-04-02 09:01:59 -0400282static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700283
J. Bruce Fields998db522010-08-07 09:21:41 -0400284static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400285{
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500286 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400287 atomic_inc(&fp->fi_access[oflag]);
288}
289
J. Bruce Fields998db522010-08-07 09:21:41 -0400290static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
291{
292 if (oflag == O_RDWR) {
293 __nfs4_file_get_access(fp, O_RDONLY);
294 __nfs4_file_get_access(fp, O_WRONLY);
295 } else
296 __nfs4_file_get_access(fp, oflag);
297}
298
299static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400300{
301 if (fp->fi_fds[oflag]) {
302 fput(fp->fi_fds[oflag]);
303 fp->fi_fds[oflag] = NULL;
304 }
305}
306
J. Bruce Fields998db522010-08-07 09:21:41 -0400307static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400308{
309 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400310 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400311 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400312 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400313 }
314}
315
J. Bruce Fields998db522010-08-07 09:21:41 -0400316static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
317{
318 if (oflag == O_RDWR) {
319 __nfs4_file_put_access(fp, O_RDONLY);
320 __nfs4_file_put_access(fp, O_WRONLY);
321 } else
322 __nfs4_file_put_access(fp, oflag);
323}
324
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500325static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
326kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400327{
328 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500329 static int min_stateid = 0;
330 struct nfs4_stid *stid;
331 int new_id;
332
333 stid = kmem_cache_alloc(slab, GFP_KERNEL);
334 if (!stid)
335 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400336
Tejun Heoebd6c702013-03-13 14:59:37 -0700337 new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
338 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500339 goto out_free;
340 stid->sc_client = cl;
341 stid->sc_type = 0;
342 stid->sc_stateid.si_opaque.so_id = new_id;
343 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
344 /* Will be incremented before return to client: */
345 stid->sc_stateid.si_generation = 0;
346
J. Bruce Fields996e0932011-10-17 11:14:48 -0400347 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500348 * It shouldn't be a problem to reuse an opaque stateid value.
349 * I don't think it is for 4.1. But with 4.0 I worry that, for
350 * example, a stray write retransmission could be accepted by
351 * the server when it should have been rejected. Therefore,
352 * adopt a trick from the sctp code to attempt to maximize the
353 * amount of time until an id is reused, by ensuring they always
354 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400355 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500356
357 min_stateid = new_id+1;
358 if (min_stateid == INT_MAX)
359 min_stateid = 0;
360 return stid;
361out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800362 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500363 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400364}
365
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400366static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
367{
368 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400372alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct nfs4_delegation *dp;
375 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400378 /*
379 * Major work on the lease subsystem (for example, to support
380 * calbacks on stat) will be required before we can support
381 * write delegations properly.
382 */
383 if (type != NFS4_OPEN_DELEGATE_READ)
384 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700385 if (fp->fi_had_conflict)
386 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700387 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700388 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400389 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700390 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return dp;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500392 dp->dl_stid.sc_type = NFS4_DELEG_STID;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400393 /*
394 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400395 * meaning of seqid 0 isn't meaningful, really, but let's avoid
396 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400397 */
398 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700399 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700400 INIT_LIST_HEAD(&dp->dl_perfile);
401 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 INIT_LIST_HEAD(&dp->dl_recall_lru);
NeilBrown13cd2182005-06-23 22:03:10 -0700403 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 dp->dl_type = type;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500406 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dp->dl_time = 0;
408 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500409 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return dp;
411}
412
J. Bruce Fields68a33962013-03-21 11:21:50 -0400413static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500414{
415 struct idr *stateids = &s->sc_client->cl_stateids;
416
417 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420void
421nfs4_put_delegation(struct nfs4_delegation *dp)
422{
423 if (atomic_dec_and_test(&dp->dl_count)) {
J. Bruce Fields68a33962013-03-21 11:21:50 -0400424 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700425 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427}
428
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500429static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500431 if (atomic_dec_and_test(&fp->fi_delegees)) {
432 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
433 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400434 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500435 fp->fi_deleg_file = NULL;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400439static void unhash_stid(struct nfs4_stid *s)
440{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500441 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/* Called under the state lock. */
445static void
446unhash_delegation(struct nfs4_delegation *dp)
447{
NeilBrownea1da632005-06-23 22:04:17 -0700448 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500450 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 list_del_init(&dp->dl_recall_lru);
452 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500453 nfs4_put_deleg_lease(dp->dl_file);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400454 put_nfs4_file(dp->dl_file);
455 dp->dl_file = NULL;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400456}
457
458
459
460static void destroy_revoked_delegation(struct nfs4_delegation *dp)
461{
462 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400463 remove_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 nfs4_put_delegation(dp);
465}
466
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400467static void destroy_delegation(struct nfs4_delegation *dp)
468{
469 unhash_delegation(dp);
470 remove_stid(&dp->dl_stid);
471 nfs4_put_delegation(dp);
472}
473
474static void revoke_delegation(struct nfs4_delegation *dp)
475{
476 struct nfs4_client *clp = dp->dl_stid.sc_client;
477
478 if (clp->cl_minorversion == 0)
479 destroy_delegation(dp);
480 else {
481 unhash_delegation(dp);
482 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
483 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
484 }
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * SETCLIENTID state
489 */
490
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400491static unsigned int clientid_hashval(u32 id)
492{
493 return id & CLIENT_HASH_MASK;
494}
495
496static unsigned int clientstr_hashval(const char *name)
497{
498 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400502 * We store the NONE, READ, WRITE, and BOTH bits separately in the
503 * st_{access,deny}_bmap field of the stateid, in order to track not
504 * only what share bits are currently in force, but also what
505 * combinations of share bits previous opens have used. This allows us
506 * to enforce the recommendation of rfc 3530 14.2.19 that the server
507 * return an error if the client attempt to downgrade to a combination
508 * of share bits not explicable by closing some of its previous opens.
509 *
510 * XXX: This enforcement is actually incomplete, since we don't keep
511 * track of access/deny bit combinations; so, e.g., we allow:
512 *
513 * OPEN allow read, deny write
514 * OPEN allow both, deny none
515 * DOWNGRADE allow read, deny none
516 *
517 * which we should reject.
518 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400519static unsigned int
520bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400521 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400522 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400523
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400524 for (i = 1; i < 4; i++) {
525 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400526 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400527 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400528 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400529}
530
Jeff Layton3a328612012-05-11 09:45:12 -0400531static bool
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400532test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400533 unsigned int access, deny;
534
Jeff Layton5ae037e2012-05-11 09:45:11 -0400535 access = bmap_to_share_mode(stp->st_access_bmap);
536 deny = bmap_to_share_mode(stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400537 if ((access & open->op_share_deny) || (deny & open->op_share_access))
Jeff Layton3a328612012-05-11 09:45:12 -0400538 return false;
539 return true;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400540}
541
Jeff Layton82c5ff12012-05-11 09:45:13 -0400542/* set share access for a given stateid */
543static inline void
544set_access(u32 access, struct nfs4_ol_stateid *stp)
545{
546 __set_bit(access, &stp->st_access_bmap);
547}
548
549/* clear share access for a given stateid */
550static inline void
551clear_access(u32 access, struct nfs4_ol_stateid *stp)
552{
553 __clear_bit(access, &stp->st_access_bmap);
554}
555
556/* test whether a given stateid has access */
557static inline bool
558test_access(u32 access, struct nfs4_ol_stateid *stp)
559{
560 return test_bit(access, &stp->st_access_bmap);
561}
562
Jeff Laytonce0fc432012-05-11 09:45:14 -0400563/* set share deny for a given stateid */
564static inline void
565set_deny(u32 access, struct nfs4_ol_stateid *stp)
566{
567 __set_bit(access, &stp->st_deny_bmap);
568}
569
570/* clear share deny for a given stateid */
571static inline void
572clear_deny(u32 access, struct nfs4_ol_stateid *stp)
573{
574 __clear_bit(access, &stp->st_deny_bmap);
575}
576
577/* test whether a given stateid is denying specific access */
578static inline bool
579test_deny(u32 access, struct nfs4_ol_stateid *stp)
580{
581 return test_bit(access, &stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400582}
583
584static int nfs4_access_to_omode(u32 access)
585{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400586 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400587 case NFS4_SHARE_ACCESS_READ:
588 return O_RDONLY;
589 case NFS4_SHARE_ACCESS_WRITE:
590 return O_WRONLY;
591 case NFS4_SHARE_ACCESS_BOTH:
592 return O_RDWR;
593 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500594 WARN_ON_ONCE(1);
595 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400596}
597
Jeff Layton82c5ff12012-05-11 09:45:13 -0400598/* release all access and file references for a given stateid */
599static void
600release_all_access(struct nfs4_ol_stateid *stp)
601{
602 int i;
603
604 for (i = 1; i < 4; i++) {
605 if (test_access(i, stp))
606 nfs4_file_put_access(stp->st_file,
607 nfs4_access_to_omode(i));
608 clear_access(i, stp);
609 }
610}
611
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400612static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500613{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500614 list_del(&stp->st_perfile);
615 list_del(&stp->st_perstateowner);
616}
617
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400618static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500619{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400620 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400621 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400622 stp->st_file = NULL;
623}
624
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400625static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400626{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400627 remove_stid(&stp->st_stid);
628 kmem_cache_free(stateid_slab, stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500629}
630
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400631static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500632{
633 struct file *file;
634
635 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400636 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500637 file = find_any_file(stp->st_file);
638 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400639 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400640 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500641 free_generic_stateid(stp);
642}
643
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400644static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500645{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400646 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500647
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400648 list_del(&lo->lo_owner.so_strhash);
649 list_del(&lo->lo_perstateid);
J. Bruce Fields009673b2011-11-07 17:40:10 -0500650 list_del(&lo->lo_owner_ino_hash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400651 while (!list_empty(&lo->lo_owner.so_stateids)) {
652 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400653 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500654 release_lock_stateid(stp);
655 }
656}
657
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400658static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500659{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400660 unhash_lockowner(lo);
661 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500662}
663
664static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400665release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500666{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400667 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500668
669 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400670 lo = list_entry(open_stp->st_lockowners.next,
671 struct nfs4_lockowner, lo_perstateid);
672 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500673 }
674}
675
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400676static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500677{
678 unhash_generic_stateid(stp);
679 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400680 close_generic_stateid(stp);
681}
682
683static void release_open_stateid(struct nfs4_ol_stateid *stp)
684{
685 unhash_open_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400686 unhash_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500687 free_generic_stateid(stp);
688}
689
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400690static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500691{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400692 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500693
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400694 list_del(&oo->oo_owner.so_strhash);
695 list_del(&oo->oo_perclient);
696 while (!list_empty(&oo->oo_owner.so_stateids)) {
697 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400698 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500699 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500700 }
701}
702
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400703static void release_last_closed_stateid(struct nfs4_openowner *oo)
704{
705 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
706
707 if (s) {
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400708 unhash_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400709 free_generic_stateid(s);
710 oo->oo_last_closed_stid = NULL;
711 }
712}
713
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400714static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500715{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400716 unhash_openowner(oo);
717 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400718 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400719 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500720}
721
Marc Eshel5282fd72009-04-03 08:27:52 +0300722static inline int
723hash_sessionid(struct nfs4_sessionid *sessionid)
724{
725 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
726
727 return sid->sequence % SESSION_HASH_SIZE;
728}
729
Trond Myklebust8f199b82012-03-20 15:11:17 -0400730#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300731static inline void
732dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
733{
734 u32 *ptr = (u32 *)(&sessionid->data[0]);
735 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
736}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400737#else
738static inline void
739dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
740{
741}
742#endif
743
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400744/*
745 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
746 * won't be used for replay.
747 */
748void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
749{
750 struct nfs4_stateowner *so = cstate->replay_owner;
751
752 if (nfserr == nfserr_replay_me)
753 return;
754
755 if (!seqid_mutating_err(ntohl(nfserr))) {
756 cstate->replay_owner = NULL;
757 return;
758 }
759 if (!so)
760 return;
761 if (so->so_is_open_owner)
762 release_last_closed_stateid(openowner(so));
763 so->so_seqid++;
764 return;
765}
Marc Eshel5282fd72009-04-03 08:27:52 +0300766
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300767static void
768gen_sessionid(struct nfsd4_session *ses)
769{
770 struct nfs4_client *clp = ses->se_client;
771 struct nfsd4_sessionid *sid;
772
773 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
774 sid->clientid = clp->cl_clientid;
775 sid->sequence = current_sessionid++;
776 sid->reserved = 0;
777}
778
779/*
Andy Adamsona6496372009-08-28 08:45:01 -0400780 * The protocol defines ca_maxresponssize_cached to include the size of
781 * the rpc header, but all we need to cache is the data starting after
782 * the end of the initial SEQUENCE operation--the rest we regenerate
783 * each time. Therefore we can advertise a ca_maxresponssize_cached
784 * value that is the number of bytes in our cache plus a few additional
785 * bytes. In order to stay on the safe side, and not promise more than
786 * we can cache, those additional bytes must be the minimum possible: 24
787 * bytes of rpc header (xid through accept state, with AUTH_NULL
788 * verifier), 12 for the compound header (with zero-length tag), and 44
789 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300790 */
Andy Adamsona6496372009-08-28 08:45:01 -0400791#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
792
Andy Adamson557ce262009-08-28 08:45:04 -0400793static void
794free_session_slots(struct nfsd4_session *ses)
795{
796 int i;
797
798 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
799 kfree(ses->se_slots[i]);
800}
801
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400802/*
803 * We don't actually need to cache the rpc and session headers, so we
804 * can allocate a little less for each slot:
805 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400806static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400807{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400808 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400809
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400810 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
811 size = 0;
812 else
813 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
814 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400815}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300816
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400817/*
818 * XXX: If we run out of reserved DRC memory we could (up to a point)
819 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800820 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400821 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400822static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400823{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400824 u32 slotsize = slot_bytes(ca);
825 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400826 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300827
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400828 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800829 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
830 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400831 num = min_t(int, num, avail / slotsize);
832 nfsd_drc_mem_used += num * slotsize;
833 spin_unlock(&nfsd_drc_lock);
834
835 return num;
836}
837
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400838static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400839{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400840 int slotsize = slot_bytes(ca);
841
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400842 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400843 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400844 spin_unlock(&nfsd_drc_lock);
845}
846
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400847static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *attrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400848{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400849 int numslots = attrs->maxreqs;
850 int slotsize = slot_bytes(attrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400851 struct nfsd4_session *new;
852 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300853
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400854 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400855 + sizeof(struct nfsd4_session) > PAGE_SIZE);
856 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400857
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400858 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300859 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400860 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400861 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400862 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400863 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400864 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400865 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400866 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400867 return new;
868out_free:
869 while (i--)
870 kfree(new->se_slots[i]);
871 kfree(new);
872 return NULL;
873}
874
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400875static void free_conn(struct nfsd4_conn *c)
876{
877 svc_xprt_put(c->cn_xprt);
878 kfree(c);
879}
880
881static void nfsd4_conn_lost(struct svc_xpt_user *u)
882{
883 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
884 struct nfs4_client *clp = c->cn_session->se_client;
885
886 spin_lock(&clp->cl_lock);
887 if (!list_empty(&c->cn_persession)) {
888 list_del(&c->cn_persession);
889 free_conn(c);
890 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500891 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -0500892 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400893}
894
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400895static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400896{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400897 struct nfsd4_conn *conn;
898
899 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
900 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400901 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400902 svc_xprt_get(rqstp->rq_xprt);
903 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400904 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400905 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
906 return conn;
907}
908
J. Bruce Fields328ead22010-09-29 16:11:06 -0400909static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
910{
911 conn->cn_session = ses;
912 list_add(&conn->cn_persession, &ses->se_conns);
913}
914
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400915static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
916{
917 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400918
919 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400920 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400921 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400922}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400923
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400924static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400925{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400926 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400927 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400928}
929
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400930static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400931{
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400932 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400933
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400934 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400935 ret = nfsd4_register_conn(conn);
936 if (ret)
937 /* oops; xprt is already down: */
938 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -0400939 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -0400940 /* callback channel may be back up */
941 nfsd4_probe_callback(ses->se_client);
942 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400943}
944
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400945static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400946{
947 u32 dir = NFS4_CDFC4_FORE;
948
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400949 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400950 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400951 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400952}
953
954/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400955static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400956{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400957 struct nfs4_client *clp = s->se_client;
958 struct nfsd4_conn *c;
959
960 spin_lock(&clp->cl_lock);
961 while (!list_empty(&s->se_conns)) {
962 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
963 list_del_init(&c->cn_persession);
964 spin_unlock(&clp->cl_lock);
965
966 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
967 free_conn(c);
968
969 spin_lock(&clp->cl_lock);
970 }
971 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400972}
973
J. Bruce Fields1377b692012-09-11 21:42:40 -0400974static void __free_session(struct nfsd4_session *ses)
975{
J. Bruce Fields1377b692012-09-11 21:42:40 -0400976 free_session_slots(ses);
977 kfree(ses);
978}
979
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400980static void free_session(struct nfsd4_session *ses)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400981{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400982 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300983
984 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400985 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400986 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fields1377b692012-09-11 21:42:40 -0400987 __free_session(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400988}
989
Fengguang Wu135ae822012-11-10 07:20:25 -0500990static 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 -0400991{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400992 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +0300993 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400994
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300995 new->se_client = clp;
996 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300997
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400998 INIT_LIST_HEAD(&new->se_conns);
999
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001000 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001001 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001002 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001003 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001004 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001005 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001006 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001007 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001008 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001009 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001010 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001011 spin_unlock(&nn->client_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001012 memcpy(&new->se_fchannel, &cses->fore_channel,
1013 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001014 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001015 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001016 /*
1017 * This is a little silly; with sessions there's no real
1018 * use for the callback address. Use the peer address
1019 * as a reasonable default for now, but consider fixing
1020 * the rpc client not to require an address in the
1021 * future:
1022 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001023 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1024 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001025 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001026}
1027
Benny Halevy9089f1b2010-05-12 00:12:26 +03001028/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001029static struct nfsd4_session *
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001030find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001031{
1032 struct nfsd4_session *elem;
1033 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001034 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001035
1036 dump_sessionid(__func__, sessionid);
1037 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001038 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001039 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001040 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1041 NFS4_MAX_SESSIONID_LEN)) {
1042 return elem;
1043 }
1044 }
1045
1046 dprintk("%s: session not found\n", __func__);
1047 return NULL;
1048}
1049
Benny Halevy9089f1b2010-05-12 00:12:26 +03001050/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001051static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001052unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001053{
1054 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001055 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001056 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001057 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1061static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001062STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001064 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001066 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001067 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return 1;
1069}
1070
1071/*
1072 * XXX Should we use a slab cache ?
1073 * This type of memory management is somewhat inefficient, but we use it
1074 * anyway since SETCLIENTID is not a common operation.
1075 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001076static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 struct nfs4_client *clp;
1079
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001080 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1081 if (clp == NULL)
1082 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001083 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001084 if (clp->cl_name.data == NULL) {
1085 kfree(clp);
1086 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001088 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return clp;
1090}
1091
1092static inline void
1093free_client(struct nfs4_client *clp)
1094{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001095 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001096
1097 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001098 while (!list_empty(&clp->cl_sessions)) {
1099 struct nfsd4_session *ses;
1100 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1101 se_perclnt);
1102 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001103 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1104 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001105 }
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001106 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001108 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 kfree(clp);
1110}
1111
Benny Halevy84d38ac2010-05-12 00:13:16 +03001112/* must be called under the client_lock */
1113static inline void
1114unhash_client_locked(struct nfs4_client *clp)
1115{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001116 struct nfsd4_session *ses;
1117
Benny Halevy84d38ac2010-05-12 00:13:16 +03001118 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001119 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001120 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1121 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001122 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001126destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001128 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001131 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 INIT_LIST_HEAD(&reaplist);
1134 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001135 while (!list_empty(&clp->cl_delegations)) {
1136 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001137 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 list_move(&dp->dl_recall_lru, &reaplist);
1139 }
1140 spin_unlock(&recall_lock);
1141 while (!list_empty(&reaplist)) {
1142 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001143 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
NeilBrownea1da632005-06-23 22:04:17 -07001145 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001146 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1147 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001149 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001150 if (clp->cl_cb_conn.cb_xprt)
1151 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001152 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001153 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001154 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001155 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001156 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001157 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001158 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001159 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1160 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001161 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001164static void expire_client(struct nfs4_client *clp)
1165{
1166 nfsd4_client_record_remove(clp);
1167 destroy_client(clp);
1168}
1169
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001170static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1171{
1172 memcpy(target->cl_verifier.data, source->data,
1173 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001176static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1179 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1180}
1181
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001182static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001183{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001184 if (source->cr_principal) {
1185 target->cr_principal =
1186 kstrdup(source->cr_principal, GFP_KERNEL);
1187 if (target->cr_principal == NULL)
1188 return -ENOMEM;
1189 } else
1190 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001191 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 target->cr_uid = source->cr_uid;
1193 target->cr_gid = source->cr_gid;
1194 target->cr_group_info = source->cr_group_info;
1195 get_group_info(target->cr_group_info);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001199static long long
1200compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1201{
1202 long long res;
1203
1204 res = o1->len - o2->len;
1205 if (res)
1206 return res;
1207 return (long long)memcmp(o1->data, o2->data, o1->len);
1208}
1209
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001210static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001211{
NeilBrowna55370a2005-06-23 22:03:52 -07001212 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001216same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1217{
1218 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001222same_clid(clientid_t *cl1, clientid_t *cl2)
1223{
1224 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001227static bool groups_equal(struct group_info *g1, struct group_info *g2)
1228{
1229 int i;
1230
1231 if (g1->ngroups != g2->ngroups)
1232 return false;
1233 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001234 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001235 return false;
1236 return true;
1237}
1238
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001239/*
1240 * RFC 3530 language requires clid_inuse be returned when the
1241 * "principal" associated with a requests differs from that previously
1242 * used. We use uid, gid's, and gss principal string as our best
1243 * approximation. We also don't want to allow non-gss use of a client
1244 * established using gss: in theory cr_principal should catch that
1245 * change, but in practice cr_principal can be null even in the gss case
1246 * since gssd doesn't always pass down a principal string.
1247 */
1248static bool is_gss_cred(struct svc_cred *cr)
1249{
1250 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1251 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1252}
1253
1254
Vivek Trivedi5559b502012-07-24 21:18:20 +05301255static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001256same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1257{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001258 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001259 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1260 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001261 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1262 return false;
1263 if (cr1->cr_principal == cr2->cr_principal)
1264 return true;
1265 if (!cr1->cr_principal || !cr2->cr_principal)
1266 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301267 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001270static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001271{
1272 static u32 current_clientid = 1;
1273
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001274 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 clp->cl_clientid.cl_id = current_clientid++;
1276}
1277
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001278static void gen_confirm(struct nfs4_client *clp)
1279{
Chuck Leverab4684d2012-03-02 17:13:50 -05001280 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001281 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Chuck Leverab4684d2012-03-02 17:13:50 -05001283 verf[0] = (__be32)get_seconds();
1284 verf[1] = (__be32)i++;
1285 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001288static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001289{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001290 struct nfs4_stid *ret;
1291
1292 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1293 if (!ret || !ret->sc_type)
1294 return NULL;
1295 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001296}
1297
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001298static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001299{
1300 struct nfs4_stid *s;
1301
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001302 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001303 if (!s)
1304 return NULL;
1305 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001306 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001307 return NULL;
1308}
1309
Jeff Layton2216d442012-11-12 15:00:57 -05001310static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001311 struct svc_rqst *rqstp, nfs4_verifier *verf)
1312{
1313 struct nfs4_client *clp;
1314 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001315 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001316 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001317 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001318
1319 clp = alloc_client(name);
1320 if (clp == NULL)
1321 return NULL;
1322
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001323 INIT_LIST_HEAD(&clp->cl_sessions);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001324 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1325 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001326 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001327 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001328 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001329 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001330 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001331 idr_init(&clp->cl_stateids);
Benny Halevy46583e22010-05-12 00:13:29 +03001332 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001333 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001334 INIT_LIST_HEAD(&clp->cl_idhash);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001335 INIT_LIST_HEAD(&clp->cl_openowners);
1336 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001337 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001338 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001339 INIT_LIST_HEAD(&clp->cl_revoked);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001340 spin_lock_init(&clp->cl_lock);
J. Bruce Fields57725152012-11-05 15:10:26 -05001341 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001342 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001343 clear_bit(0, &clp->cl_cb_slot_busy);
1344 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1345 copy_verf(clp, verf);
1346 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001347 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001348 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001349 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001350 return clp;
1351}
1352
NeilBrownfd39ca92005-06-23 22:04:03 -07001353static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001354add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1355{
1356 struct rb_node **new = &(root->rb_node), *parent = NULL;
1357 struct nfs4_client *clp;
1358
1359 while (*new) {
1360 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1361 parent = *new;
1362
1363 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1364 new = &((*new)->rb_left);
1365 else
1366 new = &((*new)->rb_right);
1367 }
1368
1369 rb_link_node(&new_clp->cl_namenode, parent, new);
1370 rb_insert_color(&new_clp->cl_namenode, root);
1371}
1372
1373static struct nfs4_client *
1374find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1375{
1376 long long cmp;
1377 struct rb_node *node = root->rb_node;
1378 struct nfs4_client *clp;
1379
1380 while (node) {
1381 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1382 cmp = compare_blob(&clp->cl_name, name);
1383 if (cmp > 0)
1384 node = node->rb_left;
1385 else if (cmp < 0)
1386 node = node->rb_right;
1387 else
1388 return clp;
1389 }
1390 return NULL;
1391}
1392
1393static void
1394add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001397 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001399 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001400 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001402 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001403 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
NeilBrownfd39ca92005-06-23 22:04:03 -07001406static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407move_to_confirmed(struct nfs4_client *clp)
1408{
1409 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001410 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001413 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001414 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001415 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001416 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 renew_client(clp);
1418}
1419
1420static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001421find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct nfs4_client *clp;
1424 unsigned int idhashval = clientid_hashval(clid->cl_id);
1425
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001426 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001427 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001428 if ((bool)clp->cl_minorversion != sessions)
1429 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001430 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 return NULL;
1435}
1436
1437static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001438find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1439{
1440 struct list_head *tbl = nn->conf_id_hashtbl;
1441
1442 return find_client_in_id_table(tbl, clid, sessions);
1443}
1444
1445static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001446find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001448 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001450 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001453static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001454{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001455 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001456}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001457
NeilBrown28ce6052005-06-23 22:03:56 -07001458static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001459find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001460{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001461 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001462}
1463
1464static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001465find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001466{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001467 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001468}
1469
NeilBrownfd39ca92005-06-23 22:04:03 -07001470static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001471gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001473 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001474 struct sockaddr *sa = svc_addr(rqstp);
1475 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001476 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Jeff Layton7077ecb2009-08-14 12:57:58 -04001478 /* Currently, we only support tcp and tcp6 for the callback channel */
1479 if (se->se_callback_netid_len == 3 &&
1480 !memcmp(se->se_callback_netid_val, "tcp", 3))
1481 expected_family = AF_INET;
1482 else if (se->se_callback_netid_len == 4 &&
1483 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1484 expected_family = AF_INET6;
1485 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 goto out_err;
1487
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001488 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001489 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001490 (struct sockaddr *)&conn->cb_addr,
1491 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001492
J. Bruce Fields07263f12010-05-31 19:09:40 -04001493 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001495
J. Bruce Fields07263f12010-05-31 19:09:40 -04001496 if (conn->cb_addr.ss_family == AF_INET6)
1497 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001498
J. Bruce Fields07263f12010-05-31 19:09:40 -04001499 conn->cb_prog = se->se_callback_prog;
1500 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001501 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return;
1503out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001504 conn->cb_addr.ss_family = AF_UNSPEC;
1505 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001506 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 "will not receive delegations\n",
1508 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return;
1511}
1512
Andy Adamson074fe892009-04-03 08:28:15 +03001513/*
Andy Adamson557ce262009-08-28 08:45:04 -04001514 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001515 */
1516void
1517nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1518{
Andy Adamson557ce262009-08-28 08:45:04 -04001519 struct nfsd4_slot *slot = resp->cstate.slot;
1520 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001521
Andy Adamson557ce262009-08-28 08:45:04 -04001522 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001523
Andy Adamson557ce262009-08-28 08:45:04 -04001524 slot->sl_opcnt = resp->opcnt;
1525 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001526
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001527 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001528 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001529 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001530 return;
1531 }
Andy Adamson557ce262009-08-28 08:45:04 -04001532 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1533 base = (char *)resp->cstate.datap -
1534 (char *)resp->xbuf->head[0].iov_base;
1535 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1536 slot->sl_datalen))
1537 WARN("%s: sessions DRC could not cache compound\n", __func__);
1538 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001539}
1540
1541/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001542 * Encode the replay sequence operation from the slot values.
1543 * If cachethis is FALSE encode the uncached rep error on the next
1544 * operation which sets resp->p and increments resp->opcnt for
1545 * nfs4svc_encode_compoundres.
1546 *
Andy Adamson074fe892009-04-03 08:28:15 +03001547 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001548static __be32
1549nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1550 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001551{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001552 struct nfsd4_op *op;
1553 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001554
Andy Adamsonabfabf82009-07-23 19:02:18 -04001555 /* Encode the replayed sequence operation */
1556 op = &args->ops[resp->opcnt - 1];
1557 nfsd4_encode_operation(resp, op);
1558
1559 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001560 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001561 op = &args->ops[resp->opcnt++];
1562 op->status = nfserr_retry_uncached_rep;
1563 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001564 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001565 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001566}
1567
1568/*
Andy Adamson557ce262009-08-28 08:45:04 -04001569 * The sequence operation is not cached because we can use the slot and
1570 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001571 */
1572__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001573nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1574 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001575{
Andy Adamson557ce262009-08-28 08:45:04 -04001576 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001577 __be32 status;
1578
Andy Adamson557ce262009-08-28 08:45:04 -04001579 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001580
Andy Adamsonabfabf82009-07-23 19:02:18 -04001581 /* Either returns 0 or nfserr_retry_uncached */
1582 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1583 if (status == nfserr_retry_uncached_rep)
1584 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001585
Andy Adamson557ce262009-08-28 08:45:04 -04001586 /* The sequence operation has been encoded, cstate->datap set. */
1587 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001588
Andy Adamson557ce262009-08-28 08:45:04 -04001589 resp->opcnt = slot->sl_opcnt;
1590 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1591 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001592
1593 return status;
1594}
1595
Andy Adamson0733d212009-04-03 08:28:01 +03001596/*
1597 * Set the exchange_id flags returned by the server.
1598 */
1599static void
1600nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1601{
1602 /* pNFS is not supported */
1603 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1604
1605 /* Referrals are supported, Migration is not. */
1606 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1607
1608 /* set the wire flags to return to client. */
1609 clid->flags = new->cl_exchange_flags;
1610}
1611
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001612static bool client_has_state(struct nfs4_client *clp)
1613{
1614 /*
1615 * Note clp->cl_openowners check isn't quite right: there's no
1616 * need to count owners without stateid's.
1617 *
1618 * Also note we should probably be using this in 4.0 case too.
1619 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001620 return !list_empty(&clp->cl_openowners)
1621 || !list_empty(&clp->cl_delegations)
1622 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001623}
1624
Al Virob37ad282006-10-19 23:28:59 -07001625__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001626nfsd4_exchange_id(struct svc_rqst *rqstp,
1627 struct nfsd4_compound_state *cstate,
1628 struct nfsd4_exchange_id *exid)
1629{
Andy Adamson0733d212009-04-03 08:28:01 +03001630 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001631 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001632 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001633 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001634 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001635 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001636 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001637
Jeff Layton363168b2009-08-14 12:57:56 -04001638 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001639 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001640 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001641 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001642 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001643
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001644 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001645 return nfserr_inval;
1646
1647 /* Currently only support SP4_NONE */
1648 switch (exid->spa_how) {
1649 case SP4_NONE:
1650 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001651 default: /* checked by xdr code */
1652 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001653 case SP4_SSV:
Andy Adamson0733d212009-04-03 08:28:01 +03001654 case SP4_MACH_CRED:
1655 return nfserr_serverfault; /* no excuse :-/ */
1656 }
1657
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001658 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001659 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001660 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001661 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001662 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1663 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1664
J. Bruce Fields136e6582012-05-12 20:37:23 -04001665 if (update) {
1666 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001667 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001668 goto out;
1669 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001670 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001671 status = nfserr_perm;
1672 goto out;
1673 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001674 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001675 status = nfserr_not_same;
1676 goto out;
1677 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001678 /* case 6 */
1679 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1680 new = conf;
1681 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001682 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001683 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001684 if (client_has_state(conf)) {
1685 status = nfserr_clid_inuse;
1686 goto out;
1687 }
Andy Adamson0733d212009-04-03 08:28:01 +03001688 expire_client(conf);
1689 goto out_new;
1690 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001691 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001692 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001693 new = conf;
1694 goto out_copy;
1695 }
1696 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001697 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001698 }
1699
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001700 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001701 status = nfserr_noent;
1702 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001703 }
1704
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001705 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001706 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001707 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001708
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001709 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001710out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001711 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001712 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001713 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001714 goto out;
1715 }
J. Bruce Fieldsc116a0a2012-09-11 14:53:09 -04001716 new->cl_minorversion = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001717
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001718 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001719 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001720out_copy:
1721 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1722 exid->clientid.cl_id = new->cl_clientid.cl_id;
1723
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001724 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001725 nfsd4_set_ex_flags(new, exid);
1726
1727 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001728 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001729 status = nfs_ok;
1730
1731out:
1732 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001733 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001734}
1735
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001736static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001737check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001738{
Andy Adamson88e588d2009-07-23 19:02:15 -04001739 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1740 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001741
1742 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001743 if (slot_inuse) {
1744 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001745 return nfserr_jukebox;
1746 else
1747 return nfserr_seq_misordered;
1748 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001749 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001750 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001751 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001752 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001753 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001754 return nfserr_seq_misordered;
1755}
1756
Andy Adamson49557cc2009-07-23 19:02:16 -04001757/*
1758 * Cache the create session result into the create session single DRC
1759 * slot cache by saving the xdr structure. sl_seqid has been set.
1760 * Do this for solo or embedded create session operations.
1761 */
1762static void
1763nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001764 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001765{
1766 slot->sl_status = nfserr;
1767 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1768}
1769
1770static __be32
1771nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1772 struct nfsd4_clid_slot *slot)
1773{
1774 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1775 return slot->sl_status;
1776}
1777
Mi Jinlong1b74c252011-07-14 14:50:17 +08001778#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1779 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1780 1 + /* MIN tag is length with zero, only length */ \
1781 3 + /* version, opcount, opcode */ \
1782 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1783 /* seqid, slotID, slotID, cache */ \
1784 4 ) * sizeof(__be32))
1785
1786#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1787 2 + /* verifier: AUTH_NULL, length 0 */\
1788 1 + /* status */ \
1789 1 + /* MIN tag is length with zero, only length */ \
1790 3 + /* opcount, opcode, opstatus*/ \
1791 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1792 /* seqid, slotID, slotID, slotID, status */ \
1793 5 ) * sizeof(__be32))
1794
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001795static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001796{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001797 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
1798
J. Bruce Fields373cd402013-04-08 15:42:12 -04001799 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
1800 return nfserr_toosmall;
1801 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
1802 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001803 ca->headerpadsz = 0;
1804 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
1805 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
1806 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
1807 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
1808 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
1809 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
1810 /*
1811 * Note decreasing slot size below client's request may make it
1812 * difficult for client to function correctly, whereas
1813 * decreasing the number of slots will (just?) affect
1814 * performance. When short on memory we therefore prefer to
1815 * decrease number of slots instead of their size. Clients that
1816 * request larger slots than they need will get poor results:
1817 */
1818 ca->maxreqs = nfsd4_get_drc_mem(ca);
1819 if (!ca->maxreqs)
1820 return nfserr_jukebox;
1821
J. Bruce Fields373cd402013-04-08 15:42:12 -04001822 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08001823}
1824
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001825static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
1826{
1827 ca->headerpadsz = 0;
1828
1829 /*
1830 * These RPC_MAX_HEADER macros are overkill, especially since we
1831 * don't even do gss on the backchannel yet. But this is still
1832 * less than 1k. Tighten up this estimate in the unlikely event
1833 * it turns out to be a problem for some client:
1834 */
1835 if (ca->maxreq_sz < NFS4_enc_cb_recall_sz + RPC_MAX_HEADER_WITH_AUTH)
1836 return nfserr_toosmall;
1837 if (ca->maxresp_sz < NFS4_dec_cb_recall_sz + RPC_MAX_REPHEADER_WITH_AUTH)
1838 return nfserr_toosmall;
1839 ca->maxresp_cached = 0;
1840 if (ca->maxops < 2)
1841 return nfserr_toosmall;
1842
1843 return nfs_ok;
1844}
1845
Andy Adamson069b6ad2009-04-03 08:27:58 +03001846__be32
1847nfsd4_create_session(struct svc_rqst *rqstp,
1848 struct nfsd4_compound_state *cstate,
1849 struct nfsd4_create_session *cr_ses)
1850{
Jeff Layton363168b2009-08-14 12:57:56 -04001851 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001852 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001853 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001854 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001855 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001856 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001857 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001858
Mi Jinlonga62573d2011-03-23 17:57:07 +08001859 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1860 return nfserr_inval;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001861 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04001862 if (status)
1863 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001864 status = check_backchannel_attrs(&cr_ses->back_channel);
1865 if (status)
1866 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001867 status = nfserr_jukebox;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001868 new = alloc_session(&cr_ses->fore_channel);
1869 if (!new)
1870 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001871 conn = alloc_conn_from_crses(rqstp, cr_ses);
1872 if (!conn)
1873 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08001874
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001875 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001876 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001877 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04001878 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001879
1880 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001881 cs_slot = &conf->cl_cs_slot;
1882 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001883 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001884 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001885 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001886 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001887 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001888 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001889 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001890 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001891 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001892 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001893 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001894 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001895 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001896 }
Andy Adamson49557cc2009-07-23 19:02:16 -04001897 cs_slot = &unconf->cl_cs_slot;
1898 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001899 if (status) {
1900 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001901 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001902 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001903 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001904 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001905 if (old) {
1906 status = mark_client_expired(old);
1907 if (status)
1908 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001909 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001910 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001911 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001912 conf = unconf;
1913 } else {
1914 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001915 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001916 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001917 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001918 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001919 * We do not support RDMA or persistent sessions
1920 */
1921 cr_ses->flags &= ~SESSION4_PERSIST;
1922 cr_ses->flags &= ~SESSION4_RDMA;
1923
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001924 init_session(rqstp, new, conf, cr_ses);
1925 nfsd4_init_conn(rqstp, conn, new);
1926
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001927 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001928 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001929 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001930 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001931
Andy Adamson49557cc2009-07-23 19:02:16 -04001932 /* cache solo and embedded create sessions under the state lock */
1933 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001934 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001935 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001936out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08001937 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001938 free_conn(conn);
1939out_free_session:
1940 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001941out_release_drc_mem:
1942 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04001943 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001944}
1945
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001946static __be32 nfsd4_map_bcts_dir(u32 *dir)
1947{
1948 switch (*dir) {
1949 case NFS4_CDFC4_FORE:
1950 case NFS4_CDFC4_BACK:
1951 return nfs_ok;
1952 case NFS4_CDFC4_FORE_OR_BOTH:
1953 case NFS4_CDFC4_BACK_OR_BOTH:
1954 *dir = NFS4_CDFC4_BOTH;
1955 return nfs_ok;
1956 };
1957 return nfserr_inval;
1958}
1959
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001960__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1961{
1962 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001963 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001964
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001965 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001966 session->se_cb_prog = bc->bc_cb_program;
1967 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001968 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001969
1970 nfsd4_probe_callback(session->se_client);
1971
1972 return nfs_ok;
1973}
1974
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001975__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1976 struct nfsd4_compound_state *cstate,
1977 struct nfsd4_bind_conn_to_session *bcts)
1978{
1979 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001980 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001981 struct nfsd4_session *session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001982 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001983
1984 if (!nfsd4_last_compound_op(rqstp))
1985 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001986 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001987 spin_lock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001988 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001989 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001990 status = nfserr_badsession;
1991 if (!session)
1992 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001993 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001994 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001995 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001996 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001997 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001998 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001999 goto out;
2000 nfsd4_init_conn(rqstp, conn, session);
2001 status = nfs_ok;
2002out:
2003 nfs4_unlock_state();
2004 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002005}
2006
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002007static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2008{
2009 if (!session)
2010 return 0;
2011 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2012}
2013
Andy Adamson069b6ad2009-04-03 08:27:58 +03002014__be32
2015nfsd4_destroy_session(struct svc_rqst *r,
2016 struct nfsd4_compound_state *cstate,
2017 struct nfsd4_destroy_session *sessionid)
2018{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002019 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002020 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002021 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002022
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002023 nfs4_lock_state();
2024 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002025 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002026 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002027 goto out;
J. Bruce Fields57716352010-04-21 12:27:19 -04002028 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002029 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002030 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002031 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002032 status = nfserr_badsession;
2033 if (!ses)
2034 goto out_client_lock;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002035 status = mark_session_dead_locked(ses);
2036 if (status)
2037 goto out_client_lock;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002038 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002039 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002040
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002041 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002042
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002043 spin_lock(&nn->client_lock);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002044 free_session(ses);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002045 status = nfs_ok;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002046out_client_lock:
2047 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002048out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002049 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002050 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002051}
2052
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002053static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002054{
2055 struct nfsd4_conn *c;
2056
2057 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002058 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002059 return c;
2060 }
2061 }
2062 return NULL;
2063}
2064
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002065static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002066{
2067 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002068 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002069 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002070
2071 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002072 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002073 if (c) {
2074 spin_unlock(&clp->cl_lock);
2075 free_conn(new);
2076 return;
2077 }
2078 __nfsd4_hash_conn(new, ses);
2079 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002080 ret = nfsd4_register_conn(new);
2081 if (ret)
2082 /* oops; xprt is already down: */
2083 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002084 return;
2085}
2086
Mi Jinlong868b89c2011-04-27 09:09:58 +08002087static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2088{
2089 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2090
2091 return args->opcnt > session->se_fchannel.maxops;
2092}
2093
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002094static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2095 struct nfsd4_session *session)
2096{
2097 struct xdr_buf *xb = &rqstp->rq_arg;
2098
2099 return xb->len > session->se_fchannel.maxreq_sz;
2100}
2101
Andy Adamson069b6ad2009-04-03 08:27:58 +03002102__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002103nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002104 struct nfsd4_compound_state *cstate,
2105 struct nfsd4_sequence *seq)
2106{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002107 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002108 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002109 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002110 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002111 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002112 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002113 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002114
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002115 if (resp->opcnt != 1)
2116 return nfserr_sequence_pos;
2117
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002118 /*
2119 * Will be either used or freed by nfsd4_sequence_check_conn
2120 * below.
2121 */
2122 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2123 if (!conn)
2124 return nfserr_jukebox;
2125
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002126 spin_lock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002127 status = nfserr_badsession;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002128 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
Benny Halevyb85d4c02009-04-03 08:28:08 +03002129 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002130 goto out_no_session;
2131 clp = session->se_client;
2132 status = get_client_locked(clp);
2133 if (status)
2134 goto out_no_session;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002135 status = nfsd4_get_session_locked(session);
2136 if (status)
2137 goto out_put_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002138
Mi Jinlong868b89c2011-04-27 09:09:58 +08002139 status = nfserr_too_many_ops;
2140 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002141 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002142
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002143 status = nfserr_req_too_big;
2144 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002145 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002146
Benny Halevyb85d4c02009-04-03 08:28:08 +03002147 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002148 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002149 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002150
Andy Adamson557ce262009-08-28 08:45:04 -04002151 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002152 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2153
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002154 /* We do not negotiate the number of slots yet, so set the
2155 * maxslots to the session maxreqs which is used to encode
2156 * sr_highest_slotid and the sr_target_slot id to maxslots */
2157 seq->maxslots = session->se_fchannel.maxreqs;
2158
J. Bruce Fields73e79482012-02-13 16:39:00 -05002159 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2160 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002161 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002162 status = nfserr_seq_misordered;
2163 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002164 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002165 cstate->slot = slot;
2166 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002167 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002168 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002169 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002170 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002171 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002172 }
2173 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002174 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002175
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002176 nfsd4_sequence_check_conn(conn, session);
2177 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002178
Benny Halevyb85d4c02009-04-03 08:28:08 +03002179 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002180 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002181 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002182 if (seq->cachethis)
2183 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002184 else
2185 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002186
2187 cstate->slot = slot;
2188 cstate->session = session;
2189
Benny Halevyb85d4c02009-04-03 08:28:08 +03002190out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002191 switch (clp->cl_cb_state) {
2192 case NFSD4_CB_DOWN:
2193 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2194 break;
2195 case NFSD4_CB_FAULT:
2196 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2197 break;
2198 default:
2199 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002200 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002201 if (!list_empty(&clp->cl_revoked))
2202 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002203out_no_session:
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002204 kfree(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002205 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002206 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002207out_put_session:
2208 nfsd4_put_session(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002209out_put_client:
2210 put_client_renew_locked(clp);
2211 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002212}
2213
Mi Jinlong345c2842011-10-20 17:51:39 +08002214__be32
2215nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2216{
2217 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002218 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002219 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002220
2221 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002222 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002223 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002224 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002225
2226 if (conf) {
2227 clp = conf;
2228
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002229 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002230 status = nfserr_clientid_busy;
2231 goto out;
2232 }
2233 } else if (unconf)
2234 clp = unconf;
2235 else {
2236 status = nfserr_stale_clientid;
2237 goto out;
2238 }
2239
2240 expire_client(clp);
2241out:
2242 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002243 return status;
2244}
2245
Andy Adamson069b6ad2009-04-03 08:27:58 +03002246__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002247nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2248{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002249 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002250
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002251 if (rc->rca_one_fs) {
2252 if (!cstate->current_fh.fh_dentry)
2253 return nfserr_nofilehandle;
2254 /*
2255 * We don't take advantage of the rca_one_fs case.
2256 * That's OK, it's optional, we can safely ignore it.
2257 */
2258 return nfs_ok;
2259 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002260
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002261 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002262 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002263 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2264 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002265 goto out;
2266
2267 status = nfserr_stale_clientid;
2268 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002269 /*
2270 * The following error isn't really legal.
2271 * But we only get here if the client just explicitly
2272 * destroyed the client. Surely it no longer cares what
2273 * error it gets back on an operation for the dead
2274 * client.
2275 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002276 goto out;
2277
2278 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002279 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002280out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002281 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002282 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002283}
2284
2285__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002286nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2287 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002289 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002291 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002292 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002293 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2294
J. Bruce Fields63db4632012-05-18 21:54:19 -04002295 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002297 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002298 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002299 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002301 if (clp_used_exchangeid(conf))
2302 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002303 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002304 char addr_str[INET6_ADDRSTRLEN];
2305 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2306 sizeof(addr_str));
2307 dprintk("NFSD: setclientid: string in use by client "
2308 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 goto out;
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002312 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002313 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002316 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002317 if (new == NULL)
2318 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002319 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002320 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002322 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002323 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002324 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002325 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002326 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2328 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2329 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2330 status = nfs_ok;
2331out:
2332 nfs4_unlock_state();
2333 return status;
2334}
2335
2336
Al Virob37ad282006-10-19 23:28:59 -07002337__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002338nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2339 struct nfsd4_compound_state *cstate,
2340 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
NeilBrown21ab45a2005-06-23 22:04:14 -07002342 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2344 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002345 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002346 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002348 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002351
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002352 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002353 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002354 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002355 * We try hard to give out unique clientid's, so if we get an
2356 * attempt to confirm the same clientid with a different cred,
2357 * there's a bug somewhere. Let's charitably assume it's our
2358 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002359 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002360 status = nfserr_serverfault;
2361 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2362 goto out;
2363 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2364 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002365 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002366 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2367 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002369 else /* case 4: client hasn't noticed we rebooted yet? */
2370 status = nfserr_stale_clientid;
2371 goto out;
2372 }
2373 status = nfs_ok;
2374 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002375 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2376 nfsd4_probe_callback(conf);
2377 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002378 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002379 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002380 if (conf) {
2381 status = mark_client_expired(conf);
2382 if (status)
2383 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002384 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002385 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002386 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002387 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 nfs4_unlock_state();
2391 return status;
2392}
2393
J. Bruce Fields32513b42011-10-13 16:00:16 -04002394static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002396 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2397}
2398
2399/* OPEN Share state helper functions */
2400static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 unsigned int hashval = file_hashval(ino);
2403
J. Bruce Fields32513b42011-10-13 16:00:16 -04002404 atomic_set(&fp->fi_ref, 1);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002405 INIT_LIST_HEAD(&fp->fi_stateids);
2406 INIT_LIST_HEAD(&fp->fi_delegations);
2407 fp->fi_inode = igrab(ino);
2408 fp->fi_had_conflict = false;
2409 fp->fi_lease = NULL;
2410 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2411 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2412 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002413 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002414 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
2417static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002418nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002419{
NeilBrowne60d4392005-06-23 22:03:01 -07002420 if (*slab == NULL)
2421 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002422 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002423 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002424}
2425
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002426void
NeilBrowne60d4392005-06-23 22:03:01 -07002427nfsd4_free_slabs(void)
2428{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002429 nfsd4_free_slab(&openowner_slab);
2430 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002431 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002432 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002433 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Bryan Schumaker72083392011-11-01 15:24:59 -04002436int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437nfsd4_init_slabs(void)
2438{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002439 openowner_slab = kmem_cache_create("nfsd4_openowners",
2440 sizeof(struct nfs4_openowner), 0, 0, NULL);
2441 if (openowner_slab == NULL)
2442 goto out_nomem;
2443 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002444 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002445 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002446 goto out_nomem;
2447 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002448 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002449 if (file_slab == NULL)
2450 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002451 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002452 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002453 if (stateid_slab == NULL)
2454 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002455 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002456 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002457 if (deleg_slab == NULL)
2458 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002460out_nomem:
2461 nfsd4_free_slabs();
2462 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2463 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002466void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002468 kfree(oo->oo_owner.so_owner.data);
2469 kmem_cache_free(openowner_slab, oo);
2470}
2471
2472void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2473{
2474 kfree(lo->lo_owner.so_owner.data);
2475 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002478static void init_nfs4_replay(struct nfs4_replay *rp)
2479{
2480 rp->rp_status = nfserr_serverfault;
2481 rp->rp_buflen = 0;
2482 rp->rp_buf = rp->rp_ibuf;
2483}
2484
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002485static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 struct nfs4_stateowner *sop;
2488
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002489 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002490 if (!sop)
2491 return NULL;
2492
2493 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2494 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002495 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002496 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002498 sop->so_owner.len = owner->len;
2499
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002500 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002501 sop->so_client = clp;
2502 init_nfs4_replay(&sop->so_replay);
2503 return sop;
2504}
2505
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002506static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002507{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002508 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2509
2510 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002511 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002514static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002516 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002518 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2519 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002521 oo->oo_owner.so_is_open_owner = 1;
2522 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002523 oo->oo_flags = NFS4_OO_NEW;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002524 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002525 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002526 INIT_LIST_HEAD(&oo->oo_close_lru);
2527 hash_openowner(oo, clp, strhashval);
2528 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
J. Bruce Fields996e0932011-10-17 11:14:48 -04002531static 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 -04002532 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002534 stp->st_stid.sc_type = NFS4_OPEN_STID;
NeilBrownea1da632005-06-23 22:04:17 -07002535 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002536 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002537 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002538 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002539 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 stp->st_access_bmap = 0;
2542 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002543 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002544 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002545 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
2548static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002549move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002551 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2552
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002553 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002555 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002556 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002560same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2561 clientid_t *clid)
2562{
2563 return (sop->so_owner.len == owner->len) &&
2564 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2565 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002568static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002569find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2570 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002572 struct nfs4_stateowner *so;
2573 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002574 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002576 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002577 if (!so->so_is_open_owner)
2578 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002579 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2580 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002581 clp = oo->oo_owner.so_client;
2582 if ((bool)clp->cl_minorversion != sessions)
2583 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002584 renew_client(oo->oo_owner.so_client);
2585 return oo;
2586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588 return NULL;
2589}
2590
2591/* search file_hashtbl[] for file */
2592static struct nfs4_file *
2593find_file(struct inode *ino)
2594{
2595 unsigned int hashval = file_hashval(ino);
2596 struct nfs4_file *fp;
2597
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002598 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002599 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002600 if (fp->fi_inode == ino) {
2601 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002602 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002606 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return NULL;
2608}
2609
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002610/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 * Called to check deny when READ with all zero stateid or
2612 * WRITE with all zero or all one stateid
2613 */
Al Virob37ad282006-10-19 23:28:59 -07002614static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2616{
2617 struct inode *ino = current_fh->fh_dentry->d_inode;
2618 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002619 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002620 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002623 if (!fp)
2624 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002625 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002627 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002628 if (test_deny(deny_type, stp) ||
2629 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002630 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
NeilBrown13cd2182005-06-23 22:03:10 -07002632 ret = nfs_ok;
2633out:
2634 put_nfs4_file(fp);
2635 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002638static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002640 struct nfs4_client *clp = dp->dl_stid.sc_client;
2641 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /* We're assuming the state code never drops its reference
2644 * without first removing the lease. Since we're in this lease
2645 * callback (and since the lease code is serialized by the kernel
2646 * lock) we know the server hasn't removed the lease yet, we know
2647 * it's safe to take a reference: */
2648 atomic_inc(&dp->dl_count);
2649
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002650 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Arnd Bergmann460781b2010-11-17 16:26:56 +01002652 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 dp->dl_time = get_seconds();
2654
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002655 nfsd4_cb_recall(dp);
2656}
2657
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002658/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002659static void nfsd_break_deleg_cb(struct file_lock *fl)
2660{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002661 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2662 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002663
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002664 if (!fp) {
2665 WARN(1, "(%p)->fl_owner NULL\n", fl);
2666 return;
2667 }
2668 if (fp->fi_had_conflict) {
2669 WARN(1, "duplicate break on %p\n", fp);
2670 return;
2671 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002672 /*
2673 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002674 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002675 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002676 */
2677 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002679 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002680 fp->fi_had_conflict = true;
2681 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2682 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002683 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686static
2687int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2688{
2689 if (arg & F_UNLCK)
2690 return lease_modify(onlist, arg);
2691 else
2692 return -EAGAIN;
2693}
2694
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002695static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002696 .lm_break = nfsd_break_deleg_cb,
2697 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698};
2699
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002700static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2701{
2702 if (nfsd4_has_session(cstate))
2703 return nfs_ok;
2704 if (seqid == so->so_seqid - 1)
2705 return nfserr_replay_me;
2706 if (seqid == so->so_seqid)
2707 return nfs_ok;
2708 return nfserr_bad_seqid;
2709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Al Virob37ad282006-10-19 23:28:59 -07002711__be32
Andy Adamson66689582009-04-03 08:28:45 +03002712nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03002713 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 clientid_t *clientid = &open->op_clientid;
2716 struct nfs4_client *clp = NULL;
2717 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002718 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002719 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002721 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002723 /*
2724 * In case we need it later, after we've already created the
2725 * file and don't want to risk a further failure:
2726 */
2727 open->op_file = nfsd4_alloc_file();
2728 if (open->op_file == NULL)
2729 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002731 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002732 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002733 open->op_openowner = oo;
2734 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002735 clp = find_confirmed_client(clientid, cstate->minorversion,
2736 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002738 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002739 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002741 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002742 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002743 clp = oo->oo_owner.so_client;
2744 release_openowner(oo);
2745 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002746 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002747 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002748 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2749 if (status)
2750 return status;
2751 clp = oo->oo_owner.so_client;
2752 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002753new_owner:
2754 oo = alloc_init_open_stateowner(strhashval, clp, open);
2755 if (oo == NULL)
2756 return nfserr_jukebox;
2757 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002758alloc_stateid:
2759 open->op_stp = nfs4_alloc_stateid(clp);
2760 if (!open->op_stp)
2761 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002762 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
Al Virob37ad282006-10-19 23:28:59 -07002765static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002766nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2767{
2768 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2769 return nfserr_openmode;
2770 else
2771 return nfs_ok;
2772}
2773
Daniel Mackc47d8322011-05-16 16:38:14 +02002774static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002775{
J. Bruce Fields24a01112010-05-18 20:01:35 -04002776 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2777}
2778
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002779static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002780{
2781 struct nfs4_stid *ret;
2782
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002783 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002784 if (!ret)
2785 return NULL;
2786 return delegstateid(ret);
2787}
2788
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002789static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2790{
2791 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2792 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2793}
2794
Al Virob37ad282006-10-19 23:28:59 -07002795static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04002796nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07002797 struct nfs4_delegation **dp)
2798{
2799 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002800 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002801
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002802 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002803 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002804 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002805 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002806 status = nfs4_check_delegmode(*dp, flags);
2807 if (status)
2808 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002809out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002810 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07002811 return nfs_ok;
2812 if (status)
2813 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002814 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002815 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002816}
2817
Al Virob37ad282006-10-19 23:28:59 -07002818static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002819nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002821 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002822 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
NeilBrown8beefa22005-06-23 22:03:08 -07002824 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 /* ignore lock owners */
2826 if (local->st_stateowner->so_is_open_owner == 0)
2827 continue;
2828 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002829 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 *stpp = local;
2831 /* check for conflicting share reservations */
2832 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002833 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002835 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002838static inline int nfs4_access_to_access(u32 nfs4_access)
2839{
2840 int flags = 0;
2841
2842 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2843 flags |= NFSD_MAY_READ;
2844 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2845 flags |= NFSD_MAY_WRITE;
2846 return flags;
2847}
2848
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002849static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2850 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002851{
2852 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002853 int oflag = nfs4_access_to_omode(open->op_share_access);
2854 int access = nfs4_access_to_access(open->op_share_access);
2855
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002856 if (!fp->fi_fds[oflag]) {
2857 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2858 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002859 if (status)
2860 return status;
2861 }
2862 nfs4_file_get_access(fp, oflag);
2863
2864 return nfs_ok;
2865}
2866
Al Virob37ad282006-10-19 23:28:59 -07002867static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2869 struct nfsd4_open *open)
2870{
2871 struct iattr iattr = {
2872 .ia_valid = ATTR_SIZE,
2873 .ia_size = 0,
2874 };
2875 if (!open->op_truncate)
2876 return 0;
2877 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002878 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2880}
2881
Al Virob37ad282006-10-19 23:28:59 -07002882static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002883nfs4_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 -07002884{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002885 u32 op_share_access = open->op_share_access;
J. Bruce Fields7d947842010-08-20 18:09:31 -04002886 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002887 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Jeff Layton82c5ff12012-05-11 09:45:13 -04002889 new_access = !test_access(op_share_access, stp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002890 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002891 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002892 if (status)
2893 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 status = nfsd4_truncate(rqstp, cur_fh, open);
2896 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002897 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002898 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002899 nfs4_file_put_access(fp, oflag);
2900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902 }
2903 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04002904 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002905 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 return nfs_ok;
2908}
2909
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05002912nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002914 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002917/* Should we give out recallable state?: */
2918static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2919{
2920 if (clp->cl_cb_state == NFSD4_CB_UP)
2921 return true;
2922 /*
2923 * In the sessions case, since we don't have to establish a
2924 * separate connection for callbacks, we assume it's OK
2925 * until we hear otherwise:
2926 */
2927 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2928}
2929
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002930static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2931{
2932 struct file_lock *fl;
2933
2934 fl = locks_alloc_lock();
2935 if (!fl)
2936 return NULL;
2937 locks_init_lock(fl);
2938 fl->fl_lmops = &nfsd_lease_mng_ops;
2939 fl->fl_flags = FL_LEASE;
2940 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2941 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002942 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002943 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002944 return fl;
2945}
2946
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002947static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2948{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002949 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002950 struct file_lock *fl;
2951 int status;
2952
2953 fl = nfs4_alloc_init_lease(dp, flag);
2954 if (!fl)
2955 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002956 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002957 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002958 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002959 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002960 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002961 locks_free_lock(fl);
2962 return -ENOMEM;
2963 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002964 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04002965 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002966 atomic_set(&fp->fi_delegees, 1);
2967 list_add(&dp->dl_perfile, &fp->fi_delegations);
2968 return 0;
2969}
2970
2971static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2972{
2973 struct nfs4_file *fp = dp->dl_file;
2974
2975 if (!fp->fi_lease)
2976 return nfs4_setlease(dp, flag);
2977 spin_lock(&recall_lock);
2978 if (fp->fi_had_conflict) {
2979 spin_unlock(&recall_lock);
2980 return -EAGAIN;
2981 }
2982 atomic_inc(&fp->fi_delegees);
2983 list_add(&dp->dl_perfile, &fp->fi_delegations);
2984 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002985 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002986 return 0;
2987}
2988
Benny Halevy4aa89132012-02-21 14:16:44 -08002989static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2990{
2991 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2992 if (status == -EAGAIN)
2993 open->op_why_no_deleg = WND4_CONTENTION;
2994 else {
2995 open->op_why_no_deleg = WND4_RESOURCE;
2996 switch (open->op_deleg_want) {
2997 case NFS4_SHARE_WANT_READ_DELEG:
2998 case NFS4_SHARE_WANT_WRITE_DELEG:
2999 case NFS4_SHARE_WANT_ANY_DELEG:
3000 break;
3001 case NFS4_SHARE_WANT_CANCEL:
3002 open->op_why_no_deleg = WND4_CANCELLED;
3003 break;
3004 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003005 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003006 }
3007 }
3008}
3009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010/*
3011 * Attempt to hand out a delegation.
3012 */
3013static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003014nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3015 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003018 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003019 int cb_up;
Benny Halevyd24433c2012-02-16 20:57:17 +02003020 int status = 0, flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003022 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07003024 open->op_recall = 0;
3025 switch (open->op_claim_type) {
3026 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003027 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003028 open->op_recall = 1;
3029 flag = open->op_delegate_type;
3030 if (flag == NFS4_OPEN_DELEGATE_NONE)
3031 goto out;
3032 break;
3033 case NFS4_OPEN_CLAIM_NULL:
3034 /* Let's not give out any delegations till everyone's
3035 * had the chance to reclaim theirs.... */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003036 if (locks_in_grace(net))
NeilBrown7b190fe2005-06-23 22:03:23 -07003037 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003038 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07003039 goto out;
3040 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3041 flag = NFS4_OPEN_DELEGATE_WRITE;
3042 else
3043 flag = NFS4_OPEN_DELEGATE_READ;
3044 break;
3045 default:
3046 goto out;
3047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003049 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003050 if (dp == NULL)
3051 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003052 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003053 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003054 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003056 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003058 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003059 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060out:
3061 open->op_delegate_type = flag;
Benny Halevyd24433c2012-02-16 20:57:17 +02003062 if (flag == NFS4_OPEN_DELEGATE_NONE) {
3063 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3064 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
3065 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3066
Benny Halevy4aa89132012-02-21 14:16:44 -08003067 /* 4.1 client asking for a delegation? */
3068 if (open->op_deleg_want)
3069 nfsd4_open_deleg_none_ext(open, status);
Benny Halevyd24433c2012-02-16 20:57:17 +02003070 }
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003071 return;
3072out_free:
J. Bruce Fields24ffb932012-12-12 15:24:12 -05003073 unhash_stid(&dp->dl_stid);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003074 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003075out_no_deleg:
3076 flag = NFS4_OPEN_DELEGATE_NONE;
3077 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
Benny Halevye27f49c2012-02-21 14:16:54 -08003080static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3081 struct nfs4_delegation *dp)
3082{
3083 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3084 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3085 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3086 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3087 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3088 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3089 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3090 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3091 }
3092 /* Otherwise the client must be confused wanting a delegation
3093 * it already has, therefore we don't return
3094 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3095 */
3096}
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098/*
3099 * called with nfs4_lock_state() held.
3100 */
Al Virob37ad282006-10-19 23:28:59 -07003101__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3103{
Andy Adamson66689582009-04-03 08:28:45 +03003104 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003105 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 struct nfs4_file *fp = NULL;
3107 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003108 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003109 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003110 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 /*
3113 * Lookup file; if found, lookup stateid and check open request,
3114 * and check for delegations in the process of being recalled.
3115 * If not found, create the nfs4_file struct
3116 */
3117 fp = find_file(ino);
3118 if (fp) {
3119 if ((status = nfs4_check_open(fp, open, &stp)))
3120 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003121 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003122 if (status)
3123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07003125 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003126 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003127 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003128 status = nfserr_jukebox;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003129 fp = open->op_file;
3130 open->op_file = NULL;
3131 nfsd4_init_file(fp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
3133
3134 /*
3135 * OPEN the file, or upgrade an existing OPEN.
3136 * If truncate fails, the OPEN fails.
3137 */
3138 if (stp) {
3139 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003140 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (status)
3142 goto out;
3143 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003144 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003145 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 goto out;
J. Bruce Fields4af82502012-06-07 17:30:45 -04003147 status = nfsd4_truncate(rqstp, current_fh, open);
3148 if (status)
3149 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003150 stp = open->op_stp;
3151 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003152 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003154 update_stateid(&stp->st_stid.sc_stateid);
3155 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Benny Halevyd24433c2012-02-16 20:57:17 +02003157 if (nfsd4_has_session(&resp->cstate)) {
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003158 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03003159
Benny Halevyd24433c2012-02-16 20:57:17 +02003160 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3161 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3162 open->op_why_no_deleg = WND4_NOT_WANTED;
3163 goto nodeleg;
3164 }
3165 }
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /*
3168 * Attempt to hand out a delegation. No error return, because the
3169 * OPEN succeeds even if we fail.
3170 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003171 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003172nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 status = nfs_ok;
3174
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003175 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003176 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003178 /* 4.1 client trying to upgrade/downgrade delegation? */
3179 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003180 open->op_deleg_want)
3181 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003182
NeilBrown13cd2182005-06-23 22:03:10 -07003183 if (fp)
3184 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003185 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003186 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /*
3188 * To finish the open response, we just need to set the rflags.
3189 */
3190 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003191 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003192 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3194
3195 return status;
3196}
3197
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003198void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3199{
3200 if (open->op_openowner) {
3201 struct nfs4_openowner *oo = open->op_openowner;
3202
3203 if (!list_empty(&oo->oo_owner.so_stateids))
3204 list_del_init(&oo->oo_close_lru);
3205 if (oo->oo_flags & NFS4_OO_NEW) {
3206 if (status) {
3207 release_openowner(oo);
3208 open->op_openowner = NULL;
3209 } else
3210 oo->oo_flags &= ~NFS4_OO_NEW;
3211 }
3212 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003213 if (open->op_file)
3214 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003215 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003216 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003217}
3218
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003219static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3220{
3221 struct nfs4_client *found;
3222
3223 if (STALE_CLIENTID(clid, nn))
3224 return nfserr_stale_clientid;
3225 found = find_confirmed_client(clid, session, nn);
3226 if (clp)
3227 *clp = found;
3228 return found ? nfs_ok : nfserr_expired;
3229}
3230
Al Virob37ad282006-10-19 23:28:59 -07003231__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003232nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3233 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003236 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003237 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239 nfs4_lock_state();
3240 dprintk("process_renew(%08x/%08x): starting\n",
3241 clid->cl_boot, clid->cl_id);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003242 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3243 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003246 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003247 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 goto out;
3249 status = nfs_ok;
3250out:
3251 nfs4_unlock_state();
3252 return status;
3253}
3254
NeilBrowna76b4312005-06-23 22:04:01 -07003255static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003256nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003257{
Jeff Layton33dcc482012-04-10 11:08:48 -04003258 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003259 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003260 return;
3261
NeilBrowna76b4312005-06-23 22:04:01 -07003262 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003263 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003264 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003265 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003266 /*
3267 * Now that every NFSv4 client has had the chance to recover and
3268 * to see the (possibly new, possibly shorter) lease time, we
3269 * can safely set the next grace time to the current lease time:
3270 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003271 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003272}
3273
NeilBrownfd39ca92005-06-23 22:04:03 -07003274static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003275nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
3277 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003278 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct nfs4_delegation *dp;
3280 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003281 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3282 time_t t, clientid_val = nn->nfsd4_lease;
3283 time_t u, test_val = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 nfs4_lock_state();
3286
3287 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003288 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003289 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003290 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003291 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 clp = list_entry(pos, struct nfs4_client, cl_lru);
3293 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3294 t = clp->cl_time - cutoff;
3295 if (clientid_val > t)
3296 clientid_val = t;
3297 break;
3298 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003299 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003300 dprintk("NFSD: client in use (clientid %08x)\n",
3301 clp->cl_clientid.cl_id);
3302 continue;
3303 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003304 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003305 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003306 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003307 list_for_each_safe(pos, next, &reaplist) {
3308 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 dprintk("NFSD: purging unused client (clientid %08x)\n",
3310 clp->cl_clientid.cl_id);
3311 expire_client(clp);
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003314 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003316 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3317 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3319 u = dp->dl_time - cutoff;
3320 if (test_val > u)
3321 test_val = u;
3322 break;
3323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 list_move(&dp->dl_recall_lru, &reaplist);
3325 }
3326 spin_unlock(&recall_lock);
3327 list_for_each_safe(pos, next, &reaplist) {
3328 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003329 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003331 test_val = nn->nfsd4_lease;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003332 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003333 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3334 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3335 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (test_val > u)
3337 test_val = u;
3338 break;
3339 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003340 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3343 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3344 nfs4_unlock_state();
3345 return clientid_val;
3346}
3347
Harvey Harrisona254b242008-02-20 12:49:00 -08003348static struct workqueue_struct *laundry_wq;
3349static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003350
3351static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003352laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
3354 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003355 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3356 work);
3357 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3358 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003360 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003362 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003365static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003366{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003367 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3368 return nfserr_bad_stateid;
3369 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003373access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003375 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3376 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3377 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
3380static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003381access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003383 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3384 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385}
3386
3387static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003388__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
Al Virob37ad282006-10-19 23:28:59 -07003390 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
J. Bruce Fields02921912010-07-29 15:16:59 -04003392 /* For lock stateid's, we test the parent open, not the lock: */
3393 if (stp->st_openstp)
3394 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003395 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003397 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 goto out;
3399 status = nfs_ok;
3400out:
3401 return status;
3402}
3403
Al Virob37ad282006-10-19 23:28:59 -07003404static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003405check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003407 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003409 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003410 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 * conflicting state; so we must wait out the grace period. */
3412 return nfserr_grace;
3413 } else if (flags & WR_STATE)
3414 return nfs4_share_conflict(current_fh,
3415 NFS4_SHARE_DENY_WRITE);
3416 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3417 return nfs4_share_conflict(current_fh,
3418 NFS4_SHARE_DENY_READ);
3419}
3420
3421/*
3422 * Allow READ/WRITE during grace period on recovered state only for files
3423 * that are not able to provide mandatory locking.
3424 */
3425static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003426grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003428 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
J. Bruce Fields81b82962011-08-23 11:03:29 -04003431/* Returns true iff a is later than b: */
3432static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3433{
3434 return (s32)a->si_generation - (s32)b->si_generation > 0;
3435}
3436
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003437static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003438{
Andy Adamson66689582009-04-03 08:28:45 +03003439 /*
3440 * When sessions are used the stateid generation number is ignored
3441 * when it is zero.
3442 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003443 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003444 return nfs_ok;
3445
3446 if (in->si_generation == ref->si_generation)
3447 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003448
J. Bruce Fields0836f582008-01-26 19:08:12 -05003449 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003450 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003451 return nfserr_bad_stateid;
3452 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003453 * However, we could see a stateid from the past, even from a
3454 * non-buggy client. For example, if the client sends a lock
3455 * while some IO is outstanding, the lock may bump si_generation
3456 * while the IO is still in flight. The client could avoid that
3457 * situation by waiting for responses on all the IO requests,
3458 * but better performance may result in retrying IO that
3459 * receives an old_stateid error if requests are rarely
3460 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003461 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003462 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003463}
3464
Chuck Lever7df302f2012-05-29 13:56:37 -04003465static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003466{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003467 struct nfs4_stid *s;
3468 struct nfs4_ol_stateid *ols;
3469 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003470
Chuck Lever7df302f2012-05-29 13:56:37 -04003471 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3472 return nfserr_bad_stateid;
3473 /* Client debugging aid. */
3474 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3475 char addr_str[INET6_ADDRSTRLEN];
3476 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3477 sizeof(addr_str));
3478 pr_warn_ratelimited("NFSD: client %s testing state ID "
3479 "with incorrect client ID\n", addr_str);
3480 return nfserr_bad_stateid;
3481 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003482 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003483 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003484 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003485 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003486 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003487 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04003488 switch (s->sc_type) {
3489 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003490 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003491 case NFS4_REVOKED_DELEG_STID:
3492 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04003493 case NFS4_OPEN_STID:
3494 case NFS4_LOCK_STID:
3495 ols = openlockstateid(s);
3496 if (ols->st_stateowner->so_is_open_owner
3497 && !(openowner(ols->st_stateowner)->oo_flags
3498 & NFS4_OO_CONFIRMED))
3499 return nfserr_bad_stateid;
3500 return nfs_ok;
3501 default:
3502 printk("unknown stateid type %x\n", s->sc_type);
3503 case NFS4_CLOSED_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003504 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04003505 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003506}
3507
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003508static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3509 struct nfs4_stid **s, bool sessions,
3510 struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003511{
3512 struct nfs4_client *cl;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003513 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003514
3515 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3516 return nfserr_bad_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003517 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3518 nn, &cl);
3519 if (status == nfserr_stale_clientid)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003520 return nfserr_stale_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003521 if (status)
3522 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003523 *s = find_stateid_by_type(cl, stateid, typemask);
3524 if (!*s)
3525 return nfserr_bad_stateid;
3526 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003527}
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529/*
3530* Checks for stateid operations
3531*/
Al Virob37ad282006-10-19 23:28:59 -07003532__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003533nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003534 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003536 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003537 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003539 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003541 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07003542 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (filpp)
3545 *filpp = NULL;
3546
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003547 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return nfserr_grace;
3549
3550 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003551 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003553 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3554 &s, cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003555 if (status)
3556 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003557 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3558 if (status)
3559 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003560 switch (s->sc_type) {
3561 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003562 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003563 status = nfs4_check_delegmode(dp, flags);
3564 if (status)
3565 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003566 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003567 *filpp = dp->dl_file->fi_deleg_file;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003568 if (!*filpp) {
3569 WARN_ON_ONCE(1);
3570 status = nfserr_serverfault;
3571 goto out;
3572 }
Dan Carpenter43b01782010-10-27 23:19:04 +02003573 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003574 break;
3575 case NFS4_OPEN_STID:
3576 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003577 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003578 status = nfs4_check_fh(current_fh, stp);
3579 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003581 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003582 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003584 status = nfs4_check_openmode(stp, flags);
3585 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003587 if (filpp) {
3588 if (flags & RD_STATE)
3589 *filpp = find_readable_file(stp->st_file);
3590 else
3591 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003592 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003593 break;
3594 default:
3595 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 }
3597 status = nfs_ok;
3598out:
3599 return status;
3600}
3601
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003602static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003603nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003604{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003605 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003606 return nfserr_locks_held;
3607 release_lock_stateid(stp);
3608 return nfs_ok;
3609}
3610
3611/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003612 * Test if the stateid is valid
3613 */
3614__be32
3615nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3616 struct nfsd4_test_stateid *test_stateid)
3617{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003618 struct nfsd4_test_stateid_id *stateid;
3619 struct nfs4_client *cl = cstate->session->se_client;
3620
3621 nfs4_lock_state();
3622 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003623 stateid->ts_id_status =
3624 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003625 nfs4_unlock_state();
3626
Bryan Schumaker17456802011-07-13 10:50:48 -04003627 return nfs_ok;
3628}
3629
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003630__be32
3631nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3632 struct nfsd4_free_stateid *free_stateid)
3633{
3634 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003635 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003636 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003637 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003638 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003639
3640 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003641 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003642 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003643 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003644 switch (s->sc_type) {
3645 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003646 ret = nfserr_locks_held;
3647 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003648 case NFS4_OPEN_STID:
3649 case NFS4_LOCK_STID:
3650 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3651 if (ret)
3652 goto out;
3653 if (s->sc_type == NFS4_LOCK_STID)
3654 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3655 else
3656 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003657 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003658 case NFS4_REVOKED_DELEG_STID:
3659 dp = delegstateid(s);
3660 destroy_revoked_delegation(dp);
3661 ret = nfs_ok;
3662 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003663 default:
3664 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003665 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003666out:
3667 nfs4_unlock_state();
3668 return ret;
3669}
3670
NeilBrown4c4cd222005-07-07 17:59:27 -07003671static inline int
3672setlkflg (int type)
3673{
3674 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3675 RD_STATE : WR_STATE;
3676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003678static __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 -04003679{
3680 struct svc_fh *current_fh = &cstate->current_fh;
3681 struct nfs4_stateowner *sop = stp->st_stateowner;
3682 __be32 status;
3683
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003684 status = nfsd4_check_seqid(cstate, sop, seqid);
3685 if (status)
3686 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003687 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
3688 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003689 /*
3690 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003691 * nfserr_replay_me from the previous step, and
3692 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003693 */
3694 return nfserr_bad_stateid;
3695 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3696 if (status)
3697 return status;
3698 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003699}
3700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701/*
3702 * Checks for sequence id mutating operations.
3703 */
Al Virob37ad282006-10-19 23:28:59 -07003704static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003705nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003706 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003707 struct nfs4_ol_stateid **stpp,
3708 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003710 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003711 struct nfs4_stid *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003713 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3714 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 *stpp = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003717 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3718 cstate->minorversion, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003719 if (status)
3720 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003721 *stpp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003722 if (!nfsd4_has_session(cstate))
3723 cstate->replay_owner = (*stpp)->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003725 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3726}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003727
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003728static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3729 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003730{
3731 __be32 status;
3732 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003734 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003735 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003736 if (status)
3737 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003738 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003739 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003740 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003741 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
Al Virob37ad282006-10-19 23:28:59 -07003744__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003745nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003746 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
Al Virob37ad282006-10-19 23:28:59 -07003748 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003749 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003750 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003751 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
3753 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003754 (int)cstate->current_fh.fh_dentry->d_name.len,
3755 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003757 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003758 if (status)
3759 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
3761 nfs4_lock_state();
3762
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003763 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003764 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003765 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003766 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003767 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003768 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003769 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003770 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003771 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003772 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003773 update_stateid(&stp->st_stid.sc_stateid);
3774 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003775 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003776 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003777
Jeff Layton2a4317c2012-03-21 16:42:43 -04003778 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003779 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003781 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003782 if (!cstate->replay_owner)
3783 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 return status;
3785}
3786
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003787static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003789 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003790 return;
3791 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04003792 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003793}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003794
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003795static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3796{
3797 switch (to_access) {
3798 case NFS4_SHARE_ACCESS_READ:
3799 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3800 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3801 break;
3802 case NFS4_SHARE_ACCESS_WRITE:
3803 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3804 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3805 break;
3806 case NFS4_SHARE_ACCESS_BOTH:
3807 break;
3808 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003809 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 }
3811}
3812
3813static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04003814reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
3816 int i;
3817 for (i = 0; i < 4; i++) {
3818 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04003819 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 }
3821}
3822
Al Virob37ad282006-10-19 23:28:59 -07003823__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003824nfsd4_open_downgrade(struct svc_rqst *rqstp,
3825 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003826 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
Al Virob37ad282006-10-19 23:28:59 -07003828 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003829 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003830 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003833 (int)cstate->current_fh.fh_dentry->d_name.len,
3834 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04003836 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02003837 if (od->od_deleg_want)
3838 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3839 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
3841 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003842 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003843 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003844 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003847 if (!test_access(od->od_share_access, stp)) {
3848 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 stp->st_access_bmap, od->od_share_access);
3850 goto out;
3851 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04003852 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3854 stp->st_deny_bmap, od->od_share_deny);
3855 goto out;
3856 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003857 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
Jeff Laytonce0fc432012-05-11 09:45:14 -04003859 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003861 update_stateid(&stp->st_stid.sc_stateid);
3862 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 status = nfs_ok;
3864out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003865 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003866 if (!cstate->replay_owner)
3867 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return status;
3869}
3870
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003871static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3872{
3873 unhash_open_stateid(s);
3874 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003875}
3876
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877/*
3878 * nfs4_unlock_state() called after encode
3879 */
Al Virob37ad282006-10-19 23:28:59 -07003880__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003881nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003882 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
Al Virob37ad282006-10-19 23:28:59 -07003884 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003885 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003886 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003887 struct net *net = SVC_NET(rqstp);
3888 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003891 (int)cstate->current_fh.fh_dentry->d_name.len,
3892 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003895 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3896 &close->cl_stateid,
3897 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003898 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003899 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003900 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003902 oo = openowner(stp->st_stateowner);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003903 update_stateid(&stp->st_stid.sc_stateid);
3904 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003906 nfsd4_close_open_stateid(stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003907
3908 if (cstate->minorversion) {
3909 unhash_stid(&stp->st_stid);
3910 free_generic_stateid(stp);
3911 } else
3912 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003913
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003914 if (list_empty(&oo->oo_owner.so_stateids)) {
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003915 if (cstate->minorversion)
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003916 release_openowner(oo);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003917 else {
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003918 /*
3919 * In the 4.0 case we need to keep the owners around a
3920 * little while to handle CLOSE replay.
3921 */
fanchaoting53584f62013-04-11 21:24:13 +08003922 move_to_close_lru(oo, SVC_NET(rqstp));
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003923 }
3924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003926 if (!cstate->replay_owner)
3927 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 return status;
3929}
3930
Al Virob37ad282006-10-19 23:28:59 -07003931__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003932nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3933 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003935 struct nfs4_delegation *dp;
3936 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003937 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07003938 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003939 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003941 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003942 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 nfs4_lock_state();
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003945 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3946 cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003947 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003948 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003949 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003950 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003951 if (status)
3952 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003953
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003954 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003956 nfs4_unlock_state();
3957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return status;
3959}
3960
3961
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
J. Bruce Fields009673b2011-11-07 17:40:10 -05003964#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Benny Halevy87df4de2008-12-15 19:42:03 +02003966static inline u64
3967end_offset(u64 start, u64 len)
3968{
3969 u64 end;
3970
3971 end = start + len;
3972 return end >= start ? end: NFS4_MAX_UINT64;
3973}
3974
3975/* last octet in a range */
3976static inline u64
3977last_byte_offset(u64 start, u64 len)
3978{
3979 u64 end;
3980
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003981 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02003982 end = start + len;
3983 return end > start ? end - 1: NFS4_MAX_UINT64;
3984}
3985
J. Bruce Fields009673b2011-11-07 17:40:10 -05003986static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
3988 return (file_hashval(inode) + cl_id
3989 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05003990 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993/*
3994 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3995 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3996 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3997 * locking, this prevents us from being completely protocol-compliant. The
3998 * real solution to this problem is to start using unsigned file offsets in
3999 * the VFS, but this is a very deep change!
4000 */
4001static inline void
4002nfs4_transform_lock_offset(struct file_lock *lock)
4003{
4004 if (lock->fl_start < 0)
4005 lock->fl_start = OFFSET_MAX;
4006 if (lock->fl_end < 0)
4007 lock->fl_end = OFFSET_MAX;
4008}
4009
NeilBrownd5b90262006-04-10 22:55:22 -07004010/* Hack!: For now, we're defining this just so we can use a pointer to it
4011 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004012static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004013};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
4015static inline void
4016nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4017{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004018 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
NeilBrownd5b90262006-04-10 22:55:22 -07004020 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004021 lo = (struct nfs4_lockowner *) fl->fl_owner;
4022 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4023 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004024 if (!deny->ld_owner.data)
4025 /* We just don't care that much */
4026 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004027 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4028 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004029 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004030nevermind:
4031 deny->ld_owner.len = 0;
4032 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004033 deny->ld_clientid.cl_boot = 0;
4034 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
4036 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004037 deny->ld_length = NFS4_MAX_UINT64;
4038 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4040 deny->ld_type = NFS4_READ_LT;
4041 if (fl->fl_type != F_RDLCK)
4042 deny->ld_type = NFS4_WRITE_LT;
4043}
4044
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004045static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
4046{
4047 struct nfs4_ol_stateid *lst;
4048
4049 if (!same_owner_str(&lo->lo_owner, owner, clid))
4050 return false;
4051 lst = list_first_entry(&lo->lo_owner.so_stateids,
4052 struct nfs4_ol_stateid, st_perstateowner);
4053 return lst->st_file->fi_inode == inode;
4054}
4055
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004056static struct nfs4_lockowner *
4057find_lockowner_str(struct inode *inode, clientid_t *clid,
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004058 struct xdr_netobj *owner, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004060 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004061 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004063 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004064 if (same_lockowner_ino(lo, inode, clid, owner))
4065 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 }
4067 return NULL;
4068}
4069
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004070static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004071{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004072 struct inode *inode = open_stp->st_file->fi_inode;
4073 unsigned int inohash = lockowner_ino_hashval(inode,
4074 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004075 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields009673b2011-11-07 17:40:10 -05004076
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004077 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004078 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004079 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004080}
4081
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082/*
4083 * Alloc a lock owner structure.
4084 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004085 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004087 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 */
4089
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004090static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004091alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004092 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004094 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4095 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004097 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4098 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004099 /* It is the openowner seqid that will be incremented in encode in the
4100 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004101 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4102 hash_lockowner(lo, strhashval, clp, open_stp);
4103 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004106static struct nfs4_ol_stateid *
4107alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004109 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004110 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
J. Bruce Fields996e0932011-10-17 11:14:48 -04004112 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004113 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004114 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004115 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown8beefa22005-06-23 22:03:08 -07004116 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004117 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4118 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004119 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004121 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004123 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 return stp;
4125}
4126
NeilBrownfd39ca92005-06-23 22:04:03 -07004127static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128check_lock_length(u64 offset, u64 length)
4129{
Benny Halevy87df4de2008-12-15 19:42:03 +02004130 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 LOFF_OVERFLOW(offset, length)));
4132}
4133
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004134static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004135{
4136 struct nfs4_file *fp = lock_stp->st_file;
4137 int oflag = nfs4_access_to_omode(access);
4138
Jeff Layton82c5ff12012-05-11 09:45:13 -04004139 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004140 return;
4141 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004142 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004143}
4144
J. Bruce Fields2355c592012-04-25 16:56:22 -04004145static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004146{
4147 struct nfs4_file *fi = ost->st_file;
4148 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4149 struct nfs4_client *cl = oo->oo_owner.so_client;
4150 struct nfs4_lockowner *lo;
4151 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004152 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004153
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004154 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4155 &lock->v.new.owner, nn);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004156 if (lo) {
4157 if (!cstate->minorversion)
4158 return nfserr_bad_seqid;
4159 /* XXX: a lockowner always has exactly one stateid: */
4160 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4161 struct nfs4_ol_stateid, st_perstateowner);
4162 return nfs_ok;
4163 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004164 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004165 &lock->v.new.owner);
4166 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4167 if (lo == NULL)
4168 return nfserr_jukebox;
4169 *lst = alloc_init_lock_stateid(lo, fi, ost);
4170 if (*lst == NULL) {
4171 release_lockowner(lo);
4172 return nfserr_jukebox;
4173 }
4174 *new = true;
4175 return nfs_ok;
4176}
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178/*
4179 * LOCK operation
4180 */
Al Virob37ad282006-10-19 23:28:59 -07004181__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004182nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004183 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004185 struct nfs4_openowner *open_sop = NULL;
4186 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004187 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004188 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004189 struct file_lock *file_lock = NULL;
4190 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004191 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004192 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004193 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004194 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004195 struct net *net = SVC_NET(rqstp);
4196 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
4198 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4199 (long long) lock->lk_offset,
4200 (long long) lock->lk_length);
4201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 if (check_lock_length(lock->lk_offset, lock->lk_length))
4203 return nfserr_inval;
4204
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004205 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004206 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004207 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4208 return status;
4209 }
4210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 nfs4_lock_state();
4212
4213 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004214 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004215
4216 if (nfsd4_has_session(cstate))
4217 /* See rfc 5661 18.10.3: given clientid is ignored: */
4218 memcpy(&lock->v.new.clientid,
4219 &cstate->session->se_client->cl_clientid,
4220 sizeof(clientid_t));
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004223 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004227 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 lock->lk_new_open_seqid,
4229 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004230 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004231 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004233 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004234 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004235 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004236 &lock->v.new.clientid))
4237 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004238 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4239 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004240 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004241 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004242 lock->lk_old_lock_seqid,
4243 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004244 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004245 if (status)
4246 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004247 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004249 lkflg = setlkflg(lock->lk_type);
4250 status = nfs4_check_openmode(lock_stp, lkflg);
4251 if (status)
4252 goto out;
4253
NeilBrown0dd395d2005-07-07 17:59:15 -07004254 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004255 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004256 goto out;
4257 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004258 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004259 goto out;
4260
Jeff Layton21179d82012-08-21 08:03:32 -04004261 file_lock = locks_alloc_lock();
4262 if (!file_lock) {
4263 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4264 status = nfserr_jukebox;
4265 goto out;
4266 }
4267
4268 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 switch (lock->lk_type) {
4270 case NFS4_READ_LT:
4271 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004272 filp = find_readable_file(lock_stp->st_file);
4273 if (filp)
4274 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004275 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 case NFS4_WRITE_LT:
4278 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004279 filp = find_writeable_file(lock_stp->st_file);
4280 if (filp)
4281 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004282 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 default:
4285 status = nfserr_inval;
4286 goto out;
4287 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004288 if (!filp) {
4289 status = nfserr_openmode;
4290 goto out;
4291 }
Jeff Layton21179d82012-08-21 08:03:32 -04004292 file_lock->fl_owner = (fl_owner_t)lock_sop;
4293 file_lock->fl_pid = current->tgid;
4294 file_lock->fl_file = filp;
4295 file_lock->fl_flags = FL_POSIX;
4296 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4297 file_lock->fl_start = lock->lk_offset;
4298 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4299 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Jeff Layton21179d82012-08-21 08:03:32 -04004301 conflock = locks_alloc_lock();
4302 if (!conflock) {
4303 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4304 status = nfserr_jukebox;
4305 goto out;
4306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Jeff Layton21179d82012-08-21 08:03:32 -04004308 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004309 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004311 update_stateid(&lock_stp->st_stid.sc_stateid);
4312 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004314 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004315 break;
4316 case (EAGAIN): /* conflock holds conflicting lock */
4317 status = nfserr_denied;
4318 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004319 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 case (EDEADLK):
4322 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004323 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004324 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004325 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004326 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004327 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004330 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004331 release_lockowner(lock_sop);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004332 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004333 if (!cstate->replay_owner)
4334 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004335 if (file_lock)
4336 locks_free_lock(file_lock);
4337 if (conflock)
4338 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 return status;
4340}
4341
4342/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004343 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4344 * so we do a temporary open here just to get an open file to pass to
4345 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4346 * inode operation.)
4347 */
Al Viro04da6e92012-04-13 00:00:04 -04004348static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004349{
4350 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004351 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4352 if (!err) {
4353 err = nfserrno(vfs_test_lock(file, lock));
4354 nfsd_close(file);
4355 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004356 return err;
4357}
4358
4359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 * LOCKT operation
4361 */
Al Virob37ad282006-10-19 23:28:59 -07004362__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004363nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4364 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
4366 struct inode *inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004367 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004368 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004369 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004370 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004372 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 return nfserr_grace;
4374
4375 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4376 return nfserr_inval;
4377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 nfs4_lock_state();
4379
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004380 if (!nfsd4_has_session(cstate)) {
4381 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4382 if (status)
4383 goto out;
4384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004386 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004389 inode = cstate->current_fh.fh_dentry->d_inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004390 file_lock = locks_alloc_lock();
4391 if (!file_lock) {
4392 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4393 status = nfserr_jukebox;
4394 goto out;
4395 }
4396 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 switch (lockt->lt_type) {
4398 case NFS4_READ_LT:
4399 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004400 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 break;
4402 case NFS4_WRITE_LT:
4403 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004404 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 break;
4406 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004407 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 status = nfserr_inval;
4409 goto out;
4410 }
4411
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004412 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004413 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004414 file_lock->fl_owner = (fl_owner_t)lo;
4415 file_lock->fl_pid = current->tgid;
4416 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Jeff Layton21179d82012-08-21 08:03:32 -04004418 file_lock->fl_start = lockt->lt_offset;
4419 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Jeff Layton21179d82012-08-21 08:03:32 -04004421 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Jeff Layton21179d82012-08-21 08:03:32 -04004423 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004424 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004425 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004426
Jeff Layton21179d82012-08-21 08:03:32 -04004427 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004429 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
4431out:
4432 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004433 if (file_lock)
4434 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return status;
4436}
4437
Al Virob37ad282006-10-19 23:28:59 -07004438__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004439nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004440 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441{
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004442 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004443 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004445 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004446 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004447 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004448 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4449
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4451 (long long) locku->lu_offset,
4452 (long long) locku->lu_length);
4453
4454 if (check_lock_length(locku->lu_offset, locku->lu_length))
4455 return nfserr_inval;
4456
4457 nfs4_lock_state();
4458
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004459 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004460 &locku->lu_stateid, NFS4_LOCK_STID,
4461 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004462 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004464 filp = find_any_file(stp->st_file);
4465 if (!filp) {
4466 status = nfserr_lock_range;
4467 goto out;
4468 }
Jeff Layton21179d82012-08-21 08:03:32 -04004469 file_lock = locks_alloc_lock();
4470 if (!file_lock) {
4471 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4472 status = nfserr_jukebox;
4473 goto out;
4474 }
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004475 lo = lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04004476 locks_init_lock(file_lock);
4477 file_lock->fl_type = F_UNLCK;
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004478 file_lock->fl_owner = (fl_owner_t)lo;
Jeff Layton21179d82012-08-21 08:03:32 -04004479 file_lock->fl_pid = current->tgid;
4480 file_lock->fl_file = filp;
4481 file_lock->fl_flags = FL_POSIX;
4482 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4483 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
Jeff Layton21179d82012-08-21 08:03:32 -04004485 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4486 locku->lu_length);
4487 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Jeff Layton21179d82012-08-21 08:03:32 -04004489 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004490 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004491 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 goto out_nfserr;
4493 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004494 update_stateid(&stp->st_stid.sc_stateid);
4495 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004497 if (nfsd4_has_session(cstate) && !check_for_locks(stp->st_file, lo)) {
4498 WARN_ON_ONCE(cstate->replay_owner);
4499 release_lockowner(lo);
4500 }
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004503 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004504 if (!cstate->replay_owner)
4505 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004506 if (file_lock)
4507 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return status;
4509
4510out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004511 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 goto out;
4513}
4514
4515/*
4516 * returns
4517 * 1: locks held by lockowner
4518 * 0: no locks held by lockowner
4519 */
4520static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004521check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
4523 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004524 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 int status = 0;
4526
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004527 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004529 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 status = 1;
4531 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
4534out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004535 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 return status;
4537}
4538
Al Virob37ad282006-10-19 23:28:59 -07004539__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004540nfsd4_release_lockowner(struct svc_rqst *rqstp,
4541 struct nfsd4_compound_state *cstate,
4542 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543{
4544 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004545 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004546 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004547 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004549 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004550 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004551 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004552 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
4554 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4555 clid->cl_boot, clid->cl_id);
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 nfs4_lock_state();
4558
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004559 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4560 if (status)
4561 goto out;
4562
NeilBrown3e9e3db2005-06-23 22:04:20 -07004563 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004564 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004565
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004566 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004567 if (sop->so_is_open_owner)
4568 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004569 if (!same_owner_str(sop, owner, clid))
4570 continue;
4571 list_for_each_entry(stp, &sop->so_stateids,
4572 st_perstateowner) {
4573 lo = lockowner(sop);
4574 if (check_for_locks(stp->st_file, lo))
4575 goto out;
4576 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004578 }
4579 /* Clients probably won't expect us to return with some (but not all)
4580 * of the lockowner state released; so don't release any until all
4581 * have been checked. */
4582 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004583 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004584 lo = list_entry(matches.next, struct nfs4_lockowner,
4585 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004586 /* unhash_stateowner deletes so_perclient only
4587 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004588 list_del(&lo->lo_list);
4589 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
4591out:
4592 nfs4_unlock_state();
4593 return status;
4594}
4595
4596static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004597alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
NeilBrowna55370a2005-06-23 22:03:52 -07004599 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600}
4601
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004602bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004603nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004604{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004605 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004606
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004607 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004608 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004609}
4610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611/*
4612 * failure => all reset bets are off, nfserr_no_grace...
4613 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004614struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004615nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616{
4617 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004618 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
NeilBrowna55370a2005-06-23 22:03:52 -07004620 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4621 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004622 if (crp) {
4623 strhashval = clientstr_hashval(name);
4624 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004625 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004626 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004627 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004628 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004629 }
4630 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631}
4632
Jeff Layton2a4317c2012-03-21 16:42:43 -04004633void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004634nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004635{
4636 list_del(&crp->cr_strhash);
4637 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004638 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004639}
4640
4641void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004642nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
4644 struct nfs4_client_reclaim *crp = NULL;
4645 int i;
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004648 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4649 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004651 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 }
4653 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004654 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
4657/*
4658 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004659struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004660nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661{
4662 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 struct nfs4_client_reclaim *crp = NULL;
4664
Jeff Layton278c9312012-11-12 15:00:52 -05004665 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Jeff Layton278c9312012-11-12 15:00:52 -05004667 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004668 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004669 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 return crp;
4671 }
4672 }
4673 return NULL;
4674}
4675
4676/*
4677* Called from OPEN. Look for clientid in reclaim list.
4678*/
Al Virob37ad282006-10-19 23:28:59 -07004679__be32
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004680nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
Jeff Laytona52d7262012-03-21 09:52:02 -04004682 struct nfs4_client *clp;
4683
4684 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004685 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004686 if (clp == NULL)
4687 return nfserr_reclaim_bad;
4688
4689 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
Bryan Schumaker65178db2011-11-01 13:35:21 -04004692#ifdef CONFIG_NFSD_FAULT_INJECTION
4693
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004694u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4695{
J. Bruce Fields221a6872013-04-01 22:23:49 -04004696 if (mark_client_expired(clp))
4697 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004698 expire_client(clp);
4699 return 1;
4700}
4701
Bryan Schumaker184c1842012-11-29 11:40:44 -05004702u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4703{
4704 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004705 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004706 printk(KERN_INFO "NFS Client: %s\n", buf);
4707 return 1;
4708}
4709
4710static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4711 const char *type)
4712{
4713 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004714 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004715 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4716}
4717
Bryan Schumakerfc291712012-11-29 11:40:40 -05004718static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4719{
4720 struct nfs4_openowner *oop;
4721 struct nfs4_lockowner *lop, *lo_next;
4722 struct nfs4_ol_stateid *stp, *st_next;
4723 u64 count = 0;
4724
4725 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4726 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4727 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4728 if (func)
4729 func(lop);
4730 if (++count == max)
4731 return count;
4732 }
4733 }
4734 }
4735
4736 return count;
4737}
4738
4739u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4740{
4741 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4742}
4743
Bryan Schumaker184c1842012-11-29 11:40:44 -05004744u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4745{
4746 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4747 nfsd_print_count(clp, count, "locked files");
4748 return count;
4749}
4750
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05004751static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4752{
4753 struct nfs4_openowner *oop, *next;
4754 u64 count = 0;
4755
4756 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4757 if (func)
4758 func(oop);
4759 if (++count == max)
4760 break;
4761 }
4762
4763 return count;
4764}
4765
4766u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4767{
4768 return nfsd_foreach_client_open(clp, max, release_openowner);
4769}
4770
Bryan Schumaker184c1842012-11-29 11:40:44 -05004771u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
4772{
4773 u64 count = nfsd_foreach_client_open(clp, max, NULL);
4774 nfsd_print_count(clp, count, "open files");
4775 return count;
4776}
4777
Bryan Schumaker269de302012-11-29 11:40:42 -05004778static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
4779 struct list_head *victims)
4780{
4781 struct nfs4_delegation *dp, *next;
4782 u64 count = 0;
4783
4784 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
4785 if (victims)
4786 list_move(&dp->dl_recall_lru, victims);
4787 if (++count == max)
4788 break;
4789 }
4790 return count;
4791}
4792
4793u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
4794{
4795 struct nfs4_delegation *dp, *next;
4796 LIST_HEAD(victims);
4797 u64 count;
4798
4799 spin_lock(&recall_lock);
4800 count = nfsd_find_all_delegations(clp, max, &victims);
4801 spin_unlock(&recall_lock);
4802
4803 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004804 revoke_delegation(dp);
Bryan Schumaker269de302012-11-29 11:40:42 -05004805
4806 return count;
4807}
4808
4809u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
4810{
4811 struct nfs4_delegation *dp, *next;
4812 LIST_HEAD(victims);
4813 u64 count;
4814
4815 spin_lock(&recall_lock);
4816 count = nfsd_find_all_delegations(clp, max, &victims);
4817 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4818 nfsd_break_one_deleg(dp);
4819 spin_unlock(&recall_lock);
4820
4821 return count;
4822}
4823
Bryan Schumaker184c1842012-11-29 11:40:44 -05004824u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
4825{
4826 u64 count = 0;
4827
4828 spin_lock(&recall_lock);
4829 count = nfsd_find_all_delegations(clp, max, NULL);
4830 spin_unlock(&recall_lock);
4831
4832 nfsd_print_count(clp, count, "delegations");
4833 return count;
4834}
4835
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004836u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004837{
4838 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004839 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004840 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004841
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004842 if (!nfsd_netns_ready(nn))
4843 return 0;
4844
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004845 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004846 count += func(clp, max - count);
4847 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004848 break;
4849 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04004850
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004851 return count;
4852}
4853
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05004854struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
4855{
4856 struct nfs4_client *clp;
4857 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4858
4859 if (!nfsd_netns_ready(nn))
4860 return NULL;
4861
4862 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
4863 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
4864 return clp;
4865 }
4866 return NULL;
4867}
4868
Bryan Schumaker65178db2011-11-01 13:35:21 -04004869#endif /* CONFIG_NFSD_FAULT_INJECTION */
4870
NeilBrownac4d8ff22005-06-23 22:03:30 -07004871/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Bryan Schumaker72083392011-11-01 15:24:59 -04004873void
NeilBrownac4d8ff22005-06-23 22:03:30 -07004874nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
NeilBrownac4d8ff22005-06-23 22:03:30 -07004876}
4877
Meelap Shahc2f1a552007-07-17 04:04:39 -07004878/*
4879 * Since the lifetime of a delegation isn't limited to that of an open, a
4880 * client may quite reasonably hang on to a delegation as long as it has
4881 * the inode cached. This becomes an obvious problem the first time a
4882 * client's inode cache approaches the size of the server's total memory.
4883 *
4884 * For now we avoid this problem by imposing a hard limit on the number
4885 * of delegations, which varies according to the server's memory size.
4886 */
4887static void
4888set_max_delegations(void)
4889{
4890 /*
4891 * Allow at most 4 delegations per megabyte of RAM. Quick
4892 * estimates suggest that in the worst case (where every delegation
4893 * is for a different inode), a delegation could take about 1.5K,
4894 * giving a worst case usage of about 6% of memory.
4895 */
4896 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4897}
4898
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004899static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004900{
4901 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4902 int i;
4903
4904 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4905 CLIENT_HASH_SIZE, GFP_KERNEL);
4906 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004907 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004908 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4909 CLIENT_HASH_SIZE, GFP_KERNEL);
4910 if (!nn->unconf_id_hashtbl)
4911 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004912 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4913 OWNER_HASH_SIZE, GFP_KERNEL);
4914 if (!nn->ownerstr_hashtbl)
4915 goto err_ownerstr;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004916 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4917 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4918 if (!nn->lockowner_ino_hashtbl)
4919 goto err_lockowner_ino;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004920 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4921 SESSION_HASH_SIZE, GFP_KERNEL);
4922 if (!nn->sessionid_hashtbl)
4923 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004924
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004925 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004926 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004927 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004928 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004929 for (i = 0; i < OWNER_HASH_SIZE; i++)
4930 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004931 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4932 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004933 for (i = 0; i < SESSION_HASH_SIZE; i++)
4934 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004935 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004936 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004937 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004938 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004939 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004940 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004941
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004942 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004943 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004944
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004945 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004946
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004947err_sessionid:
4948 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004949err_lockowner_ino:
4950 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004951err_ownerstr:
4952 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004953err_unconf_id:
4954 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004955err:
4956 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004957}
4958
4959static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004960nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004961{
4962 int i;
4963 struct nfs4_client *clp = NULL;
4964 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004965 struct rb_node *node, *tmp;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004966
4967 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4968 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4969 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4970 destroy_client(clp);
4971 }
4972 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004973
4974 node = rb_first(&nn->unconf_name_tree);
4975 while (node != NULL) {
4976 tmp = node;
4977 node = rb_next(tmp);
4978 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4979 rb_erase(tmp, &nn->unconf_name_tree);
4980 destroy_client(clp);
4981 }
4982
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004983 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004984 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004985 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004986 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004987 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004988 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004989}
4990
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004991int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004992nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07004993{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004994 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004995 int ret;
4996
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004997 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004998 if (ret)
4999 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005000 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005001 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005002 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005003 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005004 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005005 nn->nfsd4_grace, net);
5006 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005007 return 0;
5008}
5009
5010/* initialization to perform when the nfsd service is started: */
5011
5012int
5013nfs4_state_start(void)
5014{
5015 int ret;
5016
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005017 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005018 if (ret)
5019 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005020 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005021 if (laundry_wq == NULL) {
5022 ret = -ENOMEM;
5023 goto out_recovery;
5024 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005025 ret = nfsd4_create_callback_queue();
5026 if (ret)
5027 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005028
Meelap Shahc2f1a552007-07-17 04:04:39 -07005029 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005030
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005031 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005032
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005033out_free_laundry:
5034 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005035out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005036 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005039/* should be called with the state lock held */
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005040void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005041nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005045 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005047 cancel_delayed_work_sync(&nn->laundromat_work);
5048 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 INIT_LIST_HEAD(&reaplist);
5051 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005052 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5054 list_move(&dp->dl_recall_lru, &reaplist);
5055 }
5056 spin_unlock(&recall_lock);
5057 list_for_each_safe(pos, next, &reaplist) {
5058 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005059 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 }
5061
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005062 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005063 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
5066void
5067nfs4_state_shutdown(void)
5068{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005069 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005070 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005072
5073static void
5074get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5075{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005076 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5077 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005078}
5079
5080static void
5081put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5082{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005083 if (cstate->minorversion) {
5084 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5085 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5086 }
5087}
5088
5089void
5090clear_current_stateid(struct nfsd4_compound_state *cstate)
5091{
5092 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005093}
5094
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005095/*
5096 * functions to set current state id
5097 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005098void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005099nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5100{
5101 put_stateid(cstate, &odp->od_stateid);
5102}
5103
5104void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005105nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5106{
5107 put_stateid(cstate, &open->op_stateid);
5108}
5109
5110void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005111nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5112{
5113 put_stateid(cstate, &close->cl_stateid);
5114}
5115
5116void
5117nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5118{
5119 put_stateid(cstate, &lock->lk_resp_stateid);
5120}
5121
5122/*
5123 * functions to consume current state id
5124 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005125
5126void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005127nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5128{
5129 get_stateid(cstate, &odp->od_stateid);
5130}
5131
5132void
5133nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5134{
5135 get_stateid(cstate, &drp->dr_stateid);
5136}
5137
5138void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005139nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5140{
5141 get_stateid(cstate, &fsp->fr_stateid);
5142}
5143
5144void
5145nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5146{
5147 get_stateid(cstate, &setattr->sa_stateid);
5148}
5149
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005150void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005151nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5152{
5153 get_stateid(cstate, &close->cl_stateid);
5154}
5155
5156void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005157nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005158{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005159 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005160}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005161
5162void
5163nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5164{
5165 get_stateid(cstate, &read->rd_stateid);
5166}
5167
5168void
5169nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5170{
5171 get_stateid(cstate, &write->wr_stateid);
5172}