blob: 7d2e3b54b9dfc510014fd25aa960f92159551a00 [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 Fields0c7c3e62013-03-28 20:37:14 -0400310 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400311 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400312 }
313}
314
J. Bruce Fields998db522010-08-07 09:21:41 -0400315static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
316{
317 if (oflag == O_RDWR) {
318 __nfs4_file_put_access(fp, O_RDONLY);
319 __nfs4_file_put_access(fp, O_WRONLY);
320 } else
321 __nfs4_file_put_access(fp, oflag);
322}
323
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500324static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
325kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400326{
327 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500328 static int min_stateid = 0;
329 struct nfs4_stid *stid;
330 int new_id;
331
332 stid = kmem_cache_alloc(slab, GFP_KERNEL);
333 if (!stid)
334 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400335
Tejun Heoebd6c702013-03-13 14:59:37 -0700336 new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
337 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500338 goto out_free;
339 stid->sc_client = cl;
340 stid->sc_type = 0;
341 stid->sc_stateid.si_opaque.so_id = new_id;
342 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
343 /* Will be incremented before return to client: */
344 stid->sc_stateid.si_generation = 0;
345
J. Bruce Fields996e0932011-10-17 11:14:48 -0400346 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500347 * It shouldn't be a problem to reuse an opaque stateid value.
348 * I don't think it is for 4.1. But with 4.0 I worry that, for
349 * example, a stray write retransmission could be accepted by
350 * the server when it should have been rejected. Therefore,
351 * adopt a trick from the sctp code to attempt to maximize the
352 * amount of time until an id is reused, by ensuring they always
353 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400354 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500355
356 min_stateid = new_id+1;
357 if (min_stateid == INT_MAX)
358 min_stateid = 0;
359 return stid;
360out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800361 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500362 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400363}
364
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400365static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
366{
367 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400371alloc_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 -0700372{
373 struct nfs4_delegation *dp;
374 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400377 /*
378 * Major work on the lease subsystem (for example, to support
379 * calbacks on stat) will be required before we can support
380 * write delegations properly.
381 */
382 if (type != NFS4_OPEN_DELEGATE_READ)
383 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700384 if (fp->fi_had_conflict)
385 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700386 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700387 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400388 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700389 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return dp;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500391 dp->dl_stid.sc_type = NFS4_DELEG_STID;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400392 /*
393 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400394 * meaning of seqid 0 isn't meaningful, really, but let's avoid
395 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400396 */
397 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700398 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700399 INIT_LIST_HEAD(&dp->dl_perfile);
400 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 INIT_LIST_HEAD(&dp->dl_recall_lru);
NeilBrown13cd2182005-06-23 22:03:10 -0700402 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dp->dl_type = type;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500405 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 dp->dl_time = 0;
407 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500408 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return dp;
410}
411
J. Bruce Fields68a33962013-03-21 11:21:50 -0400412static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500413{
414 struct idr *stateids = &s->sc_client->cl_stateids;
415
416 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419void
420nfs4_put_delegation(struct nfs4_delegation *dp)
421{
422 if (atomic_dec_and_test(&dp->dl_count)) {
J. Bruce Fields68a33962013-03-21 11:21:50 -0400423 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700424 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426}
427
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500428static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500430 if (atomic_dec_and_test(&fp->fi_delegees)) {
431 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
432 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400433 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500434 fp->fi_deleg_file = NULL;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400438static void unhash_stid(struct nfs4_stid *s)
439{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500440 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/* Called under the state lock. */
444static void
445unhash_delegation(struct nfs4_delegation *dp)
446{
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400447 unhash_stid(&dp->dl_stid);
NeilBrownea1da632005-06-23 22:04:17 -0700448 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500450 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 list_del_init(&dp->dl_recall_lru);
452 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500453 nfs4_put_deleg_lease(dp->dl_file);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400454 put_nfs4_file(dp->dl_file);
455 dp->dl_file = NULL;
456 remove_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 nfs4_put_delegation(dp);
458}
459
460/*
461 * SETCLIENTID state
462 */
463
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400464static unsigned int clientid_hashval(u32 id)
465{
466 return id & CLIENT_HASH_MASK;
467}
468
469static unsigned int clientstr_hashval(const char *name)
470{
471 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400475 * We store the NONE, READ, WRITE, and BOTH bits separately in the
476 * st_{access,deny}_bmap field of the stateid, in order to track not
477 * only what share bits are currently in force, but also what
478 * combinations of share bits previous opens have used. This allows us
479 * to enforce the recommendation of rfc 3530 14.2.19 that the server
480 * return an error if the client attempt to downgrade to a combination
481 * of share bits not explicable by closing some of its previous opens.
482 *
483 * XXX: This enforcement is actually incomplete, since we don't keep
484 * track of access/deny bit combinations; so, e.g., we allow:
485 *
486 * OPEN allow read, deny write
487 * OPEN allow both, deny none
488 * DOWNGRADE allow read, deny none
489 *
490 * which we should reject.
491 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400492static unsigned int
493bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400494 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400495 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400496
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400497 for (i = 1; i < 4; i++) {
498 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400499 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400500 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400501 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400502}
503
Jeff Layton3a328612012-05-11 09:45:12 -0400504static bool
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400505test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400506 unsigned int access, deny;
507
Jeff Layton5ae037e2012-05-11 09:45:11 -0400508 access = bmap_to_share_mode(stp->st_access_bmap);
509 deny = bmap_to_share_mode(stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400510 if ((access & open->op_share_deny) || (deny & open->op_share_access))
Jeff Layton3a328612012-05-11 09:45:12 -0400511 return false;
512 return true;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400513}
514
Jeff Layton82c5ff12012-05-11 09:45:13 -0400515/* set share access for a given stateid */
516static inline void
517set_access(u32 access, struct nfs4_ol_stateid *stp)
518{
519 __set_bit(access, &stp->st_access_bmap);
520}
521
522/* clear share access for a given stateid */
523static inline void
524clear_access(u32 access, struct nfs4_ol_stateid *stp)
525{
526 __clear_bit(access, &stp->st_access_bmap);
527}
528
529/* test whether a given stateid has access */
530static inline bool
531test_access(u32 access, struct nfs4_ol_stateid *stp)
532{
533 return test_bit(access, &stp->st_access_bmap);
534}
535
Jeff Laytonce0fc432012-05-11 09:45:14 -0400536/* set share deny for a given stateid */
537static inline void
538set_deny(u32 access, struct nfs4_ol_stateid *stp)
539{
540 __set_bit(access, &stp->st_deny_bmap);
541}
542
543/* clear share deny for a given stateid */
544static inline void
545clear_deny(u32 access, struct nfs4_ol_stateid *stp)
546{
547 __clear_bit(access, &stp->st_deny_bmap);
548}
549
550/* test whether a given stateid is denying specific access */
551static inline bool
552test_deny(u32 access, struct nfs4_ol_stateid *stp)
553{
554 return test_bit(access, &stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400555}
556
557static int nfs4_access_to_omode(u32 access)
558{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400559 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400560 case NFS4_SHARE_ACCESS_READ:
561 return O_RDONLY;
562 case NFS4_SHARE_ACCESS_WRITE:
563 return O_WRONLY;
564 case NFS4_SHARE_ACCESS_BOTH:
565 return O_RDWR;
566 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500567 WARN_ON_ONCE(1);
568 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400569}
570
Jeff Layton82c5ff12012-05-11 09:45:13 -0400571/* release all access and file references for a given stateid */
572static void
573release_all_access(struct nfs4_ol_stateid *stp)
574{
575 int i;
576
577 for (i = 1; i < 4; i++) {
578 if (test_access(i, stp))
579 nfs4_file_put_access(stp->st_file,
580 nfs4_access_to_omode(i));
581 clear_access(i, stp);
582 }
583}
584
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400585static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500586{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500587 list_del(&stp->st_perfile);
588 list_del(&stp->st_perstateowner);
589}
590
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400591static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500592{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400593 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400594 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400595 stp->st_file = NULL;
596}
597
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400598static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400599{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400600 remove_stid(&stp->st_stid);
601 kmem_cache_free(stateid_slab, stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500602}
603
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400604static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500605{
606 struct file *file;
607
608 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400609 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500610 file = find_any_file(stp->st_file);
611 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400612 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400613 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500614 free_generic_stateid(stp);
615}
616
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400617static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500618{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400619 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500620
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400621 list_del(&lo->lo_owner.so_strhash);
622 list_del(&lo->lo_perstateid);
J. Bruce Fields009673b2011-11-07 17:40:10 -0500623 list_del(&lo->lo_owner_ino_hash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400624 while (!list_empty(&lo->lo_owner.so_stateids)) {
625 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400626 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500627 release_lock_stateid(stp);
628 }
629}
630
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400631static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500632{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400633 unhash_lockowner(lo);
634 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500635}
636
637static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400638release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500639{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400640 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500641
642 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400643 lo = list_entry(open_stp->st_lockowners.next,
644 struct nfs4_lockowner, lo_perstateid);
645 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500646 }
647}
648
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400649static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500650{
651 unhash_generic_stateid(stp);
652 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400653 close_generic_stateid(stp);
654}
655
656static void release_open_stateid(struct nfs4_ol_stateid *stp)
657{
658 unhash_open_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400659 unhash_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500660 free_generic_stateid(stp);
661}
662
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400663static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500664{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400665 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500666
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400667 list_del(&oo->oo_owner.so_strhash);
668 list_del(&oo->oo_perclient);
669 while (!list_empty(&oo->oo_owner.so_stateids)) {
670 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400671 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500672 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500673 }
674}
675
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400676static void release_last_closed_stateid(struct nfs4_openowner *oo)
677{
678 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
679
680 if (s) {
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400681 unhash_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400682 free_generic_stateid(s);
683 oo->oo_last_closed_stid = NULL;
684 }
685}
686
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400687static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500688{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400689 unhash_openowner(oo);
690 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400691 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400692 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500693}
694
Marc Eshel5282fd72009-04-03 08:27:52 +0300695static inline int
696hash_sessionid(struct nfs4_sessionid *sessionid)
697{
698 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
699
700 return sid->sequence % SESSION_HASH_SIZE;
701}
702
Trond Myklebust8f199b82012-03-20 15:11:17 -0400703#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300704static inline void
705dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
706{
707 u32 *ptr = (u32 *)(&sessionid->data[0]);
708 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
709}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400710#else
711static inline void
712dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
713{
714}
715#endif
716
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400717/*
718 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
719 * won't be used for replay.
720 */
721void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
722{
723 struct nfs4_stateowner *so = cstate->replay_owner;
724
725 if (nfserr == nfserr_replay_me)
726 return;
727
728 if (!seqid_mutating_err(ntohl(nfserr))) {
729 cstate->replay_owner = NULL;
730 return;
731 }
732 if (!so)
733 return;
734 if (so->so_is_open_owner)
735 release_last_closed_stateid(openowner(so));
736 so->so_seqid++;
737 return;
738}
Marc Eshel5282fd72009-04-03 08:27:52 +0300739
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300740static void
741gen_sessionid(struct nfsd4_session *ses)
742{
743 struct nfs4_client *clp = ses->se_client;
744 struct nfsd4_sessionid *sid;
745
746 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
747 sid->clientid = clp->cl_clientid;
748 sid->sequence = current_sessionid++;
749 sid->reserved = 0;
750}
751
752/*
Andy Adamsona6496372009-08-28 08:45:01 -0400753 * The protocol defines ca_maxresponssize_cached to include the size of
754 * the rpc header, but all we need to cache is the data starting after
755 * the end of the initial SEQUENCE operation--the rest we regenerate
756 * each time. Therefore we can advertise a ca_maxresponssize_cached
757 * value that is the number of bytes in our cache plus a few additional
758 * bytes. In order to stay on the safe side, and not promise more than
759 * we can cache, those additional bytes must be the minimum possible: 24
760 * bytes of rpc header (xid through accept state, with AUTH_NULL
761 * verifier), 12 for the compound header (with zero-length tag), and 44
762 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300763 */
Andy Adamsona6496372009-08-28 08:45:01 -0400764#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
765
Andy Adamson557ce262009-08-28 08:45:04 -0400766static void
767free_session_slots(struct nfsd4_session *ses)
768{
769 int i;
770
771 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
772 kfree(ses->se_slots[i]);
773}
774
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400775/*
776 * We don't actually need to cache the rpc and session headers, so we
777 * can allocate a little less for each slot:
778 */
779static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
780{
781 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
782}
783
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400784static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300785{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400786 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
787 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300788
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400789 return size;
790}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300791
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400792/*
793 * XXX: If we run out of reserved DRC memory we could (up to a point)
794 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800795 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400796 */
797static int nfsd4_get_drc_mem(int slotsize, u32 num)
798{
799 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300800
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400801 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400802
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400803 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800804 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
805 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400806 num = min_t(int, num, avail / slotsize);
807 nfsd_drc_mem_used += num * slotsize;
808 spin_unlock(&nfsd_drc_lock);
809
810 return num;
811}
812
813static void nfsd4_put_drc_mem(int slotsize, int num)
814{
815 spin_lock(&nfsd_drc_lock);
816 nfsd_drc_mem_used -= slotsize * num;
817 spin_unlock(&nfsd_drc_lock);
818}
819
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400820static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400821{
822 struct nfsd4_session *new;
823 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300824
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400825 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400826 + sizeof(struct nfsd4_session) > PAGE_SIZE);
827 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400828
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400829 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300830 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400831 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400832 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400833 for (i = 0; i < numslots; i++) {
834 mem = sizeof(struct nfsd4_slot) + slotsize;
835 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
836 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400837 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400838 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400839 return new;
840out_free:
841 while (i--)
842 kfree(new->se_slots[i]);
843 kfree(new);
844 return NULL;
845}
846
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300847static void init_forechannel_attrs(struct nfsd4_channel_attrs *new,
848 struct nfsd4_channel_attrs *req,
849 int numslots, int slotsize,
850 struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400851{
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300852 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400853
854 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800855 new->maxresp_cached = min_t(u32, req->maxresp_cached,
856 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400857 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
858 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
859 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
860}
861
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400862static void free_conn(struct nfsd4_conn *c)
863{
864 svc_xprt_put(c->cn_xprt);
865 kfree(c);
866}
867
868static void nfsd4_conn_lost(struct svc_xpt_user *u)
869{
870 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
871 struct nfs4_client *clp = c->cn_session->se_client;
872
873 spin_lock(&clp->cl_lock);
874 if (!list_empty(&c->cn_persession)) {
875 list_del(&c->cn_persession);
876 free_conn(c);
877 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500878 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -0500879 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400880}
881
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400882static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400883{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400884 struct nfsd4_conn *conn;
885
886 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
887 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400888 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400889 svc_xprt_get(rqstp->rq_xprt);
890 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400891 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400892 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
893 return conn;
894}
895
J. Bruce Fields328ead22010-09-29 16:11:06 -0400896static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
897{
898 conn->cn_session = ses;
899 list_add(&conn->cn_persession, &ses->se_conns);
900}
901
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400902static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
903{
904 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400905
906 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400907 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400908 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400909}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400910
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400911static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400912{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400913 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400914 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400915}
916
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400917static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400918{
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400919 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400920
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400921 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400922 ret = nfsd4_register_conn(conn);
923 if (ret)
924 /* oops; xprt is already down: */
925 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -0400926 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -0400927 /* callback channel may be back up */
928 nfsd4_probe_callback(ses->se_client);
929 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400930}
931
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400932static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400933{
934 u32 dir = NFS4_CDFC4_FORE;
935
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400936 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400937 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400938 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400939}
940
941/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400942static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400943{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400944 struct nfs4_client *clp = s->se_client;
945 struct nfsd4_conn *c;
946
947 spin_lock(&clp->cl_lock);
948 while (!list_empty(&s->se_conns)) {
949 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
950 list_del_init(&c->cn_persession);
951 spin_unlock(&clp->cl_lock);
952
953 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
954 free_conn(c);
955
956 spin_lock(&clp->cl_lock);
957 }
958 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400959}
960
J. Bruce Fields1377b692012-09-11 21:42:40 -0400961static void __free_session(struct nfsd4_session *ses)
962{
963 nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
964 free_session_slots(ses);
965 kfree(ses);
966}
967
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400968static void free_session(struct nfsd4_session *ses)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400969{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400970 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300971
972 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400973 nfsd4_del_conns(ses);
J. Bruce Fields1377b692012-09-11 21:42:40 -0400974 __free_session(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400975}
976
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300977static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan,
978 struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400979{
980 struct nfsd4_session *new;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400981 int numslots, slotsize;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400982 /*
983 * Note decreasing slot size below client's request may
984 * make it difficult for client to function correctly, whereas
985 * decreasing the number of slots will (just?) affect
986 * performance. When short on memory we therefore prefer to
987 * decrease number of slots instead of their size.
988 */
989 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
990 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800991 if (numslots < 1)
992 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400993
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400994 new = __alloc_session(slotsize, numslots);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400995 if (!new) {
Yanchuan Nian74b70dd2012-12-24 18:11:27 +0800996 nfsd4_put_drc_mem(slotsize, numslots);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400997 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400998 }
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +0300999 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize, nn);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001000 return new;
1001}
Andy Adamson557ce262009-08-28 08:45:04 -04001002
Fengguang Wu135ae822012-11-10 07:20:25 -05001003static 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 -04001004{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001005 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001006 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001007
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001008 new->se_client = clp;
1009 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001010
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001011 INIT_LIST_HEAD(&new->se_conns);
1012
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001013 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001014 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001015 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001016 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001017 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001018 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001019 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001020 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001021 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001022 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001023 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001024 spin_unlock(&nn->client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001025
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001026 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001027 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001028 /*
1029 * This is a little silly; with sessions there's no real
1030 * use for the callback address. Use the peer address
1031 * as a reasonable default for now, but consider fixing
1032 * the rpc client not to require an address in the
1033 * future:
1034 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001035 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1036 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001037 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001038}
1039
Benny Halevy9089f1b2010-05-12 00:12:26 +03001040/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001041static struct nfsd4_session *
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001042find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001043{
1044 struct nfsd4_session *elem;
1045 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001046 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001047
1048 dump_sessionid(__func__, sessionid);
1049 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001050 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001051 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001052 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1053 NFS4_MAX_SESSIONID_LEN)) {
1054 return elem;
1055 }
1056 }
1057
1058 dprintk("%s: session not found\n", __func__);
1059 return NULL;
1060}
1061
Benny Halevy9089f1b2010-05-12 00:12:26 +03001062/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001063static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001064unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001065{
1066 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001067 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001068 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001069 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1073static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001074STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001076 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001078 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001079 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 1;
1081}
1082
1083/*
1084 * XXX Should we use a slab cache ?
1085 * This type of memory management is somewhat inefficient, but we use it
1086 * anyway since SETCLIENTID is not a common operation.
1087 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001088static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 struct nfs4_client *clp;
1091
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001092 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1093 if (clp == NULL)
1094 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001095 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001096 if (clp->cl_name.data == NULL) {
1097 kfree(clp);
1098 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001100 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return clp;
1102}
1103
1104static inline void
1105free_client(struct nfs4_client *clp)
1106{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001107 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001108
1109 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001110 while (!list_empty(&clp->cl_sessions)) {
1111 struct nfsd4_session *ses;
1112 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1113 se_perclnt);
1114 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001115 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1116 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001117 }
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001118 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001120 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 kfree(clp);
1122}
1123
Benny Halevy84d38ac2010-05-12 00:13:16 +03001124/* must be called under the client_lock */
1125static inline void
1126unhash_client_locked(struct nfs4_client *clp)
1127{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001128 struct nfsd4_session *ses;
1129
Benny Halevy84d38ac2010-05-12 00:13:16 +03001130 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001131 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001132 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1133 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001134 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001138destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001140 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001143 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 INIT_LIST_HEAD(&reaplist);
1146 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001147 while (!list_empty(&clp->cl_delegations)) {
1148 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001149 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 list_move(&dp->dl_recall_lru, &reaplist);
1151 }
1152 spin_unlock(&recall_lock);
1153 while (!list_empty(&reaplist)) {
1154 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 unhash_delegation(dp);
1156 }
NeilBrownea1da632005-06-23 22:04:17 -07001157 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001158 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1159 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001161 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001162 if (clp->cl_cb_conn.cb_xprt)
1163 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001164 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001165 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001166 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001167 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001168 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001169 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001170 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001171 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1172 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001173 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001176static void expire_client(struct nfs4_client *clp)
1177{
1178 nfsd4_client_record_remove(clp);
1179 destroy_client(clp);
1180}
1181
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001182static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1183{
1184 memcpy(target->cl_verifier.data, source->data,
1185 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001188static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1191 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1192}
1193
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001194static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001195{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001196 if (source->cr_principal) {
1197 target->cr_principal =
1198 kstrdup(source->cr_principal, GFP_KERNEL);
1199 if (target->cr_principal == NULL)
1200 return -ENOMEM;
1201 } else
1202 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001203 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 target->cr_uid = source->cr_uid;
1205 target->cr_gid = source->cr_gid;
1206 target->cr_group_info = source->cr_group_info;
1207 get_group_info(target->cr_group_info);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001211static long long
1212compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1213{
1214 long long res;
1215
1216 res = o1->len - o2->len;
1217 if (res)
1218 return res;
1219 return (long long)memcmp(o1->data, o2->data, o1->len);
1220}
1221
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001222static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001223{
NeilBrowna55370a2005-06-23 22:03:52 -07001224 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001228same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1229{
1230 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001234same_clid(clientid_t *cl1, clientid_t *cl2)
1235{
1236 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001239static bool groups_equal(struct group_info *g1, struct group_info *g2)
1240{
1241 int i;
1242
1243 if (g1->ngroups != g2->ngroups)
1244 return false;
1245 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001246 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001247 return false;
1248 return true;
1249}
1250
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001251/*
1252 * RFC 3530 language requires clid_inuse be returned when the
1253 * "principal" associated with a requests differs from that previously
1254 * used. We use uid, gid's, and gss principal string as our best
1255 * approximation. We also don't want to allow non-gss use of a client
1256 * established using gss: in theory cr_principal should catch that
1257 * change, but in practice cr_principal can be null even in the gss case
1258 * since gssd doesn't always pass down a principal string.
1259 */
1260static bool is_gss_cred(struct svc_cred *cr)
1261{
1262 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1263 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1264}
1265
1266
Vivek Trivedi5559b502012-07-24 21:18:20 +05301267static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001268same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1269{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001270 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001271 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1272 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001273 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1274 return false;
1275 if (cr1->cr_principal == cr2->cr_principal)
1276 return true;
1277 if (!cr1->cr_principal || !cr2->cr_principal)
1278 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301279 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001282static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001283{
1284 static u32 current_clientid = 1;
1285
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001286 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 clp->cl_clientid.cl_id = current_clientid++;
1288}
1289
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001290static void gen_confirm(struct nfs4_client *clp)
1291{
Chuck Leverab4684d2012-03-02 17:13:50 -05001292 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001293 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Chuck Leverab4684d2012-03-02 17:13:50 -05001295 verf[0] = (__be32)get_seconds();
1296 verf[1] = (__be32)i++;
1297 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001300static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001301{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001302 struct nfs4_stid *ret;
1303
1304 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1305 if (!ret || !ret->sc_type)
1306 return NULL;
1307 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001308}
1309
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001310static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001311{
1312 struct nfs4_stid *s;
1313
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001314 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001315 if (!s)
1316 return NULL;
1317 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001318 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001319 return NULL;
1320}
1321
Jeff Layton2216d442012-11-12 15:00:57 -05001322static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001323 struct svc_rqst *rqstp, nfs4_verifier *verf)
1324{
1325 struct nfs4_client *clp;
1326 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001327 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001328 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001329 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001330
1331 clp = alloc_client(name);
1332 if (clp == NULL)
1333 return NULL;
1334
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001335 INIT_LIST_HEAD(&clp->cl_sessions);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001336 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1337 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001338 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001339 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001340 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001341 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001342 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001343 idr_init(&clp->cl_stateids);
Benny Halevy46583e22010-05-12 00:13:29 +03001344 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001345 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001346 INIT_LIST_HEAD(&clp->cl_idhash);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001347 INIT_LIST_HEAD(&clp->cl_openowners);
1348 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001349 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001350 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001351 spin_lock_init(&clp->cl_lock);
J. Bruce Fields57725152012-11-05 15:10:26 -05001352 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001353 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001354 clear_bit(0, &clp->cl_cb_slot_busy);
1355 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1356 copy_verf(clp, verf);
1357 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001358 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001359 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001360 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001361 return clp;
1362}
1363
NeilBrownfd39ca92005-06-23 22:04:03 -07001364static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001365add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1366{
1367 struct rb_node **new = &(root->rb_node), *parent = NULL;
1368 struct nfs4_client *clp;
1369
1370 while (*new) {
1371 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1372 parent = *new;
1373
1374 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1375 new = &((*new)->rb_left);
1376 else
1377 new = &((*new)->rb_right);
1378 }
1379
1380 rb_link_node(&new_clp->cl_namenode, parent, new);
1381 rb_insert_color(&new_clp->cl_namenode, root);
1382}
1383
1384static struct nfs4_client *
1385find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1386{
1387 long long cmp;
1388 struct rb_node *node = root->rb_node;
1389 struct nfs4_client *clp;
1390
1391 while (node) {
1392 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1393 cmp = compare_blob(&clp->cl_name, name);
1394 if (cmp > 0)
1395 node = node->rb_left;
1396 else if (cmp < 0)
1397 node = node->rb_right;
1398 else
1399 return clp;
1400 }
1401 return NULL;
1402}
1403
1404static void
1405add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001408 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001410 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001411 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001413 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001414 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
NeilBrownfd39ca92005-06-23 22:04:03 -07001417static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418move_to_confirmed(struct nfs4_client *clp)
1419{
1420 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001421 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001424 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001425 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001426 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001427 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 renew_client(clp);
1429}
1430
1431static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001432find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct nfs4_client *clp;
1435 unsigned int idhashval = clientid_hashval(clid->cl_id);
1436
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001437 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001438 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001439 if ((bool)clp->cl_minorversion != sessions)
1440 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001441 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 return NULL;
1446}
1447
1448static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001449find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1450{
1451 struct list_head *tbl = nn->conf_id_hashtbl;
1452
1453 return find_client_in_id_table(tbl, clid, sessions);
1454}
1455
1456static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001457find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001459 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001461 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001464static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001465{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001466 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001467}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001468
NeilBrown28ce6052005-06-23 22:03:56 -07001469static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001470find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001471{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001472 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001473}
1474
1475static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001476find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001477{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001478 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001479}
1480
NeilBrownfd39ca92005-06-23 22:04:03 -07001481static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001482gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001484 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001485 struct sockaddr *sa = svc_addr(rqstp);
1486 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001487 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Jeff Layton7077ecb2009-08-14 12:57:58 -04001489 /* Currently, we only support tcp and tcp6 for the callback channel */
1490 if (se->se_callback_netid_len == 3 &&
1491 !memcmp(se->se_callback_netid_val, "tcp", 3))
1492 expected_family = AF_INET;
1493 else if (se->se_callback_netid_len == 4 &&
1494 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1495 expected_family = AF_INET6;
1496 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto out_err;
1498
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001499 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001500 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001501 (struct sockaddr *)&conn->cb_addr,
1502 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001503
J. Bruce Fields07263f12010-05-31 19:09:40 -04001504 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001506
J. Bruce Fields07263f12010-05-31 19:09:40 -04001507 if (conn->cb_addr.ss_family == AF_INET6)
1508 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001509
J. Bruce Fields07263f12010-05-31 19:09:40 -04001510 conn->cb_prog = se->se_callback_prog;
1511 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001512 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return;
1514out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001515 conn->cb_addr.ss_family = AF_UNSPEC;
1516 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001517 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 "will not receive delegations\n",
1519 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return;
1522}
1523
Andy Adamson074fe892009-04-03 08:28:15 +03001524/*
Andy Adamson557ce262009-08-28 08:45:04 -04001525 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001526 */
1527void
1528nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1529{
Andy Adamson557ce262009-08-28 08:45:04 -04001530 struct nfsd4_slot *slot = resp->cstate.slot;
1531 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001532
Andy Adamson557ce262009-08-28 08:45:04 -04001533 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001534
Andy Adamson557ce262009-08-28 08:45:04 -04001535 slot->sl_opcnt = resp->opcnt;
1536 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001537
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001538 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001539 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001540 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001541 return;
1542 }
Andy Adamson557ce262009-08-28 08:45:04 -04001543 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1544 base = (char *)resp->cstate.datap -
1545 (char *)resp->xbuf->head[0].iov_base;
1546 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1547 slot->sl_datalen))
1548 WARN("%s: sessions DRC could not cache compound\n", __func__);
1549 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001550}
1551
1552/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001553 * Encode the replay sequence operation from the slot values.
1554 * If cachethis is FALSE encode the uncached rep error on the next
1555 * operation which sets resp->p and increments resp->opcnt for
1556 * nfs4svc_encode_compoundres.
1557 *
Andy Adamson074fe892009-04-03 08:28:15 +03001558 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001559static __be32
1560nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1561 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001562{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001563 struct nfsd4_op *op;
1564 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001565
Andy Adamsonabfabf82009-07-23 19:02:18 -04001566 /* Encode the replayed sequence operation */
1567 op = &args->ops[resp->opcnt - 1];
1568 nfsd4_encode_operation(resp, op);
1569
1570 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001571 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001572 op = &args->ops[resp->opcnt++];
1573 op->status = nfserr_retry_uncached_rep;
1574 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001575 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001576 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001577}
1578
1579/*
Andy Adamson557ce262009-08-28 08:45:04 -04001580 * The sequence operation is not cached because we can use the slot and
1581 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001582 */
1583__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001584nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1585 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001586{
Andy Adamson557ce262009-08-28 08:45:04 -04001587 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001588 __be32 status;
1589
Andy Adamson557ce262009-08-28 08:45:04 -04001590 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001591
Andy Adamsonabfabf82009-07-23 19:02:18 -04001592 /* Either returns 0 or nfserr_retry_uncached */
1593 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1594 if (status == nfserr_retry_uncached_rep)
1595 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001596
Andy Adamson557ce262009-08-28 08:45:04 -04001597 /* The sequence operation has been encoded, cstate->datap set. */
1598 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001599
Andy Adamson557ce262009-08-28 08:45:04 -04001600 resp->opcnt = slot->sl_opcnt;
1601 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1602 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001603
1604 return status;
1605}
1606
Andy Adamson0733d212009-04-03 08:28:01 +03001607/*
1608 * Set the exchange_id flags returned by the server.
1609 */
1610static void
1611nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1612{
1613 /* pNFS is not supported */
1614 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1615
1616 /* Referrals are supported, Migration is not. */
1617 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1618
1619 /* set the wire flags to return to client. */
1620 clid->flags = new->cl_exchange_flags;
1621}
1622
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001623static bool client_has_state(struct nfs4_client *clp)
1624{
1625 /*
1626 * Note clp->cl_openowners check isn't quite right: there's no
1627 * need to count owners without stateid's.
1628 *
1629 * Also note we should probably be using this in 4.0 case too.
1630 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001631 return !list_empty(&clp->cl_openowners)
1632 || !list_empty(&clp->cl_delegations)
1633 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001634}
1635
Al Virob37ad282006-10-19 23:28:59 -07001636__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001637nfsd4_exchange_id(struct svc_rqst *rqstp,
1638 struct nfsd4_compound_state *cstate,
1639 struct nfsd4_exchange_id *exid)
1640{
Andy Adamson0733d212009-04-03 08:28:01 +03001641 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001642 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001643 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001644 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001645 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001646 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001647 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001648
Jeff Layton363168b2009-08-14 12:57:56 -04001649 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001650 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001651 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001652 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001653 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001654
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001655 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001656 return nfserr_inval;
1657
1658 /* Currently only support SP4_NONE */
1659 switch (exid->spa_how) {
1660 case SP4_NONE:
1661 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001662 default: /* checked by xdr code */
1663 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001664 case SP4_SSV:
Andy Adamson0733d212009-04-03 08:28:01 +03001665 case SP4_MACH_CRED:
1666 return nfserr_serverfault; /* no excuse :-/ */
1667 }
1668
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001669 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001670 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001671 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001672 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001673 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1674 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1675
J. Bruce Fields136e6582012-05-12 20:37:23 -04001676 if (update) {
1677 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001678 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001679 goto out;
1680 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001681 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001682 status = nfserr_perm;
1683 goto out;
1684 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001685 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001686 status = nfserr_not_same;
1687 goto out;
1688 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001689 /* case 6 */
1690 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1691 new = conf;
1692 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001693 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001694 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001695 if (client_has_state(conf)) {
1696 status = nfserr_clid_inuse;
1697 goto out;
1698 }
Andy Adamson0733d212009-04-03 08:28:01 +03001699 expire_client(conf);
1700 goto out_new;
1701 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001702 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001703 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001704 new = conf;
1705 goto out_copy;
1706 }
1707 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001708 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001709 }
1710
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001711 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001712 status = nfserr_noent;
1713 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001714 }
1715
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001716 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001717 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001718 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001719
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001720 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001721out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001722 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001723 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001724 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001725 goto out;
1726 }
J. Bruce Fieldsc116a0a2012-09-11 14:53:09 -04001727 new->cl_minorversion = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001728
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001729 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001730 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001731out_copy:
1732 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1733 exid->clientid.cl_id = new->cl_clientid.cl_id;
1734
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001735 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001736 nfsd4_set_ex_flags(new, exid);
1737
1738 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001739 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001740 status = nfs_ok;
1741
1742out:
1743 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001744 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001745}
1746
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001747static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001748check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001749{
Andy Adamson88e588d2009-07-23 19:02:15 -04001750 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1751 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001752
1753 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001754 if (slot_inuse) {
1755 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001756 return nfserr_jukebox;
1757 else
1758 return nfserr_seq_misordered;
1759 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001760 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001761 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001762 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001763 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001764 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001765 return nfserr_seq_misordered;
1766}
1767
Andy Adamson49557cc2009-07-23 19:02:16 -04001768/*
1769 * Cache the create session result into the create session single DRC
1770 * slot cache by saving the xdr structure. sl_seqid has been set.
1771 * Do this for solo or embedded create session operations.
1772 */
1773static void
1774nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001775 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001776{
1777 slot->sl_status = nfserr;
1778 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1779}
1780
1781static __be32
1782nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1783 struct nfsd4_clid_slot *slot)
1784{
1785 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1786 return slot->sl_status;
1787}
1788
Mi Jinlong1b74c252011-07-14 14:50:17 +08001789#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1790 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1791 1 + /* MIN tag is length with zero, only length */ \
1792 3 + /* version, opcount, opcode */ \
1793 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1794 /* seqid, slotID, slotID, cache */ \
1795 4 ) * sizeof(__be32))
1796
1797#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1798 2 + /* verifier: AUTH_NULL, length 0 */\
1799 1 + /* status */ \
1800 1 + /* MIN tag is length with zero, only length */ \
1801 3 + /* opcount, opcode, opstatus*/ \
1802 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1803 /* seqid, slotID, slotID, slotID, status */ \
1804 5 ) * sizeof(__be32))
1805
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001806static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001807{
1808 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1809 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1810}
1811
Andy Adamson069b6ad2009-04-03 08:27:58 +03001812__be32
1813nfsd4_create_session(struct svc_rqst *rqstp,
1814 struct nfsd4_compound_state *cstate,
1815 struct nfsd4_create_session *cr_ses)
1816{
Jeff Layton363168b2009-08-14 12:57:56 -04001817 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001818 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001819 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001820 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001821 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001822 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001823 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001824
Mi Jinlonga62573d2011-03-23 17:57:07 +08001825 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1826 return nfserr_inval;
J. Bruce Fields49730502012-09-11 15:33:21 -04001827 if (check_forechannel_attrs(cr_ses->fore_channel))
1828 return nfserr_toosmall;
Stanislav Kinsbursky9dd98452012-12-06 14:23:24 +03001829 new = alloc_session(&cr_ses->fore_channel, nn);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001830 if (!new)
1831 return nfserr_jukebox;
1832 status = nfserr_jukebox;
1833 conn = alloc_conn_from_crses(rqstp, cr_ses);
1834 if (!conn)
1835 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08001836
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001837 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001838 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001839 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04001840 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001841
1842 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001843 cs_slot = &conf->cl_cs_slot;
1844 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001845 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001846 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001847 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001848 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001849 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001850 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001851 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001852 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001853 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001854 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001855 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001856 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001857 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001858 }
Andy Adamson49557cc2009-07-23 19:02:16 -04001859 cs_slot = &unconf->cl_cs_slot;
1860 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001861 if (status) {
1862 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001863 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001864 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001865 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001866 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001867 if (old) {
1868 status = mark_client_expired(old);
1869 if (status)
1870 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001871 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001872 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001873 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001874 conf = unconf;
1875 } else {
1876 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001877 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001878 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001879 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001880 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001881 * We do not support RDMA or persistent sessions
1882 */
1883 cr_ses->flags &= ~SESSION4_PERSIST;
1884 cr_ses->flags &= ~SESSION4_RDMA;
1885
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001886 init_session(rqstp, new, conf, cr_ses);
1887 nfsd4_init_conn(rqstp, conn, new);
1888
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001889 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001890 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001891 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1892 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001893 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001894 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001895
Andy Adamson49557cc2009-07-23 19:02:16 -04001896 /* cache solo and embedded create sessions under the state lock */
1897 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001898 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001899 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001900out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08001901 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001902 free_conn(conn);
1903out_free_session:
1904 __free_session(new);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04001905 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001906}
1907
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001908static __be32 nfsd4_map_bcts_dir(u32 *dir)
1909{
1910 switch (*dir) {
1911 case NFS4_CDFC4_FORE:
1912 case NFS4_CDFC4_BACK:
1913 return nfs_ok;
1914 case NFS4_CDFC4_FORE_OR_BOTH:
1915 case NFS4_CDFC4_BACK_OR_BOTH:
1916 *dir = NFS4_CDFC4_BOTH;
1917 return nfs_ok;
1918 };
1919 return nfserr_inval;
1920}
1921
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001922__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1923{
1924 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001925 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001926
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001927 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001928 session->se_cb_prog = bc->bc_cb_program;
1929 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001930 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001931
1932 nfsd4_probe_callback(session->se_client);
1933
1934 return nfs_ok;
1935}
1936
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001937__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1938 struct nfsd4_compound_state *cstate,
1939 struct nfsd4_bind_conn_to_session *bcts)
1940{
1941 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001942 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001943 struct nfsd4_session *session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001944 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001945
1946 if (!nfsd4_last_compound_op(rqstp))
1947 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001948 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001949 spin_lock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001950 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001951 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001952 status = nfserr_badsession;
1953 if (!session)
1954 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001955 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001956 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001957 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001958 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001959 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001960 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001961 goto out;
1962 nfsd4_init_conn(rqstp, conn, session);
1963 status = nfs_ok;
1964out:
1965 nfs4_unlock_state();
1966 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001967}
1968
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001969static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1970{
1971 if (!session)
1972 return 0;
1973 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1974}
1975
Andy Adamson069b6ad2009-04-03 08:27:58 +03001976__be32
1977nfsd4_destroy_session(struct svc_rqst *r,
1978 struct nfsd4_compound_state *cstate,
1979 struct nfsd4_destroy_session *sessionid)
1980{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001981 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001982 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001983 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001984
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001985 nfs4_lock_state();
1986 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001987 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001988 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001989 goto out;
J. Bruce Fields57716352010-04-21 12:27:19 -04001990 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001991 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001992 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001993 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04001994 status = nfserr_badsession;
1995 if (!ses)
1996 goto out_client_lock;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001997 status = mark_session_dead_locked(ses);
1998 if (status)
1999 goto out_client_lock;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002000 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002001 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002002
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002003 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002004
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002005 spin_lock(&nn->client_lock);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002006 free_session(ses);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002007 status = nfs_ok;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002008out_client_lock:
2009 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002010out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002011 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002012 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002013}
2014
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002015static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002016{
2017 struct nfsd4_conn *c;
2018
2019 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002020 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002021 return c;
2022 }
2023 }
2024 return NULL;
2025}
2026
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002027static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002028{
2029 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002030 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002031 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002032
2033 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002034 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002035 if (c) {
2036 spin_unlock(&clp->cl_lock);
2037 free_conn(new);
2038 return;
2039 }
2040 __nfsd4_hash_conn(new, ses);
2041 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002042 ret = nfsd4_register_conn(new);
2043 if (ret)
2044 /* oops; xprt is already down: */
2045 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002046 return;
2047}
2048
Mi Jinlong868b89c2011-04-27 09:09:58 +08002049static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2050{
2051 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2052
2053 return args->opcnt > session->se_fchannel.maxops;
2054}
2055
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002056static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2057 struct nfsd4_session *session)
2058{
2059 struct xdr_buf *xb = &rqstp->rq_arg;
2060
2061 return xb->len > session->se_fchannel.maxreq_sz;
2062}
2063
Andy Adamson069b6ad2009-04-03 08:27:58 +03002064__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002065nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002066 struct nfsd4_compound_state *cstate,
2067 struct nfsd4_sequence *seq)
2068{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002069 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002070 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002071 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002072 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002073 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002074 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002075 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002076
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002077 if (resp->opcnt != 1)
2078 return nfserr_sequence_pos;
2079
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002080 /*
2081 * Will be either used or freed by nfsd4_sequence_check_conn
2082 * below.
2083 */
2084 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2085 if (!conn)
2086 return nfserr_jukebox;
2087
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002088 spin_lock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002089 status = nfserr_badsession;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002090 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
Benny Halevyb85d4c02009-04-03 08:28:08 +03002091 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002092 goto out_no_session;
2093 clp = session->se_client;
2094 status = get_client_locked(clp);
2095 if (status)
2096 goto out_no_session;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002097 status = nfsd4_get_session_locked(session);
2098 if (status)
2099 goto out_put_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002100
Mi Jinlong868b89c2011-04-27 09:09:58 +08002101 status = nfserr_too_many_ops;
2102 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002103 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002104
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002105 status = nfserr_req_too_big;
2106 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002107 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002108
Benny Halevyb85d4c02009-04-03 08:28:08 +03002109 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002110 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002111 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002112
Andy Adamson557ce262009-08-28 08:45:04 -04002113 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002114 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2115
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002116 /* We do not negotiate the number of slots yet, so set the
2117 * maxslots to the session maxreqs which is used to encode
2118 * sr_highest_slotid and the sr_target_slot id to maxslots */
2119 seq->maxslots = session->se_fchannel.maxreqs;
2120
J. Bruce Fields73e79482012-02-13 16:39:00 -05002121 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2122 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002123 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002124 status = nfserr_seq_misordered;
2125 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002126 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002127 cstate->slot = slot;
2128 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002129 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002130 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002131 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002132 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002133 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002134 }
2135 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002136 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002137
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002138 nfsd4_sequence_check_conn(conn, session);
2139 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002140
Benny Halevyb85d4c02009-04-03 08:28:08 +03002141 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002142 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002143 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002144 if (seq->cachethis)
2145 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002146 else
2147 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002148
2149 cstate->slot = slot;
2150 cstate->session = session;
2151
Benny Halevyb85d4c02009-04-03 08:28:08 +03002152out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002153 switch (clp->cl_cb_state) {
2154 case NFSD4_CB_DOWN:
2155 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2156 break;
2157 case NFSD4_CB_FAULT:
2158 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2159 break;
2160 default:
2161 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002162 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002163out_no_session:
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002164 kfree(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002165 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002166 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002167out_put_session:
2168 nfsd4_put_session(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002169out_put_client:
2170 put_client_renew_locked(clp);
2171 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002172}
2173
Mi Jinlong345c2842011-10-20 17:51:39 +08002174__be32
2175nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2176{
2177 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002178 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002179 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002180
2181 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002182 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002183 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002184 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002185
2186 if (conf) {
2187 clp = conf;
2188
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002189 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002190 status = nfserr_clientid_busy;
2191 goto out;
2192 }
2193 } else if (unconf)
2194 clp = unconf;
2195 else {
2196 status = nfserr_stale_clientid;
2197 goto out;
2198 }
2199
2200 expire_client(clp);
2201out:
2202 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002203 return status;
2204}
2205
Andy Adamson069b6ad2009-04-03 08:27:58 +03002206__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002207nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2208{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002209 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002210
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002211 if (rc->rca_one_fs) {
2212 if (!cstate->current_fh.fh_dentry)
2213 return nfserr_nofilehandle;
2214 /*
2215 * We don't take advantage of the rca_one_fs case.
2216 * That's OK, it's optional, we can safely ignore it.
2217 */
2218 return nfs_ok;
2219 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002220
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002221 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002222 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002223 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2224 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002225 goto out;
2226
2227 status = nfserr_stale_clientid;
2228 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002229 /*
2230 * The following error isn't really legal.
2231 * But we only get here if the client just explicitly
2232 * destroyed the client. Surely it no longer cares what
2233 * error it gets back on an operation for the dead
2234 * client.
2235 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002236 goto out;
2237
2238 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002239 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002240out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002241 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002242 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002243}
2244
2245__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002246nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2247 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002249 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002251 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002252 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002253 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2254
J. Bruce Fields63db4632012-05-18 21:54:19 -04002255 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002257 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002258 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002259 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002261 if (clp_used_exchangeid(conf))
2262 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002263 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002264 char addr_str[INET6_ADDRSTRLEN];
2265 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2266 sizeof(addr_str));
2267 dprintk("NFSD: setclientid: string in use by client "
2268 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto out;
2270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002272 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002273 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002276 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002277 if (new == NULL)
2278 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002279 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002280 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002282 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002283 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002284 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002285 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002286 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2288 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2289 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2290 status = nfs_ok;
2291out:
2292 nfs4_unlock_state();
2293 return status;
2294}
2295
2296
Al Virob37ad282006-10-19 23:28:59 -07002297__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002298nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2299 struct nfsd4_compound_state *cstate,
2300 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
NeilBrown21ab45a2005-06-23 22:04:14 -07002302 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2304 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002305 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002306 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002308 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002311
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002312 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002313 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002314 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002315 * We try hard to give out unique clientid's, so if we get an
2316 * attempt to confirm the same clientid with a different cred,
2317 * there's a bug somewhere. Let's charitably assume it's our
2318 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002319 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002320 status = nfserr_serverfault;
2321 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2322 goto out;
2323 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2324 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002325 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002326 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2327 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002329 else /* case 4: client hasn't noticed we rebooted yet? */
2330 status = nfserr_stale_clientid;
2331 goto out;
2332 }
2333 status = nfs_ok;
2334 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002335 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2336 nfsd4_probe_callback(conf);
2337 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002338 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002339 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002340 if (conf) {
2341 status = mark_client_expired(conf);
2342 if (status)
2343 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002344 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002345 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002346 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002347 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 nfs4_unlock_state();
2351 return status;
2352}
2353
J. Bruce Fields32513b42011-10-13 16:00:16 -04002354static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002356 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2357}
2358
2359/* OPEN Share state helper functions */
2360static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 unsigned int hashval = file_hashval(ino);
2363
J. Bruce Fields32513b42011-10-13 16:00:16 -04002364 atomic_set(&fp->fi_ref, 1);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002365 INIT_LIST_HEAD(&fp->fi_stateids);
2366 INIT_LIST_HEAD(&fp->fi_delegations);
2367 fp->fi_inode = igrab(ino);
2368 fp->fi_had_conflict = false;
2369 fp->fi_lease = NULL;
2370 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2371 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2372 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002373 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002374 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
2377static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002378nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002379{
NeilBrowne60d4392005-06-23 22:03:01 -07002380 if (*slab == NULL)
2381 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002382 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002383 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002384}
2385
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002386void
NeilBrowne60d4392005-06-23 22:03:01 -07002387nfsd4_free_slabs(void)
2388{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002389 nfsd4_free_slab(&openowner_slab);
2390 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002391 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002392 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002393 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Bryan Schumaker72083392011-11-01 15:24:59 -04002396int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397nfsd4_init_slabs(void)
2398{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002399 openowner_slab = kmem_cache_create("nfsd4_openowners",
2400 sizeof(struct nfs4_openowner), 0, 0, NULL);
2401 if (openowner_slab == NULL)
2402 goto out_nomem;
2403 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002404 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002405 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002406 goto out_nomem;
2407 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002408 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002409 if (file_slab == NULL)
2410 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002411 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002412 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002413 if (stateid_slab == NULL)
2414 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002415 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002416 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002417 if (deleg_slab == NULL)
2418 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002420out_nomem:
2421 nfsd4_free_slabs();
2422 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2423 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002426void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002428 kfree(oo->oo_owner.so_owner.data);
2429 kmem_cache_free(openowner_slab, oo);
2430}
2431
2432void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2433{
2434 kfree(lo->lo_owner.so_owner.data);
2435 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002438static void init_nfs4_replay(struct nfs4_replay *rp)
2439{
2440 rp->rp_status = nfserr_serverfault;
2441 rp->rp_buflen = 0;
2442 rp->rp_buf = rp->rp_ibuf;
2443}
2444
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002445static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 struct nfs4_stateowner *sop;
2448
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002449 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002450 if (!sop)
2451 return NULL;
2452
2453 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2454 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002455 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002456 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002458 sop->so_owner.len = owner->len;
2459
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002460 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002461 sop->so_client = clp;
2462 init_nfs4_replay(&sop->so_replay);
2463 return sop;
2464}
2465
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002466static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002467{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002468 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2469
2470 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002471 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002474static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002476 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002478 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2479 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002481 oo->oo_owner.so_is_open_owner = 1;
2482 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002483 oo->oo_flags = NFS4_OO_NEW;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002484 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002485 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002486 INIT_LIST_HEAD(&oo->oo_close_lru);
2487 hash_openowner(oo, clp, strhashval);
2488 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
J. Bruce Fields996e0932011-10-17 11:14:48 -04002491static 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 -04002492 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002494 stp->st_stid.sc_type = NFS4_OPEN_STID;
NeilBrownea1da632005-06-23 22:04:17 -07002495 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002496 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002497 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002498 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002499 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 stp->st_access_bmap = 0;
2502 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002503 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002504 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002505 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002509move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002511 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2512
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002513 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002515 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002516 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002520same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2521 clientid_t *clid)
2522{
2523 return (sop->so_owner.len == owner->len) &&
2524 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2525 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002528static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002529find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2530 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002532 struct nfs4_stateowner *so;
2533 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002534 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002536 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002537 if (!so->so_is_open_owner)
2538 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002539 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2540 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002541 clp = oo->oo_owner.so_client;
2542 if ((bool)clp->cl_minorversion != sessions)
2543 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002544 renew_client(oo->oo_owner.so_client);
2545 return oo;
2546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548 return NULL;
2549}
2550
2551/* search file_hashtbl[] for file */
2552static struct nfs4_file *
2553find_file(struct inode *ino)
2554{
2555 unsigned int hashval = file_hashval(ino);
2556 struct nfs4_file *fp;
2557
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002558 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002559 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002560 if (fp->fi_inode == ino) {
2561 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002562 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002566 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return NULL;
2568}
2569
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002570/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 * Called to check deny when READ with all zero stateid or
2572 * WRITE with all zero or all one stateid
2573 */
Al Virob37ad282006-10-19 23:28:59 -07002574static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2576{
2577 struct inode *ino = current_fh->fh_dentry->d_inode;
2578 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002579 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002580 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002583 if (!fp)
2584 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002585 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002587 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002588 if (test_deny(deny_type, stp) ||
2589 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
NeilBrown13cd2182005-06-23 22:03:10 -07002592 ret = nfs_ok;
2593out:
2594 put_nfs4_file(fp);
2595 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002598static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002600 struct nfs4_client *clp = dp->dl_stid.sc_client;
2601 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 /* We're assuming the state code never drops its reference
2604 * without first removing the lease. Since we're in this lease
2605 * callback (and since the lease code is serialized by the kernel
2606 * lock) we know the server hasn't removed the lease yet, we know
2607 * it's safe to take a reference: */
2608 atomic_inc(&dp->dl_count);
2609
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002610 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Arnd Bergmann460781b2010-11-17 16:26:56 +01002612 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 dp->dl_time = get_seconds();
2614
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002615 nfsd4_cb_recall(dp);
2616}
2617
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002618/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002619static void nfsd_break_deleg_cb(struct file_lock *fl)
2620{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002621 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2622 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002623
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002624 if (!fp) {
2625 WARN(1, "(%p)->fl_owner NULL\n", fl);
2626 return;
2627 }
2628 if (fp->fi_had_conflict) {
2629 WARN(1, "duplicate break on %p\n", fp);
2630 return;
2631 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002632 /*
2633 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002634 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002635 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002636 */
2637 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002639 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002640 fp->fi_had_conflict = true;
2641 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2642 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002643 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646static
2647int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2648{
2649 if (arg & F_UNLCK)
2650 return lease_modify(onlist, arg);
2651 else
2652 return -EAGAIN;
2653}
2654
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002655static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002656 .lm_break = nfsd_break_deleg_cb,
2657 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658};
2659
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002660static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2661{
2662 if (nfsd4_has_session(cstate))
2663 return nfs_ok;
2664 if (seqid == so->so_seqid - 1)
2665 return nfserr_replay_me;
2666 if (seqid == so->so_seqid)
2667 return nfs_ok;
2668 return nfserr_bad_seqid;
2669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Al Virob37ad282006-10-19 23:28:59 -07002671__be32
Andy Adamson66689582009-04-03 08:28:45 +03002672nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03002673 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 clientid_t *clientid = &open->op_clientid;
2676 struct nfs4_client *clp = NULL;
2677 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002678 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002679 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002681 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002683 /*
2684 * In case we need it later, after we've already created the
2685 * file and don't want to risk a further failure:
2686 */
2687 open->op_file = nfsd4_alloc_file();
2688 if (open->op_file == NULL)
2689 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002691 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002692 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002693 open->op_openowner = oo;
2694 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002695 clp = find_confirmed_client(clientid, cstate->minorversion,
2696 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002698 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002699 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002701 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002702 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002703 clp = oo->oo_owner.so_client;
2704 release_openowner(oo);
2705 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002706 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002707 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002708 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2709 if (status)
2710 return status;
2711 clp = oo->oo_owner.so_client;
2712 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002713new_owner:
2714 oo = alloc_init_open_stateowner(strhashval, clp, open);
2715 if (oo == NULL)
2716 return nfserr_jukebox;
2717 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002718alloc_stateid:
2719 open->op_stp = nfs4_alloc_stateid(clp);
2720 if (!open->op_stp)
2721 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002722 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
Al Virob37ad282006-10-19 23:28:59 -07002725static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002726nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2727{
2728 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2729 return nfserr_openmode;
2730 else
2731 return nfs_ok;
2732}
2733
Daniel Mackc47d8322011-05-16 16:38:14 +02002734static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002735{
J. Bruce Fields24a01112010-05-18 20:01:35 -04002736 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2737}
2738
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002739static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002740{
2741 struct nfs4_stid *ret;
2742
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002743 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002744 if (!ret)
2745 return NULL;
2746 return delegstateid(ret);
2747}
2748
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002749static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2750{
2751 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2752 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2753}
2754
Al Virob37ad282006-10-19 23:28:59 -07002755static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04002756nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07002757 struct nfs4_delegation **dp)
2758{
2759 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002760 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002761
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002762 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002763 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002764 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002765 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002766 status = nfs4_check_delegmode(*dp, flags);
2767 if (status)
2768 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002769out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002770 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07002771 return nfs_ok;
2772 if (status)
2773 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002774 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002775 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002776}
2777
Al Virob37ad282006-10-19 23:28:59 -07002778static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002779nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002781 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002782 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
NeilBrown8beefa22005-06-23 22:03:08 -07002784 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 /* ignore lock owners */
2786 if (local->st_stateowner->so_is_open_owner == 0)
2787 continue;
2788 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002789 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 *stpp = local;
2791 /* check for conflicting share reservations */
2792 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002793 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002795 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002798static inline int nfs4_access_to_access(u32 nfs4_access)
2799{
2800 int flags = 0;
2801
2802 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2803 flags |= NFSD_MAY_READ;
2804 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2805 flags |= NFSD_MAY_WRITE;
2806 return flags;
2807}
2808
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002809static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2810 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002811{
2812 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002813 int oflag = nfs4_access_to_omode(open->op_share_access);
2814 int access = nfs4_access_to_access(open->op_share_access);
2815
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002816 if (!fp->fi_fds[oflag]) {
2817 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2818 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002819 if (status)
2820 return status;
2821 }
2822 nfs4_file_get_access(fp, oflag);
2823
2824 return nfs_ok;
2825}
2826
Al Virob37ad282006-10-19 23:28:59 -07002827static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2829 struct nfsd4_open *open)
2830{
2831 struct iattr iattr = {
2832 .ia_valid = ATTR_SIZE,
2833 .ia_size = 0,
2834 };
2835 if (!open->op_truncate)
2836 return 0;
2837 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002838 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2840}
2841
Al Virob37ad282006-10-19 23:28:59 -07002842static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002843nfs4_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 -07002844{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002845 u32 op_share_access = open->op_share_access;
J. Bruce Fields7d947842010-08-20 18:09:31 -04002846 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002847 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Jeff Layton82c5ff12012-05-11 09:45:13 -04002849 new_access = !test_access(op_share_access, stp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002850 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002851 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002852 if (status)
2853 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 status = nfsd4_truncate(rqstp, cur_fh, open);
2856 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002857 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002858 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002859 nfs4_file_put_access(fp, oflag);
2860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return status;
2862 }
2863 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04002864 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002865 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 return nfs_ok;
2868}
2869
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05002872nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002874 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002877/* Should we give out recallable state?: */
2878static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2879{
2880 if (clp->cl_cb_state == NFSD4_CB_UP)
2881 return true;
2882 /*
2883 * In the sessions case, since we don't have to establish a
2884 * separate connection for callbacks, we assume it's OK
2885 * until we hear otherwise:
2886 */
2887 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2888}
2889
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002890static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2891{
2892 struct file_lock *fl;
2893
2894 fl = locks_alloc_lock();
2895 if (!fl)
2896 return NULL;
2897 locks_init_lock(fl);
2898 fl->fl_lmops = &nfsd_lease_mng_ops;
2899 fl->fl_flags = FL_LEASE;
2900 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2901 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002902 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002903 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002904 return fl;
2905}
2906
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002907static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2908{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002909 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002910 struct file_lock *fl;
2911 int status;
2912
2913 fl = nfs4_alloc_init_lease(dp, flag);
2914 if (!fl)
2915 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002916 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002917 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002918 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002919 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002920 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002921 locks_free_lock(fl);
2922 return -ENOMEM;
2923 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002924 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04002925 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002926 atomic_set(&fp->fi_delegees, 1);
2927 list_add(&dp->dl_perfile, &fp->fi_delegations);
2928 return 0;
2929}
2930
2931static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2932{
2933 struct nfs4_file *fp = dp->dl_file;
2934
2935 if (!fp->fi_lease)
2936 return nfs4_setlease(dp, flag);
2937 spin_lock(&recall_lock);
2938 if (fp->fi_had_conflict) {
2939 spin_unlock(&recall_lock);
2940 return -EAGAIN;
2941 }
2942 atomic_inc(&fp->fi_delegees);
2943 list_add(&dp->dl_perfile, &fp->fi_delegations);
2944 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002945 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002946 return 0;
2947}
2948
Benny Halevy4aa89132012-02-21 14:16:44 -08002949static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2950{
2951 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2952 if (status == -EAGAIN)
2953 open->op_why_no_deleg = WND4_CONTENTION;
2954 else {
2955 open->op_why_no_deleg = WND4_RESOURCE;
2956 switch (open->op_deleg_want) {
2957 case NFS4_SHARE_WANT_READ_DELEG:
2958 case NFS4_SHARE_WANT_WRITE_DELEG:
2959 case NFS4_SHARE_WANT_ANY_DELEG:
2960 break;
2961 case NFS4_SHARE_WANT_CANCEL:
2962 open->op_why_no_deleg = WND4_CANCELLED;
2963 break;
2964 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002965 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08002966 }
2967 }
2968}
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970/*
2971 * Attempt to hand out a delegation.
2972 */
2973static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002974nfs4_open_delegation(struct net *net, struct svc_fh *fh,
2975 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002978 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002979 int cb_up;
Benny Halevyd24433c2012-02-16 20:57:17 +02002980 int status = 0, flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002982 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002984 open->op_recall = 0;
2985 switch (open->op_claim_type) {
2986 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002987 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002988 open->op_recall = 1;
2989 flag = open->op_delegate_type;
2990 if (flag == NFS4_OPEN_DELEGATE_NONE)
2991 goto out;
2992 break;
2993 case NFS4_OPEN_CLAIM_NULL:
2994 /* Let's not give out any delegations till everyone's
2995 * had the chance to reclaim theirs.... */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002996 if (locks_in_grace(net))
NeilBrown7b190fe2005-06-23 22:03:23 -07002997 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002998 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002999 goto out;
3000 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3001 flag = NFS4_OPEN_DELEGATE_WRITE;
3002 else
3003 flag = NFS4_OPEN_DELEGATE_READ;
3004 break;
3005 default:
3006 goto out;
3007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003009 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003010 if (dp == NULL)
3011 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003012 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003013 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003014 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003016 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003018 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003019 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020out:
3021 open->op_delegate_type = flag;
Benny Halevyd24433c2012-02-16 20:57:17 +02003022 if (flag == NFS4_OPEN_DELEGATE_NONE) {
3023 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3024 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
3025 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3026
Benny Halevy4aa89132012-02-21 14:16:44 -08003027 /* 4.1 client asking for a delegation? */
3028 if (open->op_deleg_want)
3029 nfsd4_open_deleg_none_ext(open, status);
Benny Halevyd24433c2012-02-16 20:57:17 +02003030 }
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003031 return;
3032out_free:
J. Bruce Fields24ffb932012-12-12 15:24:12 -05003033 unhash_stid(&dp->dl_stid);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003034 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003035out_no_deleg:
3036 flag = NFS4_OPEN_DELEGATE_NONE;
3037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Benny Halevye27f49c2012-02-21 14:16:54 -08003040static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3041 struct nfs4_delegation *dp)
3042{
3043 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3044 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3045 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3046 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3047 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3048 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3049 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3050 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3051 }
3052 /* Otherwise the client must be confused wanting a delegation
3053 * it already has, therefore we don't return
3054 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3055 */
3056}
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058/*
3059 * called with nfs4_lock_state() held.
3060 */
Al Virob37ad282006-10-19 23:28:59 -07003061__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3063{
Andy Adamson66689582009-04-03 08:28:45 +03003064 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003065 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 struct nfs4_file *fp = NULL;
3067 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003068 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003069 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003070 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 /*
3073 * Lookup file; if found, lookup stateid and check open request,
3074 * and check for delegations in the process of being recalled.
3075 * If not found, create the nfs4_file struct
3076 */
3077 fp = find_file(ino);
3078 if (fp) {
3079 if ((status = nfs4_check_open(fp, open, &stp)))
3080 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003081 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003082 if (status)
3083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07003085 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003086 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003087 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003088 status = nfserr_jukebox;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003089 fp = open->op_file;
3090 open->op_file = NULL;
3091 nfsd4_init_file(fp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093
3094 /*
3095 * OPEN the file, or upgrade an existing OPEN.
3096 * If truncate fails, the OPEN fails.
3097 */
3098 if (stp) {
3099 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003100 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (status)
3102 goto out;
3103 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003104 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003105 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 goto out;
J. Bruce Fields4af82502012-06-07 17:30:45 -04003107 status = nfsd4_truncate(rqstp, current_fh, open);
3108 if (status)
3109 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003110 stp = open->op_stp;
3111 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003112 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003114 update_stateid(&stp->st_stid.sc_stateid);
3115 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Benny Halevyd24433c2012-02-16 20:57:17 +02003117 if (nfsd4_has_session(&resp->cstate)) {
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003118 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03003119
Benny Halevyd24433c2012-02-16 20:57:17 +02003120 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3121 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3122 open->op_why_no_deleg = WND4_NOT_WANTED;
3123 goto nodeleg;
3124 }
3125 }
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 /*
3128 * Attempt to hand out a delegation. No error return, because the
3129 * OPEN succeeds even if we fail.
3130 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003131 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003132nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 status = nfs_ok;
3134
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003135 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003136 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003138 /* 4.1 client trying to upgrade/downgrade delegation? */
3139 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003140 open->op_deleg_want)
3141 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003142
NeilBrown13cd2182005-06-23 22:03:10 -07003143 if (fp)
3144 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003145 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003146 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 /*
3148 * To finish the open response, we just need to set the rflags.
3149 */
3150 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003151 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003152 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3154
3155 return status;
3156}
3157
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003158void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3159{
3160 if (open->op_openowner) {
3161 struct nfs4_openowner *oo = open->op_openowner;
3162
3163 if (!list_empty(&oo->oo_owner.so_stateids))
3164 list_del_init(&oo->oo_close_lru);
3165 if (oo->oo_flags & NFS4_OO_NEW) {
3166 if (status) {
3167 release_openowner(oo);
3168 open->op_openowner = NULL;
3169 } else
3170 oo->oo_flags &= ~NFS4_OO_NEW;
3171 }
3172 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003173 if (open->op_file)
3174 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003175 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003176 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003177}
3178
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003179static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3180{
3181 struct nfs4_client *found;
3182
3183 if (STALE_CLIENTID(clid, nn))
3184 return nfserr_stale_clientid;
3185 found = find_confirmed_client(clid, session, nn);
3186 if (clp)
3187 *clp = found;
3188 return found ? nfs_ok : nfserr_expired;
3189}
3190
Al Virob37ad282006-10-19 23:28:59 -07003191__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003192nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3193 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
3195 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003196 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003197 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199 nfs4_lock_state();
3200 dprintk("process_renew(%08x/%08x): starting\n",
3201 clid->cl_boot, clid->cl_id);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003202 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3203 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003206 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003207 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 goto out;
3209 status = nfs_ok;
3210out:
3211 nfs4_unlock_state();
3212 return status;
3213}
3214
NeilBrowna76b4312005-06-23 22:04:01 -07003215static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003216nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003217{
Jeff Layton33dcc482012-04-10 11:08:48 -04003218 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003219 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003220 return;
3221
NeilBrowna76b4312005-06-23 22:04:01 -07003222 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003223 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003224 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003225 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003226 /*
3227 * Now that every NFSv4 client has had the chance to recover and
3228 * to see the (possibly new, possibly shorter) lease time, we
3229 * can safely set the next grace time to the current lease time:
3230 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003231 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003232}
3233
NeilBrownfd39ca92005-06-23 22:04:03 -07003234static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003235nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
3237 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003238 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 struct nfs4_delegation *dp;
3240 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003241 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3242 time_t t, clientid_val = nn->nfsd4_lease;
3243 time_t u, test_val = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 nfs4_lock_state();
3246
3247 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003248 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003249 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003250 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003251 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 clp = list_entry(pos, struct nfs4_client, cl_lru);
3253 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3254 t = clp->cl_time - cutoff;
3255 if (clientid_val > t)
3256 clientid_val = t;
3257 break;
3258 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003259 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003260 dprintk("NFSD: client in use (clientid %08x)\n",
3261 clp->cl_clientid.cl_id);
3262 continue;
3263 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003264 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003265 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003266 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003267 list_for_each_safe(pos, next, &reaplist) {
3268 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 dprintk("NFSD: purging unused client (clientid %08x)\n",
3270 clp->cl_clientid.cl_id);
3271 expire_client(clp);
3272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003274 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003276 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3277 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3279 u = dp->dl_time - cutoff;
3280 if (test_val > u)
3281 test_val = u;
3282 break;
3283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 list_move(&dp->dl_recall_lru, &reaplist);
3285 }
3286 spin_unlock(&recall_lock);
3287 list_for_each_safe(pos, next, &reaplist) {
3288 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 unhash_delegation(dp);
3290 }
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003291 test_val = nn->nfsd4_lease;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003292 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003293 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3294 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3295 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (test_val > u)
3297 test_val = u;
3298 break;
3299 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003300 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
3302 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3303 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3304 nfs4_unlock_state();
3305 return clientid_val;
3306}
3307
Harvey Harrisona254b242008-02-20 12:49:00 -08003308static struct workqueue_struct *laundry_wq;
3309static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003310
3311static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003312laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
3314 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003315 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3316 work);
3317 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3318 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003320 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003322 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003325static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003326{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003327 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3328 return nfserr_bad_stateid;
3329 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003333access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003335 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3336 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3337 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338}
3339
3340static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003341access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003343 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3344 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
3347static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003348__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Al Virob37ad282006-10-19 23:28:59 -07003350 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
J. Bruce Fields02921912010-07-29 15:16:59 -04003352 /* For lock stateid's, we test the parent open, not the lock: */
3353 if (stp->st_openstp)
3354 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003355 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003357 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 goto out;
3359 status = nfs_ok;
3360out:
3361 return status;
3362}
3363
Al Virob37ad282006-10-19 23:28:59 -07003364static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003365check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003367 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003369 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003370 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 * conflicting state; so we must wait out the grace period. */
3372 return nfserr_grace;
3373 } else if (flags & WR_STATE)
3374 return nfs4_share_conflict(current_fh,
3375 NFS4_SHARE_DENY_WRITE);
3376 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3377 return nfs4_share_conflict(current_fh,
3378 NFS4_SHARE_DENY_READ);
3379}
3380
3381/*
3382 * Allow READ/WRITE during grace period on recovered state only for files
3383 * that are not able to provide mandatory locking.
3384 */
3385static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003386grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003388 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
J. Bruce Fields81b82962011-08-23 11:03:29 -04003391/* Returns true iff a is later than b: */
3392static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3393{
3394 return (s32)a->si_generation - (s32)b->si_generation > 0;
3395}
3396
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003397static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003398{
Andy Adamson66689582009-04-03 08:28:45 +03003399 /*
3400 * When sessions are used the stateid generation number is ignored
3401 * when it is zero.
3402 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003403 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003404 return nfs_ok;
3405
3406 if (in->si_generation == ref->si_generation)
3407 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003408
J. Bruce Fields0836f582008-01-26 19:08:12 -05003409 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003410 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003411 return nfserr_bad_stateid;
3412 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003413 * However, we could see a stateid from the past, even from a
3414 * non-buggy client. For example, if the client sends a lock
3415 * while some IO is outstanding, the lock may bump si_generation
3416 * while the IO is still in flight. The client could avoid that
3417 * situation by waiting for responses on all the IO requests,
3418 * but better performance may result in retrying IO that
3419 * receives an old_stateid error if requests are rarely
3420 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003421 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003422 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003423}
3424
Chuck Lever7df302f2012-05-29 13:56:37 -04003425static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003426{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003427 struct nfs4_stid *s;
3428 struct nfs4_ol_stateid *ols;
3429 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003430
Chuck Lever7df302f2012-05-29 13:56:37 -04003431 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3432 return nfserr_bad_stateid;
3433 /* Client debugging aid. */
3434 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3435 char addr_str[INET6_ADDRSTRLEN];
3436 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3437 sizeof(addr_str));
3438 pr_warn_ratelimited("NFSD: client %s testing state ID "
3439 "with incorrect client ID\n", addr_str);
3440 return nfserr_bad_stateid;
3441 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003442 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003443 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003444 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003445 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003446 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003447 return status;
3448 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3449 return nfs_ok;
3450 ols = openlockstateid(s);
3451 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003452 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003453 return nfserr_bad_stateid;
3454 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003455}
3456
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003457static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3458 struct nfs4_stid **s, bool sessions,
3459 struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003460{
3461 struct nfs4_client *cl;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003462 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003463
3464 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3465 return nfserr_bad_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003466 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3467 nn, &cl);
3468 if (status == nfserr_stale_clientid)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003469 return nfserr_stale_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003470 if (status)
3471 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003472 *s = find_stateid_by_type(cl, stateid, typemask);
3473 if (!*s)
3474 return nfserr_bad_stateid;
3475 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003476}
3477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478/*
3479* Checks for stateid operations
3480*/
Al Virob37ad282006-10-19 23:28:59 -07003481__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003482nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003483 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003485 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003486 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003488 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003490 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07003491 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 if (filpp)
3494 *filpp = NULL;
3495
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003496 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return nfserr_grace;
3498
3499 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003500 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003502 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3503 &s, cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003504 if (status)
3505 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003506 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3507 if (status)
3508 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003509 switch (s->sc_type) {
3510 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003511 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003512 status = nfs4_check_delegmode(dp, flags);
3513 if (status)
3514 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003515 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003516 *filpp = dp->dl_file->fi_deleg_file;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003517 if (!*filpp) {
3518 WARN_ON_ONCE(1);
3519 status = nfserr_serverfault;
3520 goto out;
3521 }
Dan Carpenter43b01782010-10-27 23:19:04 +02003522 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003523 break;
3524 case NFS4_OPEN_STID:
3525 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003526 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003527 status = nfs4_check_fh(current_fh, stp);
3528 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003530 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003531 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003533 status = nfs4_check_openmode(stp, flags);
3534 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003536 if (filpp) {
3537 if (flags & RD_STATE)
3538 *filpp = find_readable_file(stp->st_file);
3539 else
3540 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003541 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003542 break;
3543 default:
3544 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 }
3546 status = nfs_ok;
3547out:
3548 return status;
3549}
3550
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003551static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003552nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003553{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003554 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003555 return nfserr_locks_held;
3556 release_lock_stateid(stp);
3557 return nfs_ok;
3558}
3559
3560/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003561 * Test if the stateid is valid
3562 */
3563__be32
3564nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3565 struct nfsd4_test_stateid *test_stateid)
3566{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003567 struct nfsd4_test_stateid_id *stateid;
3568 struct nfs4_client *cl = cstate->session->se_client;
3569
3570 nfs4_lock_state();
3571 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003572 stateid->ts_id_status =
3573 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003574 nfs4_unlock_state();
3575
Bryan Schumaker17456802011-07-13 10:50:48 -04003576 return nfs_ok;
3577}
3578
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003579__be32
3580nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3581 struct nfsd4_free_stateid *free_stateid)
3582{
3583 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003584 struct nfs4_stid *s;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003585 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003586 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003587
3588 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003589 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003590 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003591 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003592 switch (s->sc_type) {
3593 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003594 ret = nfserr_locks_held;
3595 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003596 case NFS4_OPEN_STID:
3597 case NFS4_LOCK_STID:
3598 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3599 if (ret)
3600 goto out;
3601 if (s->sc_type == NFS4_LOCK_STID)
3602 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3603 else
3604 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003605 break;
3606 default:
3607 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003608 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003609out:
3610 nfs4_unlock_state();
3611 return ret;
3612}
3613
NeilBrown4c4cd222005-07-07 17:59:27 -07003614static inline int
3615setlkflg (int type)
3616{
3617 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3618 RD_STATE : WR_STATE;
3619}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003621static __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 -04003622{
3623 struct svc_fh *current_fh = &cstate->current_fh;
3624 struct nfs4_stateowner *sop = stp->st_stateowner;
3625 __be32 status;
3626
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003627 status = nfsd4_check_seqid(cstate, sop, seqid);
3628 if (status)
3629 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003630 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3631 /*
3632 * "Closed" stateid's exist *only* to return
3633 * nfserr_replay_me from the previous step.
3634 */
3635 return nfserr_bad_stateid;
3636 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3637 if (status)
3638 return status;
3639 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003640}
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642/*
3643 * Checks for sequence id mutating operations.
3644 */
Al Virob37ad282006-10-19 23:28:59 -07003645static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003646nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003647 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003648 struct nfs4_ol_stateid **stpp,
3649 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003651 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003652 struct nfs4_stid *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003654 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3655 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 *stpp = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003658 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3659 cstate->minorversion, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003660 if (status)
3661 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003662 *stpp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003663 if (!nfsd4_has_session(cstate))
3664 cstate->replay_owner = (*stpp)->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003666 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3667}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003668
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003669static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3670 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003671{
3672 __be32 status;
3673 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003675 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003676 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003677 if (status)
3678 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003679 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003680 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003681 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003682 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}
3684
Al Virob37ad282006-10-19 23:28:59 -07003685__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003686nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003687 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Al Virob37ad282006-10-19 23:28:59 -07003689 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003690 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003691 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003692 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
3694 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003695 (int)cstate->current_fh.fh_dentry->d_name.len,
3696 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003698 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003699 if (status)
3700 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
3702 nfs4_lock_state();
3703
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003704 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003705 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003706 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003707 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003708 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003709 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003710 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003711 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003712 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003713 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003714 update_stateid(&stp->st_stid.sc_stateid);
3715 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003716 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003717 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003718
Jeff Layton2a4317c2012-03-21 16:42:43 -04003719 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003720 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003722 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003723 if (!cstate->replay_owner)
3724 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 return status;
3726}
3727
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003728static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003730 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003731 return;
3732 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04003733 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003734}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003735
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003736static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3737{
3738 switch (to_access) {
3739 case NFS4_SHARE_ACCESS_READ:
3740 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3741 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3742 break;
3743 case NFS4_SHARE_ACCESS_WRITE:
3744 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3745 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3746 break;
3747 case NFS4_SHARE_ACCESS_BOTH:
3748 break;
3749 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003750 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 }
3752}
3753
3754static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04003755reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
3757 int i;
3758 for (i = 0; i < 4; i++) {
3759 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04003760 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 }
3762}
3763
Al Virob37ad282006-10-19 23:28:59 -07003764__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003765nfsd4_open_downgrade(struct svc_rqst *rqstp,
3766 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003767 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
Al Virob37ad282006-10-19 23:28:59 -07003769 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003770 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003771 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003774 (int)cstate->current_fh.fh_dentry->d_name.len,
3775 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04003777 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02003778 if (od->od_deleg_want)
3779 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3780 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003783 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003784 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003785 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003788 if (!test_access(od->od_share_access, stp)) {
3789 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 stp->st_access_bmap, od->od_share_access);
3791 goto out;
3792 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04003793 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3795 stp->st_deny_bmap, od->od_share_deny);
3796 goto out;
3797 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003798 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Jeff Laytonce0fc432012-05-11 09:45:14 -04003800 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003802 update_stateid(&stp->st_stid.sc_stateid);
3803 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 status = nfs_ok;
3805out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003806 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003807 if (!cstate->replay_owner)
3808 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 return status;
3810}
3811
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003812static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3813{
3814 unhash_open_stateid(s);
3815 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003816}
3817
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818/*
3819 * nfs4_unlock_state() called after encode
3820 */
Al Virob37ad282006-10-19 23:28:59 -07003821__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003822nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003823 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
Al Virob37ad282006-10-19 23:28:59 -07003825 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003826 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003827 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003828 struct net *net = SVC_NET(rqstp);
3829 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003832 (int)cstate->current_fh.fh_dentry->d_name.len,
3833 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
3835 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003836 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3837 &close->cl_stateid,
3838 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003839 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003840 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003841 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003843 oo = openowner(stp->st_stateowner);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003844 update_stateid(&stp->st_stid.sc_stateid);
3845 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003847 nfsd4_close_open_stateid(stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003848
3849 if (cstate->minorversion) {
3850 unhash_stid(&stp->st_stid);
3851 free_generic_stateid(stp);
3852 } else
3853 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003854
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003855 if (list_empty(&oo->oo_owner.so_stateids)) {
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003856 if (cstate->minorversion)
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003857 release_openowner(oo);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003858 else {
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003859 /*
3860 * In the 4.0 case we need to keep the owners around a
3861 * little while to handle CLOSE replay.
3862 */
3863 if (list_empty(&oo->oo_owner.so_stateids))
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003864 move_to_close_lru(oo, SVC_NET(rqstp));
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003865 }
3866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003868 if (!cstate->replay_owner)
3869 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return status;
3871}
3872
Al Virob37ad282006-10-19 23:28:59 -07003873__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003874nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3875 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003877 struct nfs4_delegation *dp;
3878 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003879 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07003880 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003881 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003883 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003884 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 nfs4_lock_state();
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003887 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3888 cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003889 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003890 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003891 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003892 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003893 if (status)
3894 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003895
3896 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003898 nfs4_unlock_state();
3899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 return status;
3901}
3902
3903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
J. Bruce Fields009673b2011-11-07 17:40:10 -05003906#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Benny Halevy87df4de2008-12-15 19:42:03 +02003908static inline u64
3909end_offset(u64 start, u64 len)
3910{
3911 u64 end;
3912
3913 end = start + len;
3914 return end >= start ? end: NFS4_MAX_UINT64;
3915}
3916
3917/* last octet in a range */
3918static inline u64
3919last_byte_offset(u64 start, u64 len)
3920{
3921 u64 end;
3922
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003923 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02003924 end = start + len;
3925 return end > start ? end - 1: NFS4_MAX_UINT64;
3926}
3927
J. Bruce Fields009673b2011-11-07 17:40:10 -05003928static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
3930 return (file_hashval(inode) + cl_id
3931 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05003932 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935/*
3936 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3937 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3938 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3939 * locking, this prevents us from being completely protocol-compliant. The
3940 * real solution to this problem is to start using unsigned file offsets in
3941 * the VFS, but this is a very deep change!
3942 */
3943static inline void
3944nfs4_transform_lock_offset(struct file_lock *lock)
3945{
3946 if (lock->fl_start < 0)
3947 lock->fl_start = OFFSET_MAX;
3948 if (lock->fl_end < 0)
3949 lock->fl_end = OFFSET_MAX;
3950}
3951
NeilBrownd5b90262006-04-10 22:55:22 -07003952/* Hack!: For now, we're defining this just so we can use a pointer to it
3953 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003954static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003955};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
3957static inline void
3958nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3959{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003960 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
NeilBrownd5b90262006-04-10 22:55:22 -07003962 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003963 lo = (struct nfs4_lockowner *) fl->fl_owner;
3964 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3965 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003966 if (!deny->ld_owner.data)
3967 /* We just don't care that much */
3968 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003969 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3970 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003971 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003972nevermind:
3973 deny->ld_owner.len = 0;
3974 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003975 deny->ld_clientid.cl_boot = 0;
3976 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
3978 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003979 deny->ld_length = NFS4_MAX_UINT64;
3980 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3982 deny->ld_type = NFS4_READ_LT;
3983 if (fl->fl_type != F_RDLCK)
3984 deny->ld_type = NFS4_WRITE_LT;
3985}
3986
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003987static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3988{
3989 struct nfs4_ol_stateid *lst;
3990
3991 if (!same_owner_str(&lo->lo_owner, owner, clid))
3992 return false;
3993 lst = list_first_entry(&lo->lo_owner.so_stateids,
3994 struct nfs4_ol_stateid, st_perstateowner);
3995 return lst->st_file->fi_inode == inode;
3996}
3997
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003998static struct nfs4_lockowner *
3999find_lockowner_str(struct inode *inode, clientid_t *clid,
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004000 struct xdr_netobj *owner, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004002 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004003 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004005 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004006 if (same_lockowner_ino(lo, inode, clid, owner))
4007 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
4009 return NULL;
4010}
4011
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004012static 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 -04004013{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004014 struct inode *inode = open_stp->st_file->fi_inode;
4015 unsigned int inohash = lockowner_ino_hashval(inode,
4016 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004017 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields009673b2011-11-07 17:40:10 -05004018
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004019 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004020 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004021 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004022}
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024/*
4025 * Alloc a lock owner structure.
4026 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004027 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004029 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 */
4031
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004032static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004033alloc_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 -04004034 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004036 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4037 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004039 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4040 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004041 /* It is the openowner seqid that will be incremented in encode in the
4042 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004043 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4044 hash_lockowner(lo, strhashval, clp, open_stp);
4045 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004048static struct nfs4_ol_stateid *
4049alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004051 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004052 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
J. Bruce Fields996e0932011-10-17 11:14:48 -04004054 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004055 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004056 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004057 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown8beefa22005-06-23 22:03:08 -07004058 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004059 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4060 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004061 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004063 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004065 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 return stp;
4067}
4068
NeilBrownfd39ca92005-06-23 22:04:03 -07004069static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070check_lock_length(u64 offset, u64 length)
4071{
Benny Halevy87df4de2008-12-15 19:42:03 +02004072 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 LOFF_OVERFLOW(offset, length)));
4074}
4075
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004076static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004077{
4078 struct nfs4_file *fp = lock_stp->st_file;
4079 int oflag = nfs4_access_to_omode(access);
4080
Jeff Layton82c5ff12012-05-11 09:45:13 -04004081 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004082 return;
4083 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004084 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004085}
4086
J. Bruce Fields2355c592012-04-25 16:56:22 -04004087static __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 -04004088{
4089 struct nfs4_file *fi = ost->st_file;
4090 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4091 struct nfs4_client *cl = oo->oo_owner.so_client;
4092 struct nfs4_lockowner *lo;
4093 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004094 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004095
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004096 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4097 &lock->v.new.owner, nn);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004098 if (lo) {
4099 if (!cstate->minorversion)
4100 return nfserr_bad_seqid;
4101 /* XXX: a lockowner always has exactly one stateid: */
4102 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4103 struct nfs4_ol_stateid, st_perstateowner);
4104 return nfs_ok;
4105 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004106 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004107 &lock->v.new.owner);
4108 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4109 if (lo == NULL)
4110 return nfserr_jukebox;
4111 *lst = alloc_init_lock_stateid(lo, fi, ost);
4112 if (*lst == NULL) {
4113 release_lockowner(lo);
4114 return nfserr_jukebox;
4115 }
4116 *new = true;
4117 return nfs_ok;
4118}
4119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120/*
4121 * LOCK operation
4122 */
Al Virob37ad282006-10-19 23:28:59 -07004123__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004124nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004125 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004127 struct nfs4_openowner *open_sop = NULL;
4128 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004129 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004130 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004131 struct file_lock *file_lock = NULL;
4132 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004133 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004134 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004135 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004136 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004137 struct net *net = SVC_NET(rqstp);
4138 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
4140 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4141 (long long) lock->lk_offset,
4142 (long long) lock->lk_length);
4143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (check_lock_length(lock->lk_offset, lock->lk_length))
4145 return nfserr_inval;
4146
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004147 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004148 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004149 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4150 return status;
4151 }
4152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 nfs4_lock_state();
4154
4155 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004156 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004157
4158 if (nfsd4_has_session(cstate))
4159 /* See rfc 5661 18.10.3: given clientid is ignored: */
4160 memcpy(&lock->v.new.clientid,
4161 &cstate->session->se_client->cl_clientid,
4162 sizeof(clientid_t));
4163
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004165 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004169 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 lock->lk_new_open_seqid,
4171 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004172 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004173 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004175 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004176 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004177 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004178 &lock->v.new.clientid))
4179 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004180 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4181 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004182 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004183 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004184 lock->lk_old_lock_seqid,
4185 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004186 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004187 if (status)
4188 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004189 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004191 lkflg = setlkflg(lock->lk_type);
4192 status = nfs4_check_openmode(lock_stp, lkflg);
4193 if (status)
4194 goto out;
4195
NeilBrown0dd395d2005-07-07 17:59:15 -07004196 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004197 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004198 goto out;
4199 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004200 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004201 goto out;
4202
Jeff Layton21179d82012-08-21 08:03:32 -04004203 file_lock = locks_alloc_lock();
4204 if (!file_lock) {
4205 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4206 status = nfserr_jukebox;
4207 goto out;
4208 }
4209
4210 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 switch (lock->lk_type) {
4212 case NFS4_READ_LT:
4213 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004214 filp = find_readable_file(lock_stp->st_file);
4215 if (filp)
4216 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004217 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 case NFS4_WRITE_LT:
4220 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004221 filp = find_writeable_file(lock_stp->st_file);
4222 if (filp)
4223 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004224 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 default:
4227 status = nfserr_inval;
4228 goto out;
4229 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004230 if (!filp) {
4231 status = nfserr_openmode;
4232 goto out;
4233 }
Jeff Layton21179d82012-08-21 08:03:32 -04004234 file_lock->fl_owner = (fl_owner_t)lock_sop;
4235 file_lock->fl_pid = current->tgid;
4236 file_lock->fl_file = filp;
4237 file_lock->fl_flags = FL_POSIX;
4238 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4239 file_lock->fl_start = lock->lk_offset;
4240 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4241 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
Jeff Layton21179d82012-08-21 08:03:32 -04004243 conflock = locks_alloc_lock();
4244 if (!conflock) {
4245 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4246 status = nfserr_jukebox;
4247 goto out;
4248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Jeff Layton21179d82012-08-21 08:03:32 -04004250 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004251 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004253 update_stateid(&lock_stp->st_stid.sc_stateid);
4254 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004256 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004257 break;
4258 case (EAGAIN): /* conflock holds conflicting lock */
4259 status = nfserr_denied;
4260 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004261 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 case (EDEADLK):
4264 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004265 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004266 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004267 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004268 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004272 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004273 release_lockowner(lock_sop);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004274 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004275 if (!cstate->replay_owner)
4276 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004277 if (file_lock)
4278 locks_free_lock(file_lock);
4279 if (conflock)
4280 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return status;
4282}
4283
4284/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004285 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4286 * so we do a temporary open here just to get an open file to pass to
4287 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4288 * inode operation.)
4289 */
Al Viro04da6e92012-04-13 00:00:04 -04004290static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004291{
4292 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004293 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4294 if (!err) {
4295 err = nfserrno(vfs_test_lock(file, lock));
4296 nfsd_close(file);
4297 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004298 return err;
4299}
4300
4301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 * LOCKT operation
4303 */
Al Virob37ad282006-10-19 23:28:59 -07004304__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004305nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4306 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
4308 struct inode *inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004309 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004310 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004311 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004312 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004314 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return nfserr_grace;
4316
4317 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4318 return nfserr_inval;
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 nfs4_lock_state();
4321
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004322 if (!nfsd4_has_session(cstate)) {
4323 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4324 if (status)
4325 goto out;
4326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004328 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004331 inode = cstate->current_fh.fh_dentry->d_inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004332 file_lock = locks_alloc_lock();
4333 if (!file_lock) {
4334 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4335 status = nfserr_jukebox;
4336 goto out;
4337 }
4338 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 switch (lockt->lt_type) {
4340 case NFS4_READ_LT:
4341 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004342 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 break;
4344 case NFS4_WRITE_LT:
4345 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004346 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 break;
4348 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004349 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 status = nfserr_inval;
4351 goto out;
4352 }
4353
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004354 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004355 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004356 file_lock->fl_owner = (fl_owner_t)lo;
4357 file_lock->fl_pid = current->tgid;
4358 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Jeff Layton21179d82012-08-21 08:03:32 -04004360 file_lock->fl_start = lockt->lt_offset;
4361 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Jeff Layton21179d82012-08-21 08:03:32 -04004363 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Jeff Layton21179d82012-08-21 08:03:32 -04004365 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004366 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004367 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004368
Jeff Layton21179d82012-08-21 08:03:32 -04004369 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004371 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 }
4373out:
4374 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004375 if (file_lock)
4376 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 return status;
4378}
4379
Al Virob37ad282006-10-19 23:28:59 -07004380__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004381nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004382 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004384 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004385 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004387 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004388 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004389 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004390 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4393 (long long) locku->lu_offset,
4394 (long long) locku->lu_length);
4395
4396 if (check_lock_length(locku->lu_offset, locku->lu_length))
4397 return nfserr_inval;
4398
4399 nfs4_lock_state();
4400
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004401 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004402 &locku->lu_stateid, NFS4_LOCK_STID,
4403 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004404 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004406 filp = find_any_file(stp->st_file);
4407 if (!filp) {
4408 status = nfserr_lock_range;
4409 goto out;
4410 }
Jeff Layton21179d82012-08-21 08:03:32 -04004411 file_lock = locks_alloc_lock();
4412 if (!file_lock) {
4413 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4414 status = nfserr_jukebox;
4415 goto out;
4416 }
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004417 lo = lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04004418 locks_init_lock(file_lock);
4419 file_lock->fl_type = F_UNLCK;
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004420 file_lock->fl_owner = (fl_owner_t)lo;
Jeff Layton21179d82012-08-21 08:03:32 -04004421 file_lock->fl_pid = current->tgid;
4422 file_lock->fl_file = filp;
4423 file_lock->fl_flags = FL_POSIX;
4424 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4425 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Jeff Layton21179d82012-08-21 08:03:32 -04004427 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4428 locku->lu_length);
4429 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Jeff Layton21179d82012-08-21 08:03:32 -04004431 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004432 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004433 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 goto out_nfserr;
4435 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004436 update_stateid(&stp->st_stid.sc_stateid);
4437 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004439 if (nfsd4_has_session(cstate) && !check_for_locks(stp->st_file, lo)) {
4440 WARN_ON_ONCE(cstate->replay_owner);
4441 release_lockowner(lo);
4442 }
4443
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004445 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004446 if (!cstate->replay_owner)
4447 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004448 if (file_lock)
4449 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 return status;
4451
4452out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004453 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 goto out;
4455}
4456
4457/*
4458 * returns
4459 * 1: locks held by lockowner
4460 * 0: no locks held by lockowner
4461 */
4462static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004463check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464{
4465 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004466 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 int status = 0;
4468
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004469 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004471 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 status = 1;
4473 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 }
4476out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004477 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 return status;
4479}
4480
Al Virob37ad282006-10-19 23:28:59 -07004481__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004482nfsd4_release_lockowner(struct svc_rqst *rqstp,
4483 struct nfsd4_compound_state *cstate,
4484 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485{
4486 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004487 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004488 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004489 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004491 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004492 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004493 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004494 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
4496 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4497 clid->cl_boot, clid->cl_id);
4498
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 nfs4_lock_state();
4500
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004501 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4502 if (status)
4503 goto out;
4504
NeilBrown3e9e3db2005-06-23 22:04:20 -07004505 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004506 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004507
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004508 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004509 if (sop->so_is_open_owner)
4510 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004511 if (!same_owner_str(sop, owner, clid))
4512 continue;
4513 list_for_each_entry(stp, &sop->so_stateids,
4514 st_perstateowner) {
4515 lo = lockowner(sop);
4516 if (check_for_locks(stp->st_file, lo))
4517 goto out;
4518 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004520 }
4521 /* Clients probably won't expect us to return with some (but not all)
4522 * of the lockowner state released; so don't release any until all
4523 * have been checked. */
4524 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004525 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004526 lo = list_entry(matches.next, struct nfs4_lockowner,
4527 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004528 /* unhash_stateowner deletes so_perclient only
4529 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004530 list_del(&lo->lo_list);
4531 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 }
4533out:
4534 nfs4_unlock_state();
4535 return status;
4536}
4537
4538static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004539alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
NeilBrowna55370a2005-06-23 22:03:52 -07004541 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542}
4543
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004544bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004545nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004546{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004547 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004548
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004549 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004550 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004551}
4552
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553/*
4554 * failure => all reset bets are off, nfserr_no_grace...
4555 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004556struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004557nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
4559 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004560 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
NeilBrowna55370a2005-06-23 22:03:52 -07004562 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4563 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004564 if (crp) {
4565 strhashval = clientstr_hashval(name);
4566 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004567 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004568 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004569 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004570 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004571 }
4572 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
Jeff Layton2a4317c2012-03-21 16:42:43 -04004575void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004576nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004577{
4578 list_del(&crp->cr_strhash);
4579 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004580 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004581}
4582
4583void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004584nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
4586 struct nfs4_client_reclaim *crp = NULL;
4587 int i;
4588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004590 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4591 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004593 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 }
4595 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004596 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597}
4598
4599/*
4600 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004601struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004602nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603{
4604 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 struct nfs4_client_reclaim *crp = NULL;
4606
Jeff Layton278c9312012-11-12 15:00:52 -05004607 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Jeff Layton278c9312012-11-12 15:00:52 -05004609 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004610 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004611 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 return crp;
4613 }
4614 }
4615 return NULL;
4616}
4617
4618/*
4619* Called from OPEN. Look for clientid in reclaim list.
4620*/
Al Virob37ad282006-10-19 23:28:59 -07004621__be32
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004622nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
Jeff Laytona52d7262012-03-21 09:52:02 -04004624 struct nfs4_client *clp;
4625
4626 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004627 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004628 if (clp == NULL)
4629 return nfserr_reclaim_bad;
4630
4631 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
Bryan Schumaker65178db2011-11-01 13:35:21 -04004634#ifdef CONFIG_NFSD_FAULT_INJECTION
4635
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004636u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4637{
J. Bruce Fields221a6872013-04-01 22:23:49 -04004638 if (mark_client_expired(clp))
4639 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004640 expire_client(clp);
4641 return 1;
4642}
4643
Bryan Schumaker184c1842012-11-29 11:40:44 -05004644u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4645{
4646 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004647 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004648 printk(KERN_INFO "NFS Client: %s\n", buf);
4649 return 1;
4650}
4651
4652static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4653 const char *type)
4654{
4655 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004656 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004657 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4658}
4659
Bryan Schumakerfc291712012-11-29 11:40:40 -05004660static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4661{
4662 struct nfs4_openowner *oop;
4663 struct nfs4_lockowner *lop, *lo_next;
4664 struct nfs4_ol_stateid *stp, *st_next;
4665 u64 count = 0;
4666
4667 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4668 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4669 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4670 if (func)
4671 func(lop);
4672 if (++count == max)
4673 return count;
4674 }
4675 }
4676 }
4677
4678 return count;
4679}
4680
4681u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4682{
4683 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4684}
4685
Bryan Schumaker184c1842012-11-29 11:40:44 -05004686u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4687{
4688 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4689 nfsd_print_count(clp, count, "locked files");
4690 return count;
4691}
4692
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05004693static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4694{
4695 struct nfs4_openowner *oop, *next;
4696 u64 count = 0;
4697
4698 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4699 if (func)
4700 func(oop);
4701 if (++count == max)
4702 break;
4703 }
4704
4705 return count;
4706}
4707
4708u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4709{
4710 return nfsd_foreach_client_open(clp, max, release_openowner);
4711}
4712
Bryan Schumaker184c1842012-11-29 11:40:44 -05004713u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
4714{
4715 u64 count = nfsd_foreach_client_open(clp, max, NULL);
4716 nfsd_print_count(clp, count, "open files");
4717 return count;
4718}
4719
Bryan Schumaker269de302012-11-29 11:40:42 -05004720static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
4721 struct list_head *victims)
4722{
4723 struct nfs4_delegation *dp, *next;
4724 u64 count = 0;
4725
4726 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
4727 if (victims)
4728 list_move(&dp->dl_recall_lru, victims);
4729 if (++count == max)
4730 break;
4731 }
4732 return count;
4733}
4734
4735u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
4736{
4737 struct nfs4_delegation *dp, *next;
4738 LIST_HEAD(victims);
4739 u64 count;
4740
4741 spin_lock(&recall_lock);
4742 count = nfsd_find_all_delegations(clp, max, &victims);
4743 spin_unlock(&recall_lock);
4744
4745 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4746 unhash_delegation(dp);
4747
4748 return count;
4749}
4750
4751u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
4752{
4753 struct nfs4_delegation *dp, *next;
4754 LIST_HEAD(victims);
4755 u64 count;
4756
4757 spin_lock(&recall_lock);
4758 count = nfsd_find_all_delegations(clp, max, &victims);
4759 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4760 nfsd_break_one_deleg(dp);
4761 spin_unlock(&recall_lock);
4762
4763 return count;
4764}
4765
Bryan Schumaker184c1842012-11-29 11:40:44 -05004766u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
4767{
4768 u64 count = 0;
4769
4770 spin_lock(&recall_lock);
4771 count = nfsd_find_all_delegations(clp, max, NULL);
4772 spin_unlock(&recall_lock);
4773
4774 nfsd_print_count(clp, count, "delegations");
4775 return count;
4776}
4777
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004778u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004779{
4780 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004781 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004782 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004783
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004784 if (!nfsd_netns_ready(nn))
4785 return 0;
4786
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004787 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004788 count += func(clp, max - count);
4789 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004790 break;
4791 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04004792
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004793 return count;
4794}
4795
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05004796struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
4797{
4798 struct nfs4_client *clp;
4799 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4800
4801 if (!nfsd_netns_ready(nn))
4802 return NULL;
4803
4804 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
4805 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
4806 return clp;
4807 }
4808 return NULL;
4809}
4810
Bryan Schumaker65178db2011-11-01 13:35:21 -04004811#endif /* CONFIG_NFSD_FAULT_INJECTION */
4812
NeilBrownac4d8ff22005-06-23 22:03:30 -07004813/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Bryan Schumaker72083392011-11-01 15:24:59 -04004815void
NeilBrownac4d8ff22005-06-23 22:03:30 -07004816nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
NeilBrownac4d8ff22005-06-23 22:03:30 -07004818}
4819
Meelap Shahc2f1a552007-07-17 04:04:39 -07004820/*
4821 * Since the lifetime of a delegation isn't limited to that of an open, a
4822 * client may quite reasonably hang on to a delegation as long as it has
4823 * the inode cached. This becomes an obvious problem the first time a
4824 * client's inode cache approaches the size of the server's total memory.
4825 *
4826 * For now we avoid this problem by imposing a hard limit on the number
4827 * of delegations, which varies according to the server's memory size.
4828 */
4829static void
4830set_max_delegations(void)
4831{
4832 /*
4833 * Allow at most 4 delegations per megabyte of RAM. Quick
4834 * estimates suggest that in the worst case (where every delegation
4835 * is for a different inode), a delegation could take about 1.5K,
4836 * giving a worst case usage of about 6% of memory.
4837 */
4838 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4839}
4840
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004841static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004842{
4843 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4844 int i;
4845
4846 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4847 CLIENT_HASH_SIZE, GFP_KERNEL);
4848 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004849 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004850 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4851 CLIENT_HASH_SIZE, GFP_KERNEL);
4852 if (!nn->unconf_id_hashtbl)
4853 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004854 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4855 OWNER_HASH_SIZE, GFP_KERNEL);
4856 if (!nn->ownerstr_hashtbl)
4857 goto err_ownerstr;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004858 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4859 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4860 if (!nn->lockowner_ino_hashtbl)
4861 goto err_lockowner_ino;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004862 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4863 SESSION_HASH_SIZE, GFP_KERNEL);
4864 if (!nn->sessionid_hashtbl)
4865 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004866
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004867 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004868 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004869 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004870 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004871 for (i = 0; i < OWNER_HASH_SIZE; i++)
4872 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004873 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4874 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004875 for (i = 0; i < SESSION_HASH_SIZE; i++)
4876 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004877 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004878 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004879 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004880 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004881 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004882 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004883
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004884 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004885 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004886
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004887 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004888
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004889err_sessionid:
4890 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004891err_lockowner_ino:
4892 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004893err_ownerstr:
4894 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004895err_unconf_id:
4896 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004897err:
4898 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004899}
4900
4901static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004902nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004903{
4904 int i;
4905 struct nfs4_client *clp = NULL;
4906 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004907 struct rb_node *node, *tmp;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004908
4909 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4910 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4911 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4912 destroy_client(clp);
4913 }
4914 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004915
4916 node = rb_first(&nn->unconf_name_tree);
4917 while (node != NULL) {
4918 tmp = node;
4919 node = rb_next(tmp);
4920 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4921 rb_erase(tmp, &nn->unconf_name_tree);
4922 destroy_client(clp);
4923 }
4924
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004925 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004926 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004927 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004928 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004929 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004930 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004931}
4932
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004933int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004934nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07004935{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004936 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004937 int ret;
4938
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004939 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004940 if (ret)
4941 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004942 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004943 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004944 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004945 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004946 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004947 nn->nfsd4_grace, net);
4948 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004949 return 0;
4950}
4951
4952/* initialization to perform when the nfsd service is started: */
4953
4954int
4955nfs4_state_start(void)
4956{
4957 int ret;
4958
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004959 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004960 if (ret)
4961 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004962 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05004963 if (laundry_wq == NULL) {
4964 ret = -ENOMEM;
4965 goto out_recovery;
4966 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004967 ret = nfsd4_create_callback_queue();
4968 if (ret)
4969 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004970
Meelap Shahc2f1a552007-07-17 04:04:39 -07004971 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004972
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004973 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004974
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004975out_free_laundry:
4976 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05004977out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004978 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979}
4980
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004981/* should be called with the state lock held */
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004982void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004983nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004987 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004989 cancel_delayed_work_sync(&nn->laundromat_work);
4990 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 INIT_LIST_HEAD(&reaplist);
4993 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004994 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4996 list_move(&dp->dl_recall_lru, &reaplist);
4997 }
4998 spin_unlock(&recall_lock);
4999 list_for_each_safe(pos, next, &reaplist) {
5000 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 unhash_delegation(dp);
5002 }
5003
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005004 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005005 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006}
5007
5008void
5009nfs4_state_shutdown(void)
5010{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005011 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005012 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005014
5015static void
5016get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5017{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005018 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5019 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005020}
5021
5022static void
5023put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5024{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005025 if (cstate->minorversion) {
5026 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5027 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5028 }
5029}
5030
5031void
5032clear_current_stateid(struct nfsd4_compound_state *cstate)
5033{
5034 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005035}
5036
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005037/*
5038 * functions to set current state id
5039 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005040void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005041nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5042{
5043 put_stateid(cstate, &odp->od_stateid);
5044}
5045
5046void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005047nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5048{
5049 put_stateid(cstate, &open->op_stateid);
5050}
5051
5052void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005053nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5054{
5055 put_stateid(cstate, &close->cl_stateid);
5056}
5057
5058void
5059nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5060{
5061 put_stateid(cstate, &lock->lk_resp_stateid);
5062}
5063
5064/*
5065 * functions to consume current state id
5066 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005067
5068void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005069nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5070{
5071 get_stateid(cstate, &odp->od_stateid);
5072}
5073
5074void
5075nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5076{
5077 get_stateid(cstate, &drp->dr_stateid);
5078}
5079
5080void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005081nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5082{
5083 get_stateid(cstate, &fsp->fr_stateid);
5084}
5085
5086void
5087nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5088{
5089 get_stateid(cstate, &setattr->sa_stateid);
5090}
5091
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005092void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005093nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5094{
5095 get_stateid(cstate, &close->cl_stateid);
5096}
5097
5098void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005099nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005100{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005101 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005102}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005103
5104void
5105nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5106{
5107 get_stateid(cstate, &read->rd_stateid);
5108}
5109
5110void
5111nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5112{
5113 get_stateid(cstate, &write->wr_stateid);
5114}