blob: a964a1761077e91e1d2b40d3bc657fd1d50c05bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020044#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040045#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050046#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040047#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040049#include "netns.h"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define NFSDDBG_FACILITY NFSDDBG_PROC
52
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050053#define all_ones {{~0,~0},~0}
54static const stateid_t one_stateid = {
55 .si_generation = ~0,
56 .si_opaque = all_ones,
57};
58static const stateid_t zero_stateid = {
59 /* all fields zero */
60};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010061static const stateid_t currentstateid = {
62 .si_generation = 1,
63};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050064
Andy Adamsonec6b5d72009-04-03 08:28:28 +030065static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070066
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050067#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
68#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010069#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040072static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
J. Bruce Fields8b671b82009-02-22 14:51:34 -080074/* Locking: */
75
76/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080077static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
J. Bruce Fields8b671b82009-02-22 14:51:34 -080079/*
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
83 */
84static DEFINE_SPINLOCK(recall_lock);
85
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040086static struct kmem_cache *openowner_slab = NULL;
87static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080088static struct kmem_cache *file_slab = NULL;
89static struct kmem_cache *stateid_slab = NULL;
90static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092void
93nfs4_lock_state(void)
94{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080095 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040098static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -080099
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400100void nfsd4_put_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800101{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400102 atomic_dec(&ses->se_ref);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800103}
104
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400105static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800106{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400107 return ses->se_flags & NFS4_SESSION_DEAD;
108}
109
110static __be32 mark_session_dead_locked(struct nfsd4_session *ses)
111{
112 if (atomic_read(&ses->se_ref))
113 return nfserr_jukebox;
114 ses->se_flags |= NFS4_SESSION_DEAD;
115 return nfs_ok;
116}
117
118static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
119{
120 if (is_session_dead(ses))
121 return nfserr_badsession;
122 atomic_inc(&ses->se_ref);
123 return nfs_ok;
Benny Halevy508dc6e2012-02-23 17:40:52 -0800124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126void
127nfs4_unlock_state(void)
128{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800129 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
J. Bruce Fields221a6872013-04-01 22:23:49 -0400132static bool is_client_expired(struct nfs4_client *clp)
133{
134 return clp->cl_time == 0;
135}
136
137static __be32 mark_client_expired_locked(struct nfs4_client *clp)
138{
139 if (atomic_read(&clp->cl_refcount))
140 return nfserr_jukebox;
141 clp->cl_time = 0;
142 return nfs_ok;
143}
144
145static __be32 mark_client_expired(struct nfs4_client *clp)
146{
147 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
148 __be32 ret;
149
150 spin_lock(&nn->client_lock);
151 ret = mark_client_expired_locked(clp);
152 spin_unlock(&nn->client_lock);
153 return ret;
154}
155
156static __be32 get_client_locked(struct nfs4_client *clp)
157{
158 if (is_client_expired(clp))
159 return nfserr_expired;
160 atomic_inc(&clp->cl_refcount);
161 return nfs_ok;
162}
163
164/* must be called under the client_lock */
165static inline void
166renew_client_locked(struct nfs4_client *clp)
167{
168 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
169
170 if (is_client_expired(clp)) {
171 WARN_ON(1);
172 printk("%s: client (clientid %08x/%08x) already expired\n",
173 __func__,
174 clp->cl_clientid.cl_boot,
175 clp->cl_clientid.cl_id);
176 return;
177 }
178
179 dprintk("renewing client (clientid %08x/%08x)\n",
180 clp->cl_clientid.cl_boot,
181 clp->cl_clientid.cl_id);
182 list_move_tail(&clp->cl_lru, &nn->client_lru);
183 clp->cl_time = get_seconds();
184}
185
186static inline void
187renew_client(struct nfs4_client *clp)
188{
189 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
190
191 spin_lock(&nn->client_lock);
192 renew_client_locked(clp);
193 spin_unlock(&nn->client_lock);
194}
195
Fengguang Wuba138432013-04-16 22:14:15 -0400196static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400197{
198 if (!atomic_dec_and_test(&clp->cl_refcount))
199 return;
200 if (!is_client_expired(clp))
201 renew_client_locked(clp);
202}
203
204void put_client_renew(struct nfs4_client *clp)
205{
206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207
208 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
209 return;
210 if (!is_client_expired(clp))
211 renew_client_locked(clp);
212 spin_unlock(&nn->client_lock);
213}
214
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static inline u32
217opaque_hashval(const void *ptr, int nbytes)
218{
219 unsigned char *cptr = (unsigned char *) ptr;
220
221 u32 x = 0;
222 while (nbytes--) {
223 x *= 37;
224 x += *cptr++;
225 }
226 return x;
227}
228
J. Bruce Fields32513b42011-10-13 16:00:16 -0400229static void nfsd4_free_file(struct nfs4_file *f)
230{
231 kmem_cache_free(file_slab, f);
232}
233
NeilBrown13cd2182005-06-23 22:03:10 -0700234static inline void
235put_nfs4_file(struct nfs4_file *fi)
236{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800237 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400238 hlist_del(&fi->fi_hash);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800239 spin_unlock(&recall_lock);
240 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400241 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800242 }
NeilBrown13cd2182005-06-23 22:03:10 -0700243}
244
245static inline void
246get_nfs4_file(struct nfs4_file *fi)
247{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800248 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700249}
250
NeilBrownef0f3392006-04-10 22:55:41 -0700251static int num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800252unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700253
254/*
255 * Open owner state (share locks)
256 */
257
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500258/* hash tables for lock and open owners */
259#define OWNER_HASH_BITS 8
260#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
261#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700262
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500263static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400264{
265 unsigned int ret;
266
267 ret = opaque_hashval(ownername->data, ownername->len);
268 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500269 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400270}
NeilBrownef0f3392006-04-10 22:55:41 -0700271
NeilBrownef0f3392006-04-10 22:55:41 -0700272/* hash table for nfs4_file */
273#define FILE_HASH_BITS 8
274#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800275
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400276static unsigned int file_hashval(struct inode *ino)
277{
278 /* XXX: why are we hashing on inode pointer, anyway? */
279 return hash_ptr(ino, FILE_HASH_BITS);
280}
281
Jeff Layton89876f82013-04-02 09:01:59 -0400282static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700283
J. Bruce Fields998db522010-08-07 09:21:41 -0400284static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400285{
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500286 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400287 atomic_inc(&fp->fi_access[oflag]);
288}
289
J. Bruce Fields998db522010-08-07 09:21:41 -0400290static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
291{
292 if (oflag == O_RDWR) {
293 __nfs4_file_get_access(fp, O_RDONLY);
294 __nfs4_file_get_access(fp, O_WRONLY);
295 } else
296 __nfs4_file_get_access(fp, oflag);
297}
298
299static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400300{
301 if (fp->fi_fds[oflag]) {
302 fput(fp->fi_fds[oflag]);
303 fp->fi_fds[oflag] = NULL;
304 }
305}
306
J. Bruce Fields998db522010-08-07 09:21:41 -0400307static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400308{
309 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400310 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400311 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400312 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400313 }
314}
315
J. Bruce Fields998db522010-08-07 09:21:41 -0400316static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
317{
318 if (oflag == O_RDWR) {
319 __nfs4_file_put_access(fp, O_RDONLY);
320 __nfs4_file_put_access(fp, O_WRONLY);
321 } else
322 __nfs4_file_put_access(fp, oflag);
323}
324
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500325static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
326kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400327{
328 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500329 static int min_stateid = 0;
330 struct nfs4_stid *stid;
331 int new_id;
332
333 stid = kmem_cache_alloc(slab, GFP_KERNEL);
334 if (!stid)
335 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400336
Tejun Heoebd6c702013-03-13 14:59:37 -0700337 new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
338 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500339 goto out_free;
340 stid->sc_client = cl;
341 stid->sc_type = 0;
342 stid->sc_stateid.si_opaque.so_id = new_id;
343 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
344 /* Will be incremented before return to client: */
345 stid->sc_stateid.si_generation = 0;
346
J. Bruce Fields996e0932011-10-17 11:14:48 -0400347 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500348 * It shouldn't be a problem to reuse an opaque stateid value.
349 * I don't think it is for 4.1. But with 4.0 I worry that, for
350 * example, a stray write retransmission could be accepted by
351 * the server when it should have been rejected. Therefore,
352 * adopt a trick from the sctp code to attempt to maximize the
353 * amount of time until an id is reused, by ensuring they always
354 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400355 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500356
357 min_stateid = new_id+1;
358 if (min_stateid == INT_MAX)
359 min_stateid = 0;
360 return stid;
361out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800362 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500363 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400364}
365
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400366static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
367{
368 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400372alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct nfs4_delegation *dp;
375 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400378 /*
379 * Major work on the lease subsystem (for example, to support
380 * calbacks on stat) will be required before we can support
381 * write delegations properly.
382 */
383 if (type != NFS4_OPEN_DELEGATE_READ)
384 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700385 if (fp->fi_had_conflict)
386 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700387 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700388 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400389 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700390 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return dp;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500392 dp->dl_stid.sc_type = NFS4_DELEG_STID;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400393 /*
394 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400395 * meaning of seqid 0 isn't meaningful, really, but let's avoid
396 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400397 */
398 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700399 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700400 INIT_LIST_HEAD(&dp->dl_perfile);
401 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 INIT_LIST_HEAD(&dp->dl_recall_lru);
NeilBrown13cd2182005-06-23 22:03:10 -0700403 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 dp->dl_type = type;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500406 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dp->dl_time = 0;
408 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500409 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return dp;
411}
412
J. Bruce Fields68a33962013-03-21 11:21:50 -0400413static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500414{
415 struct idr *stateids = &s->sc_client->cl_stateids;
416
417 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420void
421nfs4_put_delegation(struct nfs4_delegation *dp)
422{
423 if (atomic_dec_and_test(&dp->dl_count)) {
J. Bruce Fields68a33962013-03-21 11:21:50 -0400424 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700425 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427}
428
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500429static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500431 if (atomic_dec_and_test(&fp->fi_delegees)) {
432 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
433 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400434 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500435 fp->fi_deleg_file = NULL;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400439static void unhash_stid(struct nfs4_stid *s)
440{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500441 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/* Called under the state lock. */
445static void
446unhash_delegation(struct nfs4_delegation *dp)
447{
NeilBrownea1da632005-06-23 22:04:17 -0700448 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500450 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 list_del_init(&dp->dl_recall_lru);
452 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500453 nfs4_put_deleg_lease(dp->dl_file);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400454 put_nfs4_file(dp->dl_file);
455 dp->dl_file = NULL;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400456}
457
458
459
460static void destroy_revoked_delegation(struct nfs4_delegation *dp)
461{
462 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400463 remove_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 nfs4_put_delegation(dp);
465}
466
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400467static void destroy_delegation(struct nfs4_delegation *dp)
468{
469 unhash_delegation(dp);
470 remove_stid(&dp->dl_stid);
471 nfs4_put_delegation(dp);
472}
473
474static void revoke_delegation(struct nfs4_delegation *dp)
475{
476 struct nfs4_client *clp = dp->dl_stid.sc_client;
477
478 if (clp->cl_minorversion == 0)
479 destroy_delegation(dp);
480 else {
481 unhash_delegation(dp);
482 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
483 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
484 }
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * SETCLIENTID state
489 */
490
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400491static unsigned int clientid_hashval(u32 id)
492{
493 return id & CLIENT_HASH_MASK;
494}
495
496static unsigned int clientstr_hashval(const char *name)
497{
498 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400502 * We store the NONE, READ, WRITE, and BOTH bits separately in the
503 * st_{access,deny}_bmap field of the stateid, in order to track not
504 * only what share bits are currently in force, but also what
505 * combinations of share bits previous opens have used. This allows us
506 * to enforce the recommendation of rfc 3530 14.2.19 that the server
507 * return an error if the client attempt to downgrade to a combination
508 * of share bits not explicable by closing some of its previous opens.
509 *
510 * XXX: This enforcement is actually incomplete, since we don't keep
511 * track of access/deny bit combinations; so, e.g., we allow:
512 *
513 * OPEN allow read, deny write
514 * OPEN allow both, deny none
515 * DOWNGRADE allow read, deny none
516 *
517 * which we should reject.
518 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400519static unsigned int
520bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400521 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400522 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400523
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400524 for (i = 1; i < 4; i++) {
525 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400526 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400527 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400528 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400529}
530
Jeff Layton3a328612012-05-11 09:45:12 -0400531static bool
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400532test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400533 unsigned int access, deny;
534
Jeff Layton5ae037e2012-05-11 09:45:11 -0400535 access = bmap_to_share_mode(stp->st_access_bmap);
536 deny = bmap_to_share_mode(stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400537 if ((access & open->op_share_deny) || (deny & open->op_share_access))
Jeff Layton3a328612012-05-11 09:45:12 -0400538 return false;
539 return true;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400540}
541
Jeff Layton82c5ff12012-05-11 09:45:13 -0400542/* set share access for a given stateid */
543static inline void
544set_access(u32 access, struct nfs4_ol_stateid *stp)
545{
546 __set_bit(access, &stp->st_access_bmap);
547}
548
549/* clear share access for a given stateid */
550static inline void
551clear_access(u32 access, struct nfs4_ol_stateid *stp)
552{
553 __clear_bit(access, &stp->st_access_bmap);
554}
555
556/* test whether a given stateid has access */
557static inline bool
558test_access(u32 access, struct nfs4_ol_stateid *stp)
559{
560 return test_bit(access, &stp->st_access_bmap);
561}
562
Jeff Laytonce0fc432012-05-11 09:45:14 -0400563/* set share deny for a given stateid */
564static inline void
565set_deny(u32 access, struct nfs4_ol_stateid *stp)
566{
567 __set_bit(access, &stp->st_deny_bmap);
568}
569
570/* clear share deny for a given stateid */
571static inline void
572clear_deny(u32 access, struct nfs4_ol_stateid *stp)
573{
574 __clear_bit(access, &stp->st_deny_bmap);
575}
576
577/* test whether a given stateid is denying specific access */
578static inline bool
579test_deny(u32 access, struct nfs4_ol_stateid *stp)
580{
581 return test_bit(access, &stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400582}
583
584static int nfs4_access_to_omode(u32 access)
585{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400586 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400587 case NFS4_SHARE_ACCESS_READ:
588 return O_RDONLY;
589 case NFS4_SHARE_ACCESS_WRITE:
590 return O_WRONLY;
591 case NFS4_SHARE_ACCESS_BOTH:
592 return O_RDWR;
593 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500594 WARN_ON_ONCE(1);
595 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400596}
597
Jeff Layton82c5ff12012-05-11 09:45:13 -0400598/* release all access and file references for a given stateid */
599static void
600release_all_access(struct nfs4_ol_stateid *stp)
601{
602 int i;
603
604 for (i = 1; i < 4; i++) {
605 if (test_access(i, stp))
606 nfs4_file_put_access(stp->st_file,
607 nfs4_access_to_omode(i));
608 clear_access(i, stp);
609 }
610}
611
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400612static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500613{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500614 list_del(&stp->st_perfile);
615 list_del(&stp->st_perstateowner);
616}
617
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400618static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500619{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400620 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400621 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400622 stp->st_file = NULL;
623}
624
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400625static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400626{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400627 remove_stid(&stp->st_stid);
628 kmem_cache_free(stateid_slab, stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500629}
630
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400631static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500632{
633 struct file *file;
634
635 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400636 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500637 file = find_any_file(stp->st_file);
638 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400639 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400640 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500641 free_generic_stateid(stp);
642}
643
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400644static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500645{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400646 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500647
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400648 list_del(&lo->lo_owner.so_strhash);
649 list_del(&lo->lo_perstateid);
J. Bruce Fields009673b2011-11-07 17:40:10 -0500650 list_del(&lo->lo_owner_ino_hash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400651 while (!list_empty(&lo->lo_owner.so_stateids)) {
652 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400653 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500654 release_lock_stateid(stp);
655 }
656}
657
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400658static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500659{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400660 unhash_lockowner(lo);
661 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500662}
663
664static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400665release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500666{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400667 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500668
669 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400670 lo = list_entry(open_stp->st_lockowners.next,
671 struct nfs4_lockowner, lo_perstateid);
672 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500673 }
674}
675
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400676static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500677{
678 unhash_generic_stateid(stp);
679 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400680 close_generic_stateid(stp);
681}
682
683static void release_open_stateid(struct nfs4_ol_stateid *stp)
684{
685 unhash_open_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400686 unhash_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500687 free_generic_stateid(stp);
688}
689
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400690static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500691{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400692 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500693
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400694 list_del(&oo->oo_owner.so_strhash);
695 list_del(&oo->oo_perclient);
696 while (!list_empty(&oo->oo_owner.so_stateids)) {
697 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400698 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500699 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500700 }
701}
702
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400703static void release_last_closed_stateid(struct nfs4_openowner *oo)
704{
705 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
706
707 if (s) {
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400708 unhash_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400709 free_generic_stateid(s);
710 oo->oo_last_closed_stid = NULL;
711 }
712}
713
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400714static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500715{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400716 unhash_openowner(oo);
717 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400718 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400719 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500720}
721
Marc Eshel5282fd72009-04-03 08:27:52 +0300722static inline int
723hash_sessionid(struct nfs4_sessionid *sessionid)
724{
725 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
726
727 return sid->sequence % SESSION_HASH_SIZE;
728}
729
Trond Myklebust8f199b82012-03-20 15:11:17 -0400730#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300731static inline void
732dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
733{
734 u32 *ptr = (u32 *)(&sessionid->data[0]);
735 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
736}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400737#else
738static inline void
739dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
740{
741}
742#endif
743
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400744/*
745 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
746 * won't be used for replay.
747 */
748void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
749{
750 struct nfs4_stateowner *so = cstate->replay_owner;
751
752 if (nfserr == nfserr_replay_me)
753 return;
754
755 if (!seqid_mutating_err(ntohl(nfserr))) {
756 cstate->replay_owner = NULL;
757 return;
758 }
759 if (!so)
760 return;
761 if (so->so_is_open_owner)
762 release_last_closed_stateid(openowner(so));
763 so->so_seqid++;
764 return;
765}
Marc Eshel5282fd72009-04-03 08:27:52 +0300766
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300767static void
768gen_sessionid(struct nfsd4_session *ses)
769{
770 struct nfs4_client *clp = ses->se_client;
771 struct nfsd4_sessionid *sid;
772
773 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
774 sid->clientid = clp->cl_clientid;
775 sid->sequence = current_sessionid++;
776 sid->reserved = 0;
777}
778
779/*
Andy Adamsona6496372009-08-28 08:45:01 -0400780 * The protocol defines ca_maxresponssize_cached to include the size of
781 * the rpc header, but all we need to cache is the data starting after
782 * the end of the initial SEQUENCE operation--the rest we regenerate
783 * each time. Therefore we can advertise a ca_maxresponssize_cached
784 * value that is the number of bytes in our cache plus a few additional
785 * bytes. In order to stay on the safe side, and not promise more than
786 * we can cache, those additional bytes must be the minimum possible: 24
787 * bytes of rpc header (xid through accept state, with AUTH_NULL
788 * verifier), 12 for the compound header (with zero-length tag), and 44
789 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300790 */
Andy Adamsona6496372009-08-28 08:45:01 -0400791#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
792
Andy Adamson557ce262009-08-28 08:45:04 -0400793static void
794free_session_slots(struct nfsd4_session *ses)
795{
796 int i;
797
798 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
799 kfree(ses->se_slots[i]);
800}
801
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400802/*
803 * We don't actually need to cache the rpc and session headers, so we
804 * can allocate a little less for each slot:
805 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400806static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400807{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400808 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400809
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400810 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
811 size = 0;
812 else
813 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
814 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400815}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300816
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400817/*
818 * XXX: If we run out of reserved DRC memory we could (up to a point)
819 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800820 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400821 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400822static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400823{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400824 u32 slotsize = slot_bytes(ca);
825 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400826 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300827
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400828 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800829 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
830 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400831 num = min_t(int, num, avail / slotsize);
832 nfsd_drc_mem_used += num * slotsize;
833 spin_unlock(&nfsd_drc_lock);
834
835 return num;
836}
837
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400838static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400839{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400840 int slotsize = slot_bytes(ca);
841
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400842 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400843 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400844 spin_unlock(&nfsd_drc_lock);
845}
846
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400847static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *attrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400848{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400849 int numslots = attrs->maxreqs;
850 int slotsize = slot_bytes(attrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400851 struct nfsd4_session *new;
852 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300853
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400854 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400855 + sizeof(struct nfsd4_session) > PAGE_SIZE);
856 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400857
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400858 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300859 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400860 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400861 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400862 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400863 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400864 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400865 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400866 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400867 return new;
868out_free:
869 while (i--)
870 kfree(new->se_slots[i]);
871 kfree(new);
872 return NULL;
873}
874
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400875static void free_conn(struct nfsd4_conn *c)
876{
877 svc_xprt_put(c->cn_xprt);
878 kfree(c);
879}
880
881static void nfsd4_conn_lost(struct svc_xpt_user *u)
882{
883 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
884 struct nfs4_client *clp = c->cn_session->se_client;
885
886 spin_lock(&clp->cl_lock);
887 if (!list_empty(&c->cn_persession)) {
888 list_del(&c->cn_persession);
889 free_conn(c);
890 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500891 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -0500892 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400893}
894
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400895static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400896{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400897 struct nfsd4_conn *conn;
898
899 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
900 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400901 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400902 svc_xprt_get(rqstp->rq_xprt);
903 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400904 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400905 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
906 return conn;
907}
908
J. Bruce Fields328ead22010-09-29 16:11:06 -0400909static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
910{
911 conn->cn_session = ses;
912 list_add(&conn->cn_persession, &ses->se_conns);
913}
914
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400915static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
916{
917 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400918
919 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400920 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400921 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400922}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400923
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400924static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400925{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400926 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400927 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400928}
929
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400930static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400931{
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400932 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400933
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400934 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400935 ret = nfsd4_register_conn(conn);
936 if (ret)
937 /* oops; xprt is already down: */
938 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -0400939 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -0400940 /* callback channel may be back up */
941 nfsd4_probe_callback(ses->se_client);
942 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400943}
944
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400945static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400946{
947 u32 dir = NFS4_CDFC4_FORE;
948
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400949 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400950 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400951 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400952}
953
954/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400955static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400956{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400957 struct nfs4_client *clp = s->se_client;
958 struct nfsd4_conn *c;
959
960 spin_lock(&clp->cl_lock);
961 while (!list_empty(&s->se_conns)) {
962 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
963 list_del_init(&c->cn_persession);
964 spin_unlock(&clp->cl_lock);
965
966 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
967 free_conn(c);
968
969 spin_lock(&clp->cl_lock);
970 }
971 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400972}
973
J. Bruce Fields1377b692012-09-11 21:42:40 -0400974static void __free_session(struct nfsd4_session *ses)
975{
J. Bruce Fields1377b692012-09-11 21:42:40 -0400976 free_session_slots(ses);
977 kfree(ses);
978}
979
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400980static void free_session(struct nfsd4_session *ses)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400981{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400982 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300983
984 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400985 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400986 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fields1377b692012-09-11 21:42:40 -0400987 __free_session(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400988}
989
Fengguang Wu135ae822012-11-10 07:20:25 -0500990static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400991{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400992 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +0300993 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400994
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300995 new->se_client = clp;
996 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300997
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400998 INIT_LIST_HEAD(&new->se_conns);
999
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001000 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001001 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001002 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001003 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001004 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001005 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001006 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001007 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001008 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001009 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001010 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001011 spin_unlock(&nn->client_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001012 memcpy(&new->se_fchannel, &cses->fore_channel,
1013 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001014 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001015 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001016 /*
1017 * This is a little silly; with sessions there's no real
1018 * use for the callback address. Use the peer address
1019 * as a reasonable default for now, but consider fixing
1020 * the rpc client not to require an address in the
1021 * future:
1022 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001023 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1024 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001025 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001026}
1027
Benny Halevy9089f1b2010-05-12 00:12:26 +03001028/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001029static struct nfsd4_session *
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001030find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001031{
1032 struct nfsd4_session *elem;
1033 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001034 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001035
1036 dump_sessionid(__func__, sessionid);
1037 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001038 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001039 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001040 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1041 NFS4_MAX_SESSIONID_LEN)) {
1042 return elem;
1043 }
1044 }
1045
1046 dprintk("%s: session not found\n", __func__);
1047 return NULL;
1048}
1049
Benny Halevy9089f1b2010-05-12 00:12:26 +03001050/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001051static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001052unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001053{
1054 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001055 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001056 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001057 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1061static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001062STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001064 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001066 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001067 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return 1;
1069}
1070
1071/*
1072 * XXX Should we use a slab cache ?
1073 * This type of memory management is somewhat inefficient, but we use it
1074 * anyway since SETCLIENTID is not a common operation.
1075 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001076static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 struct nfs4_client *clp;
1079
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001080 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1081 if (clp == NULL)
1082 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001083 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001084 if (clp->cl_name.data == NULL) {
1085 kfree(clp);
1086 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001088 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return clp;
1090}
1091
1092static inline void
1093free_client(struct nfs4_client *clp)
1094{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001095 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001096
1097 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001098 while (!list_empty(&clp->cl_sessions)) {
1099 struct nfsd4_session *ses;
1100 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1101 se_perclnt);
1102 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001103 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1104 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001105 }
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001106 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001108 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 kfree(clp);
1110}
1111
Benny Halevy84d38ac2010-05-12 00:13:16 +03001112/* must be called under the client_lock */
1113static inline void
1114unhash_client_locked(struct nfs4_client *clp)
1115{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001116 struct nfsd4_session *ses;
1117
Benny Halevy84d38ac2010-05-12 00:13:16 +03001118 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001119 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001120 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1121 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001122 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001126destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001128 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001131 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 INIT_LIST_HEAD(&reaplist);
1134 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001135 while (!list_empty(&clp->cl_delegations)) {
1136 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001137 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 list_move(&dp->dl_recall_lru, &reaplist);
1139 }
1140 spin_unlock(&recall_lock);
1141 while (!list_empty(&reaplist)) {
1142 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001143 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
NeilBrownea1da632005-06-23 22:04:17 -07001145 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001146 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1147 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001149 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001150 if (clp->cl_cb_conn.cb_xprt)
1151 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001152 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001153 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001154 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001155 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001156 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001157 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001158 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001159 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1160 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001161 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001164static void expire_client(struct nfs4_client *clp)
1165{
1166 nfsd4_client_record_remove(clp);
1167 destroy_client(clp);
1168}
1169
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001170static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1171{
1172 memcpy(target->cl_verifier.data, source->data,
1173 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001176static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1179 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1180}
1181
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001182static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001183{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001184 if (source->cr_principal) {
1185 target->cr_principal =
1186 kstrdup(source->cr_principal, GFP_KERNEL);
1187 if (target->cr_principal == NULL)
1188 return -ENOMEM;
1189 } else
1190 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001191 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 target->cr_uid = source->cr_uid;
1193 target->cr_gid = source->cr_gid;
1194 target->cr_group_info = source->cr_group_info;
1195 get_group_info(target->cr_group_info);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001199static long long
1200compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1201{
1202 long long res;
1203
1204 res = o1->len - o2->len;
1205 if (res)
1206 return res;
1207 return (long long)memcmp(o1->data, o2->data, o1->len);
1208}
1209
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001210static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001211{
NeilBrowna55370a2005-06-23 22:03:52 -07001212 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001216same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1217{
1218 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001222same_clid(clientid_t *cl1, clientid_t *cl2)
1223{
1224 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001227static bool groups_equal(struct group_info *g1, struct group_info *g2)
1228{
1229 int i;
1230
1231 if (g1->ngroups != g2->ngroups)
1232 return false;
1233 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001234 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001235 return false;
1236 return true;
1237}
1238
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001239/*
1240 * RFC 3530 language requires clid_inuse be returned when the
1241 * "principal" associated with a requests differs from that previously
1242 * used. We use uid, gid's, and gss principal string as our best
1243 * approximation. We also don't want to allow non-gss use of a client
1244 * established using gss: in theory cr_principal should catch that
1245 * change, but in practice cr_principal can be null even in the gss case
1246 * since gssd doesn't always pass down a principal string.
1247 */
1248static bool is_gss_cred(struct svc_cred *cr)
1249{
1250 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1251 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1252}
1253
1254
Vivek Trivedi5559b502012-07-24 21:18:20 +05301255static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001256same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1257{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001258 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001259 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1260 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001261 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1262 return false;
1263 if (cr1->cr_principal == cr2->cr_principal)
1264 return true;
1265 if (!cr1->cr_principal || !cr2->cr_principal)
1266 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301267 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001270static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001271{
1272 static u32 current_clientid = 1;
1273
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001274 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 clp->cl_clientid.cl_id = current_clientid++;
1276}
1277
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001278static void gen_confirm(struct nfs4_client *clp)
1279{
Chuck Leverab4684d2012-03-02 17:13:50 -05001280 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001281 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Chuck Leverab4684d2012-03-02 17:13:50 -05001283 verf[0] = (__be32)get_seconds();
1284 verf[1] = (__be32)i++;
1285 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001288static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001289{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001290 struct nfs4_stid *ret;
1291
1292 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1293 if (!ret || !ret->sc_type)
1294 return NULL;
1295 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001296}
1297
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001298static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001299{
1300 struct nfs4_stid *s;
1301
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001302 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001303 if (!s)
1304 return NULL;
1305 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001306 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001307 return NULL;
1308}
1309
Jeff Layton2216d442012-11-12 15:00:57 -05001310static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001311 struct svc_rqst *rqstp, nfs4_verifier *verf)
1312{
1313 struct nfs4_client *clp;
1314 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001315 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001316 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001317 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001318
1319 clp = alloc_client(name);
1320 if (clp == NULL)
1321 return NULL;
1322
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001323 INIT_LIST_HEAD(&clp->cl_sessions);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001324 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1325 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001326 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001327 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001328 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001329 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001330 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001331 idr_init(&clp->cl_stateids);
Benny Halevy46583e22010-05-12 00:13:29 +03001332 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001333 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001334 INIT_LIST_HEAD(&clp->cl_idhash);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001335 INIT_LIST_HEAD(&clp->cl_openowners);
1336 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001337 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001338 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001339 INIT_LIST_HEAD(&clp->cl_revoked);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001340 spin_lock_init(&clp->cl_lock);
J. Bruce Fields57725152012-11-05 15:10:26 -05001341 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001342 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001343 clear_bit(0, &clp->cl_cb_slot_busy);
1344 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1345 copy_verf(clp, verf);
1346 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001347 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001348 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001349 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001350 return clp;
1351}
1352
NeilBrownfd39ca92005-06-23 22:04:03 -07001353static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001354add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1355{
1356 struct rb_node **new = &(root->rb_node), *parent = NULL;
1357 struct nfs4_client *clp;
1358
1359 while (*new) {
1360 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1361 parent = *new;
1362
1363 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1364 new = &((*new)->rb_left);
1365 else
1366 new = &((*new)->rb_right);
1367 }
1368
1369 rb_link_node(&new_clp->cl_namenode, parent, new);
1370 rb_insert_color(&new_clp->cl_namenode, root);
1371}
1372
1373static struct nfs4_client *
1374find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1375{
1376 long long cmp;
1377 struct rb_node *node = root->rb_node;
1378 struct nfs4_client *clp;
1379
1380 while (node) {
1381 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1382 cmp = compare_blob(&clp->cl_name, name);
1383 if (cmp > 0)
1384 node = node->rb_left;
1385 else if (cmp < 0)
1386 node = node->rb_right;
1387 else
1388 return clp;
1389 }
1390 return NULL;
1391}
1392
1393static void
1394add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001397 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001399 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001400 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001402 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001403 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
NeilBrownfd39ca92005-06-23 22:04:03 -07001406static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407move_to_confirmed(struct nfs4_client *clp)
1408{
1409 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001410 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001413 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001414 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001415 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001416 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 renew_client(clp);
1418}
1419
1420static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001421find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct nfs4_client *clp;
1424 unsigned int idhashval = clientid_hashval(clid->cl_id);
1425
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001426 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001427 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001428 if ((bool)clp->cl_minorversion != sessions)
1429 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001430 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 return NULL;
1435}
1436
1437static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001438find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1439{
1440 struct list_head *tbl = nn->conf_id_hashtbl;
1441
1442 return find_client_in_id_table(tbl, clid, sessions);
1443}
1444
1445static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001446find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001448 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001450 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001453static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001454{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001455 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001456}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001457
NeilBrown28ce6052005-06-23 22:03:56 -07001458static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001459find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001460{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001461 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001462}
1463
1464static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001465find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001466{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001467 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001468}
1469
NeilBrownfd39ca92005-06-23 22:04:03 -07001470static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001471gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001473 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001474 struct sockaddr *sa = svc_addr(rqstp);
1475 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001476 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Jeff Layton7077ecb2009-08-14 12:57:58 -04001478 /* Currently, we only support tcp and tcp6 for the callback channel */
1479 if (se->se_callback_netid_len == 3 &&
1480 !memcmp(se->se_callback_netid_val, "tcp", 3))
1481 expected_family = AF_INET;
1482 else if (se->se_callback_netid_len == 4 &&
1483 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1484 expected_family = AF_INET6;
1485 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 goto out_err;
1487
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001488 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001489 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001490 (struct sockaddr *)&conn->cb_addr,
1491 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001492
J. Bruce Fields07263f12010-05-31 19:09:40 -04001493 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001495
J. Bruce Fields07263f12010-05-31 19:09:40 -04001496 if (conn->cb_addr.ss_family == AF_INET6)
1497 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001498
J. Bruce Fields07263f12010-05-31 19:09:40 -04001499 conn->cb_prog = se->se_callback_prog;
1500 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001501 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return;
1503out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001504 conn->cb_addr.ss_family = AF_UNSPEC;
1505 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001506 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 "will not receive delegations\n",
1508 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return;
1511}
1512
Andy Adamson074fe892009-04-03 08:28:15 +03001513/*
Andy Adamson557ce262009-08-28 08:45:04 -04001514 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001515 */
1516void
1517nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1518{
Andy Adamson557ce262009-08-28 08:45:04 -04001519 struct nfsd4_slot *slot = resp->cstate.slot;
1520 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001521
Andy Adamson557ce262009-08-28 08:45:04 -04001522 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001523
Andy Adamson557ce262009-08-28 08:45:04 -04001524 slot->sl_opcnt = resp->opcnt;
1525 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001526
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001527 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001528 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001529 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001530 return;
1531 }
Andy Adamson557ce262009-08-28 08:45:04 -04001532 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1533 base = (char *)resp->cstate.datap -
1534 (char *)resp->xbuf->head[0].iov_base;
1535 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1536 slot->sl_datalen))
1537 WARN("%s: sessions DRC could not cache compound\n", __func__);
1538 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001539}
1540
1541/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001542 * Encode the replay sequence operation from the slot values.
1543 * If cachethis is FALSE encode the uncached rep error on the next
1544 * operation which sets resp->p and increments resp->opcnt for
1545 * nfs4svc_encode_compoundres.
1546 *
Andy Adamson074fe892009-04-03 08:28:15 +03001547 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001548static __be32
1549nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1550 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001551{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001552 struct nfsd4_op *op;
1553 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001554
Andy Adamsonabfabf82009-07-23 19:02:18 -04001555 /* Encode the replayed sequence operation */
1556 op = &args->ops[resp->opcnt - 1];
1557 nfsd4_encode_operation(resp, op);
1558
1559 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001560 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001561 op = &args->ops[resp->opcnt++];
1562 op->status = nfserr_retry_uncached_rep;
1563 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001564 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001565 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001566}
1567
1568/*
Andy Adamson557ce262009-08-28 08:45:04 -04001569 * The sequence operation is not cached because we can use the slot and
1570 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001571 */
1572__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001573nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1574 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001575{
Andy Adamson557ce262009-08-28 08:45:04 -04001576 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001577 __be32 status;
1578
Andy Adamson557ce262009-08-28 08:45:04 -04001579 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001580
Andy Adamsonabfabf82009-07-23 19:02:18 -04001581 /* Either returns 0 or nfserr_retry_uncached */
1582 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1583 if (status == nfserr_retry_uncached_rep)
1584 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001585
Andy Adamson557ce262009-08-28 08:45:04 -04001586 /* The sequence operation has been encoded, cstate->datap set. */
1587 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001588
Andy Adamson557ce262009-08-28 08:45:04 -04001589 resp->opcnt = slot->sl_opcnt;
1590 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1591 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001592
1593 return status;
1594}
1595
Andy Adamson0733d212009-04-03 08:28:01 +03001596/*
1597 * Set the exchange_id flags returned by the server.
1598 */
1599static void
1600nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1601{
1602 /* pNFS is not supported */
1603 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1604
1605 /* Referrals are supported, Migration is not. */
1606 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1607
1608 /* set the wire flags to return to client. */
1609 clid->flags = new->cl_exchange_flags;
1610}
1611
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001612static bool client_has_state(struct nfs4_client *clp)
1613{
1614 /*
1615 * Note clp->cl_openowners check isn't quite right: there's no
1616 * need to count owners without stateid's.
1617 *
1618 * Also note we should probably be using this in 4.0 case too.
1619 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001620 return !list_empty(&clp->cl_openowners)
1621 || !list_empty(&clp->cl_delegations)
1622 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001623}
1624
Al Virob37ad282006-10-19 23:28:59 -07001625__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001626nfsd4_exchange_id(struct svc_rqst *rqstp,
1627 struct nfsd4_compound_state *cstate,
1628 struct nfsd4_exchange_id *exid)
1629{
Andy Adamson0733d212009-04-03 08:28:01 +03001630 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001631 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001632 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001633 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001634 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001635 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001636 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001637
Jeff Layton363168b2009-08-14 12:57:56 -04001638 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001639 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001640 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001641 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001642 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001643
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001644 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001645 return nfserr_inval;
1646
1647 /* Currently only support SP4_NONE */
1648 switch (exid->spa_how) {
1649 case SP4_NONE:
1650 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001651 default: /* checked by xdr code */
1652 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001653 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04001654 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03001655 case SP4_MACH_CRED:
1656 return nfserr_serverfault; /* no excuse :-/ */
1657 }
1658
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001659 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001660 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001661 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001662 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001663 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1664 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1665
J. Bruce Fields136e6582012-05-12 20:37:23 -04001666 if (update) {
1667 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001668 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001669 goto out;
1670 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001671 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001672 status = nfserr_perm;
1673 goto out;
1674 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001675 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001676 status = nfserr_not_same;
1677 goto out;
1678 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001679 /* case 6 */
1680 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1681 new = conf;
1682 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001683 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001684 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001685 if (client_has_state(conf)) {
1686 status = nfserr_clid_inuse;
1687 goto out;
1688 }
Andy Adamson0733d212009-04-03 08:28:01 +03001689 expire_client(conf);
1690 goto out_new;
1691 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001692 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001693 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001694 new = conf;
1695 goto out_copy;
1696 }
1697 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001698 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001699 }
1700
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001701 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001702 status = nfserr_noent;
1703 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001704 }
1705
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001706 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001707 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001708 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001709
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001710 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001711out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001712 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001713 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001714 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001715 goto out;
1716 }
J. Bruce Fieldsc116a0a2012-09-11 14:53:09 -04001717 new->cl_minorversion = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001718
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001719 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001720 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001721out_copy:
1722 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1723 exid->clientid.cl_id = new->cl_clientid.cl_id;
1724
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001725 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001726 nfsd4_set_ex_flags(new, exid);
1727
1728 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001729 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001730 status = nfs_ok;
1731
1732out:
1733 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001734 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001735}
1736
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001737static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001738check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001739{
Andy Adamson88e588d2009-07-23 19:02:15 -04001740 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1741 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001742
1743 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001744 if (slot_inuse) {
1745 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001746 return nfserr_jukebox;
1747 else
1748 return nfserr_seq_misordered;
1749 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001750 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001751 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001752 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001753 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001754 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001755 return nfserr_seq_misordered;
1756}
1757
Andy Adamson49557cc2009-07-23 19:02:16 -04001758/*
1759 * Cache the create session result into the create session single DRC
1760 * slot cache by saving the xdr structure. sl_seqid has been set.
1761 * Do this for solo or embedded create session operations.
1762 */
1763static void
1764nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001765 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001766{
1767 slot->sl_status = nfserr;
1768 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1769}
1770
1771static __be32
1772nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1773 struct nfsd4_clid_slot *slot)
1774{
1775 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1776 return slot->sl_status;
1777}
1778
Mi Jinlong1b74c252011-07-14 14:50:17 +08001779#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1780 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1781 1 + /* MIN tag is length with zero, only length */ \
1782 3 + /* version, opcount, opcode */ \
1783 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1784 /* seqid, slotID, slotID, cache */ \
1785 4 ) * sizeof(__be32))
1786
1787#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1788 2 + /* verifier: AUTH_NULL, length 0 */\
1789 1 + /* status */ \
1790 1 + /* MIN tag is length with zero, only length */ \
1791 3 + /* opcount, opcode, opstatus*/ \
1792 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1793 /* seqid, slotID, slotID, slotID, status */ \
1794 5 ) * sizeof(__be32))
1795
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001796static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001797{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001798 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
1799
J. Bruce Fields373cd402013-04-08 15:42:12 -04001800 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
1801 return nfserr_toosmall;
1802 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
1803 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001804 ca->headerpadsz = 0;
1805 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
1806 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
1807 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
1808 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
1809 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
1810 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
1811 /*
1812 * Note decreasing slot size below client's request may make it
1813 * difficult for client to function correctly, whereas
1814 * decreasing the number of slots will (just?) affect
1815 * performance. When short on memory we therefore prefer to
1816 * decrease number of slots instead of their size. Clients that
1817 * request larger slots than they need will get poor results:
1818 */
1819 ca->maxreqs = nfsd4_get_drc_mem(ca);
1820 if (!ca->maxreqs)
1821 return nfserr_jukebox;
1822
J. Bruce Fields373cd402013-04-08 15:42:12 -04001823 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08001824}
1825
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001826static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
1827{
1828 ca->headerpadsz = 0;
1829
1830 /*
1831 * These RPC_MAX_HEADER macros are overkill, especially since we
1832 * don't even do gss on the backchannel yet. But this is still
1833 * less than 1k. Tighten up this estimate in the unlikely event
1834 * it turns out to be a problem for some client:
1835 */
1836 if (ca->maxreq_sz < NFS4_enc_cb_recall_sz + RPC_MAX_HEADER_WITH_AUTH)
1837 return nfserr_toosmall;
1838 if (ca->maxresp_sz < NFS4_dec_cb_recall_sz + RPC_MAX_REPHEADER_WITH_AUTH)
1839 return nfserr_toosmall;
1840 ca->maxresp_cached = 0;
1841 if (ca->maxops < 2)
1842 return nfserr_toosmall;
1843
1844 return nfs_ok;
1845}
1846
Andy Adamson069b6ad2009-04-03 08:27:58 +03001847__be32
1848nfsd4_create_session(struct svc_rqst *rqstp,
1849 struct nfsd4_compound_state *cstate,
1850 struct nfsd4_create_session *cr_ses)
1851{
Jeff Layton363168b2009-08-14 12:57:56 -04001852 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001853 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001854 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001855 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001856 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001857 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001859
Mi Jinlonga62573d2011-03-23 17:57:07 +08001860 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1861 return nfserr_inval;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001862 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04001863 if (status)
1864 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001865 status = check_backchannel_attrs(&cr_ses->back_channel);
1866 if (status)
1867 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001868 status = nfserr_jukebox;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001869 new = alloc_session(&cr_ses->fore_channel);
1870 if (!new)
1871 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001872 conn = alloc_conn_from_crses(rqstp, cr_ses);
1873 if (!conn)
1874 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08001875
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001876 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001877 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001878 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04001879 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001880
1881 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001882 cs_slot = &conf->cl_cs_slot;
1883 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001884 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001885 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001886 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001887 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001888 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001889 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001890 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001891 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001892 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001893 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001894 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001895 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001896 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001897 }
Andy Adamson49557cc2009-07-23 19:02:16 -04001898 cs_slot = &unconf->cl_cs_slot;
1899 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001900 if (status) {
1901 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001902 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001903 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001904 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001905 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001906 if (old) {
1907 status = mark_client_expired(old);
1908 if (status)
1909 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001910 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001911 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001912 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001913 conf = unconf;
1914 } else {
1915 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001916 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001917 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001918 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001919 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001920 * We do not support RDMA or persistent sessions
1921 */
1922 cr_ses->flags &= ~SESSION4_PERSIST;
1923 cr_ses->flags &= ~SESSION4_RDMA;
1924
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001925 init_session(rqstp, new, conf, cr_ses);
1926 nfsd4_init_conn(rqstp, conn, new);
1927
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001928 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001929 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001930 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001931 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001932
Andy Adamson49557cc2009-07-23 19:02:16 -04001933 /* cache solo and embedded create sessions under the state lock */
1934 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001935 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001936 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001937out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08001938 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001939 free_conn(conn);
1940out_free_session:
1941 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001942out_release_drc_mem:
1943 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04001944 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001945}
1946
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001947static __be32 nfsd4_map_bcts_dir(u32 *dir)
1948{
1949 switch (*dir) {
1950 case NFS4_CDFC4_FORE:
1951 case NFS4_CDFC4_BACK:
1952 return nfs_ok;
1953 case NFS4_CDFC4_FORE_OR_BOTH:
1954 case NFS4_CDFC4_BACK_OR_BOTH:
1955 *dir = NFS4_CDFC4_BOTH;
1956 return nfs_ok;
1957 };
1958 return nfserr_inval;
1959}
1960
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001961__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1962{
1963 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001964 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001965
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001966 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001967 session->se_cb_prog = bc->bc_cb_program;
1968 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001969 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001970
1971 nfsd4_probe_callback(session->se_client);
1972
1973 return nfs_ok;
1974}
1975
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001976__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1977 struct nfsd4_compound_state *cstate,
1978 struct nfsd4_bind_conn_to_session *bcts)
1979{
1980 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001981 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001982 struct nfsd4_session *session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001983 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001984
1985 if (!nfsd4_last_compound_op(rqstp))
1986 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001987 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001988 spin_lock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001989 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001990 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001991 status = nfserr_badsession;
1992 if (!session)
1993 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001994 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001995 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001996 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001997 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04001998 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001999 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002000 goto out;
2001 nfsd4_init_conn(rqstp, conn, session);
2002 status = nfs_ok;
2003out:
2004 nfs4_unlock_state();
2005 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002006}
2007
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002008static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2009{
2010 if (!session)
2011 return 0;
2012 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2013}
2014
Andy Adamson069b6ad2009-04-03 08:27:58 +03002015__be32
2016nfsd4_destroy_session(struct svc_rqst *r,
2017 struct nfsd4_compound_state *cstate,
2018 struct nfsd4_destroy_session *sessionid)
2019{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002020 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002021 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002022 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002023
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002024 nfs4_lock_state();
2025 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002026 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002027 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002028 goto out;
J. Bruce Fields57716352010-04-21 12:27:19 -04002029 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002030 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002031 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002032 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002033 status = nfserr_badsession;
2034 if (!ses)
2035 goto out_client_lock;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002036 status = mark_session_dead_locked(ses);
2037 if (status)
2038 goto out_client_lock;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002039 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002040 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002041
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002042 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002043
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002044 spin_lock(&nn->client_lock);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002045 free_session(ses);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002046 status = nfs_ok;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002047out_client_lock:
2048 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002049out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002050 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002051 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002052}
2053
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002054static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002055{
2056 struct nfsd4_conn *c;
2057
2058 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002059 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002060 return c;
2061 }
2062 }
2063 return NULL;
2064}
2065
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002066static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002067{
2068 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002069 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002070 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002071
2072 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002073 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002074 if (c) {
2075 spin_unlock(&clp->cl_lock);
2076 free_conn(new);
2077 return;
2078 }
2079 __nfsd4_hash_conn(new, ses);
2080 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002081 ret = nfsd4_register_conn(new);
2082 if (ret)
2083 /* oops; xprt is already down: */
2084 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002085 return;
2086}
2087
Mi Jinlong868b89c2011-04-27 09:09:58 +08002088static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2089{
2090 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2091
2092 return args->opcnt > session->se_fchannel.maxops;
2093}
2094
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002095static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2096 struct nfsd4_session *session)
2097{
2098 struct xdr_buf *xb = &rqstp->rq_arg;
2099
2100 return xb->len > session->se_fchannel.maxreq_sz;
2101}
2102
Andy Adamson069b6ad2009-04-03 08:27:58 +03002103__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002104nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002105 struct nfsd4_compound_state *cstate,
2106 struct nfsd4_sequence *seq)
2107{
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002108 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002109 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002110 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002111 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002112 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002113 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002114 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002115
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03002116 if (resp->opcnt != 1)
2117 return nfserr_sequence_pos;
2118
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002119 /*
2120 * Will be either used or freed by nfsd4_sequence_check_conn
2121 * below.
2122 */
2123 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2124 if (!conn)
2125 return nfserr_jukebox;
2126
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002127 spin_lock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002128 status = nfserr_badsession;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002129 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
Benny Halevyb85d4c02009-04-03 08:28:08 +03002130 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002131 goto out_no_session;
2132 clp = session->se_client;
2133 status = get_client_locked(clp);
2134 if (status)
2135 goto out_no_session;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002136 status = nfsd4_get_session_locked(session);
2137 if (status)
2138 goto out_put_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002139
Mi Jinlong868b89c2011-04-27 09:09:58 +08002140 status = nfserr_too_many_ops;
2141 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002142 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002143
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002144 status = nfserr_req_too_big;
2145 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002146 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002147
Benny Halevyb85d4c02009-04-03 08:28:08 +03002148 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002149 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002150 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002151
Andy Adamson557ce262009-08-28 08:45:04 -04002152 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002153 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2154
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002155 /* We do not negotiate the number of slots yet, so set the
2156 * maxslots to the session maxreqs which is used to encode
2157 * sr_highest_slotid and the sr_target_slot id to maxslots */
2158 seq->maxslots = session->se_fchannel.maxreqs;
2159
J. Bruce Fields73e79482012-02-13 16:39:00 -05002160 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2161 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002162 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002163 status = nfserr_seq_misordered;
2164 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002165 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002166 cstate->slot = slot;
2167 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002168 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002169 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002170 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002171 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002172 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002173 }
2174 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002175 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002176
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002177 nfsd4_sequence_check_conn(conn, session);
2178 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002179
Benny Halevyb85d4c02009-04-03 08:28:08 +03002180 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002181 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002182 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002183 if (seq->cachethis)
2184 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002185 else
2186 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002187
2188 cstate->slot = slot;
2189 cstate->session = session;
2190
Benny Halevyb85d4c02009-04-03 08:28:08 +03002191out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002192 switch (clp->cl_cb_state) {
2193 case NFSD4_CB_DOWN:
2194 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2195 break;
2196 case NFSD4_CB_FAULT:
2197 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2198 break;
2199 default:
2200 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002201 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002202 if (!list_empty(&clp->cl_revoked))
2203 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002204out_no_session:
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002205 kfree(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002206 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002207 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002208out_put_session:
2209 nfsd4_put_session(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002210out_put_client:
2211 put_client_renew_locked(clp);
2212 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002213}
2214
Mi Jinlong345c2842011-10-20 17:51:39 +08002215__be32
2216nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2217{
2218 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002219 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002220 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002221
2222 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002223 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002224 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002225 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002226
2227 if (conf) {
2228 clp = conf;
2229
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002230 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002231 status = nfserr_clientid_busy;
2232 goto out;
2233 }
2234 } else if (unconf)
2235 clp = unconf;
2236 else {
2237 status = nfserr_stale_clientid;
2238 goto out;
2239 }
2240
2241 expire_client(clp);
2242out:
2243 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002244 return status;
2245}
2246
Andy Adamson069b6ad2009-04-03 08:27:58 +03002247__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002248nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2249{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002250 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002251
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002252 if (rc->rca_one_fs) {
2253 if (!cstate->current_fh.fh_dentry)
2254 return nfserr_nofilehandle;
2255 /*
2256 * We don't take advantage of the rca_one_fs case.
2257 * That's OK, it's optional, we can safely ignore it.
2258 */
2259 return nfs_ok;
2260 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002261
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002262 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002263 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002264 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2265 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002266 goto out;
2267
2268 status = nfserr_stale_clientid;
2269 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002270 /*
2271 * The following error isn't really legal.
2272 * But we only get here if the client just explicitly
2273 * destroyed the client. Surely it no longer cares what
2274 * error it gets back on an operation for the dead
2275 * client.
2276 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002277 goto out;
2278
2279 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002280 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002281out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002282 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002283 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002284}
2285
2286__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002287nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2288 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002290 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002292 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002293 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002294 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2295
J. Bruce Fields63db4632012-05-18 21:54:19 -04002296 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002298 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002299 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002300 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002302 if (clp_used_exchangeid(conf))
2303 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002304 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002305 char addr_str[INET6_ADDRSTRLEN];
2306 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2307 sizeof(addr_str));
2308 dprintk("NFSD: setclientid: string in use by client "
2309 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 goto out;
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002313 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002314 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002317 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002318 if (new == NULL)
2319 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002320 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002321 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002323 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002324 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002325 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002326 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002327 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2329 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2330 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2331 status = nfs_ok;
2332out:
2333 nfs4_unlock_state();
2334 return status;
2335}
2336
2337
Al Virob37ad282006-10-19 23:28:59 -07002338__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002339nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2340 struct nfsd4_compound_state *cstate,
2341 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
NeilBrown21ab45a2005-06-23 22:04:14 -07002343 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2345 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002346 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002347 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002349 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002352
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002353 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002354 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002355 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002356 * We try hard to give out unique clientid's, so if we get an
2357 * attempt to confirm the same clientid with a different cred,
2358 * there's a bug somewhere. Let's charitably assume it's our
2359 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002360 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002361 status = nfserr_serverfault;
2362 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2363 goto out;
2364 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2365 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002366 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002367 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2368 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002370 else /* case 4: client hasn't noticed we rebooted yet? */
2371 status = nfserr_stale_clientid;
2372 goto out;
2373 }
2374 status = nfs_ok;
2375 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002376 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2377 nfsd4_probe_callback(conf);
2378 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002379 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002380 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002381 if (conf) {
2382 status = mark_client_expired(conf);
2383 if (status)
2384 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002385 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002386 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002387 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002388 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 nfs4_unlock_state();
2392 return status;
2393}
2394
J. Bruce Fields32513b42011-10-13 16:00:16 -04002395static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002397 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2398}
2399
2400/* OPEN Share state helper functions */
2401static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 unsigned int hashval = file_hashval(ino);
2404
J. Bruce Fields32513b42011-10-13 16:00:16 -04002405 atomic_set(&fp->fi_ref, 1);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002406 INIT_LIST_HEAD(&fp->fi_stateids);
2407 INIT_LIST_HEAD(&fp->fi_delegations);
2408 fp->fi_inode = igrab(ino);
2409 fp->fi_had_conflict = false;
2410 fp->fi_lease = NULL;
2411 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2412 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2413 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002414 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002415 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
2418static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002419nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002420{
NeilBrowne60d4392005-06-23 22:03:01 -07002421 if (*slab == NULL)
2422 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002423 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002424 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002425}
2426
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002427void
NeilBrowne60d4392005-06-23 22:03:01 -07002428nfsd4_free_slabs(void)
2429{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002430 nfsd4_free_slab(&openowner_slab);
2431 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002432 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002433 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002434 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Bryan Schumaker72083392011-11-01 15:24:59 -04002437int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438nfsd4_init_slabs(void)
2439{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002440 openowner_slab = kmem_cache_create("nfsd4_openowners",
2441 sizeof(struct nfs4_openowner), 0, 0, NULL);
2442 if (openowner_slab == NULL)
2443 goto out_nomem;
2444 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002445 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002446 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002447 goto out_nomem;
2448 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002449 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002450 if (file_slab == NULL)
2451 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002452 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002453 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002454 if (stateid_slab == NULL)
2455 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002456 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002457 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002458 if (deleg_slab == NULL)
2459 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002461out_nomem:
2462 nfsd4_free_slabs();
2463 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2464 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002467void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002469 kfree(oo->oo_owner.so_owner.data);
2470 kmem_cache_free(openowner_slab, oo);
2471}
2472
2473void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2474{
2475 kfree(lo->lo_owner.so_owner.data);
2476 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002479static void init_nfs4_replay(struct nfs4_replay *rp)
2480{
2481 rp->rp_status = nfserr_serverfault;
2482 rp->rp_buflen = 0;
2483 rp->rp_buf = rp->rp_ibuf;
2484}
2485
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002486static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
2488 struct nfs4_stateowner *sop;
2489
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002490 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002491 if (!sop)
2492 return NULL;
2493
2494 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2495 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002496 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002497 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002499 sop->so_owner.len = owner->len;
2500
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002501 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002502 sop->so_client = clp;
2503 init_nfs4_replay(&sop->so_replay);
2504 return sop;
2505}
2506
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002507static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002508{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002509 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2510
2511 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002512 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002515static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002517 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002519 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2520 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002522 oo->oo_owner.so_is_open_owner = 1;
2523 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002524 oo->oo_flags = NFS4_OO_NEW;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002525 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002526 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002527 INIT_LIST_HEAD(&oo->oo_close_lru);
2528 hash_openowner(oo, clp, strhashval);
2529 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
J. Bruce Fields996e0932011-10-17 11:14:48 -04002532static 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 -04002533 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002535 stp->st_stid.sc_type = NFS4_OPEN_STID;
NeilBrownea1da632005-06-23 22:04:17 -07002536 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002537 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002538 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002539 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002540 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 stp->st_access_bmap = 0;
2543 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002544 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002545 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002546 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
2549static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002550move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002552 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2553
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002554 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002556 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002557 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002561same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2562 clientid_t *clid)
2563{
2564 return (sop->so_owner.len == owner->len) &&
2565 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2566 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002569static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002570find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2571 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002573 struct nfs4_stateowner *so;
2574 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002575 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002577 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002578 if (!so->so_is_open_owner)
2579 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002580 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2581 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002582 clp = oo->oo_owner.so_client;
2583 if ((bool)clp->cl_minorversion != sessions)
2584 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002585 renew_client(oo->oo_owner.so_client);
2586 return oo;
2587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
2589 return NULL;
2590}
2591
2592/* search file_hashtbl[] for file */
2593static struct nfs4_file *
2594find_file(struct inode *ino)
2595{
2596 unsigned int hashval = file_hashval(ino);
2597 struct nfs4_file *fp;
2598
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002599 spin_lock(&recall_lock);
Jeff Layton89876f82013-04-02 09:01:59 -04002600 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002601 if (fp->fi_inode == ino) {
2602 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002603 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002607 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return NULL;
2609}
2610
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002611/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 * Called to check deny when READ with all zero stateid or
2613 * WRITE with all zero or all one stateid
2614 */
Al Virob37ad282006-10-19 23:28:59 -07002615static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2617{
2618 struct inode *ino = current_fh->fh_dentry->d_inode;
2619 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002620 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002621 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002624 if (!fp)
2625 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002626 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002628 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002629 if (test_deny(deny_type, stp) ||
2630 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
NeilBrown13cd2182005-06-23 22:03:10 -07002633 ret = nfs_ok;
2634out:
2635 put_nfs4_file(fp);
2636 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002639static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002641 struct nfs4_client *clp = dp->dl_stid.sc_client;
2642 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 /* We're assuming the state code never drops its reference
2645 * without first removing the lease. Since we're in this lease
2646 * callback (and since the lease code is serialized by the kernel
2647 * lock) we know the server hasn't removed the lease yet, we know
2648 * it's safe to take a reference: */
2649 atomic_inc(&dp->dl_count);
2650
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002651 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Arnd Bergmann460781b2010-11-17 16:26:56 +01002653 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 dp->dl_time = get_seconds();
2655
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002656 nfsd4_cb_recall(dp);
2657}
2658
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002659/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002660static void nfsd_break_deleg_cb(struct file_lock *fl)
2661{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002662 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2663 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002664
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002665 if (!fp) {
2666 WARN(1, "(%p)->fl_owner NULL\n", fl);
2667 return;
2668 }
2669 if (fp->fi_had_conflict) {
2670 WARN(1, "duplicate break on %p\n", fp);
2671 return;
2672 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002673 /*
2674 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002675 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002676 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002677 */
2678 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002680 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002681 fp->fi_had_conflict = true;
2682 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2683 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002684 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687static
2688int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2689{
2690 if (arg & F_UNLCK)
2691 return lease_modify(onlist, arg);
2692 else
2693 return -EAGAIN;
2694}
2695
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002696static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002697 .lm_break = nfsd_break_deleg_cb,
2698 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699};
2700
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002701static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2702{
2703 if (nfsd4_has_session(cstate))
2704 return nfs_ok;
2705 if (seqid == so->so_seqid - 1)
2706 return nfserr_replay_me;
2707 if (seqid == so->so_seqid)
2708 return nfs_ok;
2709 return nfserr_bad_seqid;
2710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Al Virob37ad282006-10-19 23:28:59 -07002712__be32
Andy Adamson66689582009-04-03 08:28:45 +03002713nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03002714 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 clientid_t *clientid = &open->op_clientid;
2717 struct nfs4_client *clp = NULL;
2718 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002719 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002720 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002722 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002724 /*
2725 * In case we need it later, after we've already created the
2726 * file and don't want to risk a further failure:
2727 */
2728 open->op_file = nfsd4_alloc_file();
2729 if (open->op_file == NULL)
2730 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002732 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002733 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002734 open->op_openowner = oo;
2735 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002736 clp = find_confirmed_client(clientid, cstate->minorversion,
2737 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002739 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002740 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002742 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002743 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002744 clp = oo->oo_owner.so_client;
2745 release_openowner(oo);
2746 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002747 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002748 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002749 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2750 if (status)
2751 return status;
2752 clp = oo->oo_owner.so_client;
2753 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002754new_owner:
2755 oo = alloc_init_open_stateowner(strhashval, clp, open);
2756 if (oo == NULL)
2757 return nfserr_jukebox;
2758 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002759alloc_stateid:
2760 open->op_stp = nfs4_alloc_stateid(clp);
2761 if (!open->op_stp)
2762 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002763 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Al Virob37ad282006-10-19 23:28:59 -07002766static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002767nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2768{
2769 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2770 return nfserr_openmode;
2771 else
2772 return nfs_ok;
2773}
2774
Daniel Mackc47d8322011-05-16 16:38:14 +02002775static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002776{
J. Bruce Fields24a01112010-05-18 20:01:35 -04002777 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2778}
2779
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002780static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002781{
2782 struct nfs4_stid *ret;
2783
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002784 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002785 if (!ret)
2786 return NULL;
2787 return delegstateid(ret);
2788}
2789
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002790static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2791{
2792 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2793 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2794}
2795
Al Virob37ad282006-10-19 23:28:59 -07002796static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04002797nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07002798 struct nfs4_delegation **dp)
2799{
2800 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002801 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002802
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002803 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002804 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002805 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002806 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002807 status = nfs4_check_delegmode(*dp, flags);
2808 if (status)
2809 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002810out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002811 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07002812 return nfs_ok;
2813 if (status)
2814 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002815 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002816 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002817}
2818
Al Virob37ad282006-10-19 23:28:59 -07002819static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002820nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002822 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002823 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
NeilBrown8beefa22005-06-23 22:03:08 -07002825 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /* ignore lock owners */
2827 if (local->st_stateowner->so_is_open_owner == 0)
2828 continue;
2829 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002830 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 *stpp = local;
2832 /* check for conflicting share reservations */
2833 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002834 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002836 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002839static inline int nfs4_access_to_access(u32 nfs4_access)
2840{
2841 int flags = 0;
2842
2843 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2844 flags |= NFSD_MAY_READ;
2845 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2846 flags |= NFSD_MAY_WRITE;
2847 return flags;
2848}
2849
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002850static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2851 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002852{
2853 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002854 int oflag = nfs4_access_to_omode(open->op_share_access);
2855 int access = nfs4_access_to_access(open->op_share_access);
2856
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002857 if (!fp->fi_fds[oflag]) {
2858 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2859 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002860 if (status)
2861 return status;
2862 }
2863 nfs4_file_get_access(fp, oflag);
2864
2865 return nfs_ok;
2866}
2867
Al Virob37ad282006-10-19 23:28:59 -07002868static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2870 struct nfsd4_open *open)
2871{
2872 struct iattr iattr = {
2873 .ia_valid = ATTR_SIZE,
2874 .ia_size = 0,
2875 };
2876 if (!open->op_truncate)
2877 return 0;
2878 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002879 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2881}
2882
Al Virob37ad282006-10-19 23:28:59 -07002883static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002884nfs4_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 -07002885{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002886 u32 op_share_access = open->op_share_access;
J. Bruce Fields7d947842010-08-20 18:09:31 -04002887 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002888 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Jeff Layton82c5ff12012-05-11 09:45:13 -04002890 new_access = !test_access(op_share_access, stp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002891 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002892 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002893 if (status)
2894 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 status = nfsd4_truncate(rqstp, cur_fh, open);
2897 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002898 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002899 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002900 nfs4_file_put_access(fp, oflag);
2901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return status;
2903 }
2904 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04002905 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002906 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 return nfs_ok;
2909}
2910
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05002913nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002915 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002918/* Should we give out recallable state?: */
2919static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2920{
2921 if (clp->cl_cb_state == NFSD4_CB_UP)
2922 return true;
2923 /*
2924 * In the sessions case, since we don't have to establish a
2925 * separate connection for callbacks, we assume it's OK
2926 * until we hear otherwise:
2927 */
2928 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2929}
2930
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002931static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2932{
2933 struct file_lock *fl;
2934
2935 fl = locks_alloc_lock();
2936 if (!fl)
2937 return NULL;
2938 locks_init_lock(fl);
2939 fl->fl_lmops = &nfsd_lease_mng_ops;
2940 fl->fl_flags = FL_LEASE;
2941 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2942 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002943 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002944 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002945 return fl;
2946}
2947
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002948static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2949{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002950 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002951 struct file_lock *fl;
2952 int status;
2953
2954 fl = nfs4_alloc_init_lease(dp, flag);
2955 if (!fl)
2956 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002957 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002958 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002959 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002960 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002961 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002962 locks_free_lock(fl);
2963 return -ENOMEM;
2964 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002965 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04002966 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002967 atomic_set(&fp->fi_delegees, 1);
2968 list_add(&dp->dl_perfile, &fp->fi_delegations);
2969 return 0;
2970}
2971
2972static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2973{
2974 struct nfs4_file *fp = dp->dl_file;
2975
2976 if (!fp->fi_lease)
2977 return nfs4_setlease(dp, flag);
2978 spin_lock(&recall_lock);
2979 if (fp->fi_had_conflict) {
2980 spin_unlock(&recall_lock);
2981 return -EAGAIN;
2982 }
2983 atomic_inc(&fp->fi_delegees);
2984 list_add(&dp->dl_perfile, &fp->fi_delegations);
2985 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002986 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002987 return 0;
2988}
2989
Benny Halevy4aa89132012-02-21 14:16:44 -08002990static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2991{
2992 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2993 if (status == -EAGAIN)
2994 open->op_why_no_deleg = WND4_CONTENTION;
2995 else {
2996 open->op_why_no_deleg = WND4_RESOURCE;
2997 switch (open->op_deleg_want) {
2998 case NFS4_SHARE_WANT_READ_DELEG:
2999 case NFS4_SHARE_WANT_WRITE_DELEG:
3000 case NFS4_SHARE_WANT_ANY_DELEG:
3001 break;
3002 case NFS4_SHARE_WANT_CANCEL:
3003 open->op_why_no_deleg = WND4_CANCELLED;
3004 break;
3005 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003006 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003007 }
3008 }
3009}
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011/*
3012 * Attempt to hand out a delegation.
3013 */
3014static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003015nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3016 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
3018 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003019 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003020 int cb_up;
Benny Halevyd24433c2012-02-16 20:57:17 +02003021 int status = 0, flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003023 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07003025 open->op_recall = 0;
3026 switch (open->op_claim_type) {
3027 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003028 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003029 open->op_recall = 1;
3030 flag = open->op_delegate_type;
3031 if (flag == NFS4_OPEN_DELEGATE_NONE)
3032 goto out;
3033 break;
3034 case NFS4_OPEN_CLAIM_NULL:
3035 /* Let's not give out any delegations till everyone's
3036 * had the chance to reclaim theirs.... */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003037 if (locks_in_grace(net))
NeilBrown7b190fe2005-06-23 22:03:23 -07003038 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003039 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07003040 goto out;
3041 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3042 flag = NFS4_OPEN_DELEGATE_WRITE;
3043 else
3044 flag = NFS4_OPEN_DELEGATE_READ;
3045 break;
3046 default:
3047 goto out;
3048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003050 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003051 if (dp == NULL)
3052 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003053 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003054 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003055 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003057 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003059 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003060 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061out:
3062 open->op_delegate_type = flag;
Benny Halevyd24433c2012-02-16 20:57:17 +02003063 if (flag == NFS4_OPEN_DELEGATE_NONE) {
3064 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3065 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
3066 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3067
Benny Halevy4aa89132012-02-21 14:16:44 -08003068 /* 4.1 client asking for a delegation? */
3069 if (open->op_deleg_want)
3070 nfsd4_open_deleg_none_ext(open, status);
Benny Halevyd24433c2012-02-16 20:57:17 +02003071 }
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003072 return;
3073out_free:
J. Bruce Fields24ffb932012-12-12 15:24:12 -05003074 unhash_stid(&dp->dl_stid);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003075 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003076out_no_deleg:
3077 flag = NFS4_OPEN_DELEGATE_NONE;
3078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Benny Halevye27f49c2012-02-21 14:16:54 -08003081static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3082 struct nfs4_delegation *dp)
3083{
3084 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3085 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3086 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3087 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3088 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3089 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3090 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3091 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3092 }
3093 /* Otherwise the client must be confused wanting a delegation
3094 * it already has, therefore we don't return
3095 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3096 */
3097}
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099/*
3100 * called with nfs4_lock_state() held.
3101 */
Al Virob37ad282006-10-19 23:28:59 -07003102__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3104{
Andy Adamson66689582009-04-03 08:28:45 +03003105 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003106 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct nfs4_file *fp = NULL;
3108 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003109 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003110 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003111 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /*
3114 * Lookup file; if found, lookup stateid and check open request,
3115 * and check for delegations in the process of being recalled.
3116 * If not found, create the nfs4_file struct
3117 */
3118 fp = find_file(ino);
3119 if (fp) {
3120 if ((status = nfs4_check_open(fp, open, &stp)))
3121 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003122 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003123 if (status)
3124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07003126 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003127 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003128 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003129 status = nfserr_jukebox;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003130 fp = open->op_file;
3131 open->op_file = NULL;
3132 nfsd4_init_file(fp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
3134
3135 /*
3136 * OPEN the file, or upgrade an existing OPEN.
3137 * If truncate fails, the OPEN fails.
3138 */
3139 if (stp) {
3140 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003141 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (status)
3143 goto out;
3144 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003145 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003146 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 goto out;
J. Bruce Fields4af82502012-06-07 17:30:45 -04003148 status = nfsd4_truncate(rqstp, current_fh, open);
3149 if (status)
3150 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003151 stp = open->op_stp;
3152 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003153 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003155 update_stateid(&stp->st_stid.sc_stateid);
3156 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Benny Halevyd24433c2012-02-16 20:57:17 +02003158 if (nfsd4_has_session(&resp->cstate)) {
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003159 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03003160
Benny Halevyd24433c2012-02-16 20:57:17 +02003161 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3162 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3163 open->op_why_no_deleg = WND4_NOT_WANTED;
3164 goto nodeleg;
3165 }
3166 }
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /*
3169 * Attempt to hand out a delegation. No error return, because the
3170 * OPEN succeeds even if we fail.
3171 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003172 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003173nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 status = nfs_ok;
3175
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003176 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003177 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003179 /* 4.1 client trying to upgrade/downgrade delegation? */
3180 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003181 open->op_deleg_want)
3182 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003183
NeilBrown13cd2182005-06-23 22:03:10 -07003184 if (fp)
3185 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003186 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003187 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /*
3189 * To finish the open response, we just need to set the rflags.
3190 */
3191 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003192 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003193 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3195
3196 return status;
3197}
3198
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003199void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3200{
3201 if (open->op_openowner) {
3202 struct nfs4_openowner *oo = open->op_openowner;
3203
3204 if (!list_empty(&oo->oo_owner.so_stateids))
3205 list_del_init(&oo->oo_close_lru);
3206 if (oo->oo_flags & NFS4_OO_NEW) {
3207 if (status) {
3208 release_openowner(oo);
3209 open->op_openowner = NULL;
3210 } else
3211 oo->oo_flags &= ~NFS4_OO_NEW;
3212 }
3213 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003214 if (open->op_file)
3215 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003216 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003217 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003218}
3219
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003220static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3221{
3222 struct nfs4_client *found;
3223
3224 if (STALE_CLIENTID(clid, nn))
3225 return nfserr_stale_clientid;
3226 found = find_confirmed_client(clid, session, nn);
3227 if (clp)
3228 *clp = found;
3229 return found ? nfs_ok : nfserr_expired;
3230}
3231
Al Virob37ad282006-10-19 23:28:59 -07003232__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003233nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3234 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
3236 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003237 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003238 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
3240 nfs4_lock_state();
3241 dprintk("process_renew(%08x/%08x): starting\n",
3242 clid->cl_boot, clid->cl_id);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003243 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3244 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003247 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003248 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 goto out;
3250 status = nfs_ok;
3251out:
3252 nfs4_unlock_state();
3253 return status;
3254}
3255
NeilBrowna76b4312005-06-23 22:04:01 -07003256static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003257nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003258{
Jeff Layton33dcc482012-04-10 11:08:48 -04003259 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003260 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003261 return;
3262
NeilBrowna76b4312005-06-23 22:04:01 -07003263 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003264 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003265 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003266 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003267 /*
3268 * Now that every NFSv4 client has had the chance to recover and
3269 * to see the (possibly new, possibly shorter) lease time, we
3270 * can safely set the next grace time to the current lease time:
3271 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003272 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003273}
3274
NeilBrownfd39ca92005-06-23 22:04:03 -07003275static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003276nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003279 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 struct nfs4_delegation *dp;
3281 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003282 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3283 time_t t, clientid_val = nn->nfsd4_lease;
3284 time_t u, test_val = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 nfs4_lock_state();
3287
3288 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003289 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003290 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003291 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003292 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 clp = list_entry(pos, struct nfs4_client, cl_lru);
3294 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3295 t = clp->cl_time - cutoff;
3296 if (clientid_val > t)
3297 clientid_val = t;
3298 break;
3299 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003300 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003301 dprintk("NFSD: client in use (clientid %08x)\n",
3302 clp->cl_clientid.cl_id);
3303 continue;
3304 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003305 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003306 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003307 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003308 list_for_each_safe(pos, next, &reaplist) {
3309 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 dprintk("NFSD: purging unused client (clientid %08x)\n",
3311 clp->cl_clientid.cl_id);
3312 expire_client(clp);
3313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003315 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003317 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3318 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3320 u = dp->dl_time - cutoff;
3321 if (test_val > u)
3322 test_val = u;
3323 break;
3324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 list_move(&dp->dl_recall_lru, &reaplist);
3326 }
3327 spin_unlock(&recall_lock);
3328 list_for_each_safe(pos, next, &reaplist) {
3329 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003330 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003332 test_val = nn->nfsd4_lease;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003333 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003334 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3335 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3336 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 if (test_val > u)
3338 test_val = u;
3339 break;
3340 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003341 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3344 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3345 nfs4_unlock_state();
3346 return clientid_val;
3347}
3348
Harvey Harrisona254b242008-02-20 12:49:00 -08003349static struct workqueue_struct *laundry_wq;
3350static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003351
3352static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003353laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
3355 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003356 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3357 work);
3358 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3359 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003361 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003363 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
3365
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003366static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003367{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003368 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3369 return nfserr_bad_stateid;
3370 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003374access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003376 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3377 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3378 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
3381static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003382access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003384 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3385 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
3388static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003389__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
Al Virob37ad282006-10-19 23:28:59 -07003391 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
J. Bruce Fields02921912010-07-29 15:16:59 -04003393 /* For lock stateid's, we test the parent open, not the lock: */
3394 if (stp->st_openstp)
3395 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003396 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003398 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 goto out;
3400 status = nfs_ok;
3401out:
3402 return status;
3403}
3404
Al Virob37ad282006-10-19 23:28:59 -07003405static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003406check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003408 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003410 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003411 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 * conflicting state; so we must wait out the grace period. */
3413 return nfserr_grace;
3414 } else if (flags & WR_STATE)
3415 return nfs4_share_conflict(current_fh,
3416 NFS4_SHARE_DENY_WRITE);
3417 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3418 return nfs4_share_conflict(current_fh,
3419 NFS4_SHARE_DENY_READ);
3420}
3421
3422/*
3423 * Allow READ/WRITE during grace period on recovered state only for files
3424 * that are not able to provide mandatory locking.
3425 */
3426static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003427grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003429 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
J. Bruce Fields81b82962011-08-23 11:03:29 -04003432/* Returns true iff a is later than b: */
3433static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3434{
3435 return (s32)a->si_generation - (s32)b->si_generation > 0;
3436}
3437
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003438static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003439{
Andy Adamson66689582009-04-03 08:28:45 +03003440 /*
3441 * When sessions are used the stateid generation number is ignored
3442 * when it is zero.
3443 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003444 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003445 return nfs_ok;
3446
3447 if (in->si_generation == ref->si_generation)
3448 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003449
J. Bruce Fields0836f582008-01-26 19:08:12 -05003450 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003451 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003452 return nfserr_bad_stateid;
3453 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003454 * However, we could see a stateid from the past, even from a
3455 * non-buggy client. For example, if the client sends a lock
3456 * while some IO is outstanding, the lock may bump si_generation
3457 * while the IO is still in flight. The client could avoid that
3458 * situation by waiting for responses on all the IO requests,
3459 * but better performance may result in retrying IO that
3460 * receives an old_stateid error if requests are rarely
3461 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003462 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003463 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003464}
3465
Chuck Lever7df302f2012-05-29 13:56:37 -04003466static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003467{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003468 struct nfs4_stid *s;
3469 struct nfs4_ol_stateid *ols;
3470 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003471
Chuck Lever7df302f2012-05-29 13:56:37 -04003472 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3473 return nfserr_bad_stateid;
3474 /* Client debugging aid. */
3475 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3476 char addr_str[INET6_ADDRSTRLEN];
3477 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3478 sizeof(addr_str));
3479 pr_warn_ratelimited("NFSD: client %s testing state ID "
3480 "with incorrect client ID\n", addr_str);
3481 return nfserr_bad_stateid;
3482 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003483 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003484 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003485 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003486 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003487 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003488 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04003489 switch (s->sc_type) {
3490 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003491 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003492 case NFS4_REVOKED_DELEG_STID:
3493 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04003494 case NFS4_OPEN_STID:
3495 case NFS4_LOCK_STID:
3496 ols = openlockstateid(s);
3497 if (ols->st_stateowner->so_is_open_owner
3498 && !(openowner(ols->st_stateowner)->oo_flags
3499 & NFS4_OO_CONFIRMED))
3500 return nfserr_bad_stateid;
3501 return nfs_ok;
3502 default:
3503 printk("unknown stateid type %x\n", s->sc_type);
3504 case NFS4_CLOSED_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003505 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04003506 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003507}
3508
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003509static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3510 struct nfs4_stid **s, bool sessions,
3511 struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003512{
3513 struct nfs4_client *cl;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003514 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003515
3516 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3517 return nfserr_bad_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003518 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3519 nn, &cl);
3520 if (status == nfserr_stale_clientid)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003521 return nfserr_stale_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003522 if (status)
3523 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003524 *s = find_stateid_by_type(cl, stateid, typemask);
3525 if (!*s)
3526 return nfserr_bad_stateid;
3527 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003528}
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530/*
3531* Checks for stateid operations
3532*/
Al Virob37ad282006-10-19 23:28:59 -07003533__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003534nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003535 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003537 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003538 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003540 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003542 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07003543 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 if (filpp)
3546 *filpp = NULL;
3547
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003548 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 return nfserr_grace;
3550
3551 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003552 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003554 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3555 &s, cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003556 if (status)
3557 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003558 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3559 if (status)
3560 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003561 switch (s->sc_type) {
3562 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003563 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003564 status = nfs4_check_delegmode(dp, flags);
3565 if (status)
3566 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003567 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003568 *filpp = dp->dl_file->fi_deleg_file;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003569 if (!*filpp) {
3570 WARN_ON_ONCE(1);
3571 status = nfserr_serverfault;
3572 goto out;
3573 }
Dan Carpenter43b01782010-10-27 23:19:04 +02003574 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003575 break;
3576 case NFS4_OPEN_STID:
3577 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003578 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003579 status = nfs4_check_fh(current_fh, stp);
3580 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003582 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003583 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003585 status = nfs4_check_openmode(stp, flags);
3586 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003588 if (filpp) {
3589 if (flags & RD_STATE)
3590 *filpp = find_readable_file(stp->st_file);
3591 else
3592 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003593 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003594 break;
3595 default:
3596 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
3598 status = nfs_ok;
3599out:
3600 return status;
3601}
3602
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003603static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003604nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003605{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003606 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003607 return nfserr_locks_held;
3608 release_lock_stateid(stp);
3609 return nfs_ok;
3610}
3611
3612/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003613 * Test if the stateid is valid
3614 */
3615__be32
3616nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3617 struct nfsd4_test_stateid *test_stateid)
3618{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003619 struct nfsd4_test_stateid_id *stateid;
3620 struct nfs4_client *cl = cstate->session->se_client;
3621
3622 nfs4_lock_state();
3623 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003624 stateid->ts_id_status =
3625 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003626 nfs4_unlock_state();
3627
Bryan Schumaker17456802011-07-13 10:50:48 -04003628 return nfs_ok;
3629}
3630
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003631__be32
3632nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3633 struct nfsd4_free_stateid *free_stateid)
3634{
3635 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003636 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003637 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003638 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003639 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003640
3641 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003642 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003643 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003644 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003645 switch (s->sc_type) {
3646 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003647 ret = nfserr_locks_held;
3648 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003649 case NFS4_OPEN_STID:
3650 case NFS4_LOCK_STID:
3651 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3652 if (ret)
3653 goto out;
3654 if (s->sc_type == NFS4_LOCK_STID)
3655 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3656 else
3657 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003658 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003659 case NFS4_REVOKED_DELEG_STID:
3660 dp = delegstateid(s);
3661 destroy_revoked_delegation(dp);
3662 ret = nfs_ok;
3663 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003664 default:
3665 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003666 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003667out:
3668 nfs4_unlock_state();
3669 return ret;
3670}
3671
NeilBrown4c4cd222005-07-07 17:59:27 -07003672static inline int
3673setlkflg (int type)
3674{
3675 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3676 RD_STATE : WR_STATE;
3677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003679static __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 -04003680{
3681 struct svc_fh *current_fh = &cstate->current_fh;
3682 struct nfs4_stateowner *sop = stp->st_stateowner;
3683 __be32 status;
3684
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003685 status = nfsd4_check_seqid(cstate, sop, seqid);
3686 if (status)
3687 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003688 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
3689 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003690 /*
3691 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003692 * nfserr_replay_me from the previous step, and
3693 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003694 */
3695 return nfserr_bad_stateid;
3696 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3697 if (status)
3698 return status;
3699 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003700}
3701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702/*
3703 * Checks for sequence id mutating operations.
3704 */
Al Virob37ad282006-10-19 23:28:59 -07003705static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003706nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003707 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003708 struct nfs4_ol_stateid **stpp,
3709 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003711 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003712 struct nfs4_stid *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003714 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3715 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 *stpp = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003718 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3719 cstate->minorversion, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003720 if (status)
3721 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003722 *stpp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003723 if (!nfsd4_has_session(cstate))
3724 cstate->replay_owner = (*stpp)->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003726 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3727}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003728
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003729static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3730 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003731{
3732 __be32 status;
3733 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003735 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003736 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003737 if (status)
3738 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003739 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003740 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003741 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003742 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
Al Virob37ad282006-10-19 23:28:59 -07003745__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003746nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003747 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
Al Virob37ad282006-10-19 23:28:59 -07003749 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003750 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003751 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003752 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
3754 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003755 (int)cstate->current_fh.fh_dentry->d_name.len,
3756 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003758 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003759 if (status)
3760 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 nfs4_lock_state();
3763
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003764 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003765 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003766 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003767 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003768 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003769 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003770 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003771 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003772 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003773 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003774 update_stateid(&stp->st_stid.sc_stateid);
3775 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003776 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003777 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003778
Jeff Layton2a4317c2012-03-21 16:42:43 -04003779 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003780 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003782 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003783 if (!cstate->replay_owner)
3784 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 return status;
3786}
3787
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003788static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003790 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003791 return;
3792 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04003793 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003794}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003795
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003796static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3797{
3798 switch (to_access) {
3799 case NFS4_SHARE_ACCESS_READ:
3800 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3801 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3802 break;
3803 case NFS4_SHARE_ACCESS_WRITE:
3804 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3805 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3806 break;
3807 case NFS4_SHARE_ACCESS_BOTH:
3808 break;
3809 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003810 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 }
3812}
3813
3814static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04003815reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
3817 int i;
3818 for (i = 0; i < 4; i++) {
3819 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04003820 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 }
3822}
3823
Al Virob37ad282006-10-19 23:28:59 -07003824__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003825nfsd4_open_downgrade(struct svc_rqst *rqstp,
3826 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003827 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
Al Virob37ad282006-10-19 23:28:59 -07003829 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003830 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003831 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003834 (int)cstate->current_fh.fh_dentry->d_name.len,
3835 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04003837 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02003838 if (od->od_deleg_want)
3839 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3840 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003843 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003844 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003845 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003848 if (!test_access(od->od_share_access, stp)) {
3849 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 stp->st_access_bmap, od->od_share_access);
3851 goto out;
3852 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04003853 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3855 stp->st_deny_bmap, od->od_share_deny);
3856 goto out;
3857 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003858 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Jeff Laytonce0fc432012-05-11 09:45:14 -04003860 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003862 update_stateid(&stp->st_stid.sc_stateid);
3863 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 status = nfs_ok;
3865out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003866 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003867 if (!cstate->replay_owner)
3868 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return status;
3870}
3871
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003872static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3873{
3874 unhash_open_stateid(s);
3875 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003876}
3877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878/*
3879 * nfs4_unlock_state() called after encode
3880 */
Al Virob37ad282006-10-19 23:28:59 -07003881__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003882nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003883 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
Al Virob37ad282006-10-19 23:28:59 -07003885 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003886 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003887 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003888 struct net *net = SVC_NET(rqstp);
3889 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
3891 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003892 (int)cstate->current_fh.fh_dentry->d_name.len,
3893 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
3895 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003896 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3897 &close->cl_stateid,
3898 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003899 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003900 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003901 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003903 oo = openowner(stp->st_stateowner);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003904 update_stateid(&stp->st_stid.sc_stateid);
3905 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003907 nfsd4_close_open_stateid(stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003908
3909 if (cstate->minorversion) {
3910 unhash_stid(&stp->st_stid);
3911 free_generic_stateid(stp);
3912 } else
3913 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003914
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003915 if (list_empty(&oo->oo_owner.so_stateids)) {
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003916 if (cstate->minorversion)
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003917 release_openowner(oo);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003918 else {
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003919 /*
3920 * In the 4.0 case we need to keep the owners around a
3921 * little while to handle CLOSE replay.
3922 */
fanchaoting53584f62013-04-11 21:24:13 +08003923 move_to_close_lru(oo, SVC_NET(rqstp));
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003924 }
3925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003927 if (!cstate->replay_owner)
3928 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return status;
3930}
3931
Al Virob37ad282006-10-19 23:28:59 -07003932__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003933nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3934 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003936 struct nfs4_delegation *dp;
3937 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003938 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07003939 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003940 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003942 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003943 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 nfs4_lock_state();
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003946 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3947 cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003948 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003949 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003950 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003951 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003952 if (status)
3953 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003954
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003955 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003957 nfs4_unlock_state();
3958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return status;
3960}
3961
3962
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
J. Bruce Fields009673b2011-11-07 17:40:10 -05003965#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Benny Halevy87df4de2008-12-15 19:42:03 +02003967static inline u64
3968end_offset(u64 start, u64 len)
3969{
3970 u64 end;
3971
3972 end = start + len;
3973 return end >= start ? end: NFS4_MAX_UINT64;
3974}
3975
3976/* last octet in a range */
3977static inline u64
3978last_byte_offset(u64 start, u64 len)
3979{
3980 u64 end;
3981
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003982 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02003983 end = start + len;
3984 return end > start ? end - 1: NFS4_MAX_UINT64;
3985}
3986
J. Bruce Fields009673b2011-11-07 17:40:10 -05003987static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
3989 return (file_hashval(inode) + cl_id
3990 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05003991 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994/*
3995 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3996 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3997 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3998 * locking, this prevents us from being completely protocol-compliant. The
3999 * real solution to this problem is to start using unsigned file offsets in
4000 * the VFS, but this is a very deep change!
4001 */
4002static inline void
4003nfs4_transform_lock_offset(struct file_lock *lock)
4004{
4005 if (lock->fl_start < 0)
4006 lock->fl_start = OFFSET_MAX;
4007 if (lock->fl_end < 0)
4008 lock->fl_end = OFFSET_MAX;
4009}
4010
NeilBrownd5b90262006-04-10 22:55:22 -07004011/* Hack!: For now, we're defining this just so we can use a pointer to it
4012 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004013static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004014};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
4016static inline void
4017nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4018{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004019 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
NeilBrownd5b90262006-04-10 22:55:22 -07004021 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004022 lo = (struct nfs4_lockowner *) fl->fl_owner;
4023 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4024 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004025 if (!deny->ld_owner.data)
4026 /* We just don't care that much */
4027 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004028 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4029 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004030 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004031nevermind:
4032 deny->ld_owner.len = 0;
4033 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004034 deny->ld_clientid.cl_boot = 0;
4035 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 }
4037 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004038 deny->ld_length = NFS4_MAX_UINT64;
4039 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4041 deny->ld_type = NFS4_READ_LT;
4042 if (fl->fl_type != F_RDLCK)
4043 deny->ld_type = NFS4_WRITE_LT;
4044}
4045
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004046static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
4047{
4048 struct nfs4_ol_stateid *lst;
4049
4050 if (!same_owner_str(&lo->lo_owner, owner, clid))
4051 return false;
4052 lst = list_first_entry(&lo->lo_owner.so_stateids,
4053 struct nfs4_ol_stateid, st_perstateowner);
4054 return lst->st_file->fi_inode == inode;
4055}
4056
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004057static struct nfs4_lockowner *
4058find_lockowner_str(struct inode *inode, clientid_t *clid,
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004059 struct xdr_netobj *owner, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004061 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004062 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004064 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004065 if (same_lockowner_ino(lo, inode, clid, owner))
4066 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 }
4068 return NULL;
4069}
4070
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004071static 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 -04004072{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004073 struct inode *inode = open_stp->st_file->fi_inode;
4074 unsigned int inohash = lockowner_ino_hashval(inode,
4075 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004076 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields009673b2011-11-07 17:40:10 -05004077
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004078 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004079 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004080 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004081}
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083/*
4084 * Alloc a lock owner structure.
4085 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004086 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004088 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 */
4090
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004091static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004092alloc_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 -04004093 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004095 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4096 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004098 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4099 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004100 /* It is the openowner seqid that will be incremented in encode in the
4101 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004102 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4103 hash_lockowner(lo, strhashval, clp, open_stp);
4104 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105}
4106
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004107static struct nfs4_ol_stateid *
4108alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004110 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004111 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
J. Bruce Fields996e0932011-10-17 11:14:48 -04004113 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004114 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004115 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004116 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown8beefa22005-06-23 22:03:08 -07004117 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004118 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4119 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004120 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004122 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004124 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return stp;
4126}
4127
NeilBrownfd39ca92005-06-23 22:04:03 -07004128static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129check_lock_length(u64 offset, u64 length)
4130{
Benny Halevy87df4de2008-12-15 19:42:03 +02004131 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 LOFF_OVERFLOW(offset, length)));
4133}
4134
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004135static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004136{
4137 struct nfs4_file *fp = lock_stp->st_file;
4138 int oflag = nfs4_access_to_omode(access);
4139
Jeff Layton82c5ff12012-05-11 09:45:13 -04004140 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004141 return;
4142 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004143 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004144}
4145
J. Bruce Fields2355c592012-04-25 16:56:22 -04004146static __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 -04004147{
4148 struct nfs4_file *fi = ost->st_file;
4149 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4150 struct nfs4_client *cl = oo->oo_owner.so_client;
4151 struct nfs4_lockowner *lo;
4152 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004153 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004154
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004155 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4156 &lock->v.new.owner, nn);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004157 if (lo) {
4158 if (!cstate->minorversion)
4159 return nfserr_bad_seqid;
4160 /* XXX: a lockowner always has exactly one stateid: */
4161 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4162 struct nfs4_ol_stateid, st_perstateowner);
4163 return nfs_ok;
4164 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004165 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004166 &lock->v.new.owner);
4167 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4168 if (lo == NULL)
4169 return nfserr_jukebox;
4170 *lst = alloc_init_lock_stateid(lo, fi, ost);
4171 if (*lst == NULL) {
4172 release_lockowner(lo);
4173 return nfserr_jukebox;
4174 }
4175 *new = true;
4176 return nfs_ok;
4177}
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179/*
4180 * LOCK operation
4181 */
Al Virob37ad282006-10-19 23:28:59 -07004182__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004183nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004184 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004186 struct nfs4_openowner *open_sop = NULL;
4187 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004188 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004189 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004190 struct file_lock *file_lock = NULL;
4191 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004192 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004193 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004194 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004195 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004196 struct net *net = SVC_NET(rqstp);
4197 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4200 (long long) lock->lk_offset,
4201 (long long) lock->lk_length);
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (check_lock_length(lock->lk_offset, lock->lk_length))
4204 return nfserr_inval;
4205
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004206 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004207 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004208 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4209 return status;
4210 }
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 nfs4_lock_state();
4213
4214 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004215 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004216
4217 if (nfsd4_has_session(cstate))
4218 /* See rfc 5661 18.10.3: given clientid is ignored: */
4219 memcpy(&lock->v.new.clientid,
4220 &cstate->session->se_client->cl_clientid,
4221 sizeof(clientid_t));
4222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004224 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004228 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 lock->lk_new_open_seqid,
4230 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004231 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004232 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004234 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004235 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004236 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004237 &lock->v.new.clientid))
4238 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004239 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4240 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004241 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004242 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004243 lock->lk_old_lock_seqid,
4244 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004245 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004246 if (status)
4247 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004248 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004250 lkflg = setlkflg(lock->lk_type);
4251 status = nfs4_check_openmode(lock_stp, lkflg);
4252 if (status)
4253 goto out;
4254
NeilBrown0dd395dc2005-07-07 17:59:15 -07004255 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004256 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395dc2005-07-07 17:59:15 -07004257 goto out;
4258 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004259 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395dc2005-07-07 17:59:15 -07004260 goto out;
4261
Jeff Layton21179d82012-08-21 08:03:32 -04004262 file_lock = locks_alloc_lock();
4263 if (!file_lock) {
4264 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4265 status = nfserr_jukebox;
4266 goto out;
4267 }
4268
4269 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 switch (lock->lk_type) {
4271 case NFS4_READ_LT:
4272 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004273 filp = find_readable_file(lock_stp->st_file);
4274 if (filp)
4275 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004276 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 case NFS4_WRITE_LT:
4279 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004280 filp = find_writeable_file(lock_stp->st_file);
4281 if (filp)
4282 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004283 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 default:
4286 status = nfserr_inval;
4287 goto out;
4288 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004289 if (!filp) {
4290 status = nfserr_openmode;
4291 goto out;
4292 }
Jeff Layton21179d82012-08-21 08:03:32 -04004293 file_lock->fl_owner = (fl_owner_t)lock_sop;
4294 file_lock->fl_pid = current->tgid;
4295 file_lock->fl_file = filp;
4296 file_lock->fl_flags = FL_POSIX;
4297 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4298 file_lock->fl_start = lock->lk_offset;
4299 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4300 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Jeff Layton21179d82012-08-21 08:03:32 -04004302 conflock = locks_alloc_lock();
4303 if (!conflock) {
4304 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4305 status = nfserr_jukebox;
4306 goto out;
4307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Jeff Layton21179d82012-08-21 08:03:32 -04004309 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004310 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004312 update_stateid(&lock_stp->st_stid.sc_stateid);
4313 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004315 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004316 break;
4317 case (EAGAIN): /* conflock holds conflicting lock */
4318 status = nfserr_denied;
4319 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004320 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 case (EDEADLK):
4323 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004324 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004325 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004326 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004327 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004331 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004332 release_lockowner(lock_sop);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004333 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004334 if (!cstate->replay_owner)
4335 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004336 if (file_lock)
4337 locks_free_lock(file_lock);
4338 if (conflock)
4339 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return status;
4341}
4342
4343/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004344 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4345 * so we do a temporary open here just to get an open file to pass to
4346 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4347 * inode operation.)
4348 */
Al Viro04da6e92012-04-13 00:00:04 -04004349static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004350{
4351 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004352 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4353 if (!err) {
4354 err = nfserrno(vfs_test_lock(file, lock));
4355 nfsd_close(file);
4356 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004357 return err;
4358}
4359
4360/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 * LOCKT operation
4362 */
Al Virob37ad282006-10-19 23:28:59 -07004363__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004364nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4365 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 struct inode *inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004368 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004369 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004370 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004371 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004373 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return nfserr_grace;
4375
4376 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4377 return nfserr_inval;
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 nfs4_lock_state();
4380
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004381 if (!nfsd4_has_session(cstate)) {
4382 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4383 if (status)
4384 goto out;
4385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004387 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004390 inode = cstate->current_fh.fh_dentry->d_inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004391 file_lock = locks_alloc_lock();
4392 if (!file_lock) {
4393 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4394 status = nfserr_jukebox;
4395 goto out;
4396 }
4397 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 switch (lockt->lt_type) {
4399 case NFS4_READ_LT:
4400 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004401 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 break;
4403 case NFS4_WRITE_LT:
4404 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004405 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 break;
4407 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004408 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 status = nfserr_inval;
4410 goto out;
4411 }
4412
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004413 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004414 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004415 file_lock->fl_owner = (fl_owner_t)lo;
4416 file_lock->fl_pid = current->tgid;
4417 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Jeff Layton21179d82012-08-21 08:03:32 -04004419 file_lock->fl_start = lockt->lt_offset;
4420 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Jeff Layton21179d82012-08-21 08:03:32 -04004422 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Jeff Layton21179d82012-08-21 08:03:32 -04004424 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004425 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004426 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004427
Jeff Layton21179d82012-08-21 08:03:32 -04004428 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004430 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
4432out:
4433 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004434 if (file_lock)
4435 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return status;
4437}
4438
Al Virob37ad282006-10-19 23:28:59 -07004439__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004440nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004441 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004443 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004444 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004446 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004447 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004448 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004449 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4452 (long long) locku->lu_offset,
4453 (long long) locku->lu_length);
4454
4455 if (check_lock_length(locku->lu_offset, locku->lu_length))
4456 return nfserr_inval;
4457
4458 nfs4_lock_state();
4459
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004460 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004461 &locku->lu_stateid, NFS4_LOCK_STID,
4462 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004463 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004465 filp = find_any_file(stp->st_file);
4466 if (!filp) {
4467 status = nfserr_lock_range;
4468 goto out;
4469 }
Jeff Layton21179d82012-08-21 08:03:32 -04004470 file_lock = locks_alloc_lock();
4471 if (!file_lock) {
4472 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4473 status = nfserr_jukebox;
4474 goto out;
4475 }
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004476 lo = lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04004477 locks_init_lock(file_lock);
4478 file_lock->fl_type = F_UNLCK;
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004479 file_lock->fl_owner = (fl_owner_t)lo;
Jeff Layton21179d82012-08-21 08:03:32 -04004480 file_lock->fl_pid = current->tgid;
4481 file_lock->fl_file = filp;
4482 file_lock->fl_flags = FL_POSIX;
4483 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4484 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
Jeff Layton21179d82012-08-21 08:03:32 -04004486 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4487 locku->lu_length);
4488 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Jeff Layton21179d82012-08-21 08:03:32 -04004490 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004491 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004492 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 goto out_nfserr;
4494 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004495 update_stateid(&stp->st_stid.sc_stateid);
4496 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
J. Bruce Fieldseb2099f2013-04-07 13:28:16 -04004498 if (nfsd4_has_session(cstate) && !check_for_locks(stp->st_file, lo)) {
4499 WARN_ON_ONCE(cstate->replay_owner);
4500 release_lockowner(lo);
4501 }
4502
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004504 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004505 if (!cstate->replay_owner)
4506 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004507 if (file_lock)
4508 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 return status;
4510
4511out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004512 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 goto out;
4514}
4515
4516/*
4517 * returns
4518 * 1: locks held by lockowner
4519 * 0: no locks held by lockowner
4520 */
4521static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004522check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523{
4524 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004525 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 int status = 0;
4527
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004528 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004530 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 status = 1;
4532 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 }
4535out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004536 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return status;
4538}
4539
Al Virob37ad282006-10-19 23:28:59 -07004540__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004541nfsd4_release_lockowner(struct svc_rqst *rqstp,
4542 struct nfsd4_compound_state *cstate,
4543 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
4545 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004546 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004547 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004548 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004550 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004551 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004552 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004553 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
4555 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4556 clid->cl_boot, clid->cl_id);
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 nfs4_lock_state();
4559
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004560 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4561 if (status)
4562 goto out;
4563
NeilBrown3e9e3db2005-06-23 22:04:20 -07004564 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004565 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004566
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004567 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004568 if (sop->so_is_open_owner)
4569 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004570 if (!same_owner_str(sop, owner, clid))
4571 continue;
4572 list_for_each_entry(stp, &sop->so_stateids,
4573 st_perstateowner) {
4574 lo = lockowner(sop);
4575 if (check_for_locks(stp->st_file, lo))
4576 goto out;
4577 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004579 }
4580 /* Clients probably won't expect us to return with some (but not all)
4581 * of the lockowner state released; so don't release any until all
4582 * have been checked. */
4583 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004584 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004585 lo = list_entry(matches.next, struct nfs4_lockowner,
4586 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004587 /* unhash_stateowner deletes so_perclient only
4588 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004589 list_del(&lo->lo_list);
4590 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 }
4592out:
4593 nfs4_unlock_state();
4594 return status;
4595}
4596
4597static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004598alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599{
NeilBrowna55370a2005-06-23 22:03:52 -07004600 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601}
4602
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004603bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004604nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004605{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004606 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004607
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004608 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004609 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004610}
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612/*
4613 * failure => all reset bets are off, nfserr_no_grace...
4614 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004615struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004616nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
4618 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004619 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
NeilBrowna55370a2005-06-23 22:03:52 -07004621 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4622 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004623 if (crp) {
4624 strhashval = clientstr_hashval(name);
4625 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004626 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004627 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004628 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004629 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004630 }
4631 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
Jeff Layton2a4317c2012-03-21 16:42:43 -04004634void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004635nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004636{
4637 list_del(&crp->cr_strhash);
4638 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004639 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004640}
4641
4642void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004643nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
4645 struct nfs4_client_reclaim *crp = NULL;
4646 int i;
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004649 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4650 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004652 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
4654 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004655 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
4658/*
4659 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004660struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004661nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
4663 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 struct nfs4_client_reclaim *crp = NULL;
4665
Jeff Layton278c9312012-11-12 15:00:52 -05004666 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Jeff Layton278c9312012-11-12 15:00:52 -05004668 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004669 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004670 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 return crp;
4672 }
4673 }
4674 return NULL;
4675}
4676
4677/*
4678* Called from OPEN. Look for clientid in reclaim list.
4679*/
Al Virob37ad282006-10-19 23:28:59 -07004680__be32
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004681nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
Jeff Laytona52d7262012-03-21 09:52:02 -04004683 struct nfs4_client *clp;
4684
4685 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004686 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004687 if (clp == NULL)
4688 return nfserr_reclaim_bad;
4689
4690 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
Bryan Schumaker65178db2011-11-01 13:35:21 -04004693#ifdef CONFIG_NFSD_FAULT_INJECTION
4694
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004695u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4696{
J. Bruce Fields221a6872013-04-01 22:23:49 -04004697 if (mark_client_expired(clp))
4698 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004699 expire_client(clp);
4700 return 1;
4701}
4702
Bryan Schumaker184c1842012-11-29 11:40:44 -05004703u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4704{
4705 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004706 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004707 printk(KERN_INFO "NFS Client: %s\n", buf);
4708 return 1;
4709}
4710
4711static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4712 const char *type)
4713{
4714 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004715 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004716 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4717}
4718
Bryan Schumakerfc291712012-11-29 11:40:40 -05004719static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4720{
4721 struct nfs4_openowner *oop;
4722 struct nfs4_lockowner *lop, *lo_next;
4723 struct nfs4_ol_stateid *stp, *st_next;
4724 u64 count = 0;
4725
4726 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4727 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4728 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4729 if (func)
4730 func(lop);
4731 if (++count == max)
4732 return count;
4733 }
4734 }
4735 }
4736
4737 return count;
4738}
4739
4740u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4741{
4742 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4743}
4744
Bryan Schumaker184c1842012-11-29 11:40:44 -05004745u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4746{
4747 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4748 nfsd_print_count(clp, count, "locked files");
4749 return count;
4750}
4751
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05004752static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4753{
4754 struct nfs4_openowner *oop, *next;
4755 u64 count = 0;
4756
4757 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4758 if (func)
4759 func(oop);
4760 if (++count == max)
4761 break;
4762 }
4763
4764 return count;
4765}
4766
4767u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4768{
4769 return nfsd_foreach_client_open(clp, max, release_openowner);
4770}
4771
Bryan Schumaker184c1842012-11-29 11:40:44 -05004772u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
4773{
4774 u64 count = nfsd_foreach_client_open(clp, max, NULL);
4775 nfsd_print_count(clp, count, "open files");
4776 return count;
4777}
4778
Bryan Schumaker269de302012-11-29 11:40:42 -05004779static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
4780 struct list_head *victims)
4781{
4782 struct nfs4_delegation *dp, *next;
4783 u64 count = 0;
4784
4785 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
4786 if (victims)
4787 list_move(&dp->dl_recall_lru, victims);
4788 if (++count == max)
4789 break;
4790 }
4791 return count;
4792}
4793
4794u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
4795{
4796 struct nfs4_delegation *dp, *next;
4797 LIST_HEAD(victims);
4798 u64 count;
4799
4800 spin_lock(&recall_lock);
4801 count = nfsd_find_all_delegations(clp, max, &victims);
4802 spin_unlock(&recall_lock);
4803
4804 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004805 revoke_delegation(dp);
Bryan Schumaker269de302012-11-29 11:40:42 -05004806
4807 return count;
4808}
4809
4810u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
4811{
4812 struct nfs4_delegation *dp, *next;
4813 LIST_HEAD(victims);
4814 u64 count;
4815
4816 spin_lock(&recall_lock);
4817 count = nfsd_find_all_delegations(clp, max, &victims);
4818 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4819 nfsd_break_one_deleg(dp);
4820 spin_unlock(&recall_lock);
4821
4822 return count;
4823}
4824
Bryan Schumaker184c1842012-11-29 11:40:44 -05004825u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
4826{
4827 u64 count = 0;
4828
4829 spin_lock(&recall_lock);
4830 count = nfsd_find_all_delegations(clp, max, NULL);
4831 spin_unlock(&recall_lock);
4832
4833 nfsd_print_count(clp, count, "delegations");
4834 return count;
4835}
4836
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004837u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004838{
4839 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004840 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004841 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004842
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004843 if (!nfsd_netns_ready(nn))
4844 return 0;
4845
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004846 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004847 count += func(clp, max - count);
4848 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004849 break;
4850 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04004851
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004852 return count;
4853}
4854
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05004855struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
4856{
4857 struct nfs4_client *clp;
4858 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4859
4860 if (!nfsd_netns_ready(nn))
4861 return NULL;
4862
4863 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
4864 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
4865 return clp;
4866 }
4867 return NULL;
4868}
4869
Bryan Schumaker65178db2011-11-01 13:35:21 -04004870#endif /* CONFIG_NFSD_FAULT_INJECTION */
4871
NeilBrownac4d8ff22005-06-23 22:03:30 -07004872/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
Bryan Schumaker72083392011-11-01 15:24:59 -04004874void
NeilBrownac4d8ff22005-06-23 22:03:30 -07004875nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
NeilBrownac4d8ff22005-06-23 22:03:30 -07004877}
4878
Meelap Shahc2f1a552007-07-17 04:04:39 -07004879/*
4880 * Since the lifetime of a delegation isn't limited to that of an open, a
4881 * client may quite reasonably hang on to a delegation as long as it has
4882 * the inode cached. This becomes an obvious problem the first time a
4883 * client's inode cache approaches the size of the server's total memory.
4884 *
4885 * For now we avoid this problem by imposing a hard limit on the number
4886 * of delegations, which varies according to the server's memory size.
4887 */
4888static void
4889set_max_delegations(void)
4890{
4891 /*
4892 * Allow at most 4 delegations per megabyte of RAM. Quick
4893 * estimates suggest that in the worst case (where every delegation
4894 * is for a different inode), a delegation could take about 1.5K,
4895 * giving a worst case usage of about 6% of memory.
4896 */
4897 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4898}
4899
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004900static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004901{
4902 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4903 int i;
4904
4905 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4906 CLIENT_HASH_SIZE, GFP_KERNEL);
4907 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004908 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004909 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4910 CLIENT_HASH_SIZE, GFP_KERNEL);
4911 if (!nn->unconf_id_hashtbl)
4912 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004913 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4914 OWNER_HASH_SIZE, GFP_KERNEL);
4915 if (!nn->ownerstr_hashtbl)
4916 goto err_ownerstr;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004917 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4918 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4919 if (!nn->lockowner_ino_hashtbl)
4920 goto err_lockowner_ino;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004921 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4922 SESSION_HASH_SIZE, GFP_KERNEL);
4923 if (!nn->sessionid_hashtbl)
4924 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004925
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004926 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004927 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004928 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004929 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004930 for (i = 0; i < OWNER_HASH_SIZE; i++)
4931 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004932 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4933 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004934 for (i = 0; i < SESSION_HASH_SIZE; i++)
4935 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004936 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004937 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004938 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004939 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004940 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004941 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004942
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004943 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004944 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004945
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004946 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004947
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004948err_sessionid:
4949 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004950err_lockowner_ino:
4951 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004952err_ownerstr:
4953 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004954err_unconf_id:
4955 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004956err:
4957 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004958}
4959
4960static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004961nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004962{
4963 int i;
4964 struct nfs4_client *clp = NULL;
4965 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004966 struct rb_node *node, *tmp;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004967
4968 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4969 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4970 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4971 destroy_client(clp);
4972 }
4973 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004974
4975 node = rb_first(&nn->unconf_name_tree);
4976 while (node != NULL) {
4977 tmp = node;
4978 node = rb_next(tmp);
4979 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4980 rb_erase(tmp, &nn->unconf_name_tree);
4981 destroy_client(clp);
4982 }
4983
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004984 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004985 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004986 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004987 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004988 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004989 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004990}
4991
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004992int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004993nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07004994{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004995 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004996 int ret;
4997
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004998 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004999 if (ret)
5000 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005001 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005002 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005003 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005004 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005005 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005006 nn->nfsd4_grace, net);
5007 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005008 return 0;
5009}
5010
5011/* initialization to perform when the nfsd service is started: */
5012
5013int
5014nfs4_state_start(void)
5015{
5016 int ret;
5017
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005018 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005019 if (ret)
5020 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005021 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005022 if (laundry_wq == NULL) {
5023 ret = -ENOMEM;
5024 goto out_recovery;
5025 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005026 ret = nfsd4_create_callback_queue();
5027 if (ret)
5028 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005029
Meelap Shahc2f1a552007-07-17 04:04:39 -07005030 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005031
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005032 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005033
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005034out_free_laundry:
5035 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005036out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
5039
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005040/* should be called with the state lock held */
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005041void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005042nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005046 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005048 cancel_delayed_work_sync(&nn->laundromat_work);
5049 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005050
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 INIT_LIST_HEAD(&reaplist);
5052 spin_lock(&recall_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005053 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5055 list_move(&dp->dl_recall_lru, &reaplist);
5056 }
5057 spin_unlock(&recall_lock);
5058 list_for_each_safe(pos, next, &reaplist) {
5059 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005060 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
5062
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005063 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005064 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065}
5066
5067void
5068nfs4_state_shutdown(void)
5069{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005070 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005071 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005073
5074static void
5075get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5076{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005077 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5078 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005079}
5080
5081static void
5082put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5083{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005084 if (cstate->minorversion) {
5085 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5086 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5087 }
5088}
5089
5090void
5091clear_current_stateid(struct nfsd4_compound_state *cstate)
5092{
5093 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005094}
5095
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005096/*
5097 * functions to set current state id
5098 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005099void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005100nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5101{
5102 put_stateid(cstate, &odp->od_stateid);
5103}
5104
5105void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005106nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5107{
5108 put_stateid(cstate, &open->op_stateid);
5109}
5110
5111void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005112nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5113{
5114 put_stateid(cstate, &close->cl_stateid);
5115}
5116
5117void
5118nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5119{
5120 put_stateid(cstate, &lock->lk_resp_stateid);
5121}
5122
5123/*
5124 * functions to consume current state id
5125 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005126
5127void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005128nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5129{
5130 get_stateid(cstate, &odp->od_stateid);
5131}
5132
5133void
5134nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5135{
5136 get_stateid(cstate, &drp->dr_stateid);
5137}
5138
5139void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005140nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5141{
5142 get_stateid(cstate, &fsp->fr_stateid);
5143}
5144
5145void
5146nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5147{
5148 get_stateid(cstate, &setattr->sa_stateid);
5149}
5150
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005151void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005152nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5153{
5154 get_stateid(cstate, &close->cl_stateid);
5155}
5156
5157void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005158nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005159{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005160 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005161}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005162
5163void
5164nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5165{
5166 get_stateid(cstate, &read->rd_stateid);
5167}
5168
5169void
5170nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5171{
5172 get_stateid(cstate, &write->wr_stateid);
5173}