blob: bcd2339ae8c162a54516a809410b83d5991df1a3 [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
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400723/*
724 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
725 * won't be used for replay.
726 */
727void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
728{
729 struct nfs4_stateowner *so = cstate->replay_owner;
730
731 if (nfserr == nfserr_replay_me)
732 return;
733
734 if (!seqid_mutating_err(ntohl(nfserr))) {
735 cstate->replay_owner = NULL;
736 return;
737 }
738 if (!so)
739 return;
740 if (so->so_is_open_owner)
741 release_last_closed_stateid(openowner(so));
742 so->so_seqid++;
743 return;
744}
Marc Eshel5282fd72009-04-03 08:27:52 +0300745
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300746static void
747gen_sessionid(struct nfsd4_session *ses)
748{
749 struct nfs4_client *clp = ses->se_client;
750 struct nfsd4_sessionid *sid;
751
752 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
753 sid->clientid = clp->cl_clientid;
754 sid->sequence = current_sessionid++;
755 sid->reserved = 0;
756}
757
758/*
Andy Adamsona649637c72009-08-28 08:45:01 -0400759 * The protocol defines ca_maxresponssize_cached to include the size of
760 * the rpc header, but all we need to cache is the data starting after
761 * the end of the initial SEQUENCE operation--the rest we regenerate
762 * each time. Therefore we can advertise a ca_maxresponssize_cached
763 * value that is the number of bytes in our cache plus a few additional
764 * bytes. In order to stay on the safe side, and not promise more than
765 * we can cache, those additional bytes must be the minimum possible: 24
766 * bytes of rpc header (xid through accept state, with AUTH_NULL
767 * verifier), 12 for the compound header (with zero-length tag), and 44
768 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300769 */
Andy Adamsona649637c72009-08-28 08:45:01 -0400770#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
771
Andy Adamson557ce262009-08-28 08:45:04 -0400772static void
773free_session_slots(struct nfsd4_session *ses)
774{
775 int i;
776
777 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
778 kfree(ses->se_slots[i]);
779}
780
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400781/*
782 * We don't actually need to cache the rpc and session headers, so we
783 * can allocate a little less for each slot:
784 */
785static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
786{
787 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
788}
789
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400790static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300791{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400792 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
793 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300794
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400795 return size;
796}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300797
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400798/*
799 * XXX: If we run out of reserved DRC memory we could (up to a point)
800 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800801 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400802 */
803static int nfsd4_get_drc_mem(int slotsize, u32 num)
804{
805 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300806
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400807 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400808
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400809 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800810 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
811 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400812 num = min_t(int, num, avail / slotsize);
813 nfsd_drc_mem_used += num * slotsize;
814 spin_unlock(&nfsd_drc_lock);
815
816 return num;
817}
818
819static void nfsd4_put_drc_mem(int slotsize, int num)
820{
821 spin_lock(&nfsd_drc_lock);
822 nfsd_drc_mem_used -= slotsize * num;
823 spin_unlock(&nfsd_drc_lock);
824}
825
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400826static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400827{
828 struct nfsd4_session *new;
829 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300830
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400831 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400832 + sizeof(struct nfsd4_session) > PAGE_SIZE);
833 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400834
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400835 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300836 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400837 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400838 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400839 for (i = 0; i < numslots; i++) {
840 mem = sizeof(struct nfsd4_slot) + slotsize;
841 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
842 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400843 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400844 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400845 return new;
846out_free:
847 while (i--)
848 kfree(new->se_slots[i]);
849 kfree(new);
850 return NULL;
851}
852
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300853static void init_forechannel_attrs(struct nfsd4_channel_attrs *new,
854 struct nfsd4_channel_attrs *req,
855 int numslots, int slotsize,
856 struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400857{
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300858 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400859
860 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800861 new->maxresp_cached = min_t(u32, req->maxresp_cached,
862 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400863 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
864 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
865 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
866}
867
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400868static void free_conn(struct nfsd4_conn *c)
869{
870 svc_xprt_put(c->cn_xprt);
871 kfree(c);
872}
873
874static void nfsd4_conn_lost(struct svc_xpt_user *u)
875{
876 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
877 struct nfs4_client *clp = c->cn_session->se_client;
878
879 spin_lock(&clp->cl_lock);
880 if (!list_empty(&c->cn_persession)) {
881 list_del(&c->cn_persession);
882 free_conn(c);
883 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500884 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -0500885 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400886}
887
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400888static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400889{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400890 struct nfsd4_conn *conn;
891
892 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
893 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400894 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400895 svc_xprt_get(rqstp->rq_xprt);
896 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400897 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400898 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
899 return conn;
900}
901
J. Bruce Fields328ead22010-09-29 16:11:06 -0400902static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
903{
904 conn->cn_session = ses;
905 list_add(&conn->cn_persession, &ses->se_conns);
906}
907
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400908static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
909{
910 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400911
912 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400913 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400914 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400915}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400916
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400917static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400918{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400919 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400920 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400921}
922
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400923static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400924{
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400925 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400926
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400927 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400928 ret = nfsd4_register_conn(conn);
929 if (ret)
930 /* oops; xprt is already down: */
931 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -0400932 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -0400933 /* callback channel may be back up */
934 nfsd4_probe_callback(ses->se_client);
935 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400936}
937
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400938static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400939{
940 u32 dir = NFS4_CDFC4_FORE;
941
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400942 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400943 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400944 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400945}
946
947/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400948static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400949{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400950 struct nfs4_client *clp = s->se_client;
951 struct nfsd4_conn *c;
952
953 spin_lock(&clp->cl_lock);
954 while (!list_empty(&s->se_conns)) {
955 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
956 list_del_init(&c->cn_persession);
957 spin_unlock(&clp->cl_lock);
958
959 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
960 free_conn(c);
961
962 spin_lock(&clp->cl_lock);
963 }
964 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400965}
966
J. Bruce Fields1377b692012-09-11 21:42:40 -0400967static void __free_session(struct nfsd4_session *ses)
968{
969 nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
970 free_session_slots(ses);
971 kfree(ses);
972}
973
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400974static void free_session(struct nfsd4_session *ses)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400975{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400976 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300977
978 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400979 nfsd4_del_conns(ses);
J. Bruce Fields1377b692012-09-11 21:42:40 -0400980 __free_session(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400981}
982
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300983static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan,
984 struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400985{
986 struct nfsd4_session *new;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400987 int numslots, slotsize;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400988 /*
989 * Note decreasing slot size below client's request may
990 * make it difficult for client to function correctly, whereas
991 * decreasing the number of slots will (just?) affect
992 * performance. When short on memory we therefore prefer to
993 * decrease number of slots instead of their size.
994 */
995 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
996 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800997 if (numslots < 1)
998 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400999
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001000 new = __alloc_session(slotsize, numslots);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001001 if (!new) {
Yanchuan Nian74b70dd2012-12-24 18:11:27 +08001002 nfsd4_put_drc_mem(slotsize, numslots);
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001003 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001004 }
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001005 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize, nn);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001006 return new;
1007}
Andy Adamson557ce262009-08-28 08:45:04 -04001008
Fengguang Wu135ae822012-11-10 07:20:25 -05001009static 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 -04001010{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001011 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001012 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001013
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001014 new->se_client = clp;
1015 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001016
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001017 INIT_LIST_HEAD(&new->se_conns);
1018
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001019 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001020 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001021 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001022 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001023 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001024 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001025 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001026 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001027 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001028 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001029 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001030 spin_unlock(&nn->client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001031
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001032 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001033 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001034 /*
1035 * This is a little silly; with sessions there's no real
1036 * use for the callback address. Use the peer address
1037 * as a reasonable default for now, but consider fixing
1038 * the rpc client not to require an address in the
1039 * future:
1040 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001041 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1042 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001043 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001044}
1045
Benny Halevy9089f1b2010-05-12 00:12:26 +03001046/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001047static struct nfsd4_session *
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001048find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001049{
1050 struct nfsd4_session *elem;
1051 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001052 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001053
1054 dump_sessionid(__func__, sessionid);
1055 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001056 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001057 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001058 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1059 NFS4_MAX_SESSIONID_LEN)) {
1060 return elem;
1061 }
1062 }
1063
1064 dprintk("%s: session not found\n", __func__);
1065 return NULL;
1066}
1067
Benny Halevy9089f1b2010-05-12 00:12:26 +03001068/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001069static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001070unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001071{
1072 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001073 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001074 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001075 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1079static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001080STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001082 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001084 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001085 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return 1;
1087}
1088
1089/*
1090 * XXX Should we use a slab cache ?
1091 * This type of memory management is somewhat inefficient, but we use it
1092 * anyway since SETCLIENTID is not a common operation.
1093 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001094static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 struct nfs4_client *clp;
1097
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001098 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1099 if (clp == NULL)
1100 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001101 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001102 if (clp->cl_name.data == NULL) {
1103 kfree(clp);
1104 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001106 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return clp;
1108}
1109
1110static inline void
1111free_client(struct nfs4_client *clp)
1112{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001113 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001114
1115 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001116 while (!list_empty(&clp->cl_sessions)) {
1117 struct nfsd4_session *ses;
1118 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1119 se_perclnt);
1120 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001121 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1122 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001123 }
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001124 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001126 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 kfree(clp);
1128}
1129
Benny Halevy84d38ac2010-05-12 00:13:16 +03001130/* must be called under the client_lock */
1131static inline void
1132unhash_client_locked(struct nfs4_client *clp)
1133{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001134 struct nfsd4_session *ses;
1135
Benny Halevy84d38ac2010-05-12 00:13:16 +03001136 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001137 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001138 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1139 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001140 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001144destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001146 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001149 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 INIT_LIST_HEAD(&reaplist);
1152 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001153 while (!list_empty(&clp->cl_delegations)) {
1154 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001155 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 list_move(&dp->dl_recall_lru, &reaplist);
1157 }
1158 spin_unlock(&recall_lock);
1159 while (!list_empty(&reaplist)) {
1160 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unhash_delegation(dp);
1162 }
NeilBrownea1da632005-06-23 22:04:17 -07001163 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001164 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1165 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001167 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001168 if (clp->cl_cb_conn.cb_xprt)
1169 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001170 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001171 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001172 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001173 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001174 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001175 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001176 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001177 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1178 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001179 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001182static void expire_client(struct nfs4_client *clp)
1183{
1184 nfsd4_client_record_remove(clp);
1185 destroy_client(clp);
1186}
1187
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001188static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1189{
1190 memcpy(target->cl_verifier.data, source->data,
1191 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001194static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1195{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1197 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1198}
1199
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001200static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001201{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001202 if (source->cr_principal) {
1203 target->cr_principal =
1204 kstrdup(source->cr_principal, GFP_KERNEL);
1205 if (target->cr_principal == NULL)
1206 return -ENOMEM;
1207 } else
1208 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001209 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 target->cr_uid = source->cr_uid;
1211 target->cr_gid = source->cr_gid;
1212 target->cr_group_info = source->cr_group_info;
1213 get_group_info(target->cr_group_info);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001217static long long
1218compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1219{
1220 long long res;
1221
1222 res = o1->len - o2->len;
1223 if (res)
1224 return res;
1225 return (long long)memcmp(o1->data, o2->data, o1->len);
1226}
1227
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001228static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001229{
NeilBrowna55370a2005-06-23 22:03:52 -07001230 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001234same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1235{
1236 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001240same_clid(clientid_t *cl1, clientid_t *cl2)
1241{
1242 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001245static bool groups_equal(struct group_info *g1, struct group_info *g2)
1246{
1247 int i;
1248
1249 if (g1->ngroups != g2->ngroups)
1250 return false;
1251 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001252 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001253 return false;
1254 return true;
1255}
1256
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001257/*
1258 * RFC 3530 language requires clid_inuse be returned when the
1259 * "principal" associated with a requests differs from that previously
1260 * used. We use uid, gid's, and gss principal string as our best
1261 * approximation. We also don't want to allow non-gss use of a client
1262 * established using gss: in theory cr_principal should catch that
1263 * change, but in practice cr_principal can be null even in the gss case
1264 * since gssd doesn't always pass down a principal string.
1265 */
1266static bool is_gss_cred(struct svc_cred *cr)
1267{
1268 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1269 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1270}
1271
1272
Vivek Trivedi5559b502012-07-24 21:18:20 +05301273static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001274same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1275{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001276 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001277 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1278 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001279 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1280 return false;
1281 if (cr1->cr_principal == cr2->cr_principal)
1282 return true;
1283 if (!cr1->cr_principal || !cr2->cr_principal)
1284 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301285 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001288static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001289{
1290 static u32 current_clientid = 1;
1291
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001292 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 clp->cl_clientid.cl_id = current_clientid++;
1294}
1295
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001296static void gen_confirm(struct nfs4_client *clp)
1297{
Chuck Leverab4684d2012-03-02 17:13:50 -05001298 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001299 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Chuck Leverab4684d2012-03-02 17:13:50 -05001301 verf[0] = (__be32)get_seconds();
1302 verf[1] = (__be32)i++;
1303 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001306static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001307{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001308 struct nfs4_stid *ret;
1309
1310 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1311 if (!ret || !ret->sc_type)
1312 return NULL;
1313 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001314}
1315
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001316static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001317{
1318 struct nfs4_stid *s;
1319
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001320 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001321 if (!s)
1322 return NULL;
1323 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001324 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001325 return NULL;
1326}
1327
Jeff Layton2216d442012-11-12 15:00:57 -05001328static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001329 struct svc_rqst *rqstp, nfs4_verifier *verf)
1330{
1331 struct nfs4_client *clp;
1332 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001333 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001334 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001335 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001336
1337 clp = alloc_client(name);
1338 if (clp == NULL)
1339 return NULL;
1340
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001341 INIT_LIST_HEAD(&clp->cl_sessions);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001342 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1343 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001344 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001345 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001346 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001347 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001348 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001349 idr_init(&clp->cl_stateids);
Benny Halevy46583e22010-05-12 00:13:29 +03001350 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001351 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001352 INIT_LIST_HEAD(&clp->cl_idhash);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001353 INIT_LIST_HEAD(&clp->cl_openowners);
1354 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001355 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001356 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001357 spin_lock_init(&clp->cl_lock);
J. Bruce Fields57725152012-11-05 15:10:26 -05001358 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001359 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001360 clear_bit(0, &clp->cl_cb_slot_busy);
1361 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1362 copy_verf(clp, verf);
1363 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001364 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001365 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001366 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001367 return clp;
1368}
1369
NeilBrownfd39ca92005-06-23 22:04:03 -07001370static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001371add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1372{
1373 struct rb_node **new = &(root->rb_node), *parent = NULL;
1374 struct nfs4_client *clp;
1375
1376 while (*new) {
1377 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1378 parent = *new;
1379
1380 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1381 new = &((*new)->rb_left);
1382 else
1383 new = &((*new)->rb_right);
1384 }
1385
1386 rb_link_node(&new_clp->cl_namenode, parent, new);
1387 rb_insert_color(&new_clp->cl_namenode, root);
1388}
1389
1390static struct nfs4_client *
1391find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1392{
1393 long long cmp;
1394 struct rb_node *node = root->rb_node;
1395 struct nfs4_client *clp;
1396
1397 while (node) {
1398 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1399 cmp = compare_blob(&clp->cl_name, name);
1400 if (cmp > 0)
1401 node = node->rb_left;
1402 else if (cmp < 0)
1403 node = node->rb_right;
1404 else
1405 return clp;
1406 }
1407 return NULL;
1408}
1409
1410static void
1411add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001414 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001416 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001417 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001419 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001420 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
NeilBrownfd39ca92005-06-23 22:04:03 -07001423static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424move_to_confirmed(struct nfs4_client *clp)
1425{
1426 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001427 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001430 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001431 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001432 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001433 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 renew_client(clp);
1435}
1436
1437static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001438find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct nfs4_client *clp;
1441 unsigned int idhashval = clientid_hashval(clid->cl_id);
1442
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001443 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001444 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001445 if ((bool)clp->cl_minorversion != sessions)
1446 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001447 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451 return NULL;
1452}
1453
1454static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001455find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1456{
1457 struct list_head *tbl = nn->conf_id_hashtbl;
1458
1459 return find_client_in_id_table(tbl, clid, sessions);
1460}
1461
1462static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001463find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001465 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001467 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001470static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001471{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001472 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001473}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001474
NeilBrown28ce6052005-06-23 22:03:56 -07001475static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001476find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001477{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001478 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001479}
1480
1481static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001482find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001483{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001484 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001485}
1486
NeilBrownfd39ca92005-06-23 22:04:03 -07001487static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001488gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001490 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001491 struct sockaddr *sa = svc_addr(rqstp);
1492 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001493 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Jeff Layton7077ecb2009-08-14 12:57:58 -04001495 /* Currently, we only support tcp and tcp6 for the callback channel */
1496 if (se->se_callback_netid_len == 3 &&
1497 !memcmp(se->se_callback_netid_val, "tcp", 3))
1498 expected_family = AF_INET;
1499 else if (se->se_callback_netid_len == 4 &&
1500 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1501 expected_family = AF_INET6;
1502 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 goto out_err;
1504
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001505 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001506 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001507 (struct sockaddr *)&conn->cb_addr,
1508 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001509
J. Bruce Fields07263f12010-05-31 19:09:40 -04001510 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001512
J. Bruce Fields07263f12010-05-31 19:09:40 -04001513 if (conn->cb_addr.ss_family == AF_INET6)
1514 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001515
J. Bruce Fields07263f12010-05-31 19:09:40 -04001516 conn->cb_prog = se->se_callback_prog;
1517 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001518 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return;
1520out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001521 conn->cb_addr.ss_family = AF_UNSPEC;
1522 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001523 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 "will not receive delegations\n",
1525 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return;
1528}
1529
Andy Adamson074fe892009-04-03 08:28:15 +03001530/*
Andy Adamson557ce262009-08-28 08:45:04 -04001531 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001532 */
1533void
1534nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1535{
Andy Adamson557ce262009-08-28 08:45:04 -04001536 struct nfsd4_slot *slot = resp->cstate.slot;
1537 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001538
Andy Adamson557ce262009-08-28 08:45:04 -04001539 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001540
Andy Adamson557ce262009-08-28 08:45:04 -04001541 slot->sl_opcnt = resp->opcnt;
1542 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001543
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001544 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001545 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001546 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001547 return;
1548 }
Andy Adamson557ce262009-08-28 08:45:04 -04001549 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1550 base = (char *)resp->cstate.datap -
1551 (char *)resp->xbuf->head[0].iov_base;
1552 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1553 slot->sl_datalen))
1554 WARN("%s: sessions DRC could not cache compound\n", __func__);
1555 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001556}
1557
1558/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001559 * Encode the replay sequence operation from the slot values.
1560 * If cachethis is FALSE encode the uncached rep error on the next
1561 * operation which sets resp->p and increments resp->opcnt for
1562 * nfs4svc_encode_compoundres.
1563 *
Andy Adamson074fe892009-04-03 08:28:15 +03001564 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001565static __be32
1566nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1567 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001568{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001569 struct nfsd4_op *op;
1570 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001571
Andy Adamsonabfabf82009-07-23 19:02:18 -04001572 /* Encode the replayed sequence operation */
1573 op = &args->ops[resp->opcnt - 1];
1574 nfsd4_encode_operation(resp, op);
1575
1576 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001577 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001578 op = &args->ops[resp->opcnt++];
1579 op->status = nfserr_retry_uncached_rep;
1580 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001581 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001582 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001583}
1584
1585/*
Andy Adamson557ce262009-08-28 08:45:04 -04001586 * The sequence operation is not cached because we can use the slot and
1587 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001588 */
1589__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001590nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1591 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001592{
Andy Adamson557ce262009-08-28 08:45:04 -04001593 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001594 __be32 status;
1595
Andy Adamson557ce262009-08-28 08:45:04 -04001596 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001597
Andy Adamsonabfabf82009-07-23 19:02:18 -04001598 /* Either returns 0 or nfserr_retry_uncached */
1599 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1600 if (status == nfserr_retry_uncached_rep)
1601 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001602
Andy Adamson557ce262009-08-28 08:45:04 -04001603 /* The sequence operation has been encoded, cstate->datap set. */
1604 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001605
Andy Adamson557ce262009-08-28 08:45:04 -04001606 resp->opcnt = slot->sl_opcnt;
1607 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1608 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001609
1610 return status;
1611}
1612
Andy Adamson0733d212009-04-03 08:28:01 +03001613/*
1614 * Set the exchange_id flags returned by the server.
1615 */
1616static void
1617nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1618{
1619 /* pNFS is not supported */
1620 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1621
1622 /* Referrals are supported, Migration is not. */
1623 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1624
1625 /* set the wire flags to return to client. */
1626 clid->flags = new->cl_exchange_flags;
1627}
1628
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001629static bool client_has_state(struct nfs4_client *clp)
1630{
1631 /*
1632 * Note clp->cl_openowners check isn't quite right: there's no
1633 * need to count owners without stateid's.
1634 *
1635 * Also note we should probably be using this in 4.0 case too.
1636 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001637 return !list_empty(&clp->cl_openowners)
1638 || !list_empty(&clp->cl_delegations)
1639 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001640}
1641
Al Virob37ad282006-10-19 23:28:59 -07001642__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001643nfsd4_exchange_id(struct svc_rqst *rqstp,
1644 struct nfsd4_compound_state *cstate,
1645 struct nfsd4_exchange_id *exid)
1646{
Andy Adamson0733d212009-04-03 08:28:01 +03001647 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001648 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001649 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001650 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001651 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001652 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001653 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001654
Jeff Layton363168b2009-08-14 12:57:56 -04001655 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001656 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001657 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001658 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001659 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001660
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001661 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001662 return nfserr_inval;
1663
1664 /* Currently only support SP4_NONE */
1665 switch (exid->spa_how) {
1666 case SP4_NONE:
1667 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001668 default: /* checked by xdr code */
1669 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001670 case SP4_SSV:
Andy Adamson0733d212009-04-03 08:28:01 +03001671 case SP4_MACH_CRED:
1672 return nfserr_serverfault; /* no excuse :-/ */
1673 }
1674
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001675 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001676 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001677 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001678 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001679 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1680 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1681
J. Bruce Fields136e6582012-05-12 20:37:23 -04001682 if (update) {
1683 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001684 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001685 goto out;
1686 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001687 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001688 status = nfserr_perm;
1689 goto out;
1690 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001691 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001692 status = nfserr_not_same;
1693 goto out;
1694 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001695 /* case 6 */
1696 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1697 new = conf;
1698 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001699 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001700 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001701 if (client_has_state(conf)) {
1702 status = nfserr_clid_inuse;
1703 goto out;
1704 }
Andy Adamson0733d212009-04-03 08:28:01 +03001705 expire_client(conf);
1706 goto out_new;
1707 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001708 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001709 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001710 new = conf;
1711 goto out_copy;
1712 }
1713 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001714 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001715 }
1716
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001717 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001718 status = nfserr_noent;
1719 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001720 }
1721
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001722 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001723 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001724 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001725
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001726 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001727out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001728 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001729 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001730 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001731 goto out;
1732 }
J. Bruce Fieldsc116a0a2012-09-11 14:53:09 -04001733 new->cl_minorversion = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001734
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001735 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001736 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001737out_copy:
1738 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1739 exid->clientid.cl_id = new->cl_clientid.cl_id;
1740
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001741 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001742 nfsd4_set_ex_flags(new, exid);
1743
1744 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001745 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001746 status = nfs_ok;
1747
1748out:
1749 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001750 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001751}
1752
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001753static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001754check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001755{
Andy Adamson88e588d2009-07-23 19:02:15 -04001756 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1757 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001758
1759 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001760 if (slot_inuse) {
1761 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001762 return nfserr_jukebox;
1763 else
1764 return nfserr_seq_misordered;
1765 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001766 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001767 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001768 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001769 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001770 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001771 return nfserr_seq_misordered;
1772}
1773
Andy Adamson49557cc2009-07-23 19:02:16 -04001774/*
1775 * Cache the create session result into the create session single DRC
1776 * slot cache by saving the xdr structure. sl_seqid has been set.
1777 * Do this for solo or embedded create session operations.
1778 */
1779static void
1780nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001781 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001782{
1783 slot->sl_status = nfserr;
1784 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1785}
1786
1787static __be32
1788nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1789 struct nfsd4_clid_slot *slot)
1790{
1791 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1792 return slot->sl_status;
1793}
1794
Mi Jinlong1b74c252011-07-14 14:50:17 +08001795#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1796 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1797 1 + /* MIN tag is length with zero, only length */ \
1798 3 + /* version, opcount, opcode */ \
1799 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1800 /* seqid, slotID, slotID, cache */ \
1801 4 ) * sizeof(__be32))
1802
1803#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1804 2 + /* verifier: AUTH_NULL, length 0 */\
1805 1 + /* status */ \
1806 1 + /* MIN tag is length with zero, only length */ \
1807 3 + /* opcount, opcode, opstatus*/ \
1808 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1809 /* seqid, slotID, slotID, slotID, status */ \
1810 5 ) * sizeof(__be32))
1811
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001812static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001813{
1814 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1815 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1816}
1817
Andy Adamson069b6ad2009-04-03 08:27:58 +03001818__be32
1819nfsd4_create_session(struct svc_rqst *rqstp,
1820 struct nfsd4_compound_state *cstate,
1821 struct nfsd4_create_session *cr_ses)
1822{
Jeff Layton363168b2009-08-14 12:57:56 -04001823 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001824 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001825 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001826 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001827 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001828 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001829 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001830
Mi Jinlonga62573d2011-03-23 17:57:07 +08001831 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1832 return nfserr_inval;
J. Bruce Fields49730502012-09-11 15:33:21 -04001833 if (check_forechannel_attrs(cr_ses->fore_channel))
1834 return nfserr_toosmall;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001835 new = alloc_session(&cr_ses->fore_channel, nn);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001836 if (!new)
1837 return nfserr_jukebox;
1838 status = nfserr_jukebox;
1839 conn = alloc_conn_from_crses(rqstp, cr_ses);
1840 if (!conn)
1841 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08001842
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001843 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001844 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001845 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04001846 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001847
1848 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001849 cs_slot = &conf->cl_cs_slot;
1850 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001851 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001852 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001853 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001854 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001855 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001856 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001857 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001858 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001859 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001860 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001861 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001862 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001863 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001864 }
Andy Adamson49557cc2009-07-23 19:02:16 -04001865 cs_slot = &unconf->cl_cs_slot;
1866 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001867 if (status) {
1868 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001869 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001870 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001871 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001872 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001873 if (old) {
1874 status = mark_client_expired(old);
1875 if (status)
1876 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001877 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001878 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001879 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001880 conf = unconf;
1881 } else {
1882 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001883 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001884 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001885 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001886 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001887 * We do not support RDMA or persistent sessions
1888 */
1889 cr_ses->flags &= ~SESSION4_PERSIST;
1890 cr_ses->flags &= ~SESSION4_RDMA;
1891
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001892 init_session(rqstp, new, conf, cr_ses);
1893 nfsd4_init_conn(rqstp, conn, new);
1894
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001895 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001896 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001897 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1898 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001899 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001900 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001901
Andy Adamson49557cc2009-07-23 19:02:16 -04001902 /* cache solo and embedded create sessions under the state lock */
1903 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001904 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001905 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001906out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08001907 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001908 free_conn(conn);
1909out_free_session:
1910 __free_session(new);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04001911 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001912}
1913
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001914static __be32 nfsd4_map_bcts_dir(u32 *dir)
1915{
1916 switch (*dir) {
1917 case NFS4_CDFC4_FORE:
1918 case NFS4_CDFC4_BACK:
1919 return nfs_ok;
1920 case NFS4_CDFC4_FORE_OR_BOTH:
1921 case NFS4_CDFC4_BACK_OR_BOTH:
1922 *dir = NFS4_CDFC4_BOTH;
1923 return nfs_ok;
1924 };
1925 return nfserr_inval;
1926}
1927
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001928__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1929{
1930 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001931 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001932
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001933 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001934 session->se_cb_prog = bc->bc_cb_program;
1935 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001936 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001937
1938 nfsd4_probe_callback(session->se_client);
1939
1940 return nfs_ok;
1941}
1942
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001943__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1944 struct nfsd4_compound_state *cstate,
1945 struct nfsd4_bind_conn_to_session *bcts)
1946{
1947 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001948 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001949 struct nfsd4_session *session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001950 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001951
1952 if (!nfsd4_last_compound_op(rqstp))
1953 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001954 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001955 spin_lock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001956 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001957 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001958 status = nfserr_badsession;
1959 if (!session)
1960 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001961 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001962 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001963 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001964 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001965 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001966 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001967 goto out;
1968 nfsd4_init_conn(rqstp, conn, session);
1969 status = nfs_ok;
1970out:
1971 nfs4_unlock_state();
1972 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001973}
1974
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001975static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1976{
1977 if (!session)
1978 return 0;
1979 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1980}
1981
Andy Adamson069b6ad2009-04-03 08:27:58 +03001982__be32
1983nfsd4_destroy_session(struct svc_rqst *r,
1984 struct nfsd4_compound_state *cstate,
1985 struct nfsd4_destroy_session *sessionid)
1986{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001987 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001988 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001989 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001990
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001991 nfs4_lock_state();
1992 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001993 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001994 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001995 goto out;
J. Bruce Fields57716352010-04-21 12:27:19 -04001996 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001997 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001998 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001999 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002000 status = nfserr_badsession;
2001 if (!ses)
2002 goto out_client_lock;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002003 status = mark_session_dead_locked(ses);
2004 if (status)
2005 goto out_client_lock;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002006 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002007 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002008
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002009 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002010
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002011 spin_lock(&nn->client_lock);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002012 free_session(ses);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002013 status = nfs_ok;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002014out_client_lock:
2015 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002016out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002017 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002018 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002019}
2020
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002021static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002022{
2023 struct nfsd4_conn *c;
2024
2025 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002026 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002027 return c;
2028 }
2029 }
2030 return NULL;
2031}
2032
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002033static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002034{
2035 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002036 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002037 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002038
2039 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002040 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002041 if (c) {
2042 spin_unlock(&clp->cl_lock);
2043 free_conn(new);
2044 return;
2045 }
2046 __nfsd4_hash_conn(new, ses);
2047 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002048 ret = nfsd4_register_conn(new);
2049 if (ret)
2050 /* oops; xprt is already down: */
2051 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002052 return;
2053}
2054
Mi Jinlong868b89c2011-04-27 09:09:58 +08002055static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2056{
2057 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2058
2059 return args->opcnt > session->se_fchannel.maxops;
2060}
2061
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002062static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2063 struct nfsd4_session *session)
2064{
2065 struct xdr_buf *xb = &rqstp->rq_arg;
2066
2067 return xb->len > session->se_fchannel.maxreq_sz;
2068}
2069
Andy Adamson069b6ad2009-04-03 08:27:58 +03002070__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002071nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002072 struct nfsd4_compound_state *cstate,
2073 struct nfsd4_sequence *seq)
2074{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002075 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002076 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002077 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002078 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002079 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002080 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002081 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002082
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002083 if (resp->opcnt != 1)
2084 return nfserr_sequence_pos;
2085
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002086 /*
2087 * Will be either used or freed by nfsd4_sequence_check_conn
2088 * below.
2089 */
2090 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2091 if (!conn)
2092 return nfserr_jukebox;
2093
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002094 spin_lock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002095 status = nfserr_badsession;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002096 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
Benny Halevyb85d4c02009-04-03 08:28:08 +03002097 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002098 goto out_no_session;
2099 clp = session->se_client;
2100 status = get_client_locked(clp);
2101 if (status)
2102 goto out_no_session;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002103 status = nfsd4_get_session_locked(session);
2104 if (status)
2105 goto out_put_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002106
Mi Jinlong868b89c2011-04-27 09:09:58 +08002107 status = nfserr_too_many_ops;
2108 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002109 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002110
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002111 status = nfserr_req_too_big;
2112 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002113 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002114
Benny Halevyb85d4c02009-04-03 08:28:08 +03002115 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002116 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002117 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002118
Andy Adamson557ce262009-08-28 08:45:04 -04002119 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002120 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2121
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002122 /* We do not negotiate the number of slots yet, so set the
2123 * maxslots to the session maxreqs which is used to encode
2124 * sr_highest_slotid and the sr_target_slot id to maxslots */
2125 seq->maxslots = session->se_fchannel.maxreqs;
2126
J. Bruce Fields73e79482012-02-13 16:39:00 -05002127 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2128 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002129 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002130 status = nfserr_seq_misordered;
2131 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002132 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002133 cstate->slot = slot;
2134 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002135 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002136 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002137 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002138 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002139 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002140 }
2141 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002142 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002143
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002144 nfsd4_sequence_check_conn(conn, session);
2145 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002146
Benny Halevyb85d4c02009-04-03 08:28:08 +03002147 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002148 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002149 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002150 if (seq->cachethis)
2151 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002152 else
2153 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002154
2155 cstate->slot = slot;
2156 cstate->session = session;
2157
Benny Halevyb85d4c02009-04-03 08:28:08 +03002158out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002159 switch (clp->cl_cb_state) {
2160 case NFSD4_CB_DOWN:
2161 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2162 break;
2163 case NFSD4_CB_FAULT:
2164 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2165 break;
2166 default:
2167 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002168 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002169out_no_session:
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002170 kfree(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002171 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002172 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002173out_put_session:
2174 nfsd4_put_session(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002175out_put_client:
2176 put_client_renew_locked(clp);
2177 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002178}
2179
Mi Jinlong345c2842011-10-20 17:51:39 +08002180__be32
2181nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2182{
2183 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002184 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002185 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002186
2187 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002188 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002189 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002190 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002191
2192 if (conf) {
2193 clp = conf;
2194
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002195 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002196 status = nfserr_clientid_busy;
2197 goto out;
2198 }
2199 } else if (unconf)
2200 clp = unconf;
2201 else {
2202 status = nfserr_stale_clientid;
2203 goto out;
2204 }
2205
2206 expire_client(clp);
2207out:
2208 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002209 return status;
2210}
2211
Andy Adamson069b6ad2009-04-03 08:27:58 +03002212__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002213nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2214{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002215 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002216
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002217 if (rc->rca_one_fs) {
2218 if (!cstate->current_fh.fh_dentry)
2219 return nfserr_nofilehandle;
2220 /*
2221 * We don't take advantage of the rca_one_fs case.
2222 * That's OK, it's optional, we can safely ignore it.
2223 */
2224 return nfs_ok;
2225 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002226
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002227 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002228 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002229 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2230 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002231 goto out;
2232
2233 status = nfserr_stale_clientid;
2234 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002235 /*
2236 * The following error isn't really legal.
2237 * But we only get here if the client just explicitly
2238 * destroyed the client. Surely it no longer cares what
2239 * error it gets back on an operation for the dead
2240 * client.
2241 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002242 goto out;
2243
2244 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002245 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002246out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002247 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002248 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002249}
2250
2251__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002252nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2253 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002255 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002257 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002258 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002259 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2260
J. Bruce Fields63db4632012-05-18 21:54:19 -04002261 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002263 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002264 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002265 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002267 if (clp_used_exchangeid(conf))
2268 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002269 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002270 char addr_str[INET6_ADDRSTRLEN];
2271 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2272 sizeof(addr_str));
2273 dprintk("NFSD: setclientid: string in use by client "
2274 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto out;
2276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002278 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002279 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002282 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002283 if (new == NULL)
2284 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002285 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002286 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002288 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002289 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002290 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002291 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002292 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2294 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2295 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2296 status = nfs_ok;
2297out:
2298 nfs4_unlock_state();
2299 return status;
2300}
2301
2302
Al Virob37ad282006-10-19 23:28:59 -07002303__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002304nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2305 struct nfsd4_compound_state *cstate,
2306 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
NeilBrown21ab45a2005-06-23 22:04:14 -07002308 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2310 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002311 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002312 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002314 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002317
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002318 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002319 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002320 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002321 * We try hard to give out unique clientid's, so if we get an
2322 * attempt to confirm the same clientid with a different cred,
2323 * there's a bug somewhere. Let's charitably assume it's our
2324 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002325 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002326 status = nfserr_serverfault;
2327 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2328 goto out;
2329 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2330 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002331 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002332 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2333 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002335 else /* case 4: client hasn't noticed we rebooted yet? */
2336 status = nfserr_stale_clientid;
2337 goto out;
2338 }
2339 status = nfs_ok;
2340 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002341 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2342 nfsd4_probe_callback(conf);
2343 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002344 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002345 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002346 if (conf) {
2347 status = mark_client_expired(conf);
2348 if (status)
2349 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002350 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002351 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002352 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002353 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 nfs4_unlock_state();
2357 return status;
2358}
2359
J. Bruce Fields32513b42011-10-13 16:00:16 -04002360static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002362 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2363}
2364
2365/* OPEN Share state helper functions */
2366static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 unsigned int hashval = file_hashval(ino);
2369
J. Bruce Fields32513b42011-10-13 16:00:16 -04002370 atomic_set(&fp->fi_ref, 1);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002371 INIT_LIST_HEAD(&fp->fi_stateids);
2372 INIT_LIST_HEAD(&fp->fi_delegations);
2373 fp->fi_inode = igrab(ino);
2374 fp->fi_had_conflict = false;
2375 fp->fi_lease = NULL;
2376 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2377 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2378 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002379 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002380 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
2383static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002384nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002385{
NeilBrowne60d4392005-06-23 22:03:01 -07002386 if (*slab == NULL)
2387 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002388 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002389 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002390}
2391
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002392void
NeilBrowne60d4392005-06-23 22:03:01 -07002393nfsd4_free_slabs(void)
2394{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002395 nfsd4_free_slab(&openowner_slab);
2396 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002397 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002398 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002399 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Bryan Schumaker72083392011-11-01 15:24:59 -04002402int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403nfsd4_init_slabs(void)
2404{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002405 openowner_slab = kmem_cache_create("nfsd4_openowners",
2406 sizeof(struct nfs4_openowner), 0, 0, NULL);
2407 if (openowner_slab == NULL)
2408 goto out_nomem;
2409 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002410 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002411 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002412 goto out_nomem;
2413 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002414 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002415 if (file_slab == NULL)
2416 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002417 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002418 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002419 if (stateid_slab == NULL)
2420 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002421 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002422 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002423 if (deleg_slab == NULL)
2424 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002426out_nomem:
2427 nfsd4_free_slabs();
2428 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2429 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002432void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002434 kfree(oo->oo_owner.so_owner.data);
2435 kmem_cache_free(openowner_slab, oo);
2436}
2437
2438void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2439{
2440 kfree(lo->lo_owner.so_owner.data);
2441 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002444static void init_nfs4_replay(struct nfs4_replay *rp)
2445{
2446 rp->rp_status = nfserr_serverfault;
2447 rp->rp_buflen = 0;
2448 rp->rp_buf = rp->rp_ibuf;
2449}
2450
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002451static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
2453 struct nfs4_stateowner *sop;
2454
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002455 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002456 if (!sop)
2457 return NULL;
2458
2459 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2460 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002461 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002462 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002464 sop->so_owner.len = owner->len;
2465
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002466 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002467 sop->so_client = clp;
2468 init_nfs4_replay(&sop->so_replay);
2469 return sop;
2470}
2471
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002472static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002473{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002474 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2475
2476 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002477 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002480static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002482 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002484 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2485 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002487 oo->oo_owner.so_is_open_owner = 1;
2488 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002489 oo->oo_flags = NFS4_OO_NEW;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002490 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002491 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002492 INIT_LIST_HEAD(&oo->oo_close_lru);
2493 hash_openowner(oo, clp, strhashval);
2494 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
J. Bruce Fields996e0932011-10-17 11:14:48 -04002497static 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 -04002498 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002500 stp->st_stid.sc_type = NFS4_OPEN_STID;
NeilBrownea1da632005-06-23 22:04:17 -07002501 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002502 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002503 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002504 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002505 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 stp->st_access_bmap = 0;
2508 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002509 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002510 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002511 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002515move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002517 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2518
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002519 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002521 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002522 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002526same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2527 clientid_t *clid)
2528{
2529 return (sop->so_owner.len == owner->len) &&
2530 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2531 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002534static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002535find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2536 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002538 struct nfs4_stateowner *so;
2539 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002540 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002542 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002543 if (!so->so_is_open_owner)
2544 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002545 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2546 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002547 clp = oo->oo_owner.so_client;
2548 if ((bool)clp->cl_minorversion != sessions)
2549 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002550 renew_client(oo->oo_owner.so_client);
2551 return oo;
2552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554 return NULL;
2555}
2556
2557/* search file_hashtbl[] for file */
2558static struct nfs4_file *
2559find_file(struct inode *ino)
2560{
2561 unsigned int hashval = file_hashval(ino);
2562 struct nfs4_file *fp;
2563
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002564 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002565 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002566 if (fp->fi_inode == ino) {
2567 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002568 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002572 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return NULL;
2574}
2575
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002576/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 * Called to check deny when READ with all zero stateid or
2578 * WRITE with all zero or all one stateid
2579 */
Al Virob37ad282006-10-19 23:28:59 -07002580static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2582{
2583 struct inode *ino = current_fh->fh_dentry->d_inode;
2584 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002585 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002586 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002589 if (!fp)
2590 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002591 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002593 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002594 if (test_deny(deny_type, stp) ||
2595 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
NeilBrown13cd2182005-06-23 22:03:10 -07002598 ret = nfs_ok;
2599out:
2600 put_nfs4_file(fp);
2601 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002604static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002606 struct nfs4_client *clp = dp->dl_stid.sc_client;
2607 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 /* We're assuming the state code never drops its reference
2610 * without first removing the lease. Since we're in this lease
2611 * callback (and since the lease code is serialized by the kernel
2612 * lock) we know the server hasn't removed the lease yet, we know
2613 * it's safe to take a reference: */
2614 atomic_inc(&dp->dl_count);
2615
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002616 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Arnd Bergmann460781b2010-11-17 16:26:56 +01002618 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 dp->dl_time = get_seconds();
2620
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002621 nfsd4_cb_recall(dp);
2622}
2623
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002624/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002625static void nfsd_break_deleg_cb(struct file_lock *fl)
2626{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002627 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2628 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002629
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002630 if (!fp) {
2631 WARN(1, "(%p)->fl_owner NULL\n", fl);
2632 return;
2633 }
2634 if (fp->fi_had_conflict) {
2635 WARN(1, "duplicate break on %p\n", fp);
2636 return;
2637 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002638 /*
2639 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002640 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002641 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002642 */
2643 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002645 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002646 fp->fi_had_conflict = true;
2647 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2648 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002649 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652static
2653int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2654{
2655 if (arg & F_UNLCK)
2656 return lease_modify(onlist, arg);
2657 else
2658 return -EAGAIN;
2659}
2660
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002661static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002662 .lm_break = nfsd_break_deleg_cb,
2663 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664};
2665
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002666static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2667{
2668 if (nfsd4_has_session(cstate))
2669 return nfs_ok;
2670 if (seqid == so->so_seqid - 1)
2671 return nfserr_replay_me;
2672 if (seqid == so->so_seqid)
2673 return nfs_ok;
2674 return nfserr_bad_seqid;
2675}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Al Virob37ad282006-10-19 23:28:59 -07002677__be32
Andy Adamson66689582009-04-03 08:28:45 +03002678nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03002679 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 clientid_t *clientid = &open->op_clientid;
2682 struct nfs4_client *clp = NULL;
2683 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002684 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002685 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002687 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002689 /*
2690 * In case we need it later, after we've already created the
2691 * file and don't want to risk a further failure:
2692 */
2693 open->op_file = nfsd4_alloc_file();
2694 if (open->op_file == NULL)
2695 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002697 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002698 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002699 open->op_openowner = oo;
2700 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002701 clp = find_confirmed_client(clientid, cstate->minorversion,
2702 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002704 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002705 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002707 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002708 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002709 clp = oo->oo_owner.so_client;
2710 release_openowner(oo);
2711 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002712 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002713 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002714 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2715 if (status)
2716 return status;
2717 clp = oo->oo_owner.so_client;
2718 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002719new_owner:
2720 oo = alloc_init_open_stateowner(strhashval, clp, open);
2721 if (oo == NULL)
2722 return nfserr_jukebox;
2723 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002724alloc_stateid:
2725 open->op_stp = nfs4_alloc_stateid(clp);
2726 if (!open->op_stp)
2727 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002728 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Al Virob37ad282006-10-19 23:28:59 -07002731static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002732nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2733{
2734 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2735 return nfserr_openmode;
2736 else
2737 return nfs_ok;
2738}
2739
Daniel Mackc47d8322011-05-16 16:38:14 +02002740static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002741{
J. Bruce Fields24a01112010-05-18 20:01:35 -04002742 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2743}
2744
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002745static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002746{
2747 struct nfs4_stid *ret;
2748
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002749 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002750 if (!ret)
2751 return NULL;
2752 return delegstateid(ret);
2753}
2754
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002755static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2756{
2757 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2758 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2759}
2760
Al Virob37ad282006-10-19 23:28:59 -07002761static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04002762nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07002763 struct nfs4_delegation **dp)
2764{
2765 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002766 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002767
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002768 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002769 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002770 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002771 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002772 status = nfs4_check_delegmode(*dp, flags);
2773 if (status)
2774 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002775out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002776 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07002777 return nfs_ok;
2778 if (status)
2779 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002780 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002781 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002782}
2783
Al Virob37ad282006-10-19 23:28:59 -07002784static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002785nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002787 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002788 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
NeilBrown8beefa22005-06-23 22:03:08 -07002790 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 /* ignore lock owners */
2792 if (local->st_stateowner->so_is_open_owner == 0)
2793 continue;
2794 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002795 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 *stpp = local;
2797 /* check for conflicting share reservations */
2798 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002799 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002801 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002804static inline int nfs4_access_to_access(u32 nfs4_access)
2805{
2806 int flags = 0;
2807
2808 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2809 flags |= NFSD_MAY_READ;
2810 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2811 flags |= NFSD_MAY_WRITE;
2812 return flags;
2813}
2814
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002815static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2816 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002817{
2818 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002819 int oflag = nfs4_access_to_omode(open->op_share_access);
2820 int access = nfs4_access_to_access(open->op_share_access);
2821
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002822 if (!fp->fi_fds[oflag]) {
2823 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2824 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002825 if (status)
2826 return status;
2827 }
2828 nfs4_file_get_access(fp, oflag);
2829
2830 return nfs_ok;
2831}
2832
Al Virob37ad282006-10-19 23:28:59 -07002833static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2835 struct nfsd4_open *open)
2836{
2837 struct iattr iattr = {
2838 .ia_valid = ATTR_SIZE,
2839 .ia_size = 0,
2840 };
2841 if (!open->op_truncate)
2842 return 0;
2843 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002844 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2846}
2847
Al Virob37ad282006-10-19 23:28:59 -07002848static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002849nfs4_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 -07002850{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002851 u32 op_share_access = open->op_share_access;
J. Bruce Fields7d947842010-08-20 18:09:31 -04002852 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002853 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Jeff Layton82c5ff12012-05-11 09:45:13 -04002855 new_access = !test_access(op_share_access, stp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002856 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002857 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002858 if (status)
2859 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 status = nfsd4_truncate(rqstp, cur_fh, open);
2862 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002863 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002864 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002865 nfs4_file_put_access(fp, oflag);
2866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return status;
2868 }
2869 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04002870 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002871 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 return nfs_ok;
2874}
2875
2876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05002878nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002880 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
2882
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002883/* Should we give out recallable state?: */
2884static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2885{
2886 if (clp->cl_cb_state == NFSD4_CB_UP)
2887 return true;
2888 /*
2889 * In the sessions case, since we don't have to establish a
2890 * separate connection for callbacks, we assume it's OK
2891 * until we hear otherwise:
2892 */
2893 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2894}
2895
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002896static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2897{
2898 struct file_lock *fl;
2899
2900 fl = locks_alloc_lock();
2901 if (!fl)
2902 return NULL;
2903 locks_init_lock(fl);
2904 fl->fl_lmops = &nfsd_lease_mng_ops;
2905 fl->fl_flags = FL_LEASE;
2906 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2907 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002908 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002909 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002910 return fl;
2911}
2912
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002913static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2914{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002915 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002916 struct file_lock *fl;
2917 int status;
2918
2919 fl = nfs4_alloc_init_lease(dp, flag);
2920 if (!fl)
2921 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002922 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002923 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002924 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002925 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002926 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002927 locks_free_lock(fl);
2928 return -ENOMEM;
2929 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002930 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04002931 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002932 atomic_set(&fp->fi_delegees, 1);
2933 list_add(&dp->dl_perfile, &fp->fi_delegations);
2934 return 0;
2935}
2936
2937static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2938{
2939 struct nfs4_file *fp = dp->dl_file;
2940
2941 if (!fp->fi_lease)
2942 return nfs4_setlease(dp, flag);
2943 spin_lock(&recall_lock);
2944 if (fp->fi_had_conflict) {
2945 spin_unlock(&recall_lock);
2946 return -EAGAIN;
2947 }
2948 atomic_inc(&fp->fi_delegees);
2949 list_add(&dp->dl_perfile, &fp->fi_delegations);
2950 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002951 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002952 return 0;
2953}
2954
Benny Halevy4aa89132012-02-21 14:16:44 -08002955static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2956{
2957 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2958 if (status == -EAGAIN)
2959 open->op_why_no_deleg = WND4_CONTENTION;
2960 else {
2961 open->op_why_no_deleg = WND4_RESOURCE;
2962 switch (open->op_deleg_want) {
2963 case NFS4_SHARE_WANT_READ_DELEG:
2964 case NFS4_SHARE_WANT_WRITE_DELEG:
2965 case NFS4_SHARE_WANT_ANY_DELEG:
2966 break;
2967 case NFS4_SHARE_WANT_CANCEL:
2968 open->op_why_no_deleg = WND4_CANCELLED;
2969 break;
2970 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002971 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08002972 }
2973 }
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976/*
2977 * Attempt to hand out a delegation.
2978 */
2979static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002980nfs4_open_delegation(struct net *net, struct svc_fh *fh,
2981 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002984 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002985 int cb_up;
Benny Halevyd24433c2012-02-16 20:57:17 +02002986 int status = 0, flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002988 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002990 open->op_recall = 0;
2991 switch (open->op_claim_type) {
2992 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002993 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002994 open->op_recall = 1;
2995 flag = open->op_delegate_type;
2996 if (flag == NFS4_OPEN_DELEGATE_NONE)
2997 goto out;
2998 break;
2999 case NFS4_OPEN_CLAIM_NULL:
3000 /* Let's not give out any delegations till everyone's
3001 * had the chance to reclaim theirs.... */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003002 if (locks_in_grace(net))
NeilBrown7b190fe2005-06-23 22:03:23 -07003003 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003004 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07003005 goto out;
3006 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3007 flag = NFS4_OPEN_DELEGATE_WRITE;
3008 else
3009 flag = NFS4_OPEN_DELEGATE_READ;
3010 break;
3011 default:
3012 goto out;
3013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003015 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003016 if (dp == NULL)
3017 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003018 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003019 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003020 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003022 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003024 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003025 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026out:
3027 open->op_delegate_type = flag;
Benny Halevyd24433c2012-02-16 20:57:17 +02003028 if (flag == NFS4_OPEN_DELEGATE_NONE) {
3029 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3030 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
3031 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3032
Benny Halevy4aa89132012-02-21 14:16:44 -08003033 /* 4.1 client asking for a delegation? */
3034 if (open->op_deleg_want)
3035 nfsd4_open_deleg_none_ext(open, status);
Benny Halevyd24433c2012-02-16 20:57:17 +02003036 }
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003037 return;
3038out_free:
J. Bruce Fields24ffb932012-12-12 15:24:12 -05003039 unhash_stid(&dp->dl_stid);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003040 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003041out_no_deleg:
3042 flag = NFS4_OPEN_DELEGATE_NONE;
3043 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
Benny Halevye27f49c2012-02-21 14:16:54 -08003046static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3047 struct nfs4_delegation *dp)
3048{
3049 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3050 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3051 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3052 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3053 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3054 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3055 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3056 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3057 }
3058 /* Otherwise the client must be confused wanting a delegation
3059 * it already has, therefore we don't return
3060 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3061 */
3062}
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064/*
3065 * called with nfs4_lock_state() held.
3066 */
Al Virob37ad282006-10-19 23:28:59 -07003067__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3069{
Andy Adamson66689582009-04-03 08:28:45 +03003070 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003071 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 struct nfs4_file *fp = NULL;
3073 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003074 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003075 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003076 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 /*
3079 * Lookup file; if found, lookup stateid and check open request,
3080 * and check for delegations in the process of being recalled.
3081 * If not found, create the nfs4_file struct
3082 */
3083 fp = find_file(ino);
3084 if (fp) {
3085 if ((status = nfs4_check_open(fp, open, &stp)))
3086 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003087 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003088 if (status)
3089 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07003091 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003092 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003093 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003094 status = nfserr_jukebox;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003095 fp = open->op_file;
3096 open->op_file = NULL;
3097 nfsd4_init_file(fp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
3099
3100 /*
3101 * OPEN the file, or upgrade an existing OPEN.
3102 * If truncate fails, the OPEN fails.
3103 */
3104 if (stp) {
3105 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003106 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if (status)
3108 goto out;
3109 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003110 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003111 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 goto out;
J. Bruce Fields4af82502012-06-07 17:30:45 -04003113 status = nfsd4_truncate(rqstp, current_fh, open);
3114 if (status)
3115 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003116 stp = open->op_stp;
3117 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003118 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003120 update_stateid(&stp->st_stid.sc_stateid);
3121 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Benny Halevyd24433c2012-02-16 20:57:17 +02003123 if (nfsd4_has_session(&resp->cstate)) {
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003124 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03003125
Benny Halevyd24433c2012-02-16 20:57:17 +02003126 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3127 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3128 open->op_why_no_deleg = WND4_NOT_WANTED;
3129 goto nodeleg;
3130 }
3131 }
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 /*
3134 * Attempt to hand out a delegation. No error return, because the
3135 * OPEN succeeds even if we fail.
3136 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003137 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003138nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 status = nfs_ok;
3140
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003141 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003142 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003144 /* 4.1 client trying to upgrade/downgrade delegation? */
3145 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003146 open->op_deleg_want)
3147 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003148
NeilBrown13cd2182005-06-23 22:03:10 -07003149 if (fp)
3150 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003151 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003152 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /*
3154 * To finish the open response, we just need to set the rflags.
3155 */
3156 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003157 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003158 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3160
3161 return status;
3162}
3163
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003164void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3165{
3166 if (open->op_openowner) {
3167 struct nfs4_openowner *oo = open->op_openowner;
3168
3169 if (!list_empty(&oo->oo_owner.so_stateids))
3170 list_del_init(&oo->oo_close_lru);
3171 if (oo->oo_flags & NFS4_OO_NEW) {
3172 if (status) {
3173 release_openowner(oo);
3174 open->op_openowner = NULL;
3175 } else
3176 oo->oo_flags &= ~NFS4_OO_NEW;
3177 }
3178 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003179 if (open->op_file)
3180 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003181 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003182 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003183}
3184
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003185static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3186{
3187 struct nfs4_client *found;
3188
3189 if (STALE_CLIENTID(clid, nn))
3190 return nfserr_stale_clientid;
3191 found = find_confirmed_client(clid, session, nn);
3192 if (clp)
3193 *clp = found;
3194 return found ? nfs_ok : nfserr_expired;
3195}
3196
Al Virob37ad282006-10-19 23:28:59 -07003197__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003198nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3199 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003202 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003203 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 nfs4_lock_state();
3206 dprintk("process_renew(%08x/%08x): starting\n",
3207 clid->cl_boot, clid->cl_id);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003208 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3209 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003212 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003213 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 goto out;
3215 status = nfs_ok;
3216out:
3217 nfs4_unlock_state();
3218 return status;
3219}
3220
NeilBrowna76b4312005-06-23 22:04:01 -07003221static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003222nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003223{
Jeff Layton33dcc482012-04-10 11:08:48 -04003224 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003225 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003226 return;
3227
NeilBrowna76b4312005-06-23 22:04:01 -07003228 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003229 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003230 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003231 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003232 /*
3233 * Now that every NFSv4 client has had the chance to recover and
3234 * to see the (possibly new, possibly shorter) lease time, we
3235 * can safely set the next grace time to the current lease time:
3236 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003237 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003238}
3239
NeilBrownfd39ca92005-06-23 22:04:03 -07003240static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003241nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
3243 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003244 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 struct nfs4_delegation *dp;
3246 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003247 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3248 time_t t, clientid_val = nn->nfsd4_lease;
3249 time_t u, test_val = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 nfs4_lock_state();
3252
3253 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003254 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003255 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003256 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003257 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 clp = list_entry(pos, struct nfs4_client, cl_lru);
3259 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3260 t = clp->cl_time - cutoff;
3261 if (clientid_val > t)
3262 clientid_val = t;
3263 break;
3264 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003265 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003266 dprintk("NFSD: client in use (clientid %08x)\n",
3267 clp->cl_clientid.cl_id);
3268 continue;
3269 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003270 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003271 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003272 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003273 list_for_each_safe(pos, next, &reaplist) {
3274 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 dprintk("NFSD: purging unused client (clientid %08x)\n",
3276 clp->cl_clientid.cl_id);
3277 expire_client(clp);
3278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003280 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003282 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3283 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3285 u = dp->dl_time - cutoff;
3286 if (test_val > u)
3287 test_val = u;
3288 break;
3289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 list_move(&dp->dl_recall_lru, &reaplist);
3291 }
3292 spin_unlock(&recall_lock);
3293 list_for_each_safe(pos, next, &reaplist) {
3294 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 unhash_delegation(dp);
3296 }
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003297 test_val = nn->nfsd4_lease;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003298 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003299 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3300 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3301 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (test_val > u)
3303 test_val = u;
3304 break;
3305 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003306 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
3308 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3309 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3310 nfs4_unlock_state();
3311 return clientid_val;
3312}
3313
Harvey Harrisona254b242008-02-20 12:49:00 -08003314static struct workqueue_struct *laundry_wq;
3315static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003316
3317static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003318laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003321 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3322 work);
3323 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3324 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003326 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003328 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003331static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003332{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003333 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3334 return nfserr_bad_stateid;
3335 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336}
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003339access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003341 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3342 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3343 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
3346static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003347access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003349 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3350 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
3353static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003354__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Al Virob37ad282006-10-19 23:28:59 -07003356 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
J. Bruce Fields02921912010-07-29 15:16:59 -04003358 /* For lock stateid's, we test the parent open, not the lock: */
3359 if (stp->st_openstp)
3360 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003361 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003363 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 goto out;
3365 status = nfs_ok;
3366out:
3367 return status;
3368}
3369
Al Virob37ad282006-10-19 23:28:59 -07003370static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003371check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003373 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003375 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003376 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 * conflicting state; so we must wait out the grace period. */
3378 return nfserr_grace;
3379 } else if (flags & WR_STATE)
3380 return nfs4_share_conflict(current_fh,
3381 NFS4_SHARE_DENY_WRITE);
3382 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3383 return nfs4_share_conflict(current_fh,
3384 NFS4_SHARE_DENY_READ);
3385}
3386
3387/*
3388 * Allow READ/WRITE during grace period on recovered state only for files
3389 * that are not able to provide mandatory locking.
3390 */
3391static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003392grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003394 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
J. Bruce Fields81b82962011-08-23 11:03:29 -04003397/* Returns true iff a is later than b: */
3398static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3399{
3400 return (s32)a->si_generation - (s32)b->si_generation > 0;
3401}
3402
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003403static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003404{
Andy Adamson66689582009-04-03 08:28:45 +03003405 /*
3406 * When sessions are used the stateid generation number is ignored
3407 * when it is zero.
3408 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003409 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003410 return nfs_ok;
3411
3412 if (in->si_generation == ref->si_generation)
3413 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003414
J. Bruce Fields0836f582008-01-26 19:08:12 -05003415 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003416 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003417 return nfserr_bad_stateid;
3418 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003419 * However, we could see a stateid from the past, even from a
3420 * non-buggy client. For example, if the client sends a lock
3421 * while some IO is outstanding, the lock may bump si_generation
3422 * while the IO is still in flight. The client could avoid that
3423 * situation by waiting for responses on all the IO requests,
3424 * but better performance may result in retrying IO that
3425 * receives an old_stateid error if requests are rarely
3426 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003427 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003428 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003429}
3430
Chuck Lever7df302f2012-05-29 13:56:37 -04003431static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003432{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003433 struct nfs4_stid *s;
3434 struct nfs4_ol_stateid *ols;
3435 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003436
Chuck Lever7df302f2012-05-29 13:56:37 -04003437 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3438 return nfserr_bad_stateid;
3439 /* Client debugging aid. */
3440 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3441 char addr_str[INET6_ADDRSTRLEN];
3442 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3443 sizeof(addr_str));
3444 pr_warn_ratelimited("NFSD: client %s testing state ID "
3445 "with incorrect client ID\n", addr_str);
3446 return nfserr_bad_stateid;
3447 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003448 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003449 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003450 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003451 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003452 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003453 return status;
3454 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3455 return nfs_ok;
3456 ols = openlockstateid(s);
3457 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003458 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003459 return nfserr_bad_stateid;
3460 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003461}
3462
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003463static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3464 struct nfs4_stid **s, bool sessions,
3465 struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003466{
3467 struct nfs4_client *cl;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003468 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003469
3470 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3471 return nfserr_bad_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003472 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3473 nn, &cl);
3474 if (status == nfserr_stale_clientid)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003475 return nfserr_stale_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003476 if (status)
3477 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003478 *s = find_stateid_by_type(cl, stateid, typemask);
3479 if (!*s)
3480 return nfserr_bad_stateid;
3481 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003482}
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484/*
3485* Checks for stateid operations
3486*/
Al Virob37ad282006-10-19 23:28:59 -07003487__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003488nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003489 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003491 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003492 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003494 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003496 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07003497 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (filpp)
3500 *filpp = NULL;
3501
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003502 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return nfserr_grace;
3504
3505 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003506 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003508 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3509 &s, cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003510 if (status)
3511 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003512 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3513 if (status)
3514 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003515 switch (s->sc_type) {
3516 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003517 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003518 status = nfs4_check_delegmode(dp, flags);
3519 if (status)
3520 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003521 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003522 *filpp = dp->dl_file->fi_deleg_file;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003523 if (!*filpp) {
3524 WARN_ON_ONCE(1);
3525 status = nfserr_serverfault;
3526 goto out;
3527 }
Dan Carpenter43b01782010-10-27 23:19:04 +02003528 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003529 break;
3530 case NFS4_OPEN_STID:
3531 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003532 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003533 status = nfs4_check_fh(current_fh, stp);
3534 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003536 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003537 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003539 status = nfs4_check_openmode(stp, flags);
3540 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003542 if (filpp) {
3543 if (flags & RD_STATE)
3544 *filpp = find_readable_file(stp->st_file);
3545 else
3546 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003547 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003548 break;
3549 default:
3550 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
3552 status = nfs_ok;
3553out:
3554 return status;
3555}
3556
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003557static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003558nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003559{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003560 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003561 return nfserr_locks_held;
3562 release_lock_stateid(stp);
3563 return nfs_ok;
3564}
3565
3566/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003567 * Test if the stateid is valid
3568 */
3569__be32
3570nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3571 struct nfsd4_test_stateid *test_stateid)
3572{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003573 struct nfsd4_test_stateid_id *stateid;
3574 struct nfs4_client *cl = cstate->session->se_client;
3575
3576 nfs4_lock_state();
3577 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003578 stateid->ts_id_status =
3579 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003580 nfs4_unlock_state();
3581
Bryan Schumaker17456802011-07-13 10:50:48 -04003582 return nfs_ok;
3583}
3584
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003585__be32
3586nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3587 struct nfsd4_free_stateid *free_stateid)
3588{
3589 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003590 struct nfs4_stid *s;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003591 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003592 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003593
3594 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003595 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003596 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003597 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003598 switch (s->sc_type) {
3599 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003600 ret = nfserr_locks_held;
3601 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003602 case NFS4_OPEN_STID:
3603 case NFS4_LOCK_STID:
3604 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3605 if (ret)
3606 goto out;
3607 if (s->sc_type == NFS4_LOCK_STID)
3608 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3609 else
3610 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003611 break;
3612 default:
3613 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003614 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003615out:
3616 nfs4_unlock_state();
3617 return ret;
3618}
3619
NeilBrown4c4cd222005-07-07 17:59:27 -07003620static inline int
3621setlkflg (int type)
3622{
3623 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3624 RD_STATE : WR_STATE;
3625}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003627static __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 -04003628{
3629 struct svc_fh *current_fh = &cstate->current_fh;
3630 struct nfs4_stateowner *sop = stp->st_stateowner;
3631 __be32 status;
3632
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003633 status = nfsd4_check_seqid(cstate, sop, seqid);
3634 if (status)
3635 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003636 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3637 /*
3638 * "Closed" stateid's exist *only* to return
3639 * nfserr_replay_me from the previous step.
3640 */
3641 return nfserr_bad_stateid;
3642 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3643 if (status)
3644 return status;
3645 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003646}
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648/*
3649 * Checks for sequence id mutating operations.
3650 */
Al Virob37ad282006-10-19 23:28:59 -07003651static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003652nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003653 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003654 struct nfs4_ol_stateid **stpp,
3655 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003657 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003658 struct nfs4_stid *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003660 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3661 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 *stpp = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003664 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3665 cstate->minorversion, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003666 if (status)
3667 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003668 *stpp = openlockstateid(s);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003669 cstate->replay_owner = (*stpp)->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003671 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3672}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003673
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003674static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3675 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003676{
3677 __be32 status;
3678 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003680 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003681 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003682 if (status)
3683 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003684 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003685 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003686 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003687 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
Al Virob37ad282006-10-19 23:28:59 -07003690__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003691nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003692 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
Al Virob37ad282006-10-19 23:28:59 -07003694 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003695 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003696 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003697 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
3699 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003700 (int)cstate->current_fh.fh_dentry->d_name.len,
3701 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003703 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003704 if (status)
3705 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
3707 nfs4_lock_state();
3708
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003709 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003710 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003711 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003712 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003713 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003714 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003715 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003716 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003717 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003718 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003719 update_stateid(&stp->st_stid.sc_stateid);
3720 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003721 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003722 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003723
Jeff Layton2a4317c2012-03-21 16:42:43 -04003724 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003725 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003727 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003728 if (!cstate->replay_owner)
3729 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 return status;
3731}
3732
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003733static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003735 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003736 return;
3737 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04003738 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003739}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003740
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003741static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3742{
3743 switch (to_access) {
3744 case NFS4_SHARE_ACCESS_READ:
3745 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3746 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3747 break;
3748 case NFS4_SHARE_ACCESS_WRITE:
3749 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3750 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3751 break;
3752 case NFS4_SHARE_ACCESS_BOTH:
3753 break;
3754 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003755 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
3757}
3758
3759static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04003760reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
3762 int i;
3763 for (i = 0; i < 4; i++) {
3764 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04003765 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 }
3767}
3768
Al Virob37ad282006-10-19 23:28:59 -07003769__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003770nfsd4_open_downgrade(struct svc_rqst *rqstp,
3771 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003772 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Al Virob37ad282006-10-19 23:28:59 -07003774 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003775 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003776 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003779 (int)cstate->current_fh.fh_dentry->d_name.len,
3780 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04003782 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02003783 if (od->od_deleg_want)
3784 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3785 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003788 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003789 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003790 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003793 if (!test_access(od->od_share_access, stp)) {
3794 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 stp->st_access_bmap, od->od_share_access);
3796 goto out;
3797 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04003798 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3800 stp->st_deny_bmap, od->od_share_deny);
3801 goto out;
3802 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003803 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Jeff Laytonce0fc432012-05-11 09:45:14 -04003805 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003807 update_stateid(&stp->st_stid.sc_stateid);
3808 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 status = nfs_ok;
3810out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003811 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003812 if (!cstate->replay_owner)
3813 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return status;
3815}
3816
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003817static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3818{
3819 unhash_open_stateid(s);
3820 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003821}
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823/*
3824 * nfs4_unlock_state() called after encode
3825 */
Al Virob37ad282006-10-19 23:28:59 -07003826__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003827nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003828 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
Al Virob37ad282006-10-19 23:28:59 -07003830 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003831 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003832 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003833 struct net *net = SVC_NET(rqstp);
3834 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003837 (int)cstate->current_fh.fh_dentry->d_name.len,
3838 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003841 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3842 &close->cl_stateid,
3843 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003844 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003845 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003846 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003848 oo = openowner(stp->st_stateowner);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003849 update_stateid(&stp->st_stid.sc_stateid);
3850 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003852 nfsd4_close_open_stateid(stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003853
3854 if (cstate->minorversion) {
3855 unhash_stid(&stp->st_stid);
3856 free_generic_stateid(stp);
3857 } else
3858 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003859
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003860 if (list_empty(&oo->oo_owner.so_stateids)) {
3861 if (cstate->minorversion) {
3862 release_openowner(oo);
3863 cstate->replay_owner = NULL;
3864 } else {
3865 /*
3866 * In the 4.0 case we need to keep the owners around a
3867 * little while to handle CLOSE replay.
3868 */
3869 if (list_empty(&oo->oo_owner.so_stateids))
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003870 move_to_close_lru(oo, SVC_NET(rqstp));
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003871 }
3872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003874 if (!cstate->replay_owner)
3875 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 return status;
3877}
3878
Al Virob37ad282006-10-19 23:28:59 -07003879__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003880nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3881 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003883 struct nfs4_delegation *dp;
3884 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003885 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07003886 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003887 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003889 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003890 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 nfs4_lock_state();
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003893 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3894 cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003895 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003896 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003897 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003898 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003899 if (status)
3900 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003901
3902 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003904 nfs4_unlock_state();
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 return status;
3907}
3908
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
J. Bruce Fields009673b2011-11-07 17:40:10 -05003912#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Benny Halevy87df4de2008-12-15 19:42:03 +02003914static inline u64
3915end_offset(u64 start, u64 len)
3916{
3917 u64 end;
3918
3919 end = start + len;
3920 return end >= start ? end: NFS4_MAX_UINT64;
3921}
3922
3923/* last octet in a range */
3924static inline u64
3925last_byte_offset(u64 start, u64 len)
3926{
3927 u64 end;
3928
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003929 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02003930 end = start + len;
3931 return end > start ? end - 1: NFS4_MAX_UINT64;
3932}
3933
J. Bruce Fields009673b2011-11-07 17:40:10 -05003934static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
3936 return (file_hashval(inode) + cl_id
3937 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05003938 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941/*
3942 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3943 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3944 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3945 * locking, this prevents us from being completely protocol-compliant. The
3946 * real solution to this problem is to start using unsigned file offsets in
3947 * the VFS, but this is a very deep change!
3948 */
3949static inline void
3950nfs4_transform_lock_offset(struct file_lock *lock)
3951{
3952 if (lock->fl_start < 0)
3953 lock->fl_start = OFFSET_MAX;
3954 if (lock->fl_end < 0)
3955 lock->fl_end = OFFSET_MAX;
3956}
3957
NeilBrownd5b90262006-04-10 22:55:22 -07003958/* Hack!: For now, we're defining this just so we can use a pointer to it
3959 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003960static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003961};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
3963static inline void
3964nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3965{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003966 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
NeilBrownd5b90262006-04-10 22:55:22 -07003968 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003969 lo = (struct nfs4_lockowner *) fl->fl_owner;
3970 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3971 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003972 if (!deny->ld_owner.data)
3973 /* We just don't care that much */
3974 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003975 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3976 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003977 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003978nevermind:
3979 deny->ld_owner.len = 0;
3980 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003981 deny->ld_clientid.cl_boot = 0;
3982 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 }
3984 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003985 deny->ld_length = NFS4_MAX_UINT64;
3986 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3988 deny->ld_type = NFS4_READ_LT;
3989 if (fl->fl_type != F_RDLCK)
3990 deny->ld_type = NFS4_WRITE_LT;
3991}
3992
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003993static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3994{
3995 struct nfs4_ol_stateid *lst;
3996
3997 if (!same_owner_str(&lo->lo_owner, owner, clid))
3998 return false;
3999 lst = list_first_entry(&lo->lo_owner.so_stateids,
4000 struct nfs4_ol_stateid, st_perstateowner);
4001 return lst->st_file->fi_inode == inode;
4002}
4003
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004004static struct nfs4_lockowner *
4005find_lockowner_str(struct inode *inode, clientid_t *clid,
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004006 struct xdr_netobj *owner, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004008 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004009 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004011 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004012 if (same_lockowner_ino(lo, inode, clid, owner))
4013 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 }
4015 return NULL;
4016}
4017
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004018static 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 -04004019{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004020 struct inode *inode = open_stp->st_file->fi_inode;
4021 unsigned int inohash = lockowner_ino_hashval(inode,
4022 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004023 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields009673b2011-11-07 17:40:10 -05004024
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004025 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004026 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004027 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004028}
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030/*
4031 * Alloc a lock owner structure.
4032 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004033 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004035 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 */
4037
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004038static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004039alloc_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 -04004040 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004042 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4043 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004045 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4046 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004047 /* It is the openowner seqid that will be incremented in encode in the
4048 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004049 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4050 hash_lockowner(lo, strhashval, clp, open_stp);
4051 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004054static struct nfs4_ol_stateid *
4055alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004057 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004058 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
J. Bruce Fields996e0932011-10-17 11:14:48 -04004060 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004061 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004062 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004063 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown8beefa22005-06-23 22:03:08 -07004064 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004065 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4066 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004067 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004069 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004071 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return stp;
4073}
4074
NeilBrownfd39ca92005-06-23 22:04:03 -07004075static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076check_lock_length(u64 offset, u64 length)
4077{
Benny Halevy87df4de2008-12-15 19:42:03 +02004078 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 LOFF_OVERFLOW(offset, length)));
4080}
4081
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004082static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004083{
4084 struct nfs4_file *fp = lock_stp->st_file;
4085 int oflag = nfs4_access_to_omode(access);
4086
Jeff Layton82c5ff12012-05-11 09:45:13 -04004087 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004088 return;
4089 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004090 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004091}
4092
J. Bruce Fields2355c592012-04-25 16:56:22 -04004093static __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 -04004094{
4095 struct nfs4_file *fi = ost->st_file;
4096 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4097 struct nfs4_client *cl = oo->oo_owner.so_client;
4098 struct nfs4_lockowner *lo;
4099 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004100 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004101
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004102 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4103 &lock->v.new.owner, nn);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004104 if (lo) {
4105 if (!cstate->minorversion)
4106 return nfserr_bad_seqid;
4107 /* XXX: a lockowner always has exactly one stateid: */
4108 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4109 struct nfs4_ol_stateid, st_perstateowner);
4110 return nfs_ok;
4111 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004112 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004113 &lock->v.new.owner);
4114 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4115 if (lo == NULL)
4116 return nfserr_jukebox;
4117 *lst = alloc_init_lock_stateid(lo, fi, ost);
4118 if (*lst == NULL) {
4119 release_lockowner(lo);
4120 return nfserr_jukebox;
4121 }
4122 *new = true;
4123 return nfs_ok;
4124}
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126/*
4127 * LOCK operation
4128 */
Al Virob37ad282006-10-19 23:28:59 -07004129__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004130nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004131 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004133 struct nfs4_openowner *open_sop = NULL;
4134 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004135 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004136 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004137 struct file_lock *file_lock = NULL;
4138 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004139 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004140 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004141 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004142 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004143 struct net *net = SVC_NET(rqstp);
4144 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4147 (long long) lock->lk_offset,
4148 (long long) lock->lk_length);
4149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 if (check_lock_length(lock->lk_offset, lock->lk_length))
4151 return nfserr_inval;
4152
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004153 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004154 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004155 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4156 return status;
4157 }
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 nfs4_lock_state();
4160
4161 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004162 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004163
4164 if (nfsd4_has_session(cstate))
4165 /* See rfc 5661 18.10.3: given clientid is ignored: */
4166 memcpy(&lock->v.new.clientid,
4167 &cstate->session->se_client->cl_clientid,
4168 sizeof(clientid_t));
4169
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004171 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004175 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 lock->lk_new_open_seqid,
4177 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004178 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004179 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004181 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004182 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004183 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004184 &lock->v.new.clientid))
4185 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004186 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4187 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004188 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004189 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004190 lock->lk_old_lock_seqid,
4191 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004192 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004193 if (status)
4194 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004195 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004197 lkflg = setlkflg(lock->lk_type);
4198 status = nfs4_check_openmode(lock_stp, lkflg);
4199 if (status)
4200 goto out;
4201
NeilBrown0dd395d2005-07-07 17:59:15 -07004202 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004203 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004204 goto out;
4205 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004206 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004207 goto out;
4208
Jeff Layton21179d82012-08-21 08:03:32 -04004209 file_lock = locks_alloc_lock();
4210 if (!file_lock) {
4211 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4212 status = nfserr_jukebox;
4213 goto out;
4214 }
4215
4216 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 switch (lock->lk_type) {
4218 case NFS4_READ_LT:
4219 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004220 filp = find_readable_file(lock_stp->st_file);
4221 if (filp)
4222 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004223 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 case NFS4_WRITE_LT:
4226 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004227 filp = find_writeable_file(lock_stp->st_file);
4228 if (filp)
4229 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004230 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004231 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 default:
4233 status = nfserr_inval;
4234 goto out;
4235 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004236 if (!filp) {
4237 status = nfserr_openmode;
4238 goto out;
4239 }
Jeff Layton21179d82012-08-21 08:03:32 -04004240 file_lock->fl_owner = (fl_owner_t)lock_sop;
4241 file_lock->fl_pid = current->tgid;
4242 file_lock->fl_file = filp;
4243 file_lock->fl_flags = FL_POSIX;
4244 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4245 file_lock->fl_start = lock->lk_offset;
4246 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4247 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Jeff Layton21179d82012-08-21 08:03:32 -04004249 conflock = locks_alloc_lock();
4250 if (!conflock) {
4251 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4252 status = nfserr_jukebox;
4253 goto out;
4254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Jeff Layton21179d82012-08-21 08:03:32 -04004256 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004257 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004259 update_stateid(&lock_stp->st_stid.sc_stateid);
4260 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004262 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004263 break;
4264 case (EAGAIN): /* conflock holds conflicting lock */
4265 status = nfserr_denied;
4266 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004267 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 case (EDEADLK):
4270 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004271 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004272 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004273 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004274 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004278 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004279 release_lockowner(lock_sop);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004280 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004281 if (!cstate->replay_owner)
4282 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004283 if (file_lock)
4284 locks_free_lock(file_lock);
4285 if (conflock)
4286 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return status;
4288}
4289
4290/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004291 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4292 * so we do a temporary open here just to get an open file to pass to
4293 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4294 * inode operation.)
4295 */
Al Viro04da6e92012-04-13 00:00:04 -04004296static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004297{
4298 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004299 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4300 if (!err) {
4301 err = nfserrno(vfs_test_lock(file, lock));
4302 nfsd_close(file);
4303 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004304 return err;
4305}
4306
4307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 * LOCKT operation
4309 */
Al Virob37ad282006-10-19 23:28:59 -07004310__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004311nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4312 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
4314 struct inode *inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004315 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004316 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004317 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004318 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004320 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return nfserr_grace;
4322
4323 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4324 return nfserr_inval;
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 nfs4_lock_state();
4327
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004328 if (!nfsd4_has_session(cstate)) {
4329 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4330 if (status)
4331 goto out;
4332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004334 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004337 inode = cstate->current_fh.fh_dentry->d_inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004338 file_lock = locks_alloc_lock();
4339 if (!file_lock) {
4340 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4341 status = nfserr_jukebox;
4342 goto out;
4343 }
4344 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 switch (lockt->lt_type) {
4346 case NFS4_READ_LT:
4347 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004348 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 break;
4350 case NFS4_WRITE_LT:
4351 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004352 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 break;
4354 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004355 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 status = nfserr_inval;
4357 goto out;
4358 }
4359
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004360 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004361 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004362 file_lock->fl_owner = (fl_owner_t)lo;
4363 file_lock->fl_pid = current->tgid;
4364 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Jeff Layton21179d82012-08-21 08:03:32 -04004366 file_lock->fl_start = lockt->lt_offset;
4367 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Jeff Layton21179d82012-08-21 08:03:32 -04004369 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Jeff Layton21179d82012-08-21 08:03:32 -04004371 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004372 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004373 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004374
Jeff Layton21179d82012-08-21 08:03:32 -04004375 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004377 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
4379out:
4380 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004381 if (file_lock)
4382 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return status;
4384}
4385
Al Virob37ad282006-10-19 23:28:59 -07004386__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004387nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004388 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004390 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004392 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004393 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004394 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004395 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4398 (long long) locku->lu_offset,
4399 (long long) locku->lu_length);
4400
4401 if (check_lock_length(locku->lu_offset, locku->lu_length))
4402 return nfserr_inval;
4403
4404 nfs4_lock_state();
4405
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004406 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004407 &locku->lu_stateid, NFS4_LOCK_STID,
4408 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004409 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004411 filp = find_any_file(stp->st_file);
4412 if (!filp) {
4413 status = nfserr_lock_range;
4414 goto out;
4415 }
Jeff Layton21179d82012-08-21 08:03:32 -04004416 file_lock = locks_alloc_lock();
4417 if (!file_lock) {
4418 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4419 status = nfserr_jukebox;
4420 goto out;
4421 }
4422 locks_init_lock(file_lock);
4423 file_lock->fl_type = F_UNLCK;
4424 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4425 file_lock->fl_pid = current->tgid;
4426 file_lock->fl_file = filp;
4427 file_lock->fl_flags = FL_POSIX;
4428 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4429 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Jeff Layton21179d82012-08-21 08:03:32 -04004431 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4432 locku->lu_length);
4433 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 /*
4436 * Try to unlock the file in the VFS.
4437 */
Jeff Layton21179d82012-08-21 08:03:32 -04004438 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004439 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004440 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 goto out_nfserr;
4442 }
4443 /*
4444 * OK, unlock succeeded; the only thing left to do is update the stateid.
4445 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004446 update_stateid(&stp->st_stid.sc_stateid);
4447 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004450 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004451 if (!cstate->replay_owner)
4452 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004453 if (file_lock)
4454 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 return status;
4456
4457out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004458 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 goto out;
4460}
4461
4462/*
4463 * returns
4464 * 1: locks held by lockowner
4465 * 0: no locks held by lockowner
4466 */
4467static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004468check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
4470 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004471 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 int status = 0;
4473
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004474 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004476 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 status = 1;
4478 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 }
4481out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004482 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 return status;
4484}
4485
Al Virob37ad282006-10-19 23:28:59 -07004486__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004487nfsd4_release_lockowner(struct svc_rqst *rqstp,
4488 struct nfsd4_compound_state *cstate,
4489 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
4491 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004492 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004493 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004494 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004496 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004497 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004498 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004499 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
4501 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4502 clid->cl_boot, clid->cl_id);
4503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 nfs4_lock_state();
4505
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004506 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4507 if (status)
4508 goto out;
4509
NeilBrown3e9e3db2005-06-23 22:04:20 -07004510 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004511 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004512
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004513 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004514 if (sop->so_is_open_owner)
4515 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004516 if (!same_owner_str(sop, owner, clid))
4517 continue;
4518 list_for_each_entry(stp, &sop->so_stateids,
4519 st_perstateowner) {
4520 lo = lockowner(sop);
4521 if (check_for_locks(stp->st_file, lo))
4522 goto out;
4523 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004525 }
4526 /* Clients probably won't expect us to return with some (but not all)
4527 * of the lockowner state released; so don't release any until all
4528 * have been checked. */
4529 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004530 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004531 lo = list_entry(matches.next, struct nfs4_lockowner,
4532 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004533 /* unhash_stateowner deletes so_perclient only
4534 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004535 list_del(&lo->lo_list);
4536 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 }
4538out:
4539 nfs4_unlock_state();
4540 return status;
4541}
4542
4543static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004544alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
NeilBrowna55370a2005-06-23 22:03:52 -07004546 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547}
4548
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004549bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004550nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004551{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004552 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004553
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004554 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004555 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004556}
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558/*
4559 * failure => all reset bets are off, nfserr_no_grace...
4560 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004561struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004562nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
4564 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004565 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
NeilBrowna55370a2005-06-23 22:03:52 -07004567 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4568 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004569 if (crp) {
4570 strhashval = clientstr_hashval(name);
4571 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004572 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004573 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004574 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004575 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004576 }
4577 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578}
4579
Jeff Layton2a4317c2012-03-21 16:42:43 -04004580void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004581nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004582{
4583 list_del(&crp->cr_strhash);
4584 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004585 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004586}
4587
4588void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004589nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590{
4591 struct nfs4_client_reclaim *crp = NULL;
4592 int i;
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004595 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4596 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004598 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 }
4600 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004601 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602}
4603
4604/*
4605 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004606struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004607nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608{
4609 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 struct nfs4_client_reclaim *crp = NULL;
4611
Jeff Layton278c9312012-11-12 15:00:52 -05004612 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Jeff Layton278c9312012-11-12 15:00:52 -05004614 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004615 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004616 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 return crp;
4618 }
4619 }
4620 return NULL;
4621}
4622
4623/*
4624* Called from OPEN. Look for clientid in reclaim list.
4625*/
Al Virob37ad282006-10-19 23:28:59 -07004626__be32
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004627nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
Jeff Laytona52d7262012-03-21 09:52:02 -04004629 struct nfs4_client *clp;
4630
4631 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004632 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004633 if (clp == NULL)
4634 return nfserr_reclaim_bad;
4635
4636 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
Bryan Schumaker65178db2011-11-01 13:35:21 -04004639#ifdef CONFIG_NFSD_FAULT_INJECTION
4640
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004641u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4642{
J. Bruce Fields221a6872013-04-01 22:23:49 -04004643 if (mark_client_expired(clp))
4644 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004645 expire_client(clp);
4646 return 1;
4647}
4648
Bryan Schumaker184c1842012-11-29 11:40:44 -05004649u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
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\n", buf);
4654 return 1;
4655}
4656
4657static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4658 const char *type)
4659{
4660 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004661 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004662 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4663}
4664
Bryan Schumakerfc291712012-11-29 11:40:40 -05004665static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4666{
4667 struct nfs4_openowner *oop;
4668 struct nfs4_lockowner *lop, *lo_next;
4669 struct nfs4_ol_stateid *stp, *st_next;
4670 u64 count = 0;
4671
4672 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4673 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4674 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4675 if (func)
4676 func(lop);
4677 if (++count == max)
4678 return count;
4679 }
4680 }
4681 }
4682
4683 return count;
4684}
4685
4686u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4687{
4688 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4689}
4690
Bryan Schumaker184c1842012-11-29 11:40:44 -05004691u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4692{
4693 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4694 nfsd_print_count(clp, count, "locked files");
4695 return count;
4696}
4697
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05004698static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4699{
4700 struct nfs4_openowner *oop, *next;
4701 u64 count = 0;
4702
4703 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4704 if (func)
4705 func(oop);
4706 if (++count == max)
4707 break;
4708 }
4709
4710 return count;
4711}
4712
4713u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4714{
4715 return nfsd_foreach_client_open(clp, max, release_openowner);
4716}
4717
Bryan Schumaker184c1842012-11-29 11:40:44 -05004718u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
4719{
4720 u64 count = nfsd_foreach_client_open(clp, max, NULL);
4721 nfsd_print_count(clp, count, "open files");
4722 return count;
4723}
4724
Bryan Schumaker269de302012-11-29 11:40:42 -05004725static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
4726 struct list_head *victims)
4727{
4728 struct nfs4_delegation *dp, *next;
4729 u64 count = 0;
4730
4731 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
4732 if (victims)
4733 list_move(&dp->dl_recall_lru, victims);
4734 if (++count == max)
4735 break;
4736 }
4737 return count;
4738}
4739
4740u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
4741{
4742 struct nfs4_delegation *dp, *next;
4743 LIST_HEAD(victims);
4744 u64 count;
4745
4746 spin_lock(&recall_lock);
4747 count = nfsd_find_all_delegations(clp, max, &victims);
4748 spin_unlock(&recall_lock);
4749
4750 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4751 unhash_delegation(dp);
4752
4753 return count;
4754}
4755
4756u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
4757{
4758 struct nfs4_delegation *dp, *next;
4759 LIST_HEAD(victims);
4760 u64 count;
4761
4762 spin_lock(&recall_lock);
4763 count = nfsd_find_all_delegations(clp, max, &victims);
4764 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4765 nfsd_break_one_deleg(dp);
4766 spin_unlock(&recall_lock);
4767
4768 return count;
4769}
4770
Bryan Schumaker184c1842012-11-29 11:40:44 -05004771u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
4772{
4773 u64 count = 0;
4774
4775 spin_lock(&recall_lock);
4776 count = nfsd_find_all_delegations(clp, max, NULL);
4777 spin_unlock(&recall_lock);
4778
4779 nfsd_print_count(clp, count, "delegations");
4780 return count;
4781}
4782
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004783u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004784{
4785 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004786 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004787 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004788
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004789 if (!nfsd_netns_ready(nn))
4790 return 0;
4791
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004792 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004793 count += func(clp, max - count);
4794 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004795 break;
4796 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04004797
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004798 return count;
4799}
4800
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05004801struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
4802{
4803 struct nfs4_client *clp;
4804 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4805
4806 if (!nfsd_netns_ready(nn))
4807 return NULL;
4808
4809 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
4810 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
4811 return clp;
4812 }
4813 return NULL;
4814}
4815
Bryan Schumaker65178db2011-11-01 13:35:21 -04004816#endif /* CONFIG_NFSD_FAULT_INJECTION */
4817
NeilBrownac4d8ff22005-06-23 22:03:30 -07004818/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
Bryan Schumaker72083392011-11-01 15:24:59 -04004820void
NeilBrownac4d8ff22005-06-23 22:03:30 -07004821nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822{
NeilBrownac4d8ff22005-06-23 22:03:30 -07004823}
4824
Meelap Shahc2f1a552007-07-17 04:04:39 -07004825/*
4826 * Since the lifetime of a delegation isn't limited to that of an open, a
4827 * client may quite reasonably hang on to a delegation as long as it has
4828 * the inode cached. This becomes an obvious problem the first time a
4829 * client's inode cache approaches the size of the server's total memory.
4830 *
4831 * For now we avoid this problem by imposing a hard limit on the number
4832 * of delegations, which varies according to the server's memory size.
4833 */
4834static void
4835set_max_delegations(void)
4836{
4837 /*
4838 * Allow at most 4 delegations per megabyte of RAM. Quick
4839 * estimates suggest that in the worst case (where every delegation
4840 * is for a different inode), a delegation could take about 1.5K,
4841 * giving a worst case usage of about 6% of memory.
4842 */
4843 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4844}
4845
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004846static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004847{
4848 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4849 int i;
4850
4851 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4852 CLIENT_HASH_SIZE, GFP_KERNEL);
4853 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004854 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004855 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4856 CLIENT_HASH_SIZE, GFP_KERNEL);
4857 if (!nn->unconf_id_hashtbl)
4858 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004859 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4860 OWNER_HASH_SIZE, GFP_KERNEL);
4861 if (!nn->ownerstr_hashtbl)
4862 goto err_ownerstr;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004863 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4864 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4865 if (!nn->lockowner_ino_hashtbl)
4866 goto err_lockowner_ino;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004867 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4868 SESSION_HASH_SIZE, GFP_KERNEL);
4869 if (!nn->sessionid_hashtbl)
4870 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004871
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004872 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004873 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004874 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004875 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004876 for (i = 0; i < OWNER_HASH_SIZE; i++)
4877 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004878 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4879 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004880 for (i = 0; i < SESSION_HASH_SIZE; i++)
4881 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004882 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004883 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004884 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004885 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004886 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004887 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004888
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004889 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004890 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004891
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004892 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004893
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004894err_sessionid:
4895 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004896err_lockowner_ino:
4897 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004898err_ownerstr:
4899 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004900err_unconf_id:
4901 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004902err:
4903 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004904}
4905
4906static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004907nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004908{
4909 int i;
4910 struct nfs4_client *clp = NULL;
4911 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004912 struct rb_node *node, *tmp;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004913
4914 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4915 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4916 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4917 destroy_client(clp);
4918 }
4919 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004920
4921 node = rb_first(&nn->unconf_name_tree);
4922 while (node != NULL) {
4923 tmp = node;
4924 node = rb_next(tmp);
4925 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4926 rb_erase(tmp, &nn->unconf_name_tree);
4927 destroy_client(clp);
4928 }
4929
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004930 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004931 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004932 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004933 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004934 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004935 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004936}
4937
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004938int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004939nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07004940{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004941 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004942 int ret;
4943
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004944 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004945 if (ret)
4946 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004947 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004948 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004949 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004950 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004951 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004952 nn->nfsd4_grace, net);
4953 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004954 return 0;
4955}
4956
4957/* initialization to perform when the nfsd service is started: */
4958
4959int
4960nfs4_state_start(void)
4961{
4962 int ret;
4963
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004964 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004965 if (ret)
4966 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004967 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05004968 if (laundry_wq == NULL) {
4969 ret = -ENOMEM;
4970 goto out_recovery;
4971 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004972 ret = nfsd4_create_callback_queue();
4973 if (ret)
4974 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004975
Meelap Shahc2f1a552007-07-17 04:04:39 -07004976 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004977
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004978 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004979
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004980out_free_laundry:
4981 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05004982out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984}
4985
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004986/* should be called with the state lock held */
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004987void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004988nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004992 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004994 cancel_delayed_work_sync(&nn->laundromat_work);
4995 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 INIT_LIST_HEAD(&reaplist);
4998 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004999 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5001 list_move(&dp->dl_recall_lru, &reaplist);
5002 }
5003 spin_unlock(&recall_lock);
5004 list_for_each_safe(pos, next, &reaplist) {
5005 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 unhash_delegation(dp);
5007 }
5008
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005009 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005010 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011}
5012
5013void
5014nfs4_state_shutdown(void)
5015{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005016 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005017 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005019
5020static void
5021get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5022{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005023 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5024 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005025}
5026
5027static void
5028put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5029{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005030 if (cstate->minorversion) {
5031 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5032 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5033 }
5034}
5035
5036void
5037clear_current_stateid(struct nfsd4_compound_state *cstate)
5038{
5039 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005040}
5041
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005042/*
5043 * functions to set current state id
5044 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005045void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005046nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5047{
5048 put_stateid(cstate, &odp->od_stateid);
5049}
5050
5051void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005052nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5053{
5054 put_stateid(cstate, &open->op_stateid);
5055}
5056
5057void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005058nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5059{
5060 put_stateid(cstate, &close->cl_stateid);
5061}
5062
5063void
5064nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5065{
5066 put_stateid(cstate, &lock->lk_resp_stateid);
5067}
5068
5069/*
5070 * functions to consume current state id
5071 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005072
5073void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005074nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5075{
5076 get_stateid(cstate, &odp->od_stateid);
5077}
5078
5079void
5080nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5081{
5082 get_stateid(cstate, &drp->dr_stateid);
5083}
5084
5085void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005086nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5087{
5088 get_stateid(cstate, &fsp->fr_stateid);
5089}
5090
5091void
5092nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5093{
5094 get_stateid(cstate, &setattr->sa_stateid);
5095}
5096
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005097void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005098nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5099{
5100 get_stateid(cstate, &close->cl_stateid);
5101}
5102
5103void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005104nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005105{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005106 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005107}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005108
5109void
5110nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5111{
5112 get_stateid(cstate, &read->rd_stateid);
5113}
5114
5115void
5116nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5117{
5118 get_stateid(cstate, &write->wr_stateid);
5119}