blob: aae93045ce6b551844179e8031d17b1a54951d80 [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 Fields0a3adad2009-11-04 18:12:35 -050045#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040046#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040048#include "netns.h"
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define NFSDDBG_FACILITY NFSDDBG_PROC
51
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050052#define all_ones {{~0,~0},~0}
53static const stateid_t one_stateid = {
54 .si_generation = ~0,
55 .si_opaque = all_ones,
56};
57static const stateid_t zero_stateid = {
58 /* all fields zero */
59};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010060static const stateid_t currentstateid = {
61 .si_generation = 1,
62};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050063
Andy Adamsonec6b5d72009-04-03 08:28:28 +030064static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070065
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050066#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
67#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010068#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040071static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
J. Bruce Fields8b671b82009-02-22 14:51:34 -080073/* Locking: */
74
75/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080076static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
J. Bruce Fields8b671b82009-02-22 14:51:34 -080078/*
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
82 */
83static DEFINE_SPINLOCK(recall_lock);
84
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040085static struct kmem_cache *openowner_slab = NULL;
86static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080087static struct kmem_cache *file_slab = NULL;
88static struct kmem_cache *stateid_slab = NULL;
89static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091void
92nfs4_lock_state(void)
93{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080094 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040097static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080098
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099void nfsd4_put_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101 atomic_dec(&ses->se_ref);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102}
103
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400104static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800105{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400106 return ses->se_flags & NFS4_SESSION_DEAD;
107}
108
109static __be32 mark_session_dead_locked(struct nfsd4_session *ses)
110{
111 if (atomic_read(&ses->se_ref))
112 return nfserr_jukebox;
113 ses->se_flags |= NFS4_SESSION_DEAD;
114 return nfs_ok;
115}
116
117static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
118{
119 if (is_session_dead(ses))
120 return nfserr_badsession;
121 atomic_inc(&ses->se_ref);
122 return nfs_ok;
Benny Halevy508dc6e2012-02-23 17:40:52 -0800123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125void
126nfs4_unlock_state(void)
127{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800128 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
J. Bruce Fields221a6872013-04-01 22:23:49 -0400131static bool is_client_expired(struct nfs4_client *clp)
132{
133 return clp->cl_time == 0;
134}
135
136static __be32 mark_client_expired_locked(struct nfs4_client *clp)
137{
138 if (atomic_read(&clp->cl_refcount))
139 return nfserr_jukebox;
140 clp->cl_time = 0;
141 return nfs_ok;
142}
143
144static __be32 mark_client_expired(struct nfs4_client *clp)
145{
146 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
147 __be32 ret;
148
149 spin_lock(&nn->client_lock);
150 ret = mark_client_expired_locked(clp);
151 spin_unlock(&nn->client_lock);
152 return ret;
153}
154
155static __be32 get_client_locked(struct nfs4_client *clp)
156{
157 if (is_client_expired(clp))
158 return nfserr_expired;
159 atomic_inc(&clp->cl_refcount);
160 return nfs_ok;
161}
162
163/* must be called under the client_lock */
164static inline void
165renew_client_locked(struct nfs4_client *clp)
166{
167 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
168
169 if (is_client_expired(clp)) {
170 WARN_ON(1);
171 printk("%s: client (clientid %08x/%08x) already expired\n",
172 __func__,
173 clp->cl_clientid.cl_boot,
174 clp->cl_clientid.cl_id);
175 return;
176 }
177
178 dprintk("renewing client (clientid %08x/%08x)\n",
179 clp->cl_clientid.cl_boot,
180 clp->cl_clientid.cl_id);
181 list_move_tail(&clp->cl_lru, &nn->client_lru);
182 clp->cl_time = get_seconds();
183}
184
185static inline void
186renew_client(struct nfs4_client *clp)
187{
188 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
189
190 spin_lock(&nn->client_lock);
191 renew_client_locked(clp);
192 spin_unlock(&nn->client_lock);
193}
194
195void put_client_renew_locked(struct nfs4_client *clp)
196{
197 if (!atomic_dec_and_test(&clp->cl_refcount))
198 return;
199 if (!is_client_expired(clp))
200 renew_client_locked(clp);
201}
202
203void put_client_renew(struct nfs4_client *clp)
204{
205 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
206
207 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
208 return;
209 if (!is_client_expired(clp))
210 renew_client_locked(clp);
211 spin_unlock(&nn->client_lock);
212}
213
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static inline u32
216opaque_hashval(const void *ptr, int nbytes)
217{
218 unsigned char *cptr = (unsigned char *) ptr;
219
220 u32 x = 0;
221 while (nbytes--) {
222 x *= 37;
223 x += *cptr++;
224 }
225 return x;
226}
227
J. Bruce Fields32513b42011-10-13 16:00:16 -0400228static void nfsd4_free_file(struct nfs4_file *f)
229{
230 kmem_cache_free(file_slab, f);
231}
232
NeilBrown13cd2182005-06-23 22:03:10 -0700233static inline void
234put_nfs4_file(struct nfs4_file *fi)
235{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800236 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400237 hlist_del(&fi->fi_hash);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800238 spin_unlock(&recall_lock);
239 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400240 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800241 }
NeilBrown13cd2182005-06-23 22:03:10 -0700242}
243
244static inline void
245get_nfs4_file(struct nfs4_file *fi)
246{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800247 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700248}
249
NeilBrownef0f3392006-04-10 22:55:41 -0700250static int num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800251unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700252
253/*
254 * Open owner state (share locks)
255 */
256
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500257/* hash tables for lock and open owners */
258#define OWNER_HASH_BITS 8
259#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
260#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700261
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500262static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400263{
264 unsigned int ret;
265
266 ret = opaque_hashval(ownername->data, ownername->len);
267 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500268 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400269}
NeilBrownef0f3392006-04-10 22:55:41 -0700270
NeilBrownef0f3392006-04-10 22:55:41 -0700271/* hash table for nfs4_file */
272#define FILE_HASH_BITS 8
273#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800274
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400275static unsigned int file_hashval(struct inode *ino)
276{
277 /* XXX: why are we hashing on inode pointer, anyway? */
278 return hash_ptr(ino, FILE_HASH_BITS);
279}
280
Jeff Layton89876f82013-04-02 09:01:59 -0400281static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700282
J. Bruce Fields998db522010-08-07 09:21:41 -0400283static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400284{
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500285 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400286 atomic_inc(&fp->fi_access[oflag]);
287}
288
J. Bruce Fields998db522010-08-07 09:21:41 -0400289static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
290{
291 if (oflag == O_RDWR) {
292 __nfs4_file_get_access(fp, O_RDONLY);
293 __nfs4_file_get_access(fp, O_WRONLY);
294 } else
295 __nfs4_file_get_access(fp, oflag);
296}
297
298static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400299{
300 if (fp->fi_fds[oflag]) {
301 fput(fp->fi_fds[oflag]);
302 fp->fi_fds[oflag] = NULL;
303 }
304}
305
J. Bruce Fields998db522010-08-07 09:21:41 -0400306static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400307{
308 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400309 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400310 /*
311 * It's also safe to get rid of the RDWR open *if*
312 * we no longer have need of the other kind of access
313 * or if we already have the other kind of open:
314 */
315 if (fp->fi_fds[1-oflag]
316 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
317 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400318 }
319}
320
J. Bruce Fields998db522010-08-07 09:21:41 -0400321static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
322{
323 if (oflag == O_RDWR) {
324 __nfs4_file_put_access(fp, O_RDONLY);
325 __nfs4_file_put_access(fp, O_WRONLY);
326 } else
327 __nfs4_file_put_access(fp, oflag);
328}
329
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500330static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
331kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400332{
333 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500334 static int min_stateid = 0;
335 struct nfs4_stid *stid;
336 int new_id;
337
338 stid = kmem_cache_alloc(slab, GFP_KERNEL);
339 if (!stid)
340 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400341
Tejun Heoebd6c702013-03-13 14:59:37 -0700342 new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
343 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500344 goto out_free;
345 stid->sc_client = cl;
346 stid->sc_type = 0;
347 stid->sc_stateid.si_opaque.so_id = new_id;
348 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
349 /* Will be incremented before return to client: */
350 stid->sc_stateid.si_generation = 0;
351
J. Bruce Fields996e0932011-10-17 11:14:48 -0400352 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500353 * It shouldn't be a problem to reuse an opaque stateid value.
354 * I don't think it is for 4.1. But with 4.0 I worry that, for
355 * example, a stray write retransmission could be accepted by
356 * the server when it should have been rejected. Therefore,
357 * adopt a trick from the sctp code to attempt to maximize the
358 * amount of time until an id is reused, by ensuring they always
359 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400360 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500361
362 min_stateid = new_id+1;
363 if (min_stateid == INT_MAX)
364 min_stateid = 0;
365 return stid;
366out_free:
367 kfree(stid);
368 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400369}
370
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400371static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
372{
373 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400377alloc_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 -0700378{
379 struct nfs4_delegation *dp;
380 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400383 /*
384 * Major work on the lease subsystem (for example, to support
385 * calbacks on stat) will be required before we can support
386 * write delegations properly.
387 */
388 if (type != NFS4_OPEN_DELEGATE_READ)
389 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700390 if (fp->fi_had_conflict)
391 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700392 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700393 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400394 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700395 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return dp;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500397 dp->dl_stid.sc_type = NFS4_DELEG_STID;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400398 /*
399 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400400 * meaning of seqid 0 isn't meaningful, really, but let's avoid
401 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400402 */
403 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700404 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700405 INIT_LIST_HEAD(&dp->dl_perfile);
406 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 INIT_LIST_HEAD(&dp->dl_recall_lru);
NeilBrown13cd2182005-06-23 22:03:10 -0700408 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 dp->dl_type = type;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500411 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 dp->dl_time = 0;
413 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500414 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return dp;
416}
417
J. Bruce Fields68a33962013-03-21 11:21:50 -0400418static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500419{
420 struct idr *stateids = &s->sc_client->cl_stateids;
421
422 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425void
426nfs4_put_delegation(struct nfs4_delegation *dp)
427{
428 if (atomic_dec_and_test(&dp->dl_count)) {
J. Bruce Fields68a33962013-03-21 11:21:50 -0400429 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700430 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432}
433
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500434static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500436 if (atomic_dec_and_test(&fp->fi_delegees)) {
437 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
438 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400439 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500440 fp->fi_deleg_file = NULL;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400444static void unhash_stid(struct nfs4_stid *s)
445{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500446 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* Called under the state lock. */
450static void
451unhash_delegation(struct nfs4_delegation *dp)
452{
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400453 unhash_stid(&dp->dl_stid);
NeilBrownea1da632005-06-23 22:04:17 -0700454 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500456 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 list_del_init(&dp->dl_recall_lru);
458 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500459 nfs4_put_deleg_lease(dp->dl_file);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400460 put_nfs4_file(dp->dl_file);
461 dp->dl_file = NULL;
462 remove_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 nfs4_put_delegation(dp);
464}
465
466/*
467 * SETCLIENTID state
468 */
469
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400470static unsigned int clientid_hashval(u32 id)
471{
472 return id & CLIENT_HASH_MASK;
473}
474
475static unsigned int clientstr_hashval(const char *name)
476{
477 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400481 * We store the NONE, READ, WRITE, and BOTH bits separately in the
482 * st_{access,deny}_bmap field of the stateid, in order to track not
483 * only what share bits are currently in force, but also what
484 * combinations of share bits previous opens have used. This allows us
485 * to enforce the recommendation of rfc 3530 14.2.19 that the server
486 * return an error if the client attempt to downgrade to a combination
487 * of share bits not explicable by closing some of its previous opens.
488 *
489 * XXX: This enforcement is actually incomplete, since we don't keep
490 * track of access/deny bit combinations; so, e.g., we allow:
491 *
492 * OPEN allow read, deny write
493 * OPEN allow both, deny none
494 * DOWNGRADE allow read, deny none
495 *
496 * which we should reject.
497 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400498static unsigned int
499bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400500 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400501 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400502
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400503 for (i = 1; i < 4; i++) {
504 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400505 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400506 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400507 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400508}
509
Jeff Layton3a328612012-05-11 09:45:12 -0400510static bool
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400511test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400512 unsigned int access, deny;
513
Jeff Layton5ae037e2012-05-11 09:45:11 -0400514 access = bmap_to_share_mode(stp->st_access_bmap);
515 deny = bmap_to_share_mode(stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400516 if ((access & open->op_share_deny) || (deny & open->op_share_access))
Jeff Layton3a328612012-05-11 09:45:12 -0400517 return false;
518 return true;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400519}
520
Jeff Layton82c5ff12012-05-11 09:45:13 -0400521/* set share access for a given stateid */
522static inline void
523set_access(u32 access, struct nfs4_ol_stateid *stp)
524{
525 __set_bit(access, &stp->st_access_bmap);
526}
527
528/* clear share access for a given stateid */
529static inline void
530clear_access(u32 access, struct nfs4_ol_stateid *stp)
531{
532 __clear_bit(access, &stp->st_access_bmap);
533}
534
535/* test whether a given stateid has access */
536static inline bool
537test_access(u32 access, struct nfs4_ol_stateid *stp)
538{
539 return test_bit(access, &stp->st_access_bmap);
540}
541
Jeff Laytonce0fc432012-05-11 09:45:14 -0400542/* set share deny for a given stateid */
543static inline void
544set_deny(u32 access, struct nfs4_ol_stateid *stp)
545{
546 __set_bit(access, &stp->st_deny_bmap);
547}
548
549/* clear share deny for a given stateid */
550static inline void
551clear_deny(u32 access, struct nfs4_ol_stateid *stp)
552{
553 __clear_bit(access, &stp->st_deny_bmap);
554}
555
556/* test whether a given stateid is denying specific access */
557static inline bool
558test_deny(u32 access, struct nfs4_ol_stateid *stp)
559{
560 return test_bit(access, &stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400561}
562
563static int nfs4_access_to_omode(u32 access)
564{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400565 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400566 case NFS4_SHARE_ACCESS_READ:
567 return O_RDONLY;
568 case NFS4_SHARE_ACCESS_WRITE:
569 return O_WRONLY;
570 case NFS4_SHARE_ACCESS_BOTH:
571 return O_RDWR;
572 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500573 WARN_ON_ONCE(1);
574 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400575}
576
Jeff Layton82c5ff12012-05-11 09:45:13 -0400577/* release all access and file references for a given stateid */
578static void
579release_all_access(struct nfs4_ol_stateid *stp)
580{
581 int i;
582
583 for (i = 1; i < 4; i++) {
584 if (test_access(i, stp))
585 nfs4_file_put_access(stp->st_file,
586 nfs4_access_to_omode(i));
587 clear_access(i, stp);
588 }
589}
590
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400591static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500592{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500593 list_del(&stp->st_perfile);
594 list_del(&stp->st_perstateowner);
595}
596
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400597static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500598{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400599 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400600 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400601 stp->st_file = NULL;
602}
603
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400604static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400605{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400606 remove_stid(&stp->st_stid);
607 kmem_cache_free(stateid_slab, stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500608}
609
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400610static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500611{
612 struct file *file;
613
614 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400615 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500616 file = find_any_file(stp->st_file);
617 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400618 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400619 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500620 free_generic_stateid(stp);
621}
622
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400623static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500624{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400625 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500626
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400627 list_del(&lo->lo_owner.so_strhash);
628 list_del(&lo->lo_perstateid);
J. Bruce Fields009673b2011-11-07 17:40:10 -0500629 list_del(&lo->lo_owner_ino_hash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400630 while (!list_empty(&lo->lo_owner.so_stateids)) {
631 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400632 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500633 release_lock_stateid(stp);
634 }
635}
636
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400637static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500638{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400639 unhash_lockowner(lo);
640 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500641}
642
643static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400644release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500645{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400646 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500647
648 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400649 lo = list_entry(open_stp->st_lockowners.next,
650 struct nfs4_lockowner, lo_perstateid);
651 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500652 }
653}
654
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400655static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500656{
657 unhash_generic_stateid(stp);
658 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400659 close_generic_stateid(stp);
660}
661
662static void release_open_stateid(struct nfs4_ol_stateid *stp)
663{
664 unhash_open_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400665 unhash_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500666 free_generic_stateid(stp);
667}
668
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400669static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500670{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400671 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500672
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400673 list_del(&oo->oo_owner.so_strhash);
674 list_del(&oo->oo_perclient);
675 while (!list_empty(&oo->oo_owner.so_stateids)) {
676 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400677 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500678 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500679 }
680}
681
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400682static void release_last_closed_stateid(struct nfs4_openowner *oo)
683{
684 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
685
686 if (s) {
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400687 unhash_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400688 free_generic_stateid(s);
689 oo->oo_last_closed_stid = NULL;
690 }
691}
692
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400693static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500694{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400695 unhash_openowner(oo);
696 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400697 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400698 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500699}
700
Marc Eshel5282fd72009-04-03 08:27:52 +0300701static inline int
702hash_sessionid(struct nfs4_sessionid *sessionid)
703{
704 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
705
706 return sid->sequence % SESSION_HASH_SIZE;
707}
708
Trond Myklebust8f199b82012-03-20 15:11:17 -0400709#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300710static inline void
711dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
712{
713 u32 *ptr = (u32 *)(&sessionid->data[0]);
714 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
715}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400716#else
717static inline void
718dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
719{
720}
721#endif
722
Marc Eshel5282fd72009-04-03 08:27:52 +0300723
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300724static void
725gen_sessionid(struct nfsd4_session *ses)
726{
727 struct nfs4_client *clp = ses->se_client;
728 struct nfsd4_sessionid *sid;
729
730 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
731 sid->clientid = clp->cl_clientid;
732 sid->sequence = current_sessionid++;
733 sid->reserved = 0;
734}
735
736/*
Andy Adamsona6496372009-08-28 08:45:01 -0400737 * The protocol defines ca_maxresponssize_cached to include the size of
738 * the rpc header, but all we need to cache is the data starting after
739 * the end of the initial SEQUENCE operation--the rest we regenerate
740 * each time. Therefore we can advertise a ca_maxresponssize_cached
741 * value that is the number of bytes in our cache plus a few additional
742 * bytes. In order to stay on the safe side, and not promise more than
743 * we can cache, those additional bytes must be the minimum possible: 24
744 * bytes of rpc header (xid through accept state, with AUTH_NULL
745 * verifier), 12 for the compound header (with zero-length tag), and 44
746 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300747 */
Andy Adamsona6496372009-08-28 08:45:01 -0400748#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
749
Andy Adamson557ce262009-08-28 08:45:04 -0400750static void
751free_session_slots(struct nfsd4_session *ses)
752{
753 int i;
754
755 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
756 kfree(ses->se_slots[i]);
757}
758
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400759/*
760 * We don't actually need to cache the rpc and session headers, so we
761 * can allocate a little less for each slot:
762 */
763static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
764{
765 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
766}
767
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400768static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300769{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400770 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
771 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300772
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400773 return size;
774}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300775
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400776/*
777 * XXX: If we run out of reserved DRC memory we could (up to a point)
778 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800779 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400780 */
781static int nfsd4_get_drc_mem(int slotsize, u32 num)
782{
783 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300784
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400785 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400786
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400787 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800788 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
789 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400790 num = min_t(int, num, avail / slotsize);
791 nfsd_drc_mem_used += num * slotsize;
792 spin_unlock(&nfsd_drc_lock);
793
794 return num;
795}
796
797static void nfsd4_put_drc_mem(int slotsize, int num)
798{
799 spin_lock(&nfsd_drc_lock);
800 nfsd_drc_mem_used -= slotsize * num;
801 spin_unlock(&nfsd_drc_lock);
802}
803
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400804static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400805{
806 struct nfsd4_session *new;
807 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300808
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400809 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400810 + sizeof(struct nfsd4_session) > PAGE_SIZE);
811 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400812
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400813 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300814 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400815 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400816 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400817 for (i = 0; i < numslots; i++) {
818 mem = sizeof(struct nfsd4_slot) + slotsize;
819 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
820 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400821 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400822 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400823 return new;
824out_free:
825 while (i--)
826 kfree(new->se_slots[i]);
827 kfree(new);
828 return NULL;
829}
830
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300831static void init_forechannel_attrs(struct nfsd4_channel_attrs *new,
832 struct nfsd4_channel_attrs *req,
833 int numslots, int slotsize,
834 struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400835{
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300836 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400837
838 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800839 new->maxresp_cached = min_t(u32, req->maxresp_cached,
840 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400841 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
842 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
843 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
844}
845
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400846static void free_conn(struct nfsd4_conn *c)
847{
848 svc_xprt_put(c->cn_xprt);
849 kfree(c);
850}
851
852static void nfsd4_conn_lost(struct svc_xpt_user *u)
853{
854 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
855 struct nfs4_client *clp = c->cn_session->se_client;
856
857 spin_lock(&clp->cl_lock);
858 if (!list_empty(&c->cn_persession)) {
859 list_del(&c->cn_persession);
860 free_conn(c);
861 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500862 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -0500863 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400864}
865
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400866static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400867{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400868 struct nfsd4_conn *conn;
869
870 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
871 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400872 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400873 svc_xprt_get(rqstp->rq_xprt);
874 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400875 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400876 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
877 return conn;
878}
879
J. Bruce Fields328ead22010-09-29 16:11:06 -0400880static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
881{
882 conn->cn_session = ses;
883 list_add(&conn->cn_persession, &ses->se_conns);
884}
885
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400886static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
887{
888 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400889
890 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400891 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400892 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400893}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400894
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400895static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400896{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400897 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400898 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400899}
900
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400901static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400902{
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400903 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400904
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400905 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400906 ret = nfsd4_register_conn(conn);
907 if (ret)
908 /* oops; xprt is already down: */
909 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -0400910 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -0400911 /* callback channel may be back up */
912 nfsd4_probe_callback(ses->se_client);
913 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400914}
915
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400916static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400917{
918 u32 dir = NFS4_CDFC4_FORE;
919
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400920 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400921 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400922 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400923}
924
925/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400926static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400927{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400928 struct nfs4_client *clp = s->se_client;
929 struct nfsd4_conn *c;
930
931 spin_lock(&clp->cl_lock);
932 while (!list_empty(&s->se_conns)) {
933 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
934 list_del_init(&c->cn_persession);
935 spin_unlock(&clp->cl_lock);
936
937 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
938 free_conn(c);
939
940 spin_lock(&clp->cl_lock);
941 }
942 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400943}
944
J. Bruce Fields1377b692012-09-11 21:42:40 -0400945static void __free_session(struct nfsd4_session *ses)
946{
947 nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
948 free_session_slots(ses);
949 kfree(ses);
950}
951
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400952static void free_session(struct nfsd4_session *ses)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400953{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400954 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300955
956 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400957 nfsd4_del_conns(ses);
J. Bruce Fields1377b692012-09-11 21:42:40 -0400958 __free_session(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400959}
960
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300961static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan,
962 struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400963{
964 struct nfsd4_session *new;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400965 int numslots, slotsize;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400966 /*
967 * Note decreasing slot size below client's request may
968 * make it difficult for client to function correctly, whereas
969 * decreasing the number of slots will (just?) affect
970 * performance. When short on memory we therefore prefer to
971 * decrease number of slots instead of their size.
972 */
973 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
974 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800975 if (numslots < 1)
976 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400977
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400978 new = __alloc_session(slotsize, numslots);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400979 if (!new) {
Yanchuan Nian74b70dd2012-12-24 18:11:27 +0800980 nfsd4_put_drc_mem(slotsize, numslots);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400981 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400982 }
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300983 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize, nn);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400984 return new;
985}
Andy Adamson557ce262009-08-28 08:45:04 -0400986
Fengguang Wu135ae822012-11-10 07:20:25 -0500987static 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 -0400988{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400989 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +0300990 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400991
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300992 new->se_client = clp;
993 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300994
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400995 INIT_LIST_HEAD(&new->se_conns);
996
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400997 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300998 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400999 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001000 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001001 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001002 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001003 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001004 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001005 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001006 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001007 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001008 spin_unlock(&nn->client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001009
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001010 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001011 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001012 /*
1013 * This is a little silly; with sessions there's no real
1014 * use for the callback address. Use the peer address
1015 * as a reasonable default for now, but consider fixing
1016 * the rpc client not to require an address in the
1017 * future:
1018 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001019 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1020 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001021 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001022}
1023
Benny Halevy9089f1b2010-05-12 00:12:26 +03001024/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001025static struct nfsd4_session *
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001026find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001027{
1028 struct nfsd4_session *elem;
1029 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001030 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001031
1032 dump_sessionid(__func__, sessionid);
1033 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001034 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001035 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001036 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1037 NFS4_MAX_SESSIONID_LEN)) {
1038 return elem;
1039 }
1040 }
1041
1042 dprintk("%s: session not found\n", __func__);
1043 return NULL;
1044}
1045
Benny Halevy9089f1b2010-05-12 00:12:26 +03001046/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001047static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001048unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001049{
1050 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001051 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001052 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001053 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1057static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001058STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001060 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001062 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001063 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return 1;
1065}
1066
1067/*
1068 * XXX Should we use a slab cache ?
1069 * This type of memory management is somewhat inefficient, but we use it
1070 * anyway since SETCLIENTID is not a common operation.
1071 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001072static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 struct nfs4_client *clp;
1075
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001076 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1077 if (clp == NULL)
1078 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001079 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001080 if (clp->cl_name.data == NULL) {
1081 kfree(clp);
1082 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001084 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return clp;
1086}
1087
1088static inline void
1089free_client(struct nfs4_client *clp)
1090{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001091 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001092
1093 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001094 while (!list_empty(&clp->cl_sessions)) {
1095 struct nfsd4_session *ses;
1096 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1097 se_perclnt);
1098 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001099 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1100 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001101 }
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001102 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001104 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 kfree(clp);
1106}
1107
Benny Halevy84d38ac2010-05-12 00:13:16 +03001108/* must be called under the client_lock */
1109static inline void
1110unhash_client_locked(struct nfs4_client *clp)
1111{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001112 struct nfsd4_session *ses;
1113
Benny Halevy84d38ac2010-05-12 00:13:16 +03001114 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001115 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001116 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1117 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001118 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001122destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001124 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001127 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 INIT_LIST_HEAD(&reaplist);
1130 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001131 while (!list_empty(&clp->cl_delegations)) {
1132 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001133 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 list_move(&dp->dl_recall_lru, &reaplist);
1135 }
1136 spin_unlock(&recall_lock);
1137 while (!list_empty(&reaplist)) {
1138 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 unhash_delegation(dp);
1140 }
NeilBrownea1da632005-06-23 22:04:17 -07001141 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001142 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1143 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001145 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001146 if (clp->cl_cb_conn.cb_xprt)
1147 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001148 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001149 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001150 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001151 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001152 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001153 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001154 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001155 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1156 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001157 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001160static void expire_client(struct nfs4_client *clp)
1161{
1162 nfsd4_client_record_remove(clp);
1163 destroy_client(clp);
1164}
1165
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001166static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1167{
1168 memcpy(target->cl_verifier.data, source->data,
1169 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001172static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1175 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1176}
1177
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001178static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001179{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001180 if (source->cr_principal) {
1181 target->cr_principal =
1182 kstrdup(source->cr_principal, GFP_KERNEL);
1183 if (target->cr_principal == NULL)
1184 return -ENOMEM;
1185 } else
1186 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001187 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 target->cr_uid = source->cr_uid;
1189 target->cr_gid = source->cr_gid;
1190 target->cr_group_info = source->cr_group_info;
1191 get_group_info(target->cr_group_info);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001195static long long
1196compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1197{
1198 long long res;
1199
1200 res = o1->len - o2->len;
1201 if (res)
1202 return res;
1203 return (long long)memcmp(o1->data, o2->data, o1->len);
1204}
1205
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001206static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001207{
NeilBrowna55370a2005-06-23 22:03:52 -07001208 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001212same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1213{
1214 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001218same_clid(clientid_t *cl1, clientid_t *cl2)
1219{
1220 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001223static bool groups_equal(struct group_info *g1, struct group_info *g2)
1224{
1225 int i;
1226
1227 if (g1->ngroups != g2->ngroups)
1228 return false;
1229 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001230 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001231 return false;
1232 return true;
1233}
1234
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001235/*
1236 * RFC 3530 language requires clid_inuse be returned when the
1237 * "principal" associated with a requests differs from that previously
1238 * used. We use uid, gid's, and gss principal string as our best
1239 * approximation. We also don't want to allow non-gss use of a client
1240 * established using gss: in theory cr_principal should catch that
1241 * change, but in practice cr_principal can be null even in the gss case
1242 * since gssd doesn't always pass down a principal string.
1243 */
1244static bool is_gss_cred(struct svc_cred *cr)
1245{
1246 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1247 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1248}
1249
1250
Vivek Trivedi5559b502012-07-24 21:18:20 +05301251static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001252same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1253{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001254 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001255 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1256 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001257 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1258 return false;
1259 if (cr1->cr_principal == cr2->cr_principal)
1260 return true;
1261 if (!cr1->cr_principal || !cr2->cr_principal)
1262 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301263 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001266static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001267{
1268 static u32 current_clientid = 1;
1269
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001270 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 clp->cl_clientid.cl_id = current_clientid++;
1272}
1273
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001274static void gen_confirm(struct nfs4_client *clp)
1275{
Chuck Leverab4684d2012-03-02 17:13:50 -05001276 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001277 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Chuck Leverab4684d2012-03-02 17:13:50 -05001279 verf[0] = (__be32)get_seconds();
1280 verf[1] = (__be32)i++;
1281 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001284static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001285{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001286 struct nfs4_stid *ret;
1287
1288 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1289 if (!ret || !ret->sc_type)
1290 return NULL;
1291 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001292}
1293
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001294static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001295{
1296 struct nfs4_stid *s;
1297
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001298 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001299 if (!s)
1300 return NULL;
1301 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001302 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001303 return NULL;
1304}
1305
Jeff Layton2216d442012-11-12 15:00:57 -05001306static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001307 struct svc_rqst *rqstp, nfs4_verifier *verf)
1308{
1309 struct nfs4_client *clp;
1310 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001311 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001312 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001313 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001314
1315 clp = alloc_client(name);
1316 if (clp == NULL)
1317 return NULL;
1318
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001319 INIT_LIST_HEAD(&clp->cl_sessions);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001320 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1321 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001322 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001323 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001324 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001325 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001326 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001327 idr_init(&clp->cl_stateids);
Benny Halevy46583e22010-05-12 00:13:29 +03001328 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001329 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001330 INIT_LIST_HEAD(&clp->cl_idhash);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001331 INIT_LIST_HEAD(&clp->cl_openowners);
1332 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001333 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001334 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001335 spin_lock_init(&clp->cl_lock);
J. Bruce Fields57725152012-11-05 15:10:26 -05001336 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001337 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001338 clear_bit(0, &clp->cl_cb_slot_busy);
1339 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1340 copy_verf(clp, verf);
1341 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001342 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001343 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001344 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001345 return clp;
1346}
1347
NeilBrownfd39ca92005-06-23 22:04:03 -07001348static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001349add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1350{
1351 struct rb_node **new = &(root->rb_node), *parent = NULL;
1352 struct nfs4_client *clp;
1353
1354 while (*new) {
1355 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1356 parent = *new;
1357
1358 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1359 new = &((*new)->rb_left);
1360 else
1361 new = &((*new)->rb_right);
1362 }
1363
1364 rb_link_node(&new_clp->cl_namenode, parent, new);
1365 rb_insert_color(&new_clp->cl_namenode, root);
1366}
1367
1368static struct nfs4_client *
1369find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1370{
1371 long long cmp;
1372 struct rb_node *node = root->rb_node;
1373 struct nfs4_client *clp;
1374
1375 while (node) {
1376 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1377 cmp = compare_blob(&clp->cl_name, name);
1378 if (cmp > 0)
1379 node = node->rb_left;
1380 else if (cmp < 0)
1381 node = node->rb_right;
1382 else
1383 return clp;
1384 }
1385 return NULL;
1386}
1387
1388static void
1389add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001392 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001394 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001395 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001397 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001398 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
NeilBrownfd39ca92005-06-23 22:04:03 -07001401static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402move_to_confirmed(struct nfs4_client *clp)
1403{
1404 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001405 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001408 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001409 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001410 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001411 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 renew_client(clp);
1413}
1414
1415static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001416find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct nfs4_client *clp;
1419 unsigned int idhashval = clientid_hashval(clid->cl_id);
1420
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001421 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001422 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001423 if ((bool)clp->cl_minorversion != sessions)
1424 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001425 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 return NULL;
1430}
1431
1432static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001433find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1434{
1435 struct list_head *tbl = nn->conf_id_hashtbl;
1436
1437 return find_client_in_id_table(tbl, clid, sessions);
1438}
1439
1440static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001441find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001443 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001445 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001448static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001449{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001450 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001451}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001452
NeilBrown28ce6052005-06-23 22:03:56 -07001453static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001454find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001455{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001456 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001457}
1458
1459static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001460find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001461{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001462 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001463}
1464
NeilBrownfd39ca92005-06-23 22:04:03 -07001465static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001466gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001468 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001469 struct sockaddr *sa = svc_addr(rqstp);
1470 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001471 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Jeff Layton7077ecb2009-08-14 12:57:58 -04001473 /* Currently, we only support tcp and tcp6 for the callback channel */
1474 if (se->se_callback_netid_len == 3 &&
1475 !memcmp(se->se_callback_netid_val, "tcp", 3))
1476 expected_family = AF_INET;
1477 else if (se->se_callback_netid_len == 4 &&
1478 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1479 expected_family = AF_INET6;
1480 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto out_err;
1482
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001483 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001484 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001485 (struct sockaddr *)&conn->cb_addr,
1486 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001487
J. Bruce Fields07263f12010-05-31 19:09:40 -04001488 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001490
J. Bruce Fields07263f12010-05-31 19:09:40 -04001491 if (conn->cb_addr.ss_family == AF_INET6)
1492 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001493
J. Bruce Fields07263f12010-05-31 19:09:40 -04001494 conn->cb_prog = se->se_callback_prog;
1495 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001496 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return;
1498out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001499 conn->cb_addr.ss_family = AF_UNSPEC;
1500 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001501 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 "will not receive delegations\n",
1503 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return;
1506}
1507
Andy Adamson074fe892009-04-03 08:28:15 +03001508/*
Andy Adamson557ce262009-08-28 08:45:04 -04001509 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001510 */
1511void
1512nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1513{
Andy Adamson557ce262009-08-28 08:45:04 -04001514 struct nfsd4_slot *slot = resp->cstate.slot;
1515 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001516
Andy Adamson557ce262009-08-28 08:45:04 -04001517 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001518
Andy Adamson557ce262009-08-28 08:45:04 -04001519 slot->sl_opcnt = resp->opcnt;
1520 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001521
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001522 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001523 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001524 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001525 return;
1526 }
Andy Adamson557ce262009-08-28 08:45:04 -04001527 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1528 base = (char *)resp->cstate.datap -
1529 (char *)resp->xbuf->head[0].iov_base;
1530 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1531 slot->sl_datalen))
1532 WARN("%s: sessions DRC could not cache compound\n", __func__);
1533 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001534}
1535
1536/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001537 * Encode the replay sequence operation from the slot values.
1538 * If cachethis is FALSE encode the uncached rep error on the next
1539 * operation which sets resp->p and increments resp->opcnt for
1540 * nfs4svc_encode_compoundres.
1541 *
Andy Adamson074fe892009-04-03 08:28:15 +03001542 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001543static __be32
1544nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1545 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001546{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001547 struct nfsd4_op *op;
1548 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001549
Andy Adamsonabfabf82009-07-23 19:02:18 -04001550 /* Encode the replayed sequence operation */
1551 op = &args->ops[resp->opcnt - 1];
1552 nfsd4_encode_operation(resp, op);
1553
1554 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001555 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001556 op = &args->ops[resp->opcnt++];
1557 op->status = nfserr_retry_uncached_rep;
1558 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001559 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001560 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001561}
1562
1563/*
Andy Adamson557ce262009-08-28 08:45:04 -04001564 * The sequence operation is not cached because we can use the slot and
1565 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001566 */
1567__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001568nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1569 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001570{
Andy Adamson557ce262009-08-28 08:45:04 -04001571 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001572 __be32 status;
1573
Andy Adamson557ce262009-08-28 08:45:04 -04001574 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001575
Andy Adamsonabfabf82009-07-23 19:02:18 -04001576 /* Either returns 0 or nfserr_retry_uncached */
1577 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1578 if (status == nfserr_retry_uncached_rep)
1579 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001580
Andy Adamson557ce262009-08-28 08:45:04 -04001581 /* The sequence operation has been encoded, cstate->datap set. */
1582 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001583
Andy Adamson557ce262009-08-28 08:45:04 -04001584 resp->opcnt = slot->sl_opcnt;
1585 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1586 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001587
1588 return status;
1589}
1590
Andy Adamson0733d212009-04-03 08:28:01 +03001591/*
1592 * Set the exchange_id flags returned by the server.
1593 */
1594static void
1595nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1596{
1597 /* pNFS is not supported */
1598 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1599
1600 /* Referrals are supported, Migration is not. */
1601 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1602
1603 /* set the wire flags to return to client. */
1604 clid->flags = new->cl_exchange_flags;
1605}
1606
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001607static bool client_has_state(struct nfs4_client *clp)
1608{
1609 /*
1610 * Note clp->cl_openowners check isn't quite right: there's no
1611 * need to count owners without stateid's.
1612 *
1613 * Also note we should probably be using this in 4.0 case too.
1614 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001615 return !list_empty(&clp->cl_openowners)
1616 || !list_empty(&clp->cl_delegations)
1617 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001618}
1619
Al Virob37ad282006-10-19 23:28:59 -07001620__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001621nfsd4_exchange_id(struct svc_rqst *rqstp,
1622 struct nfsd4_compound_state *cstate,
1623 struct nfsd4_exchange_id *exid)
1624{
Andy Adamson0733d212009-04-03 08:28:01 +03001625 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001626 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001627 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001628 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001629 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001630 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001631 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001632
Jeff Layton363168b2009-08-14 12:57:56 -04001633 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001634 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001635 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001636 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001637 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001638
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001639 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001640 return nfserr_inval;
1641
1642 /* Currently only support SP4_NONE */
1643 switch (exid->spa_how) {
1644 case SP4_NONE:
1645 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001646 default: /* checked by xdr code */
1647 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001648 case SP4_SSV:
Andy Adamson0733d212009-04-03 08:28:01 +03001649 case SP4_MACH_CRED:
1650 return nfserr_serverfault; /* no excuse :-/ */
1651 }
1652
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001653 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001654 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001655 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001656 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001657 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1658 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1659
J. Bruce Fields136e6582012-05-12 20:37:23 -04001660 if (update) {
1661 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001662 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001663 goto out;
1664 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001665 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001666 status = nfserr_perm;
1667 goto out;
1668 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001669 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001670 status = nfserr_not_same;
1671 goto out;
1672 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001673 /* case 6 */
1674 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1675 new = conf;
1676 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001677 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001678 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001679 if (client_has_state(conf)) {
1680 status = nfserr_clid_inuse;
1681 goto out;
1682 }
Andy Adamson0733d212009-04-03 08:28:01 +03001683 expire_client(conf);
1684 goto out_new;
1685 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001686 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001687 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001688 new = conf;
1689 goto out_copy;
1690 }
1691 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001692 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001693 }
1694
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001695 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001696 status = nfserr_noent;
1697 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001698 }
1699
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001700 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001701 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001702 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001703
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001704 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001705out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001706 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001707 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001708 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001709 goto out;
1710 }
J. Bruce Fieldsc116a0a2012-09-11 14:53:09 -04001711 new->cl_minorversion = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001712
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001713 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001714 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001715out_copy:
1716 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1717 exid->clientid.cl_id = new->cl_clientid.cl_id;
1718
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001719 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001720 nfsd4_set_ex_flags(new, exid);
1721
1722 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001723 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001724 status = nfs_ok;
1725
1726out:
1727 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001728 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001729}
1730
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001731static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001732check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001733{
Andy Adamson88e588d2009-07-23 19:02:15 -04001734 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1735 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001736
1737 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001738 if (slot_inuse) {
1739 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001740 return nfserr_jukebox;
1741 else
1742 return nfserr_seq_misordered;
1743 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001744 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001745 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001746 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001747 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001748 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001749 return nfserr_seq_misordered;
1750}
1751
Andy Adamson49557cc2009-07-23 19:02:16 -04001752/*
1753 * Cache the create session result into the create session single DRC
1754 * slot cache by saving the xdr structure. sl_seqid has been set.
1755 * Do this for solo or embedded create session operations.
1756 */
1757static void
1758nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001759 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001760{
1761 slot->sl_status = nfserr;
1762 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1763}
1764
1765static __be32
1766nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1767 struct nfsd4_clid_slot *slot)
1768{
1769 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1770 return slot->sl_status;
1771}
1772
Mi Jinlong1b74c252011-07-14 14:50:17 +08001773#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1774 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1775 1 + /* MIN tag is length with zero, only length */ \
1776 3 + /* version, opcount, opcode */ \
1777 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1778 /* seqid, slotID, slotID, cache */ \
1779 4 ) * sizeof(__be32))
1780
1781#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1782 2 + /* verifier: AUTH_NULL, length 0 */\
1783 1 + /* status */ \
1784 1 + /* MIN tag is length with zero, only length */ \
1785 3 + /* opcount, opcode, opstatus*/ \
1786 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1787 /* seqid, slotID, slotID, slotID, status */ \
1788 5 ) * sizeof(__be32))
1789
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001790static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001791{
1792 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1793 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1794}
1795
Andy Adamson069b6ad2009-04-03 08:27:58 +03001796__be32
1797nfsd4_create_session(struct svc_rqst *rqstp,
1798 struct nfsd4_compound_state *cstate,
1799 struct nfsd4_create_session *cr_ses)
1800{
Jeff Layton363168b2009-08-14 12:57:56 -04001801 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001802 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001803 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001804 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001805 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001806 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001807 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001808
Mi Jinlonga62573d2011-03-23 17:57:07 +08001809 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1810 return nfserr_inval;
J. Bruce Fields49730502012-09-11 15:33:21 -04001811 if (check_forechannel_attrs(cr_ses->fore_channel))
1812 return nfserr_toosmall;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001813 new = alloc_session(&cr_ses->fore_channel, nn);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001814 if (!new)
1815 return nfserr_jukebox;
1816 status = nfserr_jukebox;
1817 conn = alloc_conn_from_crses(rqstp, cr_ses);
1818 if (!conn)
1819 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08001820
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001821 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001822 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001823 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04001824 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001825
1826 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001827 cs_slot = &conf->cl_cs_slot;
1828 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001829 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001830 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001831 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001832 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001833 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001834 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001835 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001836 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001837 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001838 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001839 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001840 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001841 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001842 }
Andy Adamson49557cc2009-07-23 19:02:16 -04001843 cs_slot = &unconf->cl_cs_slot;
1844 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001845 if (status) {
1846 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001847 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001848 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001849 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001850 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001851 if (old) {
1852 status = mark_client_expired(old);
1853 if (status)
1854 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001855 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001856 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001857 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001858 conf = unconf;
1859 } else {
1860 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001861 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001862 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001863 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001864 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001865 * We do not support RDMA or persistent sessions
1866 */
1867 cr_ses->flags &= ~SESSION4_PERSIST;
1868 cr_ses->flags &= ~SESSION4_RDMA;
1869
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001870 init_session(rqstp, new, conf, cr_ses);
1871 nfsd4_init_conn(rqstp, conn, new);
1872
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001873 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001874 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001875 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1876 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001877 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001878 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001879
Andy Adamson49557cc2009-07-23 19:02:16 -04001880 /* cache solo and embedded create sessions under the state lock */
1881 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001882 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001883 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001884out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08001885 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001886 free_conn(conn);
1887out_free_session:
1888 __free_session(new);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04001889 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001890}
1891
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001892static __be32 nfsd4_map_bcts_dir(u32 *dir)
1893{
1894 switch (*dir) {
1895 case NFS4_CDFC4_FORE:
1896 case NFS4_CDFC4_BACK:
1897 return nfs_ok;
1898 case NFS4_CDFC4_FORE_OR_BOTH:
1899 case NFS4_CDFC4_BACK_OR_BOTH:
1900 *dir = NFS4_CDFC4_BOTH;
1901 return nfs_ok;
1902 };
1903 return nfserr_inval;
1904}
1905
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001906__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1907{
1908 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001909 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001910
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001911 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001912 session->se_cb_prog = bc->bc_cb_program;
1913 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001914 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001915
1916 nfsd4_probe_callback(session->se_client);
1917
1918 return nfs_ok;
1919}
1920
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001921__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1922 struct nfsd4_compound_state *cstate,
1923 struct nfsd4_bind_conn_to_session *bcts)
1924{
1925 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001926 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001927 struct nfsd4_session *session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001928 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001929
1930 if (!nfsd4_last_compound_op(rqstp))
1931 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001932 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001933 spin_lock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001934 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001935 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001936 status = nfserr_badsession;
1937 if (!session)
1938 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001939 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001940 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001941 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001942 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001943 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001944 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001945 goto out;
1946 nfsd4_init_conn(rqstp, conn, session);
1947 status = nfs_ok;
1948out:
1949 nfs4_unlock_state();
1950 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001951}
1952
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001953static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1954{
1955 if (!session)
1956 return 0;
1957 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1958}
1959
Andy Adamson069b6ad2009-04-03 08:27:58 +03001960__be32
1961nfsd4_destroy_session(struct svc_rqst *r,
1962 struct nfsd4_compound_state *cstate,
1963 struct nfsd4_destroy_session *sessionid)
1964{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001965 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001966 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001967 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001968
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001969 nfs4_lock_state();
1970 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001971 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001972 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001973 goto out;
J. Bruce Fields57716352010-04-21 12:27:19 -04001974 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001975 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001976 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001977 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001978 status = nfserr_badsession;
1979 if (!ses)
1980 goto out_client_lock;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001981 status = mark_session_dead_locked(ses);
1982 if (status)
1983 goto out_client_lock;
Benny Halevye10e0cf2009-04-03 08:28:38 +03001984 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001985 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001986
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001987 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001988
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001989 spin_lock(&nn->client_lock);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001990 free_session(ses);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001991 status = nfs_ok;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001992out_client_lock:
1993 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001994out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001995 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03001996 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001997}
1998
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001999static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002000{
2001 struct nfsd4_conn *c;
2002
2003 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002004 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002005 return c;
2006 }
2007 }
2008 return NULL;
2009}
2010
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002011static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002012{
2013 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002014 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002015 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002016
2017 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002018 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002019 if (c) {
2020 spin_unlock(&clp->cl_lock);
2021 free_conn(new);
2022 return;
2023 }
2024 __nfsd4_hash_conn(new, ses);
2025 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002026 ret = nfsd4_register_conn(new);
2027 if (ret)
2028 /* oops; xprt is already down: */
2029 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002030 return;
2031}
2032
Mi Jinlong868b89c2011-04-27 09:09:58 +08002033static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2034{
2035 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2036
2037 return args->opcnt > session->se_fchannel.maxops;
2038}
2039
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002040static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2041 struct nfsd4_session *session)
2042{
2043 struct xdr_buf *xb = &rqstp->rq_arg;
2044
2045 return xb->len > session->se_fchannel.maxreq_sz;
2046}
2047
Andy Adamson069b6ad2009-04-03 08:27:58 +03002048__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002049nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002050 struct nfsd4_compound_state *cstate,
2051 struct nfsd4_sequence *seq)
2052{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002053 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002054 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002055 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002056 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002057 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002058 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002059 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002060
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002061 if (resp->opcnt != 1)
2062 return nfserr_sequence_pos;
2063
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002064 /*
2065 * Will be either used or freed by nfsd4_sequence_check_conn
2066 * below.
2067 */
2068 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2069 if (!conn)
2070 return nfserr_jukebox;
2071
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002072 spin_lock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002073 status = nfserr_badsession;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002074 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
Benny Halevyb85d4c02009-04-03 08:28:08 +03002075 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002076 goto out_no_session;
2077 clp = session->se_client;
2078 status = get_client_locked(clp);
2079 if (status)
2080 goto out_no_session;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002081 status = nfsd4_get_session_locked(session);
2082 if (status)
2083 goto out_put_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002084
Mi Jinlong868b89c2011-04-27 09:09:58 +08002085 status = nfserr_too_many_ops;
2086 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002087 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002088
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002089 status = nfserr_req_too_big;
2090 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002091 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002092
Benny Halevyb85d4c02009-04-03 08:28:08 +03002093 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002094 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002095 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002096
Andy Adamson557ce262009-08-28 08:45:04 -04002097 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002098 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2099
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002100 /* We do not negotiate the number of slots yet, so set the
2101 * maxslots to the session maxreqs which is used to encode
2102 * sr_highest_slotid and the sr_target_slot id to maxslots */
2103 seq->maxslots = session->se_fchannel.maxreqs;
2104
J. Bruce Fields73e79482012-02-13 16:39:00 -05002105 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2106 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002107 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002108 status = nfserr_seq_misordered;
2109 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002110 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002111 cstate->slot = slot;
2112 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002113 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002114 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002115 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002116 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002117 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002118 }
2119 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002120 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002121
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002122 nfsd4_sequence_check_conn(conn, session);
2123 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002124
Benny Halevyb85d4c02009-04-03 08:28:08 +03002125 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002126 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002127 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002128 if (seq->cachethis)
2129 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002130 else
2131 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002132
2133 cstate->slot = slot;
2134 cstate->session = session;
2135
Benny Halevyb85d4c02009-04-03 08:28:08 +03002136out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002137 switch (clp->cl_cb_state) {
2138 case NFSD4_CB_DOWN:
2139 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2140 break;
2141 case NFSD4_CB_FAULT:
2142 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2143 break;
2144 default:
2145 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002146 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002147out_no_session:
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002148 kfree(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002149 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002150 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002151out_put_session:
2152 nfsd4_put_session(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002153out_put_client:
2154 put_client_renew_locked(clp);
2155 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002156}
2157
Mi Jinlong345c2842011-10-20 17:51:39 +08002158__be32
2159nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2160{
2161 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002162 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002163 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002164
2165 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002166 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002167 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002168 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002169
2170 if (conf) {
2171 clp = conf;
2172
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002173 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002174 status = nfserr_clientid_busy;
2175 goto out;
2176 }
2177 } else if (unconf)
2178 clp = unconf;
2179 else {
2180 status = nfserr_stale_clientid;
2181 goto out;
2182 }
2183
2184 expire_client(clp);
2185out:
2186 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002187 return status;
2188}
2189
Andy Adamson069b6ad2009-04-03 08:27:58 +03002190__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002191nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2192{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002193 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002194
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002195 if (rc->rca_one_fs) {
2196 if (!cstate->current_fh.fh_dentry)
2197 return nfserr_nofilehandle;
2198 /*
2199 * We don't take advantage of the rca_one_fs case.
2200 * That's OK, it's optional, we can safely ignore it.
2201 */
2202 return nfs_ok;
2203 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002204
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002205 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002206 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002207 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2208 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002209 goto out;
2210
2211 status = nfserr_stale_clientid;
2212 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002213 /*
2214 * The following error isn't really legal.
2215 * But we only get here if the client just explicitly
2216 * destroyed the client. Surely it no longer cares what
2217 * error it gets back on an operation for the dead
2218 * client.
2219 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002220 goto out;
2221
2222 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002223 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002224out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002225 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002226 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002227}
2228
2229__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002230nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2231 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002233 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002235 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002236 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002237 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2238
J. Bruce Fields63db4632012-05-18 21:54:19 -04002239 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002241 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002242 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002243 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002245 if (clp_used_exchangeid(conf))
2246 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002247 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002248 char addr_str[INET6_ADDRSTRLEN];
2249 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2250 sizeof(addr_str));
2251 dprintk("NFSD: setclientid: string in use by client "
2252 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 goto out;
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002256 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002257 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002260 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002261 if (new == NULL)
2262 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002263 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002264 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002266 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002267 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002268 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002269 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002270 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2272 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2273 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2274 status = nfs_ok;
2275out:
2276 nfs4_unlock_state();
2277 return status;
2278}
2279
2280
Al Virob37ad282006-10-19 23:28:59 -07002281__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002282nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2283 struct nfsd4_compound_state *cstate,
2284 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
NeilBrown21ab45a2005-06-23 22:04:14 -07002286 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2288 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002289 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002290 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002292 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002295
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002296 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002297 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002298 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002299 * We try hard to give out unique clientid's, so if we get an
2300 * attempt to confirm the same clientid with a different cred,
2301 * there's a bug somewhere. Let's charitably assume it's our
2302 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002303 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002304 status = nfserr_serverfault;
2305 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2306 goto out;
2307 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2308 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002309 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002310 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2311 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002313 else /* case 4: client hasn't noticed we rebooted yet? */
2314 status = nfserr_stale_clientid;
2315 goto out;
2316 }
2317 status = nfs_ok;
2318 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002319 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2320 nfsd4_probe_callback(conf);
2321 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002322 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002323 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002324 if (conf) {
2325 status = mark_client_expired(conf);
2326 if (status)
2327 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002328 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002329 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002330 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002331 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 nfs4_unlock_state();
2335 return status;
2336}
2337
J. Bruce Fields32513b42011-10-13 16:00:16 -04002338static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002340 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2341}
2342
2343/* OPEN Share state helper functions */
2344static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 unsigned int hashval = file_hashval(ino);
2347
J. Bruce Fields32513b42011-10-13 16:00:16 -04002348 atomic_set(&fp->fi_ref, 1);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002349 INIT_LIST_HEAD(&fp->fi_stateids);
2350 INIT_LIST_HEAD(&fp->fi_delegations);
2351 fp->fi_inode = igrab(ino);
2352 fp->fi_had_conflict = false;
2353 fp->fi_lease = NULL;
2354 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2355 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2356 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002357 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002358 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
2361static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002362nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002363{
NeilBrowne60d4392005-06-23 22:03:01 -07002364 if (*slab == NULL)
2365 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002366 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002367 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002368}
2369
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002370void
NeilBrowne60d4392005-06-23 22:03:01 -07002371nfsd4_free_slabs(void)
2372{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002373 nfsd4_free_slab(&openowner_slab);
2374 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002375 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002376 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002377 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Bryan Schumaker72083392011-11-01 15:24:59 -04002380int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381nfsd4_init_slabs(void)
2382{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002383 openowner_slab = kmem_cache_create("nfsd4_openowners",
2384 sizeof(struct nfs4_openowner), 0, 0, NULL);
2385 if (openowner_slab == NULL)
2386 goto out_nomem;
2387 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002388 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002389 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002390 goto out_nomem;
2391 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002392 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002393 if (file_slab == NULL)
2394 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002395 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002396 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002397 if (stateid_slab == NULL)
2398 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002399 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002400 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002401 if (deleg_slab == NULL)
2402 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002404out_nomem:
2405 nfsd4_free_slabs();
2406 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2407 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002410void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002412 kfree(oo->oo_owner.so_owner.data);
2413 kmem_cache_free(openowner_slab, oo);
2414}
2415
2416void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2417{
2418 kfree(lo->lo_owner.so_owner.data);
2419 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002422static void init_nfs4_replay(struct nfs4_replay *rp)
2423{
2424 rp->rp_status = nfserr_serverfault;
2425 rp->rp_buflen = 0;
2426 rp->rp_buf = rp->rp_ibuf;
2427}
2428
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002429static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 struct nfs4_stateowner *sop;
2432
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002433 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002434 if (!sop)
2435 return NULL;
2436
2437 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2438 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002439 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002440 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002442 sop->so_owner.len = owner->len;
2443
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002444 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002445 sop->so_client = clp;
2446 init_nfs4_replay(&sop->so_replay);
2447 return sop;
2448}
2449
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002450static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002451{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002452 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2453
2454 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002455 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002458static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002460 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002462 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2463 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002465 oo->oo_owner.so_is_open_owner = 1;
2466 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002467 oo->oo_flags = NFS4_OO_NEW;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002468 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002469 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002470 INIT_LIST_HEAD(&oo->oo_close_lru);
2471 hash_openowner(oo, clp, strhashval);
2472 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
J. Bruce Fields996e0932011-10-17 11:14:48 -04002475static 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 -04002476 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002478 stp->st_stid.sc_type = NFS4_OPEN_STID;
NeilBrownea1da632005-06-23 22:04:17 -07002479 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002480 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002481 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002482 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002483 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 stp->st_access_bmap = 0;
2486 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002487 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002488 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002489 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002493move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002495 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2496
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002497 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002499 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002500 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002504same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2505 clientid_t *clid)
2506{
2507 return (sop->so_owner.len == owner->len) &&
2508 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2509 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002512static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002513find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2514 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002516 struct nfs4_stateowner *so;
2517 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002518 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002520 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002521 if (!so->so_is_open_owner)
2522 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002523 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2524 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002525 clp = oo->oo_owner.so_client;
2526 if ((bool)clp->cl_minorversion != sessions)
2527 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002528 renew_client(oo->oo_owner.so_client);
2529 return oo;
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
2532 return NULL;
2533}
2534
2535/* search file_hashtbl[] for file */
2536static struct nfs4_file *
2537find_file(struct inode *ino)
2538{
2539 unsigned int hashval = file_hashval(ino);
2540 struct nfs4_file *fp;
2541
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002542 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002543 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002544 if (fp->fi_inode == ino) {
2545 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002546 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002550 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 return NULL;
2552}
2553
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002554/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 * Called to check deny when READ with all zero stateid or
2556 * WRITE with all zero or all one stateid
2557 */
Al Virob37ad282006-10-19 23:28:59 -07002558static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2560{
2561 struct inode *ino = current_fh->fh_dentry->d_inode;
2562 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002563 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002564 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002567 if (!fp)
2568 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002569 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002571 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002572 if (test_deny(deny_type, stp) ||
2573 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
NeilBrown13cd2182005-06-23 22:03:10 -07002576 ret = nfs_ok;
2577out:
2578 put_nfs4_file(fp);
2579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002582static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002584 struct nfs4_client *clp = dp->dl_stid.sc_client;
2585 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 /* We're assuming the state code never drops its reference
2588 * without first removing the lease. Since we're in this lease
2589 * callback (and since the lease code is serialized by the kernel
2590 * lock) we know the server hasn't removed the lease yet, we know
2591 * it's safe to take a reference: */
2592 atomic_inc(&dp->dl_count);
2593
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002594 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Arnd Bergmann460781b2010-11-17 16:26:56 +01002596 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 dp->dl_time = get_seconds();
2598
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002599 nfsd4_cb_recall(dp);
2600}
2601
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002602/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002603static void nfsd_break_deleg_cb(struct file_lock *fl)
2604{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002605 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2606 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002607
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002608 if (!fp) {
2609 WARN(1, "(%p)->fl_owner NULL\n", fl);
2610 return;
2611 }
2612 if (fp->fi_had_conflict) {
2613 WARN(1, "duplicate break on %p\n", fp);
2614 return;
2615 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002616 /*
2617 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002618 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002619 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002620 */
2621 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002623 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002624 fp->fi_had_conflict = true;
2625 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2626 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002627 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630static
2631int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2632{
2633 if (arg & F_UNLCK)
2634 return lease_modify(onlist, arg);
2635 else
2636 return -EAGAIN;
2637}
2638
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002639static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002640 .lm_break = nfsd_break_deleg_cb,
2641 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642};
2643
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002644static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2645{
2646 if (nfsd4_has_session(cstate))
2647 return nfs_ok;
2648 if (seqid == so->so_seqid - 1)
2649 return nfserr_replay_me;
2650 if (seqid == so->so_seqid)
2651 return nfs_ok;
2652 return nfserr_bad_seqid;
2653}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Al Virob37ad282006-10-19 23:28:59 -07002655__be32
Andy Adamson66689582009-04-03 08:28:45 +03002656nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03002657 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 clientid_t *clientid = &open->op_clientid;
2660 struct nfs4_client *clp = NULL;
2661 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002662 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002663 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002665 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002667 /*
2668 * In case we need it later, after we've already created the
2669 * file and don't want to risk a further failure:
2670 */
2671 open->op_file = nfsd4_alloc_file();
2672 if (open->op_file == NULL)
2673 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002675 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002676 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002677 open->op_openowner = oo;
2678 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002679 clp = find_confirmed_client(clientid, cstate->minorversion,
2680 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002682 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002683 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002685 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002686 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002687 clp = oo->oo_owner.so_client;
2688 release_openowner(oo);
2689 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002690 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002691 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002692 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2693 if (status)
2694 return status;
2695 clp = oo->oo_owner.so_client;
2696 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002697new_owner:
2698 oo = alloc_init_open_stateowner(strhashval, clp, open);
2699 if (oo == NULL)
2700 return nfserr_jukebox;
2701 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002702alloc_stateid:
2703 open->op_stp = nfs4_alloc_stateid(clp);
2704 if (!open->op_stp)
2705 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002706 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
Al Virob37ad282006-10-19 23:28:59 -07002709static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002710nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2711{
2712 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2713 return nfserr_openmode;
2714 else
2715 return nfs_ok;
2716}
2717
Daniel Mackc47d8322011-05-16 16:38:14 +02002718static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002719{
J. Bruce Fields24a01112010-05-18 20:01:35 -04002720 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2721}
2722
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002723static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002724{
2725 struct nfs4_stid *ret;
2726
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002727 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002728 if (!ret)
2729 return NULL;
2730 return delegstateid(ret);
2731}
2732
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002733static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2734{
2735 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2736 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2737}
2738
Al Virob37ad282006-10-19 23:28:59 -07002739static __be32
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002740nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07002741 struct nfs4_delegation **dp)
2742{
2743 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002744 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002745
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002746 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002747 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002748 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002749 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002750 status = nfs4_check_delegmode(*dp, flags);
2751 if (status)
2752 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002753out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002754 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07002755 return nfs_ok;
2756 if (status)
2757 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002758 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002759 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002760}
2761
Al Virob37ad282006-10-19 23:28:59 -07002762static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002763nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002765 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002766 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
NeilBrown8beefa22005-06-23 22:03:08 -07002768 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 /* ignore lock owners */
2770 if (local->st_stateowner->so_is_open_owner == 0)
2771 continue;
2772 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002773 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 *stpp = local;
2775 /* check for conflicting share reservations */
2776 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002777 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002779 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002782static inline int nfs4_access_to_access(u32 nfs4_access)
2783{
2784 int flags = 0;
2785
2786 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2787 flags |= NFSD_MAY_READ;
2788 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2789 flags |= NFSD_MAY_WRITE;
2790 return flags;
2791}
2792
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002793static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2794 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002795{
2796 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002797 int oflag = nfs4_access_to_omode(open->op_share_access);
2798 int access = nfs4_access_to_access(open->op_share_access);
2799
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002800 if (!fp->fi_fds[oflag]) {
2801 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2802 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002803 if (status)
2804 return status;
2805 }
2806 nfs4_file_get_access(fp, oflag);
2807
2808 return nfs_ok;
2809}
2810
Al Virob37ad282006-10-19 23:28:59 -07002811static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2813 struct nfsd4_open *open)
2814{
2815 struct iattr iattr = {
2816 .ia_valid = ATTR_SIZE,
2817 .ia_size = 0,
2818 };
2819 if (!open->op_truncate)
2820 return 0;
2821 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002822 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2824}
2825
Al Virob37ad282006-10-19 23:28:59 -07002826static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002827nfs4_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 -07002828{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002829 u32 op_share_access = open->op_share_access;
J. Bruce Fields7d947842010-08-20 18:09:31 -04002830 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002831 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Jeff Layton82c5ff12012-05-11 09:45:13 -04002833 new_access = !test_access(op_share_access, stp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002834 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002835 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002836 if (status)
2837 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 status = nfsd4_truncate(rqstp, cur_fh, open);
2840 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002841 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002842 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002843 nfs4_file_put_access(fp, oflag);
2844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return status;
2846 }
2847 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04002848 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002849 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 return nfs_ok;
2852}
2853
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05002856nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002858 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002861/* Should we give out recallable state?: */
2862static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2863{
2864 if (clp->cl_cb_state == NFSD4_CB_UP)
2865 return true;
2866 /*
2867 * In the sessions case, since we don't have to establish a
2868 * separate connection for callbacks, we assume it's OK
2869 * until we hear otherwise:
2870 */
2871 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2872}
2873
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002874static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2875{
2876 struct file_lock *fl;
2877
2878 fl = locks_alloc_lock();
2879 if (!fl)
2880 return NULL;
2881 locks_init_lock(fl);
2882 fl->fl_lmops = &nfsd_lease_mng_ops;
2883 fl->fl_flags = FL_LEASE;
2884 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2885 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002886 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002887 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002888 return fl;
2889}
2890
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002891static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2892{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002893 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002894 struct file_lock *fl;
2895 int status;
2896
2897 fl = nfs4_alloc_init_lease(dp, flag);
2898 if (!fl)
2899 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002900 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002901 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002902 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002903 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002904 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002905 locks_free_lock(fl);
2906 return -ENOMEM;
2907 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002908 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04002909 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002910 atomic_set(&fp->fi_delegees, 1);
2911 list_add(&dp->dl_perfile, &fp->fi_delegations);
2912 return 0;
2913}
2914
2915static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2916{
2917 struct nfs4_file *fp = dp->dl_file;
2918
2919 if (!fp->fi_lease)
2920 return nfs4_setlease(dp, flag);
2921 spin_lock(&recall_lock);
2922 if (fp->fi_had_conflict) {
2923 spin_unlock(&recall_lock);
2924 return -EAGAIN;
2925 }
2926 atomic_inc(&fp->fi_delegees);
2927 list_add(&dp->dl_perfile, &fp->fi_delegations);
2928 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002929 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002930 return 0;
2931}
2932
Benny Halevy4aa89132012-02-21 14:16:44 -08002933static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2934{
2935 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2936 if (status == -EAGAIN)
2937 open->op_why_no_deleg = WND4_CONTENTION;
2938 else {
2939 open->op_why_no_deleg = WND4_RESOURCE;
2940 switch (open->op_deleg_want) {
2941 case NFS4_SHARE_WANT_READ_DELEG:
2942 case NFS4_SHARE_WANT_WRITE_DELEG:
2943 case NFS4_SHARE_WANT_ANY_DELEG:
2944 break;
2945 case NFS4_SHARE_WANT_CANCEL:
2946 open->op_why_no_deleg = WND4_CANCELLED;
2947 break;
2948 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002949 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08002950 }
2951 }
2952}
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954/*
2955 * Attempt to hand out a delegation.
2956 */
2957static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002958nfs4_open_delegation(struct net *net, struct svc_fh *fh,
2959 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
2961 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002962 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002963 int cb_up;
Benny Halevyd24433c2012-02-16 20:57:17 +02002964 int status = 0, flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002966 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002968 open->op_recall = 0;
2969 switch (open->op_claim_type) {
2970 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002971 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002972 open->op_recall = 1;
2973 flag = open->op_delegate_type;
2974 if (flag == NFS4_OPEN_DELEGATE_NONE)
2975 goto out;
2976 break;
2977 case NFS4_OPEN_CLAIM_NULL:
2978 /* Let's not give out any delegations till everyone's
2979 * had the chance to reclaim theirs.... */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002980 if (locks_in_grace(net))
NeilBrown7b190fe2005-06-23 22:03:23 -07002981 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002982 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002983 goto out;
2984 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2985 flag = NFS4_OPEN_DELEGATE_WRITE;
2986 else
2987 flag = NFS4_OPEN_DELEGATE_READ;
2988 break;
2989 default:
2990 goto out;
2991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002993 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002994 if (dp == NULL)
2995 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002996 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002997 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002998 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003000 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003002 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003003 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004out:
3005 open->op_delegate_type = flag;
Benny Halevyd24433c2012-02-16 20:57:17 +02003006 if (flag == NFS4_OPEN_DELEGATE_NONE) {
3007 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3008 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
3009 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3010
Benny Halevy4aa89132012-02-21 14:16:44 -08003011 /* 4.1 client asking for a delegation? */
3012 if (open->op_deleg_want)
3013 nfsd4_open_deleg_none_ext(open, status);
Benny Halevyd24433c2012-02-16 20:57:17 +02003014 }
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003015 return;
3016out_free:
J. Bruce Fields24ffb932012-12-12 15:24:12 -05003017 unhash_stid(&dp->dl_stid);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003018 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003019out_no_deleg:
3020 flag = NFS4_OPEN_DELEGATE_NONE;
3021 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Benny Halevye27f49c2012-02-21 14:16:54 -08003024static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3025 struct nfs4_delegation *dp)
3026{
3027 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3028 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3029 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3030 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3031 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3032 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3033 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3034 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3035 }
3036 /* Otherwise the client must be confused wanting a delegation
3037 * it already has, therefore we don't return
3038 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3039 */
3040}
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042/*
3043 * called with nfs4_lock_state() held.
3044 */
Al Virob37ad282006-10-19 23:28:59 -07003045__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3047{
Andy Adamson66689582009-04-03 08:28:45 +03003048 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003049 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 struct nfs4_file *fp = NULL;
3051 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003052 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003053 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003054 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 /*
3057 * Lookup file; if found, lookup stateid and check open request,
3058 * and check for delegations in the process of being recalled.
3059 * If not found, create the nfs4_file struct
3060 */
3061 fp = find_file(ino);
3062 if (fp) {
3063 if ((status = nfs4_check_open(fp, open, &stp)))
3064 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003065 status = nfs4_check_deleg(cl, fp, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003066 if (status)
3067 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07003069 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003070 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003071 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003072 status = nfserr_jukebox;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003073 fp = open->op_file;
3074 open->op_file = NULL;
3075 nfsd4_init_file(fp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
3077
3078 /*
3079 * OPEN the file, or upgrade an existing OPEN.
3080 * If truncate fails, the OPEN fails.
3081 */
3082 if (stp) {
3083 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003084 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (status)
3086 goto out;
3087 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003088 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003089 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 goto out;
J. Bruce Fields4af82502012-06-07 17:30:45 -04003091 status = nfsd4_truncate(rqstp, current_fh, open);
3092 if (status)
3093 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003094 stp = open->op_stp;
3095 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003096 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003098 update_stateid(&stp->st_stid.sc_stateid);
3099 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Benny Halevyd24433c2012-02-16 20:57:17 +02003101 if (nfsd4_has_session(&resp->cstate)) {
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003102 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03003103
Benny Halevyd24433c2012-02-16 20:57:17 +02003104 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3105 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3106 open->op_why_no_deleg = WND4_NOT_WANTED;
3107 goto nodeleg;
3108 }
3109 }
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 /*
3112 * Attempt to hand out a delegation. No error return, because the
3113 * OPEN succeeds even if we fail.
3114 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003115 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003116nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 status = nfs_ok;
3118
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003119 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003120 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003122 /* 4.1 client trying to upgrade/downgrade delegation? */
3123 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003124 open->op_deleg_want)
3125 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003126
NeilBrown13cd2182005-06-23 22:03:10 -07003127 if (fp)
3128 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003129 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003130 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /*
3132 * To finish the open response, we just need to set the rflags.
3133 */
3134 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003135 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003136 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3138
3139 return status;
3140}
3141
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003142void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3143{
3144 if (open->op_openowner) {
3145 struct nfs4_openowner *oo = open->op_openowner;
3146
3147 if (!list_empty(&oo->oo_owner.so_stateids))
3148 list_del_init(&oo->oo_close_lru);
3149 if (oo->oo_flags & NFS4_OO_NEW) {
3150 if (status) {
3151 release_openowner(oo);
3152 open->op_openowner = NULL;
3153 } else
3154 oo->oo_flags &= ~NFS4_OO_NEW;
3155 }
3156 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003157 if (open->op_file)
3158 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003159 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003160 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003161}
3162
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003163static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3164{
3165 struct nfs4_client *found;
3166
3167 if (STALE_CLIENTID(clid, nn))
3168 return nfserr_stale_clientid;
3169 found = find_confirmed_client(clid, session, nn);
3170 if (clp)
3171 *clp = found;
3172 return found ? nfs_ok : nfserr_expired;
3173}
3174
Al Virob37ad282006-10-19 23:28:59 -07003175__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003176nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3177 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003180 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003181 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 nfs4_lock_state();
3184 dprintk("process_renew(%08x/%08x): starting\n",
3185 clid->cl_boot, clid->cl_id);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003186 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3187 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003190 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003191 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 goto out;
3193 status = nfs_ok;
3194out:
3195 nfs4_unlock_state();
3196 return status;
3197}
3198
NeilBrowna76b4312005-06-23 22:04:01 -07003199static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003200nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003201{
Jeff Layton33dcc482012-04-10 11:08:48 -04003202 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003203 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003204 return;
3205
NeilBrowna76b4312005-06-23 22:04:01 -07003206 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003207 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003208 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003209 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003210 /*
3211 * Now that every NFSv4 client has had the chance to recover and
3212 * to see the (possibly new, possibly shorter) lease time, we
3213 * can safely set the next grace time to the current lease time:
3214 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003215 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003216}
3217
NeilBrownfd39ca92005-06-23 22:04:03 -07003218static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003219nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
3221 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003222 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 struct nfs4_delegation *dp;
3224 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003225 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3226 time_t t, clientid_val = nn->nfsd4_lease;
3227 time_t u, test_val = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229 nfs4_lock_state();
3230
3231 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003232 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003233 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003234 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003235 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 clp = list_entry(pos, struct nfs4_client, cl_lru);
3237 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3238 t = clp->cl_time - cutoff;
3239 if (clientid_val > t)
3240 clientid_val = t;
3241 break;
3242 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003243 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003244 dprintk("NFSD: client in use (clientid %08x)\n",
3245 clp->cl_clientid.cl_id);
3246 continue;
3247 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003248 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003249 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003250 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003251 list_for_each_safe(pos, next, &reaplist) {
3252 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 dprintk("NFSD: purging unused client (clientid %08x)\n",
3254 clp->cl_clientid.cl_id);
3255 expire_client(clp);
3256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003258 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003260 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3261 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3263 u = dp->dl_time - cutoff;
3264 if (test_val > u)
3265 test_val = u;
3266 break;
3267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 list_move(&dp->dl_recall_lru, &reaplist);
3269 }
3270 spin_unlock(&recall_lock);
3271 list_for_each_safe(pos, next, &reaplist) {
3272 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 unhash_delegation(dp);
3274 }
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003275 test_val = nn->nfsd4_lease;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003276 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003277 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3278 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3279 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 if (test_val > u)
3281 test_val = u;
3282 break;
3283 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003284 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 }
3286 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3287 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3288 nfs4_unlock_state();
3289 return clientid_val;
3290}
3291
Harvey Harrisona254b242008-02-20 12:49:00 -08003292static struct workqueue_struct *laundry_wq;
3293static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003294
3295static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003296laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
3298 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003299 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3300 work);
3301 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3302 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003304 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003306 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003309static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003310{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003311 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3312 return nfserr_bad_stateid;
3313 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003317access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003319 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3320 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3321 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
3324static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003325access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003327 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3328 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
3331static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003332__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Al Virob37ad282006-10-19 23:28:59 -07003334 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
J. Bruce Fields02921912010-07-29 15:16:59 -04003336 /* For lock stateid's, we test the parent open, not the lock: */
3337 if (stp->st_openstp)
3338 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003339 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003341 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 goto out;
3343 status = nfs_ok;
3344out:
3345 return status;
3346}
3347
Al Virob37ad282006-10-19 23:28:59 -07003348static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003349check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003351 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003353 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003354 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 * conflicting state; so we must wait out the grace period. */
3356 return nfserr_grace;
3357 } else if (flags & WR_STATE)
3358 return nfs4_share_conflict(current_fh,
3359 NFS4_SHARE_DENY_WRITE);
3360 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3361 return nfs4_share_conflict(current_fh,
3362 NFS4_SHARE_DENY_READ);
3363}
3364
3365/*
3366 * Allow READ/WRITE during grace period on recovered state only for files
3367 * that are not able to provide mandatory locking.
3368 */
3369static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003370grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003372 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
J. Bruce Fields81b82962011-08-23 11:03:29 -04003375/* Returns true iff a is later than b: */
3376static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3377{
3378 return (s32)a->si_generation - (s32)b->si_generation > 0;
3379}
3380
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003381static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003382{
Andy Adamson66689582009-04-03 08:28:45 +03003383 /*
3384 * When sessions are used the stateid generation number is ignored
3385 * when it is zero.
3386 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003387 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003388 return nfs_ok;
3389
3390 if (in->si_generation == ref->si_generation)
3391 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003392
J. Bruce Fields0836f582008-01-26 19:08:12 -05003393 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003394 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003395 return nfserr_bad_stateid;
3396 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003397 * However, we could see a stateid from the past, even from a
3398 * non-buggy client. For example, if the client sends a lock
3399 * while some IO is outstanding, the lock may bump si_generation
3400 * while the IO is still in flight. The client could avoid that
3401 * situation by waiting for responses on all the IO requests,
3402 * but better performance may result in retrying IO that
3403 * receives an old_stateid error if requests are rarely
3404 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003405 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003406 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003407}
3408
Chuck Lever7df302f2012-05-29 13:56:37 -04003409static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003410{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003411 struct nfs4_stid *s;
3412 struct nfs4_ol_stateid *ols;
3413 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003414
Chuck Lever7df302f2012-05-29 13:56:37 -04003415 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3416 return nfserr_bad_stateid;
3417 /* Client debugging aid. */
3418 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3419 char addr_str[INET6_ADDRSTRLEN];
3420 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3421 sizeof(addr_str));
3422 pr_warn_ratelimited("NFSD: client %s testing state ID "
3423 "with incorrect client ID\n", addr_str);
3424 return nfserr_bad_stateid;
3425 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003426 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003427 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003428 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003429 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003430 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003431 return status;
3432 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3433 return nfs_ok;
3434 ols = openlockstateid(s);
3435 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003436 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003437 return nfserr_bad_stateid;
3438 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003439}
3440
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003441static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3442 struct nfs4_stid **s, bool sessions,
3443 struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003444{
3445 struct nfs4_client *cl;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003446 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003447
3448 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3449 return nfserr_bad_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003450 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3451 nn, &cl);
3452 if (status == nfserr_stale_clientid)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003453 return nfserr_stale_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003454 if (status)
3455 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003456 *s = find_stateid_by_type(cl, stateid, typemask);
3457 if (!*s)
3458 return nfserr_bad_stateid;
3459 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003460}
3461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462/*
3463* Checks for stateid operations
3464*/
Al Virob37ad282006-10-19 23:28:59 -07003465__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003466nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003467 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003469 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003470 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003472 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003474 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07003475 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 if (filpp)
3478 *filpp = NULL;
3479
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003480 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 return nfserr_grace;
3482
3483 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003484 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003486 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3487 &s, cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003488 if (status)
3489 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003490 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3491 if (status)
3492 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003493 switch (s->sc_type) {
3494 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003495 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003496 status = nfs4_check_delegmode(dp, flags);
3497 if (status)
3498 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003499 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003500 *filpp = dp->dl_file->fi_deleg_file;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003501 if (!*filpp) {
3502 WARN_ON_ONCE(1);
3503 status = nfserr_serverfault;
3504 goto out;
3505 }
Dan Carpenter43b01782010-10-27 23:19:04 +02003506 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003507 break;
3508 case NFS4_OPEN_STID:
3509 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003510 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003511 status = nfs4_check_fh(current_fh, stp);
3512 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003514 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003515 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003517 status = nfs4_check_openmode(stp, flags);
3518 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003520 if (filpp) {
3521 if (flags & RD_STATE)
3522 *filpp = find_readable_file(stp->st_file);
3523 else
3524 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003525 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003526 break;
3527 default:
3528 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
3530 status = nfs_ok;
3531out:
3532 return status;
3533}
3534
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003535static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003536nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003537{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003538 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003539 return nfserr_locks_held;
3540 release_lock_stateid(stp);
3541 return nfs_ok;
3542}
3543
3544/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003545 * Test if the stateid is valid
3546 */
3547__be32
3548nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3549 struct nfsd4_test_stateid *test_stateid)
3550{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003551 struct nfsd4_test_stateid_id *stateid;
3552 struct nfs4_client *cl = cstate->session->se_client;
3553
3554 nfs4_lock_state();
3555 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003556 stateid->ts_id_status =
3557 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003558 nfs4_unlock_state();
3559
Bryan Schumaker17456802011-07-13 10:50:48 -04003560 return nfs_ok;
3561}
3562
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003563__be32
3564nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3565 struct nfsd4_free_stateid *free_stateid)
3566{
3567 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003568 struct nfs4_stid *s;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003569 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003570 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003571
3572 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003573 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003574 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003575 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003576 switch (s->sc_type) {
3577 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003578 ret = nfserr_locks_held;
3579 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003580 case NFS4_OPEN_STID:
3581 case NFS4_LOCK_STID:
3582 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3583 if (ret)
3584 goto out;
3585 if (s->sc_type == NFS4_LOCK_STID)
3586 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3587 else
3588 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003589 break;
3590 default:
3591 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003592 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003593out:
3594 nfs4_unlock_state();
3595 return ret;
3596}
3597
NeilBrown4c4cd222005-07-07 17:59:27 -07003598static inline int
3599setlkflg (int type)
3600{
3601 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3602 RD_STATE : WR_STATE;
3603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003605static __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 -04003606{
3607 struct svc_fh *current_fh = &cstate->current_fh;
3608 struct nfs4_stateowner *sop = stp->st_stateowner;
3609 __be32 status;
3610
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003611 status = nfsd4_check_seqid(cstate, sop, seqid);
3612 if (status)
3613 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003614 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3615 /*
3616 * "Closed" stateid's exist *only* to return
3617 * nfserr_replay_me from the previous step.
3618 */
3619 return nfserr_bad_stateid;
3620 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3621 if (status)
3622 return status;
3623 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003624}
3625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626/*
3627 * Checks for sequence id mutating operations.
3628 */
Al Virob37ad282006-10-19 23:28:59 -07003629static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003630nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003631 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003632 struct nfs4_ol_stateid **stpp,
3633 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003635 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003636 struct nfs4_stid *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003638 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3639 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003640
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 *stpp = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003642 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3643 cstate->minorversion, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003644 if (status)
3645 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003646 *stpp = openlockstateid(s);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003647 cstate->replay_owner = (*stpp)->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003649 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3650}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003651
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003652static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3653 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003654{
3655 __be32 status;
3656 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003658 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003659 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003660 if (status)
3661 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003662 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003663 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003664 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003665 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666}
3667
Al Virob37ad282006-10-19 23:28:59 -07003668__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003669nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003670 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Al Virob37ad282006-10-19 23:28:59 -07003672 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003673 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003674 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003675 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
3677 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003678 (int)cstate->current_fh.fh_dentry->d_name.len,
3679 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003681 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003682 if (status)
3683 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
3685 nfs4_lock_state();
3686
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003687 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003688 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003689 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003690 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003691 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003692 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003693 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003694 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003695 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003696 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003697 update_stateid(&stp->st_stid.sc_stateid);
3698 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003699 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003700 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003701
Jeff Layton2a4317c2012-03-21 16:42:43 -04003702 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003703 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003705 if (!cstate->replay_owner)
3706 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return status;
3708}
3709
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003710static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003712 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003713 return;
3714 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04003715 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003716}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003717
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003718static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3719{
3720 switch (to_access) {
3721 case NFS4_SHARE_ACCESS_READ:
3722 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3723 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3724 break;
3725 case NFS4_SHARE_ACCESS_WRITE:
3726 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3727 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3728 break;
3729 case NFS4_SHARE_ACCESS_BOTH:
3730 break;
3731 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003732 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 }
3734}
3735
3736static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04003737reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
3739 int i;
3740 for (i = 0; i < 4; i++) {
3741 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04003742 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 }
3744}
3745
Al Virob37ad282006-10-19 23:28:59 -07003746__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003747nfsd4_open_downgrade(struct svc_rqst *rqstp,
3748 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003749 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
Al Virob37ad282006-10-19 23:28:59 -07003751 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003752 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003753 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
3755 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003756 (int)cstate->current_fh.fh_dentry->d_name.len,
3757 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04003759 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02003760 if (od->od_deleg_want)
3761 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3762 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
3764 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003765 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003766 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003767 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003770 if (!test_access(od->od_share_access, stp)) {
3771 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 stp->st_access_bmap, od->od_share_access);
3773 goto out;
3774 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04003775 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3777 stp->st_deny_bmap, od->od_share_deny);
3778 goto out;
3779 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003780 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Jeff Laytonce0fc432012-05-11 09:45:14 -04003782 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003784 update_stateid(&stp->st_stid.sc_stateid);
3785 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 status = nfs_ok;
3787out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003788 if (!cstate->replay_owner)
3789 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return status;
3791}
3792
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003793void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3794{
3795 struct nfs4_openowner *oo;
3796 struct nfs4_ol_stateid *s;
3797
3798 if (!so->so_is_open_owner)
3799 return;
3800 oo = openowner(so);
3801 s = oo->oo_last_closed_stid;
3802 if (!s)
3803 return;
3804 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3805 /* Release the last_closed_stid on the next seqid bump: */
3806 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3807 return;
3808 }
3809 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003810 release_last_closed_stateid(oo);
3811}
3812
3813static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3814{
3815 unhash_open_stateid(s);
3816 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003817}
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819/*
3820 * nfs4_unlock_state() called after encode
3821 */
Al Virob37ad282006-10-19 23:28:59 -07003822__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003823nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003824 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
Al Virob37ad282006-10-19 23:28:59 -07003826 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003827 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003828 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003829 struct net *net = SVC_NET(rqstp);
3830 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 dprintk("NFSD: nfsd4_close 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
3836 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003837 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3838 &close->cl_stateid,
3839 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003840 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003841 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003843 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003845 update_stateid(&stp->st_stid.sc_stateid);
3846 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003848 nfsd4_close_open_stateid(stp);
J. Bruce Fieldscf9182e2012-08-29 15:21:58 -07003849 release_last_closed_stateid(oo);
ycnian@gmail.com491402a2013-03-11 08:46:14 +08003850 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003851 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003852
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003853 if (list_empty(&oo->oo_owner.so_stateids)) {
3854 if (cstate->minorversion) {
3855 release_openowner(oo);
3856 cstate->replay_owner = NULL;
3857 } else {
3858 /*
3859 * In the 4.0 case we need to keep the owners around a
3860 * little while to handle CLOSE replay.
3861 */
3862 if (list_empty(&oo->oo_owner.so_stateids))
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003863 move_to_close_lru(oo, SVC_NET(rqstp));
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003864 }
3865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003867 if (!cstate->replay_owner)
3868 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return status;
3870}
3871
Al Virob37ad282006-10-19 23:28:59 -07003872__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003873nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3874 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003876 struct nfs4_delegation *dp;
3877 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003878 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07003879 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003880 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003882 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003883 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 nfs4_lock_state();
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003886 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3887 cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003888 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003889 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003890 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003891 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003892 if (status)
3893 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003894
3895 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003897 nfs4_unlock_state();
3898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return status;
3900}
3901
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
J. Bruce Fields009673b2011-11-07 17:40:10 -05003905#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Benny Halevy87df4de2008-12-15 19:42:03 +02003907static inline u64
3908end_offset(u64 start, u64 len)
3909{
3910 u64 end;
3911
3912 end = start + len;
3913 return end >= start ? end: NFS4_MAX_UINT64;
3914}
3915
3916/* last octet in a range */
3917static inline u64
3918last_byte_offset(u64 start, u64 len)
3919{
3920 u64 end;
3921
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003922 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02003923 end = start + len;
3924 return end > start ? end - 1: NFS4_MAX_UINT64;
3925}
3926
J. Bruce Fields009673b2011-11-07 17:40:10 -05003927static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
3929 return (file_hashval(inode) + cl_id
3930 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05003931 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932}
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934/*
3935 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3936 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3937 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3938 * locking, this prevents us from being completely protocol-compliant. The
3939 * real solution to this problem is to start using unsigned file offsets in
3940 * the VFS, but this is a very deep change!
3941 */
3942static inline void
3943nfs4_transform_lock_offset(struct file_lock *lock)
3944{
3945 if (lock->fl_start < 0)
3946 lock->fl_start = OFFSET_MAX;
3947 if (lock->fl_end < 0)
3948 lock->fl_end = OFFSET_MAX;
3949}
3950
NeilBrownd5b90262006-04-10 22:55:22 -07003951/* Hack!: For now, we're defining this just so we can use a pointer to it
3952 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003953static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003954};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
3956static inline void
3957nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3958{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003959 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
NeilBrownd5b90262006-04-10 22:55:22 -07003961 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003962 lo = (struct nfs4_lockowner *) fl->fl_owner;
3963 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3964 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003965 if (!deny->ld_owner.data)
3966 /* We just don't care that much */
3967 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003968 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3969 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003970 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003971nevermind:
3972 deny->ld_owner.len = 0;
3973 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003974 deny->ld_clientid.cl_boot = 0;
3975 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 }
3977 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003978 deny->ld_length = NFS4_MAX_UINT64;
3979 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3981 deny->ld_type = NFS4_READ_LT;
3982 if (fl->fl_type != F_RDLCK)
3983 deny->ld_type = NFS4_WRITE_LT;
3984}
3985
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003986static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3987{
3988 struct nfs4_ol_stateid *lst;
3989
3990 if (!same_owner_str(&lo->lo_owner, owner, clid))
3991 return false;
3992 lst = list_first_entry(&lo->lo_owner.so_stateids,
3993 struct nfs4_ol_stateid, st_perstateowner);
3994 return lst->st_file->fi_inode == inode;
3995}
3996
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003997static struct nfs4_lockowner *
3998find_lockowner_str(struct inode *inode, clientid_t *clid,
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03003999 struct xdr_netobj *owner, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004001 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004002 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004004 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004005 if (same_lockowner_ino(lo, inode, clid, owner))
4006 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 }
4008 return NULL;
4009}
4010
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004011static 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 -04004012{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004013 struct inode *inode = open_stp->st_file->fi_inode;
4014 unsigned int inohash = lockowner_ino_hashval(inode,
4015 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004016 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields009673b2011-11-07 17:40:10 -05004017
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004018 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004019 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004020 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004021}
4022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023/*
4024 * Alloc a lock owner structure.
4025 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004026 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004028 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 */
4030
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004031static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004032alloc_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 -04004033 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004035 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4036 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004038 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4039 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004040 /* It is the openowner seqid that will be incremented in encode in the
4041 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004042 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4043 hash_lockowner(lo, strhashval, clp, open_stp);
4044 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004047static struct nfs4_ol_stateid *
4048alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004050 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004051 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
J. Bruce Fields996e0932011-10-17 11:14:48 -04004053 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004054 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004055 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004056 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown8beefa22005-06-23 22:03:08 -07004057 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004058 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4059 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004060 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004062 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004064 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return stp;
4066}
4067
NeilBrownfd39ca92005-06-23 22:04:03 -07004068static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069check_lock_length(u64 offset, u64 length)
4070{
Benny Halevy87df4de2008-12-15 19:42:03 +02004071 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 LOFF_OVERFLOW(offset, length)));
4073}
4074
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004075static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004076{
4077 struct nfs4_file *fp = lock_stp->st_file;
4078 int oflag = nfs4_access_to_omode(access);
4079
Jeff Layton82c5ff12012-05-11 09:45:13 -04004080 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004081 return;
4082 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004083 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004084}
4085
J. Bruce Fields2355c592012-04-25 16:56:22 -04004086static __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 -04004087{
4088 struct nfs4_file *fi = ost->st_file;
4089 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4090 struct nfs4_client *cl = oo->oo_owner.so_client;
4091 struct nfs4_lockowner *lo;
4092 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004093 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004094
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004095 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4096 &lock->v.new.owner, nn);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004097 if (lo) {
4098 if (!cstate->minorversion)
4099 return nfserr_bad_seqid;
4100 /* XXX: a lockowner always has exactly one stateid: */
4101 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4102 struct nfs4_ol_stateid, st_perstateowner);
4103 return nfs_ok;
4104 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004105 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004106 &lock->v.new.owner);
4107 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4108 if (lo == NULL)
4109 return nfserr_jukebox;
4110 *lst = alloc_init_lock_stateid(lo, fi, ost);
4111 if (*lst == NULL) {
4112 release_lockowner(lo);
4113 return nfserr_jukebox;
4114 }
4115 *new = true;
4116 return nfs_ok;
4117}
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119/*
4120 * LOCK operation
4121 */
Al Virob37ad282006-10-19 23:28:59 -07004122__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004123nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004124 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004126 struct nfs4_openowner *open_sop = NULL;
4127 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004128 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004129 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004130 struct file_lock *file_lock = NULL;
4131 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004132 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004133 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004134 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004135 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004136 struct net *net = SVC_NET(rqstp);
4137 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4140 (long long) lock->lk_offset,
4141 (long long) lock->lk_length);
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 if (check_lock_length(lock->lk_offset, lock->lk_length))
4144 return nfserr_inval;
4145
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004146 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004147 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004148 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4149 return status;
4150 }
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 nfs4_lock_state();
4153
4154 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004155 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004156
4157 if (nfsd4_has_session(cstate))
4158 /* See rfc 5661 18.10.3: given clientid is ignored: */
4159 memcpy(&lock->v.new.clientid,
4160 &cstate->session->se_client->cl_clientid,
4161 sizeof(clientid_t));
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004164 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004168 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 lock->lk_new_open_seqid,
4170 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004171 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004172 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004174 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004175 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004176 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004177 &lock->v.new.clientid))
4178 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004179 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4180 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004181 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004182 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004183 lock->lk_old_lock_seqid,
4184 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004185 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004186 if (status)
4187 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004188 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004190 lkflg = setlkflg(lock->lk_type);
4191 status = nfs4_check_openmode(lock_stp, lkflg);
4192 if (status)
4193 goto out;
4194
NeilBrown0dd395d2005-07-07 17:59:15 -07004195 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004196 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004197 goto out;
4198 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004199 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004200 goto out;
4201
Jeff Layton21179d82012-08-21 08:03:32 -04004202 file_lock = locks_alloc_lock();
4203 if (!file_lock) {
4204 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4205 status = nfserr_jukebox;
4206 goto out;
4207 }
4208
4209 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 switch (lock->lk_type) {
4211 case NFS4_READ_LT:
4212 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004213 filp = find_readable_file(lock_stp->st_file);
4214 if (filp)
4215 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004216 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 case NFS4_WRITE_LT:
4219 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004220 filp = find_writeable_file(lock_stp->st_file);
4221 if (filp)
4222 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004223 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 default:
4226 status = nfserr_inval;
4227 goto out;
4228 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004229 if (!filp) {
4230 status = nfserr_openmode;
4231 goto out;
4232 }
Jeff Layton21179d82012-08-21 08:03:32 -04004233 file_lock->fl_owner = (fl_owner_t)lock_sop;
4234 file_lock->fl_pid = current->tgid;
4235 file_lock->fl_file = filp;
4236 file_lock->fl_flags = FL_POSIX;
4237 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4238 file_lock->fl_start = lock->lk_offset;
4239 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4240 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Jeff Layton21179d82012-08-21 08:03:32 -04004242 conflock = locks_alloc_lock();
4243 if (!conflock) {
4244 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4245 status = nfserr_jukebox;
4246 goto out;
4247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Jeff Layton21179d82012-08-21 08:03:32 -04004249 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004250 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004252 update_stateid(&lock_stp->st_stid.sc_stateid);
4253 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004255 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004256 break;
4257 case (EAGAIN): /* conflock holds conflicting lock */
4258 status = nfserr_denied;
4259 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004260 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 case (EDEADLK):
4263 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004264 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004265 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004266 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004267 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004271 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004272 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004273 if (!cstate->replay_owner)
4274 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004275 if (file_lock)
4276 locks_free_lock(file_lock);
4277 if (conflock)
4278 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return status;
4280}
4281
4282/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004283 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4284 * so we do a temporary open here just to get an open file to pass to
4285 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4286 * inode operation.)
4287 */
Al Viro04da6e92012-04-13 00:00:04 -04004288static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004289{
4290 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004291 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4292 if (!err) {
4293 err = nfserrno(vfs_test_lock(file, lock));
4294 nfsd_close(file);
4295 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004296 return err;
4297}
4298
4299/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 * LOCKT operation
4301 */
Al Virob37ad282006-10-19 23:28:59 -07004302__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004303nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4304 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 struct inode *inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004307 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004308 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004309 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004310 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004312 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return nfserr_grace;
4314
4315 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4316 return nfserr_inval;
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 nfs4_lock_state();
4319
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004320 if (!nfsd4_has_session(cstate)) {
4321 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4322 if (status)
4323 goto out;
4324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004326 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004329 inode = cstate->current_fh.fh_dentry->d_inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004330 file_lock = locks_alloc_lock();
4331 if (!file_lock) {
4332 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4333 status = nfserr_jukebox;
4334 goto out;
4335 }
4336 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 switch (lockt->lt_type) {
4338 case NFS4_READ_LT:
4339 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004340 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 break;
4342 case NFS4_WRITE_LT:
4343 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004344 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 break;
4346 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004347 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 status = nfserr_inval;
4349 goto out;
4350 }
4351
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004352 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004353 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004354 file_lock->fl_owner = (fl_owner_t)lo;
4355 file_lock->fl_pid = current->tgid;
4356 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Jeff Layton21179d82012-08-21 08:03:32 -04004358 file_lock->fl_start = lockt->lt_offset;
4359 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Jeff Layton21179d82012-08-21 08:03:32 -04004361 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Jeff Layton21179d82012-08-21 08:03:32 -04004363 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004364 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004365 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004366
Jeff Layton21179d82012-08-21 08:03:32 -04004367 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004369 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
4371out:
4372 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004373 if (file_lock)
4374 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 return status;
4376}
4377
Al Virob37ad282006-10-19 23:28:59 -07004378__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004379nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004380 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004382 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004384 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004385 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004386 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004387 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4390 (long long) locku->lu_offset,
4391 (long long) locku->lu_length);
4392
4393 if (check_lock_length(locku->lu_offset, locku->lu_length))
4394 return nfserr_inval;
4395
4396 nfs4_lock_state();
4397
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004398 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004399 &locku->lu_stateid, NFS4_LOCK_STID,
4400 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004401 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004403 filp = find_any_file(stp->st_file);
4404 if (!filp) {
4405 status = nfserr_lock_range;
4406 goto out;
4407 }
Jeff Layton21179d82012-08-21 08:03:32 -04004408 file_lock = locks_alloc_lock();
4409 if (!file_lock) {
4410 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4411 status = nfserr_jukebox;
4412 goto out;
4413 }
4414 locks_init_lock(file_lock);
4415 file_lock->fl_type = F_UNLCK;
4416 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4417 file_lock->fl_pid = current->tgid;
4418 file_lock->fl_file = filp;
4419 file_lock->fl_flags = FL_POSIX;
4420 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4421 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Jeff Layton21179d82012-08-21 08:03:32 -04004423 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4424 locku->lu_length);
4425 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427 /*
4428 * Try to unlock the file in the VFS.
4429 */
Jeff Layton21179d82012-08-21 08:03:32 -04004430 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004431 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004432 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 goto out_nfserr;
4434 }
4435 /*
4436 * OK, unlock succeeded; the only thing left to do is update the stateid.
4437 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004438 update_stateid(&stp->st_stid.sc_stateid);
4439 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441out:
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004442 if (!cstate->replay_owner)
4443 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004444 if (file_lock)
4445 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 return status;
4447
4448out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004449 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 goto out;
4451}
4452
4453/*
4454 * returns
4455 * 1: locks held by lockowner
4456 * 0: no locks held by lockowner
4457 */
4458static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004459check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
4461 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004462 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 int status = 0;
4464
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004465 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004467 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 status = 1;
4469 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 }
4472out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004473 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 return status;
4475}
4476
Al Virob37ad282006-10-19 23:28:59 -07004477__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004478nfsd4_release_lockowner(struct svc_rqst *rqstp,
4479 struct nfsd4_compound_state *cstate,
4480 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
4482 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004483 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004484 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004485 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004487 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004488 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004489 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004490 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4493 clid->cl_boot, clid->cl_id);
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 nfs4_lock_state();
4496
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004497 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4498 if (status)
4499 goto out;
4500
NeilBrown3e9e3db2005-06-23 22:04:20 -07004501 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004502 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004503
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004504 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004505 if (sop->so_is_open_owner)
4506 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004507 if (!same_owner_str(sop, owner, clid))
4508 continue;
4509 list_for_each_entry(stp, &sop->so_stateids,
4510 st_perstateowner) {
4511 lo = lockowner(sop);
4512 if (check_for_locks(stp->st_file, lo))
4513 goto out;
4514 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004516 }
4517 /* Clients probably won't expect us to return with some (but not all)
4518 * of the lockowner state released; so don't release any until all
4519 * have been checked. */
4520 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004521 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004522 lo = list_entry(matches.next, struct nfs4_lockowner,
4523 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004524 /* unhash_stateowner deletes so_perclient only
4525 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004526 list_del(&lo->lo_list);
4527 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 }
4529out:
4530 nfs4_unlock_state();
4531 return status;
4532}
4533
4534static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004535alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
NeilBrowna55370a2005-06-23 22:03:52 -07004537 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538}
4539
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004540bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004541nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004542{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004543 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004544
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004545 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004546 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004547}
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549/*
4550 * failure => all reset bets are off, nfserr_no_grace...
4551 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004552struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004553nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
4555 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004556 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
NeilBrowna55370a2005-06-23 22:03:52 -07004558 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4559 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004560 if (crp) {
4561 strhashval = clientstr_hashval(name);
4562 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004563 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004564 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004565 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004566 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004567 }
4568 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569}
4570
Jeff Layton2a4317c2012-03-21 16:42:43 -04004571void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004572nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004573{
4574 list_del(&crp->cr_strhash);
4575 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004576 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004577}
4578
4579void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004580nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
4582 struct nfs4_client_reclaim *crp = NULL;
4583 int i;
4584
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004586 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4587 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004589 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
4591 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004592 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593}
4594
4595/*
4596 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004597struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004598nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599{
4600 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 struct nfs4_client_reclaim *crp = NULL;
4602
Jeff Layton278c9312012-11-12 15:00:52 -05004603 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Jeff Layton278c9312012-11-12 15:00:52 -05004605 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004606 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004607 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return crp;
4609 }
4610 }
4611 return NULL;
4612}
4613
4614/*
4615* Called from OPEN. Look for clientid in reclaim list.
4616*/
Al Virob37ad282006-10-19 23:28:59 -07004617__be32
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004618nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619{
Jeff Laytona52d7262012-03-21 09:52:02 -04004620 struct nfs4_client *clp;
4621
4622 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004623 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004624 if (clp == NULL)
4625 return nfserr_reclaim_bad;
4626
4627 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
4629
Bryan Schumaker65178db2011-11-01 13:35:21 -04004630#ifdef CONFIG_NFSD_FAULT_INJECTION
4631
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004632u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4633{
J. Bruce Fields221a6872013-04-01 22:23:49 -04004634 if (mark_client_expired(clp))
4635 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004636 expire_client(clp);
4637 return 1;
4638}
4639
Bryan Schumaker184c1842012-11-29 11:40:44 -05004640u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4641{
4642 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004643 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004644 printk(KERN_INFO "NFS Client: %s\n", buf);
4645 return 1;
4646}
4647
4648static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4649 const char *type)
4650{
4651 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004652 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004653 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4654}
4655
Bryan Schumakerfc291712012-11-29 11:40:40 -05004656static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4657{
4658 struct nfs4_openowner *oop;
4659 struct nfs4_lockowner *lop, *lo_next;
4660 struct nfs4_ol_stateid *stp, *st_next;
4661 u64 count = 0;
4662
4663 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4664 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4665 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4666 if (func)
4667 func(lop);
4668 if (++count == max)
4669 return count;
4670 }
4671 }
4672 }
4673
4674 return count;
4675}
4676
4677u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4678{
4679 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4680}
4681
Bryan Schumaker184c1842012-11-29 11:40:44 -05004682u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4683{
4684 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4685 nfsd_print_count(clp, count, "locked files");
4686 return count;
4687}
4688
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05004689static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4690{
4691 struct nfs4_openowner *oop, *next;
4692 u64 count = 0;
4693
4694 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4695 if (func)
4696 func(oop);
4697 if (++count == max)
4698 break;
4699 }
4700
4701 return count;
4702}
4703
4704u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4705{
4706 return nfsd_foreach_client_open(clp, max, release_openowner);
4707}
4708
Bryan Schumaker184c1842012-11-29 11:40:44 -05004709u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
4710{
4711 u64 count = nfsd_foreach_client_open(clp, max, NULL);
4712 nfsd_print_count(clp, count, "open files");
4713 return count;
4714}
4715
Bryan Schumaker269de302012-11-29 11:40:42 -05004716static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
4717 struct list_head *victims)
4718{
4719 struct nfs4_delegation *dp, *next;
4720 u64 count = 0;
4721
4722 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
4723 if (victims)
4724 list_move(&dp->dl_recall_lru, victims);
4725 if (++count == max)
4726 break;
4727 }
4728 return count;
4729}
4730
4731u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
4732{
4733 struct nfs4_delegation *dp, *next;
4734 LIST_HEAD(victims);
4735 u64 count;
4736
4737 spin_lock(&recall_lock);
4738 count = nfsd_find_all_delegations(clp, max, &victims);
4739 spin_unlock(&recall_lock);
4740
4741 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4742 unhash_delegation(dp);
4743
4744 return count;
4745}
4746
4747u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
4748{
4749 struct nfs4_delegation *dp, *next;
4750 LIST_HEAD(victims);
4751 u64 count;
4752
4753 spin_lock(&recall_lock);
4754 count = nfsd_find_all_delegations(clp, max, &victims);
4755 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4756 nfsd_break_one_deleg(dp);
4757 spin_unlock(&recall_lock);
4758
4759 return count;
4760}
4761
Bryan Schumaker184c1842012-11-29 11:40:44 -05004762u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
4763{
4764 u64 count = 0;
4765
4766 spin_lock(&recall_lock);
4767 count = nfsd_find_all_delegations(clp, max, NULL);
4768 spin_unlock(&recall_lock);
4769
4770 nfsd_print_count(clp, count, "delegations");
4771 return count;
4772}
4773
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004774u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004775{
4776 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004777 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004778 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004779
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004780 if (!nfsd_netns_ready(nn))
4781 return 0;
4782
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004783 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004784 count += func(clp, max - count);
4785 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004786 break;
4787 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04004788
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004789 return count;
4790}
4791
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05004792struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
4793{
4794 struct nfs4_client *clp;
4795 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4796
4797 if (!nfsd_netns_ready(nn))
4798 return NULL;
4799
4800 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
4801 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
4802 return clp;
4803 }
4804 return NULL;
4805}
4806
Bryan Schumaker65178db2011-11-01 13:35:21 -04004807#endif /* CONFIG_NFSD_FAULT_INJECTION */
4808
NeilBrownac4d8ff22005-06-23 22:03:30 -07004809/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Bryan Schumaker72083392011-11-01 15:24:59 -04004811void
NeilBrownac4d8ff22005-06-23 22:03:30 -07004812nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813{
NeilBrownac4d8ff22005-06-23 22:03:30 -07004814}
4815
Meelap Shahc2f1a552007-07-17 04:04:39 -07004816/*
4817 * Since the lifetime of a delegation isn't limited to that of an open, a
4818 * client may quite reasonably hang on to a delegation as long as it has
4819 * the inode cached. This becomes an obvious problem the first time a
4820 * client's inode cache approaches the size of the server's total memory.
4821 *
4822 * For now we avoid this problem by imposing a hard limit on the number
4823 * of delegations, which varies according to the server's memory size.
4824 */
4825static void
4826set_max_delegations(void)
4827{
4828 /*
4829 * Allow at most 4 delegations per megabyte of RAM. Quick
4830 * estimates suggest that in the worst case (where every delegation
4831 * is for a different inode), a delegation could take about 1.5K,
4832 * giving a worst case usage of about 6% of memory.
4833 */
4834 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4835}
4836
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004837static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004838{
4839 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4840 int i;
4841
4842 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4843 CLIENT_HASH_SIZE, GFP_KERNEL);
4844 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004845 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004846 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4847 CLIENT_HASH_SIZE, GFP_KERNEL);
4848 if (!nn->unconf_id_hashtbl)
4849 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004850 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4851 OWNER_HASH_SIZE, GFP_KERNEL);
4852 if (!nn->ownerstr_hashtbl)
4853 goto err_ownerstr;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004854 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4855 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4856 if (!nn->lockowner_ino_hashtbl)
4857 goto err_lockowner_ino;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004858 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4859 SESSION_HASH_SIZE, GFP_KERNEL);
4860 if (!nn->sessionid_hashtbl)
4861 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004862
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004863 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004864 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004865 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004866 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004867 for (i = 0; i < OWNER_HASH_SIZE; i++)
4868 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004869 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4870 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004871 for (i = 0; i < SESSION_HASH_SIZE; i++)
4872 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004873 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004874 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004875 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004876 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004877 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004878 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004879
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004880 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004881 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004882
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004883 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004884
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004885err_sessionid:
4886 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004887err_lockowner_ino:
4888 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004889err_ownerstr:
4890 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004891err_unconf_id:
4892 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004893err:
4894 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004895}
4896
4897static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004898nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004899{
4900 int i;
4901 struct nfs4_client *clp = NULL;
4902 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004903 struct rb_node *node, *tmp;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004904
4905 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4906 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4907 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4908 destroy_client(clp);
4909 }
4910 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004911
4912 node = rb_first(&nn->unconf_name_tree);
4913 while (node != NULL) {
4914 tmp = node;
4915 node = rb_next(tmp);
4916 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4917 rb_erase(tmp, &nn->unconf_name_tree);
4918 destroy_client(clp);
4919 }
4920
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004921 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004922 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004923 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004924 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004925 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004926 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004927}
4928
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004929int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004930nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07004931{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004932 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004933 int ret;
4934
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004935 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004936 if (ret)
4937 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004938 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004939 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004940 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004941 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004942 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004943 nn->nfsd4_grace, net);
4944 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004945 return 0;
4946}
4947
4948/* initialization to perform when the nfsd service is started: */
4949
4950int
4951nfs4_state_start(void)
4952{
4953 int ret;
4954
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004955 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004956 if (ret)
4957 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004958 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05004959 if (laundry_wq == NULL) {
4960 ret = -ENOMEM;
4961 goto out_recovery;
4962 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004963 ret = nfsd4_create_callback_queue();
4964 if (ret)
4965 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004966
Meelap Shahc2f1a552007-07-17 04:04:39 -07004967 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004968
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004969 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004970
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004971out_free_laundry:
4972 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05004973out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004974 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975}
4976
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004977/* should be called with the state lock held */
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004978void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004979nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004983 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004985 cancel_delayed_work_sync(&nn->laundromat_work);
4986 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004987
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 INIT_LIST_HEAD(&reaplist);
4989 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004990 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4992 list_move(&dp->dl_recall_lru, &reaplist);
4993 }
4994 spin_unlock(&recall_lock);
4995 list_for_each_safe(pos, next, &reaplist) {
4996 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 unhash_delegation(dp);
4998 }
4999
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005000 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005001 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
5003
5004void
5005nfs4_state_shutdown(void)
5006{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005007 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005008 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005010
5011static void
5012get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5013{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005014 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5015 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005016}
5017
5018static void
5019put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5020{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005021 if (cstate->minorversion) {
5022 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5023 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5024 }
5025}
5026
5027void
5028clear_current_stateid(struct nfsd4_compound_state *cstate)
5029{
5030 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005031}
5032
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005033/*
5034 * functions to set current state id
5035 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005036void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005037nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5038{
5039 put_stateid(cstate, &odp->od_stateid);
5040}
5041
5042void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005043nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5044{
5045 put_stateid(cstate, &open->op_stateid);
5046}
5047
5048void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005049nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5050{
5051 put_stateid(cstate, &close->cl_stateid);
5052}
5053
5054void
5055nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5056{
5057 put_stateid(cstate, &lock->lk_resp_stateid);
5058}
5059
5060/*
5061 * functions to consume current state id
5062 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005063
5064void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005065nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5066{
5067 get_stateid(cstate, &odp->od_stateid);
5068}
5069
5070void
5071nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5072{
5073 get_stateid(cstate, &drp->dr_stateid);
5074}
5075
5076void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005077nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5078{
5079 get_stateid(cstate, &fsp->fr_stateid);
5080}
5081
5082void
5083nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5084{
5085 get_stateid(cstate, &setattr->sa_stateid);
5086}
5087
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005088void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005089nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5090{
5091 get_stateid(cstate, &close->cl_stateid);
5092}
5093
5094void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005095nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005096{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005097 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005098}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005099
5100void
5101nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5102{
5103 get_stateid(cstate, &read->rd_stateid);
5104}
5105
5106void
5107nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5108{
5109 get_stateid(cstate, &write->wr_stateid);
5110}