blob: 00d4398e2324f05f08e1d3104c5b810950abe14a [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 Layton363168b2009-08-14 12:57:56 -040043#include <linux/sunrpc/clnt.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020044#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050045#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040046#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040048#include "netns.h"
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define NFSDDBG_FACILITY NFSDDBG_PROC
51
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050052#define all_ones {{~0,~0},~0}
53static const stateid_t one_stateid = {
54 .si_generation = ~0,
55 .si_opaque = all_ones,
56};
57static const stateid_t zero_stateid = {
58 /* all fields zero */
59};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010060static const stateid_t currentstateid = {
61 .si_generation = 1,
62};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050063
Andy Adamsonec6b5d72009-04-03 08:28:28 +030064static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070065
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050066#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
67#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010068#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040071static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
J. Bruce Fields8b671b82009-02-22 14:51:34 -080073/* Locking: */
74
75/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080076static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
J. Bruce Fields8b671b82009-02-22 14:51:34 -080078/*
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
82 */
83static DEFINE_SPINLOCK(recall_lock);
84
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040085static struct kmem_cache *openowner_slab = NULL;
86static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080087static struct kmem_cache *file_slab = NULL;
88static struct kmem_cache *stateid_slab = NULL;
89static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091void
92nfs4_lock_state(void)
93{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080094 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Benny Halevy508dc6e2012-02-23 17:40:52 -080097static void free_session(struct kref *);
98
99/* Must be called under the client_lock */
100static void nfsd4_put_session_locked(struct nfsd4_session *ses)
101{
102 kref_put(&ses->se_ref, free_session);
103}
104
105static void nfsd4_get_session(struct nfsd4_session *ses)
106{
107 kref_get(&ses->se_ref);
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110void
111nfs4_unlock_state(void)
112{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800113 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116static inline u32
117opaque_hashval(const void *ptr, int nbytes)
118{
119 unsigned char *cptr = (unsigned char *) ptr;
120
121 u32 x = 0;
122 while (nbytes--) {
123 x *= 37;
124 x += *cptr++;
125 }
126 return x;
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static struct list_head del_recall_lru;
130
J. Bruce Fields32513b42011-10-13 16:00:16 -0400131static void nfsd4_free_file(struct nfs4_file *f)
132{
133 kmem_cache_free(file_slab, f);
134}
135
NeilBrown13cd2182005-06-23 22:03:10 -0700136static inline void
137put_nfs4_file(struct nfs4_file *fi)
138{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800139 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
140 list_del(&fi->fi_hash);
141 spin_unlock(&recall_lock);
142 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400143 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800144 }
NeilBrown13cd2182005-06-23 22:03:10 -0700145}
146
147static inline void
148get_nfs4_file(struct nfs4_file *fi)
149{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800150 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700151}
152
NeilBrownef0f3392006-04-10 22:55:41 -0700153static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700154unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700155
156/*
157 * Open owner state (share locks)
158 */
159
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500160/* hash tables for lock and open owners */
161#define OWNER_HASH_BITS 8
162#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
163#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700164
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500165static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400166{
167 unsigned int ret;
168
169 ret = opaque_hashval(ownername->data, ownername->len);
170 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500171 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400172}
NeilBrownef0f3392006-04-10 22:55:41 -0700173
NeilBrownef0f3392006-04-10 22:55:41 -0700174/* hash table for nfs4_file */
175#define FILE_HASH_BITS 8
176#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800177
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400178static unsigned int file_hashval(struct inode *ino)
179{
180 /* XXX: why are we hashing on inode pointer, anyway? */
181 return hash_ptr(ino, FILE_HASH_BITS);
182}
183
NeilBrownef0f3392006-04-10 22:55:41 -0700184static struct list_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700185
J. Bruce Fields998db522010-08-07 09:21:41 -0400186static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400187{
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500188 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400189 atomic_inc(&fp->fi_access[oflag]);
190}
191
J. Bruce Fields998db522010-08-07 09:21:41 -0400192static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
193{
194 if (oflag == O_RDWR) {
195 __nfs4_file_get_access(fp, O_RDONLY);
196 __nfs4_file_get_access(fp, O_WRONLY);
197 } else
198 __nfs4_file_get_access(fp, oflag);
199}
200
201static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400202{
203 if (fp->fi_fds[oflag]) {
204 fput(fp->fi_fds[oflag]);
205 fp->fi_fds[oflag] = NULL;
206 }
207}
208
J. Bruce Fields998db522010-08-07 09:21:41 -0400209static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400210{
211 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400212 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400213 /*
214 * It's also safe to get rid of the RDWR open *if*
215 * we no longer have need of the other kind of access
216 * or if we already have the other kind of open:
217 */
218 if (fp->fi_fds[1-oflag]
219 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
220 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400221 }
222}
223
J. Bruce Fields998db522010-08-07 09:21:41 -0400224static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
225{
226 if (oflag == O_RDWR) {
227 __nfs4_file_put_access(fp, O_RDONLY);
228 __nfs4_file_put_access(fp, O_WRONLY);
229 } else
230 __nfs4_file_put_access(fp, oflag);
231}
232
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400233static inline int get_new_stid(struct nfs4_stid *stid)
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400234{
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400235 static int min_stateid = 0;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400236 struct idr *stateids = &stid->sc_client->cl_stateids;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400237 int new_stid;
238 int error;
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400239
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400240 error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400241 /*
J. Bruce Fields996e0932011-10-17 11:14:48 -0400242 * Note: the necessary preallocation was done in
243 * nfs4_alloc_stateid(). The idr code caps the number of
244 * preallocations that can exist at a time, but the state lock
245 * prevents anyone from using ours before we get here:
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400246 */
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500247 WARN_ON_ONCE(error);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400248 /*
249 * It shouldn't be a problem to reuse an opaque stateid value.
250 * I don't think it is for 4.1. But with 4.0 I worry that, for
251 * example, a stray write retransmission could be accepted by
252 * the server when it should have been rejected. Therefore,
253 * adopt a trick from the sctp code to attempt to maximize the
254 * amount of time until an id is reused, by ensuring they always
255 * "increase" (mod INT_MAX):
256 */
257
258 min_stateid = new_stid+1;
259 if (min_stateid == INT_MAX)
260 min_stateid = 0;
261 return new_stid;
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400262}
263
J. Bruce Fields996e0932011-10-17 11:14:48 -0400264static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400265{
266 stateid_t *s = &stid->sc_stateid;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400267 int new_id;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400268
269 stid->sc_type = type;
270 stid->sc_client = cl;
271 s->si_opaque.so_clid = cl->cl_clientid;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400272 new_id = get_new_stid(stid);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400273 s->si_opaque.so_id = (u32)new_id;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400274 /* Will be incremented before return to client: */
275 s->si_generation = 0;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400276}
277
278static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
279{
280 struct idr *stateids = &cl->cl_stateids;
281
282 if (!idr_pre_get(stateids, GFP_KERNEL))
283 return NULL;
284 /*
285 * Note: if we fail here (or any time between now and the time
286 * we actually get the new idr), we won't need to undo the idr
287 * preallocation, since the idr code caps the number of
288 * preallocated entries.
289 */
290 return kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400291}
292
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400293static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
294{
295 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400299alloc_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 -0700300{
301 struct nfs4_delegation *dp;
302 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400305 /*
306 * Major work on the lease subsystem (for example, to support
307 * calbacks on stat) will be required before we can support
308 * write delegations properly.
309 */
310 if (type != NFS4_OPEN_DELEGATE_READ)
311 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700312 if (fp->fi_had_conflict)
313 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700314 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700315 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400316 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700317 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return dp;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400319 init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400320 /*
321 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400322 * meaning of seqid 0 isn't meaningful, really, but let's avoid
323 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400324 */
325 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700326 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700327 INIT_LIST_HEAD(&dp->dl_perfile);
328 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 INIT_LIST_HEAD(&dp->dl_recall_lru);
NeilBrown13cd2182005-06-23 22:03:10 -0700330 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 dp->dl_type = type;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500333 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dp->dl_time = 0;
335 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500336 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return dp;
338}
339
340void
341nfs4_put_delegation(struct nfs4_delegation *dp)
342{
343 if (atomic_dec_and_test(&dp->dl_count)) {
344 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700345 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700346 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700347 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349}
350
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500351static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500353 if (atomic_dec_and_test(&fp->fi_delegees)) {
354 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
355 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400356 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500357 fp->fi_deleg_file = NULL;
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400361static void unhash_stid(struct nfs4_stid *s)
362{
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400363 struct idr *stateids = &s->sc_client->cl_stateids;
364
365 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/* Called under the state lock. */
369static void
370unhash_delegation(struct nfs4_delegation *dp)
371{
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400372 unhash_stid(&dp->dl_stid);
NeilBrownea1da632005-06-23 22:04:17 -0700373 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500375 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 list_del_init(&dp->dl_recall_lru);
377 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500378 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 nfs4_put_delegation(dp);
380}
381
382/*
383 * SETCLIENTID state
384 */
385
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400386static unsigned int clientid_hashval(u32 id)
387{
388 return id & CLIENT_HASH_MASK;
389}
390
391static unsigned int clientstr_hashval(const char *name)
392{
393 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400397 * We store the NONE, READ, WRITE, and BOTH bits separately in the
398 * st_{access,deny}_bmap field of the stateid, in order to track not
399 * only what share bits are currently in force, but also what
400 * combinations of share bits previous opens have used. This allows us
401 * to enforce the recommendation of rfc 3530 14.2.19 that the server
402 * return an error if the client attempt to downgrade to a combination
403 * of share bits not explicable by closing some of its previous opens.
404 *
405 * XXX: This enforcement is actually incomplete, since we don't keep
406 * track of access/deny bit combinations; so, e.g., we allow:
407 *
408 * OPEN allow read, deny write
409 * OPEN allow both, deny none
410 * DOWNGRADE allow read, deny none
411 *
412 * which we should reject.
413 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400414static unsigned int
415bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400416 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400417 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400418
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400419 for (i = 1; i < 4; i++) {
420 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400421 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400422 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400423 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400424}
425
Jeff Layton3a328612012-05-11 09:45:12 -0400426static bool
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400427test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400428 unsigned int access, deny;
429
Jeff Layton5ae037e2012-05-11 09:45:11 -0400430 access = bmap_to_share_mode(stp->st_access_bmap);
431 deny = bmap_to_share_mode(stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400432 if ((access & open->op_share_deny) || (deny & open->op_share_access))
Jeff Layton3a328612012-05-11 09:45:12 -0400433 return false;
434 return true;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400435}
436
Jeff Layton82c5ff12012-05-11 09:45:13 -0400437/* set share access for a given stateid */
438static inline void
439set_access(u32 access, struct nfs4_ol_stateid *stp)
440{
441 __set_bit(access, &stp->st_access_bmap);
442}
443
444/* clear share access for a given stateid */
445static inline void
446clear_access(u32 access, struct nfs4_ol_stateid *stp)
447{
448 __clear_bit(access, &stp->st_access_bmap);
449}
450
451/* test whether a given stateid has access */
452static inline bool
453test_access(u32 access, struct nfs4_ol_stateid *stp)
454{
455 return test_bit(access, &stp->st_access_bmap);
456}
457
Jeff Laytonce0fc432012-05-11 09:45:14 -0400458/* set share deny for a given stateid */
459static inline void
460set_deny(u32 access, struct nfs4_ol_stateid *stp)
461{
462 __set_bit(access, &stp->st_deny_bmap);
463}
464
465/* clear share deny for a given stateid */
466static inline void
467clear_deny(u32 access, struct nfs4_ol_stateid *stp)
468{
469 __clear_bit(access, &stp->st_deny_bmap);
470}
471
472/* test whether a given stateid is denying specific access */
473static inline bool
474test_deny(u32 access, struct nfs4_ol_stateid *stp)
475{
476 return test_bit(access, &stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400477}
478
479static int nfs4_access_to_omode(u32 access)
480{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400481 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400482 case NFS4_SHARE_ACCESS_READ:
483 return O_RDONLY;
484 case NFS4_SHARE_ACCESS_WRITE:
485 return O_WRONLY;
486 case NFS4_SHARE_ACCESS_BOTH:
487 return O_RDWR;
488 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500489 WARN_ON_ONCE(1);
490 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400491}
492
Jeff Layton82c5ff12012-05-11 09:45:13 -0400493/* release all access and file references for a given stateid */
494static void
495release_all_access(struct nfs4_ol_stateid *stp)
496{
497 int i;
498
499 for (i = 1; i < 4; i++) {
500 if (test_access(i, stp))
501 nfs4_file_put_access(stp->st_file,
502 nfs4_access_to_omode(i));
503 clear_access(i, stp);
504 }
505}
506
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400507static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500508{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500509 list_del(&stp->st_perfile);
510 list_del(&stp->st_perstateowner);
511}
512
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400513static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500514{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400515 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400516 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400517 stp->st_file = NULL;
518}
519
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400520static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400521{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500522 kmem_cache_free(stateid_slab, stp);
523}
524
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400525static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500526{
527 struct file *file;
528
529 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400530 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500531 file = find_any_file(stp->st_file);
532 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400533 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400534 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500535 free_generic_stateid(stp);
536}
537
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400538static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500539{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400540 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500541
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400542 list_del(&lo->lo_owner.so_strhash);
543 list_del(&lo->lo_perstateid);
J. Bruce Fields009673b2011-11-07 17:40:10 -0500544 list_del(&lo->lo_owner_ino_hash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400545 while (!list_empty(&lo->lo_owner.so_stateids)) {
546 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400547 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500548 release_lock_stateid(stp);
549 }
550}
551
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400552static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500553{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400554 unhash_lockowner(lo);
555 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500556}
557
558static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400559release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500560{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400561 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500562
563 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400564 lo = list_entry(open_stp->st_lockowners.next,
565 struct nfs4_lockowner, lo_perstateid);
566 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500567 }
568}
569
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400570static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500571{
572 unhash_generic_stateid(stp);
573 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400574 close_generic_stateid(stp);
575}
576
577static void release_open_stateid(struct nfs4_ol_stateid *stp)
578{
579 unhash_open_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400580 unhash_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500581 free_generic_stateid(stp);
582}
583
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400584static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500585{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400586 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500587
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400588 list_del(&oo->oo_owner.so_strhash);
589 list_del(&oo->oo_perclient);
590 while (!list_empty(&oo->oo_owner.so_stateids)) {
591 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400592 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500593 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500594 }
595}
596
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400597static void release_last_closed_stateid(struct nfs4_openowner *oo)
598{
599 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
600
601 if (s) {
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400602 unhash_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400603 free_generic_stateid(s);
604 oo->oo_last_closed_stid = NULL;
605 }
606}
607
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400608static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500609{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400610 unhash_openowner(oo);
611 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400612 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400613 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500614}
615
Marc Eshel5282fd72009-04-03 08:27:52 +0300616static inline int
617hash_sessionid(struct nfs4_sessionid *sessionid)
618{
619 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
620
621 return sid->sequence % SESSION_HASH_SIZE;
622}
623
Trond Myklebust8f199b82012-03-20 15:11:17 -0400624#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300625static inline void
626dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
627{
628 u32 *ptr = (u32 *)(&sessionid->data[0]);
629 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
630}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400631#else
632static inline void
633dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
634{
635}
636#endif
637
Marc Eshel5282fd72009-04-03 08:27:52 +0300638
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300639static void
640gen_sessionid(struct nfsd4_session *ses)
641{
642 struct nfs4_client *clp = ses->se_client;
643 struct nfsd4_sessionid *sid;
644
645 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
646 sid->clientid = clp->cl_clientid;
647 sid->sequence = current_sessionid++;
648 sid->reserved = 0;
649}
650
651/*
Andy Adamsona6496372009-08-28 08:45:01 -0400652 * The protocol defines ca_maxresponssize_cached to include the size of
653 * the rpc header, but all we need to cache is the data starting after
654 * the end of the initial SEQUENCE operation--the rest we regenerate
655 * each time. Therefore we can advertise a ca_maxresponssize_cached
656 * value that is the number of bytes in our cache plus a few additional
657 * bytes. In order to stay on the safe side, and not promise more than
658 * we can cache, those additional bytes must be the minimum possible: 24
659 * bytes of rpc header (xid through accept state, with AUTH_NULL
660 * verifier), 12 for the compound header (with zero-length tag), and 44
661 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300662 */
Andy Adamsona6496372009-08-28 08:45:01 -0400663#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
664
Andy Adamson557ce262009-08-28 08:45:04 -0400665static void
666free_session_slots(struct nfsd4_session *ses)
667{
668 int i;
669
670 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
671 kfree(ses->se_slots[i]);
672}
673
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400674/*
675 * We don't actually need to cache the rpc and session headers, so we
676 * can allocate a little less for each slot:
677 */
678static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
679{
680 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
681}
682
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400683static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300684{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400685 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
686 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300687
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400688 return size;
689}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300690
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400691/*
692 * XXX: If we run out of reserved DRC memory we could (up to a point)
693 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800694 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400695 */
696static int nfsd4_get_drc_mem(int slotsize, u32 num)
697{
698 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300699
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400700 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400701
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400702 spin_lock(&nfsd_drc_lock);
703 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
704 nfsd_drc_max_mem - nfsd_drc_mem_used);
705 num = min_t(int, num, avail / slotsize);
706 nfsd_drc_mem_used += num * slotsize;
707 spin_unlock(&nfsd_drc_lock);
708
709 return num;
710}
711
712static void nfsd4_put_drc_mem(int slotsize, int num)
713{
714 spin_lock(&nfsd_drc_lock);
715 nfsd_drc_mem_used -= slotsize * num;
716 spin_unlock(&nfsd_drc_lock);
717}
718
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400719static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400720{
721 struct nfsd4_session *new;
722 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300723
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400724 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400725 + sizeof(struct nfsd4_session) > PAGE_SIZE);
726 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400727
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400728 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300729 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400730 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400731 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400732 for (i = 0; i < numslots; i++) {
733 mem = sizeof(struct nfsd4_slot) + slotsize;
734 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
735 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400736 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400737 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400738 return new;
739out_free:
740 while (i--)
741 kfree(new->se_slots[i]);
742 kfree(new);
743 return NULL;
744}
745
746static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
747{
748 u32 maxrpc = nfsd_serv->sv_max_mesg;
749
750 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800751 new->maxresp_cached = min_t(u32, req->maxresp_cached,
752 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400753 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
754 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
755 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
756}
757
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400758static void free_conn(struct nfsd4_conn *c)
759{
760 svc_xprt_put(c->cn_xprt);
761 kfree(c);
762}
763
764static void nfsd4_conn_lost(struct svc_xpt_user *u)
765{
766 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
767 struct nfs4_client *clp = c->cn_session->se_client;
768
769 spin_lock(&clp->cl_lock);
770 if (!list_empty(&c->cn_persession)) {
771 list_del(&c->cn_persession);
772 free_conn(c);
773 }
774 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500775 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400776}
777
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400778static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400779{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400780 struct nfsd4_conn *conn;
781
782 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
783 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400784 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400785 svc_xprt_get(rqstp->rq_xprt);
786 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400787 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400788 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
789 return conn;
790}
791
J. Bruce Fields328ead22010-09-29 16:11:06 -0400792static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
793{
794 conn->cn_session = ses;
795 list_add(&conn->cn_persession, &ses->se_conns);
796}
797
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400798static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
799{
800 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400801
802 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400803 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400804 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400805}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400806
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400807static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400808{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400809 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400810 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400811}
812
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400813static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400814{
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400815 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400816
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400817 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400818 ret = nfsd4_register_conn(conn);
819 if (ret)
820 /* oops; xprt is already down: */
821 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -0400822 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -0400823 /* callback channel may be back up */
824 nfsd4_probe_callback(ses->se_client);
825 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400826}
827
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400828static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400829{
830 u32 dir = NFS4_CDFC4_FORE;
831
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400832 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400833 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400834 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400835}
836
837/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400838static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400839{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400840 struct nfs4_client *clp = s->se_client;
841 struct nfsd4_conn *c;
842
843 spin_lock(&clp->cl_lock);
844 while (!list_empty(&s->se_conns)) {
845 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
846 list_del_init(&c->cn_persession);
847 spin_unlock(&clp->cl_lock);
848
849 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
850 free_conn(c);
851
852 spin_lock(&clp->cl_lock);
853 }
854 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400855}
856
J. Bruce Fields1377b692012-09-11 21:42:40 -0400857static void __free_session(struct nfsd4_session *ses)
858{
859 nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
860 free_session_slots(ses);
861 kfree(ses);
862}
863
Benny Halevy508dc6e2012-02-23 17:40:52 -0800864static void free_session(struct kref *kref)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400865{
866 struct nfsd4_session *ses;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300867 struct nfsd_net *nn;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400868
869 ses = container_of(kref, struct nfsd4_session, se_ref);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300870 nn = net_generic(ses->se_client->net, nfsd_net_id);
871
872 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400873 nfsd4_del_conns(ses);
J. Bruce Fields1377b692012-09-11 21:42:40 -0400874 __free_session(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400875}
876
Benny Halevy508dc6e2012-02-23 17:40:52 -0800877void nfsd4_put_session(struct nfsd4_session *ses)
878{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300879 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
880
881 spin_lock(&nn->client_lock);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800882 nfsd4_put_session_locked(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300883 spin_unlock(&nn->client_lock);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800884}
885
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400886static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400887{
888 struct nfsd4_session *new;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400889 int numslots, slotsize;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400890 /*
891 * Note decreasing slot size below client's request may
892 * make it difficult for client to function correctly, whereas
893 * decreasing the number of slots will (just?) affect
894 * performance. When short on memory we therefore prefer to
895 * decrease number of slots instead of their size.
896 */
897 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
898 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800899 if (numslots < 1)
900 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400901
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400902 new = __alloc_session(slotsize, numslots);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400903 if (!new) {
904 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400905 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400906 }
907 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400908 return new;
909}
Andy Adamson557ce262009-08-28 08:45:04 -0400910
Fengguang Wu135ae822012-11-10 07:20:25 -0500911static 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 -0400912{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400913 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +0300914 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400915
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300916 new->se_client = clp;
917 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300918
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400919 INIT_LIST_HEAD(&new->se_conns);
920
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400921 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300922 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400923 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400924 new->se_cb_sec = cses->cb_sec;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300925 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400926 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300927 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +0300928 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400929 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300930 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400931 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +0300932 spin_unlock(&nn->client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300933
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400934 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400935 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400936 /*
937 * This is a little silly; with sessions there's no real
938 * use for the callback address. Use the peer address
939 * as a reasonable default for now, but consider fixing
940 * the rpc client not to require an address in the
941 * future:
942 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400943 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
944 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400945 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300946}
947
Benny Halevy9089f1b2010-05-12 00:12:26 +0300948/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300949static struct nfsd4_session *
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +0300950find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +0300951{
952 struct nfsd4_session *elem;
953 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +0300954 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +0300955
956 dump_sessionid(__func__, sessionid);
957 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300958 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +0300959 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300960 if (!memcmp(elem->se_sessionid.data, sessionid->data,
961 NFS4_MAX_SESSIONID_LEN)) {
962 return elem;
963 }
964 }
965
966 dprintk("%s: session not found\n", __func__);
967 return NULL;
968}
969
Benny Halevy9089f1b2010-05-12 00:12:26 +0300970/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300971static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300972unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300973{
974 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400975 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300976 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400977 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300978}
979
Benny Halevy36acb662010-05-12 00:13:04 +0300980/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300982renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +0300984 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
985
Benny Halevy07cd4902010-05-12 00:13:41 +0300986 if (is_client_expired(clp)) {
J. Bruce Fields74477582012-05-04 14:57:52 -0400987 WARN_ON(1);
988 printk("%s: client (clientid %08x/%08x) already expired\n",
Benny Halevy07cd4902010-05-12 00:13:41 +0300989 __func__,
990 clp->cl_clientid.cl_boot,
991 clp->cl_clientid.cl_id);
992 return;
993 }
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 dprintk("renewing client (clientid %08x/%08x)\n",
996 clp->cl_clientid.cl_boot,
997 clp->cl_clientid.cl_id);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +0300998 list_move_tail(&clp->cl_lru, &nn->client_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 clp->cl_time = get_seconds();
1000}
1001
Benny Halevy36acb662010-05-12 00:13:04 +03001002static inline void
1003renew_client(struct nfs4_client *clp)
1004{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001005 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1006
1007 spin_lock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03001008 renew_client_locked(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001009 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03001010}
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1013static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001014STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001016 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001018 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001019 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 1;
1021}
1022
1023/*
1024 * XXX Should we use a slab cache ?
1025 * This type of memory management is somewhat inefficient, but we use it
1026 * anyway since SETCLIENTID is not a common operation.
1027 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001028static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct nfs4_client *clp;
1031
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001032 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1033 if (clp == NULL)
1034 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001035 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001036 if (clp->cl_name.data == NULL) {
1037 kfree(clp);
1038 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001040 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return clp;
1042}
1043
1044static inline void
1045free_client(struct nfs4_client *clp)
1046{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001047 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1048
1049 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001050 while (!list_empty(&clp->cl_sessions)) {
1051 struct nfsd4_session *ses;
1052 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1053 se_perclnt);
1054 list_del(&ses->se_perclnt);
Benny Halevy508dc6e2012-02-23 17:40:52 -08001055 nfsd4_put_session_locked(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001056 }
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001057 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 kfree(clp->cl_name.data);
1059 kfree(clp);
1060}
1061
Benny Halevyd7682982010-05-12 00:13:54 +03001062void
1063release_session_client(struct nfsd4_session *session)
1064{
1065 struct nfs4_client *clp = session->se_client;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001066 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Benny Halevyd7682982010-05-12 00:13:54 +03001067
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001068 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
Benny Halevyd7682982010-05-12 00:13:54 +03001069 return;
1070 if (is_client_expired(clp)) {
1071 free_client(clp);
1072 session->se_client = NULL;
1073 } else
1074 renew_client_locked(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001075 spin_unlock(&nn->client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +03001076}
1077
Benny Halevy84d38ac2010-05-12 00:13:16 +03001078/* must be called under the client_lock */
1079static inline void
1080unhash_client_locked(struct nfs4_client *clp)
1081{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001082 struct nfsd4_session *ses;
1083
Benny Halevy07cd4902010-05-12 00:13:41 +03001084 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001085 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001086 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001087 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1088 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001089 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001093destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001095 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001098 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 INIT_LIST_HEAD(&reaplist);
1101 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001102 while (!list_empty(&clp->cl_delegations)) {
1103 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001104 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 list_move(&dp->dl_recall_lru, &reaplist);
1106 }
1107 spin_unlock(&recall_lock);
1108 while (!list_empty(&reaplist)) {
1109 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 unhash_delegation(dp);
1111 }
NeilBrownea1da632005-06-23 22:04:17 -07001112 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001113 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1114 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001116 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001117 if (clp->cl_cb_conn.cb_xprt)
1118 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001119 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001120 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001121 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001122 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001123 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001124 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001125 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +03001126 if (atomic_read(&clp->cl_refcount) == 0)
1127 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001128 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001131static void expire_client(struct nfs4_client *clp)
1132{
1133 nfsd4_client_record_remove(clp);
1134 destroy_client(clp);
1135}
1136
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001137static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1138{
1139 memcpy(target->cl_verifier.data, source->data,
1140 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001143static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1146 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1147}
1148
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001149static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001150{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001151 if (source->cr_principal) {
1152 target->cr_principal =
1153 kstrdup(source->cr_principal, GFP_KERNEL);
1154 if (target->cr_principal == NULL)
1155 return -ENOMEM;
1156 } else
1157 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001158 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 target->cr_uid = source->cr_uid;
1160 target->cr_gid = source->cr_gid;
1161 target->cr_group_info = source->cr_group_info;
1162 get_group_info(target->cr_group_info);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001166static long long
1167compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1168{
1169 long long res;
1170
1171 res = o1->len - o2->len;
1172 if (res)
1173 return res;
1174 return (long long)memcmp(o1->data, o2->data, o1->len);
1175}
1176
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001177static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001178{
NeilBrowna55370a2005-06-23 22:03:52 -07001179 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001183same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1184{
1185 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
1188static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001189same_clid(clientid_t *cl1, clientid_t *cl2)
1190{
1191 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001194static bool groups_equal(struct group_info *g1, struct group_info *g2)
1195{
1196 int i;
1197
1198 if (g1->ngroups != g2->ngroups)
1199 return false;
1200 for (i=0; i<g1->ngroups; i++)
1201 if (GROUP_AT(g1, i) != GROUP_AT(g2, i))
1202 return false;
1203 return true;
1204}
1205
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001206/*
1207 * RFC 3530 language requires clid_inuse be returned when the
1208 * "principal" associated with a requests differs from that previously
1209 * used. We use uid, gid's, and gss principal string as our best
1210 * approximation. We also don't want to allow non-gss use of a client
1211 * established using gss: in theory cr_principal should catch that
1212 * change, but in practice cr_principal can be null even in the gss case
1213 * since gssd doesn't always pass down a principal string.
1214 */
1215static bool is_gss_cred(struct svc_cred *cr)
1216{
1217 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1218 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1219}
1220
1221
Vivek Trivedi5559b502012-07-24 21:18:20 +05301222static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001223same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1224{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001225 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001226 || (cr1->cr_uid != cr2->cr_uid)
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001227 || (cr1->cr_gid != cr2->cr_gid)
1228 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1229 return false;
1230 if (cr1->cr_principal == cr2->cr_principal)
1231 return true;
1232 if (!cr1->cr_principal || !cr2->cr_principal)
1233 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301234 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001237static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001238{
1239 static u32 current_clientid = 1;
1240
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001241 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 clp->cl_clientid.cl_id = current_clientid++;
1243}
1244
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001245static void gen_confirm(struct nfs4_client *clp)
1246{
Chuck Leverab4684d2012-03-02 17:13:50 -05001247 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001248 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Chuck Leverab4684d2012-03-02 17:13:50 -05001250 verf[0] = (__be32)get_seconds();
1251 verf[1] = (__be32)i++;
1252 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001255static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001256{
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001257 return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001258}
1259
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001260static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001261{
1262 struct nfs4_stid *s;
1263
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001264 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001265 if (!s)
1266 return NULL;
1267 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001268 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001269 return NULL;
1270}
1271
Jeff Layton2216d442012-11-12 15:00:57 -05001272static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001273 struct svc_rqst *rqstp, nfs4_verifier *verf)
1274{
1275 struct nfs4_client *clp;
1276 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001277 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001278 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001279 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001280
1281 clp = alloc_client(name);
1282 if (clp == NULL)
1283 return NULL;
1284
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001285 INIT_LIST_HEAD(&clp->cl_sessions);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001286 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1287 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001288 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001289 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001290 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001291 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001292 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001293 idr_init(&clp->cl_stateids);
Benny Halevy46583e22010-05-12 00:13:29 +03001294 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001295 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001296 INIT_LIST_HEAD(&clp->cl_idhash);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001297 INIT_LIST_HEAD(&clp->cl_openowners);
1298 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001299 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001300 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001301 spin_lock_init(&clp->cl_lock);
J. Bruce Fields57725152012-11-05 15:10:26 -05001302 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001303 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001304 clear_bit(0, &clp->cl_cb_slot_busy);
1305 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1306 copy_verf(clp, verf);
1307 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001308 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001309 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001310 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001311 return clp;
1312}
1313
NeilBrownfd39ca92005-06-23 22:04:03 -07001314static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001315add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1316{
1317 struct rb_node **new = &(root->rb_node), *parent = NULL;
1318 struct nfs4_client *clp;
1319
1320 while (*new) {
1321 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1322 parent = *new;
1323
1324 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1325 new = &((*new)->rb_left);
1326 else
1327 new = &((*new)->rb_right);
1328 }
1329
1330 rb_link_node(&new_clp->cl_namenode, parent, new);
1331 rb_insert_color(&new_clp->cl_namenode, root);
1332}
1333
1334static struct nfs4_client *
1335find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1336{
1337 long long cmp;
1338 struct rb_node *node = root->rb_node;
1339 struct nfs4_client *clp;
1340
1341 while (node) {
1342 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1343 cmp = compare_blob(&clp->cl_name, name);
1344 if (cmp > 0)
1345 node = node->rb_left;
1346 else if (cmp < 0)
1347 node = node->rb_right;
1348 else
1349 return clp;
1350 }
1351 return NULL;
1352}
1353
1354static void
1355add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001358 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001360 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001361 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001363 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001364 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
NeilBrownfd39ca92005-06-23 22:04:03 -07001367static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368move_to_confirmed(struct nfs4_client *clp)
1369{
1370 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001371 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001374 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001375 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001376 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001377 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 renew_client(clp);
1379}
1380
1381static struct nfs4_client *
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001382find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct nfs4_client *clp;
1385 unsigned int idhashval = clientid_hashval(clid->cl_id);
1386
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001387 list_for_each_entry(clp, &nn->conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001388 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001389 if ((bool)clp->cl_minorversion != sessions)
1390 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001391 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395 return NULL;
1396}
1397
1398static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001399find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 struct nfs4_client *clp;
1402 unsigned int idhashval = clientid_hashval(clid->cl_id);
1403
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001404 list_for_each_entry(clp, &nn->unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001405 if (same_clid(&clp->cl_clientid, clid)) {
1406 if ((bool)clp->cl_minorversion != sessions)
1407 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return clp;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 return NULL;
1412}
1413
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001414static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001415{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001416 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001417}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001418
NeilBrown28ce6052005-06-23 22:03:56 -07001419static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001420find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001421{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001422 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001423}
1424
1425static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001426find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001427{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001428 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001429}
1430
NeilBrownfd39ca92005-06-23 22:04:03 -07001431static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001432gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001434 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001435 struct sockaddr *sa = svc_addr(rqstp);
1436 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001437 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Jeff Layton7077ecb2009-08-14 12:57:58 -04001439 /* Currently, we only support tcp and tcp6 for the callback channel */
1440 if (se->se_callback_netid_len == 3 &&
1441 !memcmp(se->se_callback_netid_val, "tcp", 3))
1442 expected_family = AF_INET;
1443 else if (se->se_callback_netid_len == 4 &&
1444 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1445 expected_family = AF_INET6;
1446 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 goto out_err;
1448
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001449 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001450 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001451 (struct sockaddr *)&conn->cb_addr,
1452 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001453
J. Bruce Fields07263f12010-05-31 19:09:40 -04001454 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001456
J. Bruce Fields07263f12010-05-31 19:09:40 -04001457 if (conn->cb_addr.ss_family == AF_INET6)
1458 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001459
J. Bruce Fields07263f12010-05-31 19:09:40 -04001460 conn->cb_prog = se->se_callback_prog;
1461 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001462 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return;
1464out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001465 conn->cb_addr.ss_family = AF_UNSPEC;
1466 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001467 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 "will not receive delegations\n",
1469 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return;
1472}
1473
Andy Adamson074fe892009-04-03 08:28:15 +03001474/*
Andy Adamson557ce262009-08-28 08:45:04 -04001475 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001476 */
1477void
1478nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1479{
Andy Adamson557ce262009-08-28 08:45:04 -04001480 struct nfsd4_slot *slot = resp->cstate.slot;
1481 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001482
Andy Adamson557ce262009-08-28 08:45:04 -04001483 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001484
Andy Adamson557ce262009-08-28 08:45:04 -04001485 slot->sl_opcnt = resp->opcnt;
1486 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001487
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001488 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001489 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001490 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001491 return;
1492 }
Andy Adamson557ce262009-08-28 08:45:04 -04001493 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1494 base = (char *)resp->cstate.datap -
1495 (char *)resp->xbuf->head[0].iov_base;
1496 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1497 slot->sl_datalen))
1498 WARN("%s: sessions DRC could not cache compound\n", __func__);
1499 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001500}
1501
1502/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001503 * Encode the replay sequence operation from the slot values.
1504 * If cachethis is FALSE encode the uncached rep error on the next
1505 * operation which sets resp->p and increments resp->opcnt for
1506 * nfs4svc_encode_compoundres.
1507 *
Andy Adamson074fe892009-04-03 08:28:15 +03001508 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001509static __be32
1510nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1511 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001512{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001513 struct nfsd4_op *op;
1514 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001515
Andy Adamsonabfabf82009-07-23 19:02:18 -04001516 /* Encode the replayed sequence operation */
1517 op = &args->ops[resp->opcnt - 1];
1518 nfsd4_encode_operation(resp, op);
1519
1520 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001521 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001522 op = &args->ops[resp->opcnt++];
1523 op->status = nfserr_retry_uncached_rep;
1524 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001525 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001526 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001527}
1528
1529/*
Andy Adamson557ce262009-08-28 08:45:04 -04001530 * The sequence operation is not cached because we can use the slot and
1531 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001532 */
1533__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001534nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1535 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001536{
Andy Adamson557ce262009-08-28 08:45:04 -04001537 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001538 __be32 status;
1539
Andy Adamson557ce262009-08-28 08:45:04 -04001540 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001541
Andy Adamsonabfabf82009-07-23 19:02:18 -04001542 /* Either returns 0 or nfserr_retry_uncached */
1543 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1544 if (status == nfserr_retry_uncached_rep)
1545 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001546
Andy Adamson557ce262009-08-28 08:45:04 -04001547 /* The sequence operation has been encoded, cstate->datap set. */
1548 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001549
Andy Adamson557ce262009-08-28 08:45:04 -04001550 resp->opcnt = slot->sl_opcnt;
1551 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1552 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001553
1554 return status;
1555}
1556
Andy Adamson0733d212009-04-03 08:28:01 +03001557/*
1558 * Set the exchange_id flags returned by the server.
1559 */
1560static void
1561nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1562{
1563 /* pNFS is not supported */
1564 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1565
1566 /* Referrals are supported, Migration is not. */
1567 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1568
1569 /* set the wire flags to return to client. */
1570 clid->flags = new->cl_exchange_flags;
1571}
1572
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001573static bool client_has_state(struct nfs4_client *clp)
1574{
1575 /*
1576 * Note clp->cl_openowners check isn't quite right: there's no
1577 * need to count owners without stateid's.
1578 *
1579 * Also note we should probably be using this in 4.0 case too.
1580 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001581 return !list_empty(&clp->cl_openowners)
1582 || !list_empty(&clp->cl_delegations)
1583 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001584}
1585
Al Virob37ad282006-10-19 23:28:59 -07001586__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001587nfsd4_exchange_id(struct svc_rqst *rqstp,
1588 struct nfsd4_compound_state *cstate,
1589 struct nfsd4_exchange_id *exid)
1590{
Andy Adamson0733d212009-04-03 08:28:01 +03001591 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001592 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001593 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001594 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001595 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001596 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001597 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001598
Jeff Layton363168b2009-08-14 12:57:56 -04001599 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001600 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001601 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001602 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001603 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001604
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001605 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001606 return nfserr_inval;
1607
1608 /* Currently only support SP4_NONE */
1609 switch (exid->spa_how) {
1610 case SP4_NONE:
1611 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001612 default: /* checked by xdr code */
1613 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001614 case SP4_SSV:
Andy Adamson0733d212009-04-03 08:28:01 +03001615 case SP4_MACH_CRED:
1616 return nfserr_serverfault; /* no excuse :-/ */
1617 }
1618
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001619 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001620 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001621 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001622 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001623 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1624 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1625
J. Bruce Fields136e6582012-05-12 20:37:23 -04001626 if (update) {
1627 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001628 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001629 goto out;
1630 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001631 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001632 status = nfserr_perm;
1633 goto out;
1634 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001635 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001636 status = nfserr_not_same;
1637 goto out;
1638 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001639 /* case 6 */
1640 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1641 new = conf;
1642 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001643 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001644 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001645 if (client_has_state(conf)) {
1646 status = nfserr_clid_inuse;
1647 goto out;
1648 }
Andy Adamson0733d212009-04-03 08:28:01 +03001649 expire_client(conf);
1650 goto out_new;
1651 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001652 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001653 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001654 new = conf;
1655 goto out_copy;
1656 }
1657 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001658 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001659 }
1660
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001661 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001662 status = nfserr_noent;
1663 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001664 }
1665
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001666 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001667 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001668 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001669
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001670 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001671out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001672 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001673 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001674 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001675 goto out;
1676 }
J. Bruce Fieldsc116a0a2012-09-11 14:53:09 -04001677 new->cl_minorversion = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001678
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001679 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001680 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001681out_copy:
1682 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1683 exid->clientid.cl_id = new->cl_clientid.cl_id;
1684
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001685 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001686 nfsd4_set_ex_flags(new, exid);
1687
1688 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001689 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001690 status = nfs_ok;
1691
1692out:
1693 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001694 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001695}
1696
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001697static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001698check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001699{
Andy Adamson88e588d2009-07-23 19:02:15 -04001700 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1701 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001702
1703 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001704 if (slot_inuse) {
1705 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001706 return nfserr_jukebox;
1707 else
1708 return nfserr_seq_misordered;
1709 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001710 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001711 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001712 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001713 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001714 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001715 return nfserr_seq_misordered;
1716}
1717
Andy Adamson49557cc2009-07-23 19:02:16 -04001718/*
1719 * Cache the create session result into the create session single DRC
1720 * slot cache by saving the xdr structure. sl_seqid has been set.
1721 * Do this for solo or embedded create session operations.
1722 */
1723static void
1724nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001725 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001726{
1727 slot->sl_status = nfserr;
1728 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1729}
1730
1731static __be32
1732nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1733 struct nfsd4_clid_slot *slot)
1734{
1735 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1736 return slot->sl_status;
1737}
1738
Mi Jinlong1b74c252011-07-14 14:50:17 +08001739#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1740 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1741 1 + /* MIN tag is length with zero, only length */ \
1742 3 + /* version, opcount, opcode */ \
1743 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1744 /* seqid, slotID, slotID, cache */ \
1745 4 ) * sizeof(__be32))
1746
1747#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1748 2 + /* verifier: AUTH_NULL, length 0 */\
1749 1 + /* status */ \
1750 1 + /* MIN tag is length with zero, only length */ \
1751 3 + /* opcount, opcode, opstatus*/ \
1752 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1753 /* seqid, slotID, slotID, slotID, status */ \
1754 5 ) * sizeof(__be32))
1755
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001756static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001757{
1758 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1759 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1760}
1761
Andy Adamson069b6ad2009-04-03 08:27:58 +03001762__be32
1763nfsd4_create_session(struct svc_rqst *rqstp,
1764 struct nfsd4_compound_state *cstate,
1765 struct nfsd4_create_session *cr_ses)
1766{
Jeff Layton363168b2009-08-14 12:57:56 -04001767 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001768 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001769 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001770 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001771 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001772 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001773 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001774
Mi Jinlonga62573d2011-03-23 17:57:07 +08001775 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1776 return nfserr_inval;
J. Bruce Fields49730502012-09-11 15:33:21 -04001777 if (check_forechannel_attrs(cr_ses->fore_channel))
1778 return nfserr_toosmall;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001779 new = alloc_session(&cr_ses->fore_channel);
1780 if (!new)
1781 return nfserr_jukebox;
1782 status = nfserr_jukebox;
1783 conn = alloc_conn_from_crses(rqstp, cr_ses);
1784 if (!conn)
1785 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08001786
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001787 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001788 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001789 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001790
1791 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001792 cs_slot = &conf->cl_cs_slot;
1793 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001794 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001795 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001796 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001797 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001798 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001799 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001800 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001801 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001802 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001803 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001804 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001805 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001806 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001807 }
Andy Adamson49557cc2009-07-23 19:02:16 -04001808 cs_slot = &unconf->cl_cs_slot;
1809 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001810 if (status) {
1811 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001812 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001813 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001814 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001815 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001816 if (old)
1817 expire_client(old);
1818 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001819 conf = unconf;
1820 } else {
1821 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001822 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001823 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001824 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001825 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001826 * We do not support RDMA or persistent sessions
1827 */
1828 cr_ses->flags &= ~SESSION4_PERSIST;
1829 cr_ses->flags &= ~SESSION4_RDMA;
1830
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001831 init_session(rqstp, new, conf, cr_ses);
1832 nfsd4_init_conn(rqstp, conn, new);
1833
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001834 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001835 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001836 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1837 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001838 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001839 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001840
Andy Adamson49557cc2009-07-23 19:02:16 -04001841 /* cache solo and embedded create sessions under the state lock */
1842 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001843out:
1844 nfs4_unlock_state();
1845 dprintk("%s returns %d\n", __func__, ntohl(status));
1846 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001847out_free_conn:
1848 free_conn(conn);
1849out_free_session:
1850 __free_session(new);
1851 goto out;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001852}
1853
J. Bruce Fields57716352010-04-21 12:27:19 -04001854static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1855{
1856 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1857 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1858
1859 return argp->opcnt == resp->opcnt;
1860}
1861
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001862static __be32 nfsd4_map_bcts_dir(u32 *dir)
1863{
1864 switch (*dir) {
1865 case NFS4_CDFC4_FORE:
1866 case NFS4_CDFC4_BACK:
1867 return nfs_ok;
1868 case NFS4_CDFC4_FORE_OR_BOTH:
1869 case NFS4_CDFC4_BACK_OR_BOTH:
1870 *dir = NFS4_CDFC4_BOTH;
1871 return nfs_ok;
1872 };
1873 return nfserr_inval;
1874}
1875
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001876__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1877{
1878 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001879 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001880
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001881 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001882 session->se_cb_prog = bc->bc_cb_program;
1883 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001884 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001885
1886 nfsd4_probe_callback(session->se_client);
1887
1888 return nfs_ok;
1889}
1890
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001891__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1892 struct nfsd4_compound_state *cstate,
1893 struct nfsd4_bind_conn_to_session *bcts)
1894{
1895 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001896 struct nfsd4_conn *conn;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001897 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001898
1899 if (!nfsd4_last_compound_op(rqstp))
1900 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001901 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001902 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001903 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1904 * client_lock iself: */
1905 if (cstate->session) {
1906 nfsd4_get_session(cstate->session);
1907 atomic_inc(&cstate->session->se_client->cl_refcount);
1908 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001909 spin_unlock(&nn->client_lock);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001910 if (!cstate->session)
1911 return nfserr_badsession;
1912
1913 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001914 if (status)
1915 return status;
1916 conn = alloc_conn(rqstp, bcts->dir);
1917 if (!conn)
1918 return nfserr_jukebox;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001919 nfsd4_init_conn(rqstp, conn, cstate->session);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001920 return nfs_ok;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001921}
1922
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001923static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1924{
1925 if (!session)
1926 return 0;
1927 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1928}
1929
Andy Adamson069b6ad2009-04-03 08:27:58 +03001930__be32
1931nfsd4_destroy_session(struct svc_rqst *r,
1932 struct nfsd4_compound_state *cstate,
1933 struct nfsd4_destroy_session *sessionid)
1934{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001935 struct nfsd4_session *ses;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001936 __be32 status = nfserr_badsession;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001937 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001938
1939 /* Notes:
1940 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1941 * - Should we return nfserr_back_chan_busy if waiting for
1942 * callbacks on to-be-destroyed session?
1943 * - Do we need to clear any callback info from previous session?
1944 */
1945
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001946 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001947 if (!nfsd4_last_compound_op(r))
1948 return nfserr_not_only_op;
1949 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001950 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001951 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001952 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
Benny Halevye10e0cf2009-04-03 08:28:38 +03001953 if (!ses) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001954 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001955 goto out;
1956 }
1957
1958 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001959 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001960
Benny Halevyab707e152010-05-12 00:14:06 +03001961 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001962 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001963 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001964
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001965 spin_lock(&nn->client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001966 nfsd4_del_conns(ses);
Benny Halevy508dc6e2012-02-23 17:40:52 -08001967 nfsd4_put_session_locked(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001968 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001969 status = nfs_ok;
1970out:
1971 dprintk("%s returns %d\n", __func__, ntohl(status));
1972 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001973}
1974
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001975static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001976{
1977 struct nfsd4_conn *c;
1978
1979 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001980 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001981 return c;
1982 }
1983 }
1984 return NULL;
1985}
1986
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001987static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001988{
1989 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001990 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001991 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001992
1993 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001994 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001995 if (c) {
1996 spin_unlock(&clp->cl_lock);
1997 free_conn(new);
1998 return;
1999 }
2000 __nfsd4_hash_conn(new, ses);
2001 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002002 ret = nfsd4_register_conn(new);
2003 if (ret)
2004 /* oops; xprt is already down: */
2005 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002006 return;
2007}
2008
Mi Jinlong868b89c2011-04-27 09:09:58 +08002009static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2010{
2011 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2012
2013 return args->opcnt > session->se_fchannel.maxops;
2014}
2015
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002016static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2017 struct nfsd4_session *session)
2018{
2019 struct xdr_buf *xb = &rqstp->rq_arg;
2020
2021 return xb->len > session->se_fchannel.maxreq_sz;
2022}
2023
Andy Adamson069b6ad2009-04-03 08:27:58 +03002024__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002025nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002026 struct nfsd4_compound_state *cstate,
2027 struct nfsd4_sequence *seq)
2028{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002029 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002030 struct nfsd4_session *session;
2031 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002032 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002033 __be32 status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002034 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002035
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002036 if (resp->opcnt != 1)
2037 return nfserr_sequence_pos;
2038
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002039 /*
2040 * Will be either used or freed by nfsd4_sequence_check_conn
2041 * below.
2042 */
2043 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2044 if (!conn)
2045 return nfserr_jukebox;
2046
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002047 spin_lock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002048 status = nfserr_badsession;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002049 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
Benny Halevyb85d4c02009-04-03 08:28:08 +03002050 if (!session)
2051 goto out;
2052
Mi Jinlong868b89c2011-04-27 09:09:58 +08002053 status = nfserr_too_many_ops;
2054 if (nfsd4_session_too_many_ops(rqstp, session))
2055 goto out;
2056
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002057 status = nfserr_req_too_big;
2058 if (nfsd4_request_too_big(rqstp, session))
2059 goto out;
2060
Benny Halevyb85d4c02009-04-03 08:28:08 +03002061 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002062 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002063 goto out;
2064
Andy Adamson557ce262009-08-28 08:45:04 -04002065 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002066 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2067
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002068 /* We do not negotiate the number of slots yet, so set the
2069 * maxslots to the session maxreqs which is used to encode
2070 * sr_highest_slotid and the sr_target_slot id to maxslots */
2071 seq->maxslots = session->se_fchannel.maxreqs;
2072
J. Bruce Fields73e79482012-02-13 16:39:00 -05002073 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2074 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002075 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002076 status = nfserr_seq_misordered;
2077 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2078 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002079 cstate->slot = slot;
2080 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002081 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002082 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002083 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002084 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002085 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002086 }
2087 if (status)
2088 goto out;
2089
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002090 nfsd4_sequence_check_conn(conn, session);
2091 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002092
Benny Halevyb85d4c02009-04-03 08:28:08 +03002093 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002094 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002095 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002096 if (seq->cachethis)
2097 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002098 else
2099 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002100
2101 cstate->slot = slot;
2102 cstate->session = session;
2103
Benny Halevyb85d4c02009-04-03 08:28:08 +03002104out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04002105 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002106 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05002107 struct nfs4_client *clp = session->se_client;
2108
Benny Halevy36acb662010-05-12 00:13:04 +03002109 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05002110 atomic_inc(&clp->cl_refcount);
Benny Halevy54237322011-10-19 19:12:58 -07002111 switch (clp->cl_cb_state) {
2112 case NFSD4_CB_DOWN:
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07002113 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevy54237322011-10-19 19:12:58 -07002114 break;
2115 case NFSD4_CB_FAULT:
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07002116 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
Benny Halevy54237322011-10-19 19:12:58 -07002117 break;
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07002118 default:
2119 seq->status_flags = 0;
Benny Halevy54237322011-10-19 19:12:58 -07002120 }
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002121 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002122 kfree(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002123 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002124 dprintk("%s: return %d\n", __func__, ntohl(status));
2125 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002126}
2127
Mi Jinlong345c2842011-10-20 17:51:39 +08002128__be32
2129nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2130{
2131 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002132 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002133 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002134
2135 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002136 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002137 conf = find_confirmed_client(&dc->clientid, true, nn);
Mi Jinlong345c2842011-10-20 17:51:39 +08002138
2139 if (conf) {
2140 clp = conf;
2141
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002142 if (!is_client_expired(conf) && client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002143 status = nfserr_clientid_busy;
2144 goto out;
2145 }
2146
2147 /* rfc5661 18.50.3 */
2148 if (cstate->session && conf == cstate->session->se_client) {
2149 status = nfserr_clientid_busy;
2150 goto out;
2151 }
2152 } else if (unconf)
2153 clp = unconf;
2154 else {
2155 status = nfserr_stale_clientid;
2156 goto out;
2157 }
2158
2159 expire_client(clp);
2160out:
2161 nfs4_unlock_state();
2162 dprintk("%s return %d\n", __func__, ntohl(status));
2163 return status;
2164}
2165
Andy Adamson069b6ad2009-04-03 08:27:58 +03002166__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002167nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2168{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002169 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002170
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002171 if (rc->rca_one_fs) {
2172 if (!cstate->current_fh.fh_dentry)
2173 return nfserr_nofilehandle;
2174 /*
2175 * We don't take advantage of the rca_one_fs case.
2176 * That's OK, it's optional, we can safely ignore it.
2177 */
2178 return nfs_ok;
2179 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002180
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002181 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002182 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002183 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2184 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002185 goto out;
2186
2187 status = nfserr_stale_clientid;
2188 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002189 /*
2190 * The following error isn't really legal.
2191 * But we only get here if the client just explicitly
2192 * destroyed the client. Surely it no longer cares what
2193 * error it gets back on an operation for the dead
2194 * client.
2195 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002196 goto out;
2197
2198 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002199 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002200out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002201 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002202 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002203}
2204
2205__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002206nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2207 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002209 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002211 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002212 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002213 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2214
J. Bruce Fields63db4632012-05-18 21:54:19 -04002215 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002217 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002218 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002219 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002221 if (clp_used_exchangeid(conf))
2222 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002223 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002224 char addr_str[INET6_ADDRSTRLEN];
2225 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2226 sizeof(addr_str));
2227 dprintk("NFSD: setclientid: string in use by client "
2228 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 goto out;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002232 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002233 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002236 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002237 if (new == NULL)
2238 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002239 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002240 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002242 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002243 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002244 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002245 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002246 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2248 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2249 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2250 status = nfs_ok;
2251out:
2252 nfs4_unlock_state();
2253 return status;
2254}
2255
2256
Al Virob37ad282006-10-19 23:28:59 -07002257__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002258nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2259 struct nfsd4_compound_state *cstate,
2260 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
NeilBrown21ab45a2005-06-23 22:04:14 -07002262 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2264 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002265 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002266 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002268 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002271
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002272 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002273 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002274 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002275 * We try hard to give out unique clientid's, so if we get an
2276 * attempt to confirm the same clientid with a different cred,
2277 * there's a bug somewhere. Let's charitably assume it's our
2278 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002279 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002280 status = nfserr_serverfault;
2281 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2282 goto out;
2283 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2284 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002285 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002286 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2287 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002289 else /* case 4: client hasn't noticed we rebooted yet? */
2290 status = nfserr_stale_clientid;
2291 goto out;
2292 }
2293 status = nfs_ok;
2294 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002295 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2296 nfsd4_probe_callback(conf);
2297 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002298 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002299 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002300 if (conf)
J. Bruce Fields8695b902012-05-19 10:05:58 -04002301 expire_client(conf);
J. Bruce Fields8695b902012-05-19 10:05:58 -04002302 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002303 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 nfs4_unlock_state();
2307 return status;
2308}
2309
J. Bruce Fields32513b42011-10-13 16:00:16 -04002310static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002312 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2313}
2314
2315/* OPEN Share state helper functions */
2316static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 unsigned int hashval = file_hashval(ino);
2319
J. Bruce Fields32513b42011-10-13 16:00:16 -04002320 atomic_set(&fp->fi_ref, 1);
2321 INIT_LIST_HEAD(&fp->fi_hash);
2322 INIT_LIST_HEAD(&fp->fi_stateids);
2323 INIT_LIST_HEAD(&fp->fi_delegations);
2324 fp->fi_inode = igrab(ino);
2325 fp->fi_had_conflict = false;
2326 fp->fi_lease = NULL;
2327 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2328 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2329 spin_lock(&recall_lock);
2330 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2331 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002335nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002336{
NeilBrowne60d4392005-06-23 22:03:01 -07002337 if (*slab == NULL)
2338 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002339 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002340 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002341}
2342
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002343void
NeilBrowne60d4392005-06-23 22:03:01 -07002344nfsd4_free_slabs(void)
2345{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002346 nfsd4_free_slab(&openowner_slab);
2347 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002348 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002349 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002350 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Bryan Schumaker72083392011-11-01 15:24:59 -04002353int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354nfsd4_init_slabs(void)
2355{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002356 openowner_slab = kmem_cache_create("nfsd4_openowners",
2357 sizeof(struct nfs4_openowner), 0, 0, NULL);
2358 if (openowner_slab == NULL)
2359 goto out_nomem;
2360 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002361 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002362 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002363 goto out_nomem;
2364 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002365 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002366 if (file_slab == NULL)
2367 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002368 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002369 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002370 if (stateid_slab == NULL)
2371 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002372 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002373 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002374 if (deleg_slab == NULL)
2375 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002377out_nomem:
2378 nfsd4_free_slabs();
2379 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2380 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002383void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002385 kfree(oo->oo_owner.so_owner.data);
2386 kmem_cache_free(openowner_slab, oo);
2387}
2388
2389void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2390{
2391 kfree(lo->lo_owner.so_owner.data);
2392 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002395static void init_nfs4_replay(struct nfs4_replay *rp)
2396{
2397 rp->rp_status = nfserr_serverfault;
2398 rp->rp_buflen = 0;
2399 rp->rp_buf = rp->rp_ibuf;
2400}
2401
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002402static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 struct nfs4_stateowner *sop;
2405
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002406 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002407 if (!sop)
2408 return NULL;
2409
2410 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2411 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002412 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002413 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002415 sop->so_owner.len = owner->len;
2416
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002417 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002418 sop->so_client = clp;
2419 init_nfs4_replay(&sop->so_replay);
2420 return sop;
2421}
2422
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002423static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002424{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002425 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2426
2427 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002428 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002431static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002433 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002435 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2436 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002438 oo->oo_owner.so_is_open_owner = 1;
2439 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002440 oo->oo_flags = NFS4_OO_NEW;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002441 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002442 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002443 INIT_LIST_HEAD(&oo->oo_close_lru);
2444 hash_openowner(oo, clp, strhashval);
2445 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
J. Bruce Fields996e0932011-10-17 11:14:48 -04002448static 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 -04002449 struct nfs4_openowner *oo = open->op_openowner;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04002450 struct nfs4_client *clp = oo->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
J. Bruce Fields996e0932011-10-17 11:14:48 -04002452 init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
NeilBrownea1da632005-06-23 22:04:17 -07002453 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002454 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002455 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002456 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002457 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 stp->st_access_bmap = 0;
2460 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002461 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002462 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002463 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
2466static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002467move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002469 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2470
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002471 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002473 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002474 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002478same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2479 clientid_t *clid)
2480{
2481 return (sop->so_owner.len == owner->len) &&
2482 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2483 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002486static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002487find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2488 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002490 struct nfs4_stateowner *so;
2491 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002492 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002494 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002495 if (!so->so_is_open_owner)
2496 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002497 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2498 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002499 clp = oo->oo_owner.so_client;
2500 if ((bool)clp->cl_minorversion != sessions)
2501 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002502 renew_client(oo->oo_owner.so_client);
2503 return oo;
2504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506 return NULL;
2507}
2508
2509/* search file_hashtbl[] for file */
2510static struct nfs4_file *
2511find_file(struct inode *ino)
2512{
2513 unsigned int hashval = file_hashval(ino);
2514 struct nfs4_file *fp;
2515
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002516 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002518 if (fp->fi_inode == ino) {
2519 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002520 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002524 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return NULL;
2526}
2527
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002528/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 * Called to check deny when READ with all zero stateid or
2530 * WRITE with all zero or all one stateid
2531 */
Al Virob37ad282006-10-19 23:28:59 -07002532static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2534{
2535 struct inode *ino = current_fh->fh_dentry->d_inode;
2536 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002537 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002538 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 dprintk("NFSD: nfs4_share_conflict\n");
2541
2542 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002543 if (!fp)
2544 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002545 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002547 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002548 if (test_deny(deny_type, stp) ||
2549 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
NeilBrown13cd2182005-06-23 22:03:10 -07002552 ret = nfs_ok;
2553out:
2554 put_nfs4_file(fp);
2555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002558static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 /* We're assuming the state code never drops its reference
2561 * without first removing the lease. Since we're in this lease
2562 * callback (and since the lease code is serialized by the kernel
2563 * lock) we know the server hasn't removed the lease yet, we know
2564 * it's safe to take a reference: */
2565 atomic_inc(&dp->dl_count);
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Arnd Bergmann460781b2010-11-17 16:26:56 +01002569 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 dp->dl_time = get_seconds();
2571
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002572 nfsd4_cb_recall(dp);
2573}
2574
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002575/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002576static void nfsd_break_deleg_cb(struct file_lock *fl)
2577{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002578 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2579 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002580
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002581 if (!fp) {
2582 WARN(1, "(%p)->fl_owner NULL\n", fl);
2583 return;
2584 }
2585 if (fp->fi_had_conflict) {
2586 WARN(1, "duplicate break on %p\n", fp);
2587 return;
2588 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002589 /*
2590 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002591 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002592 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002593 */
2594 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002596 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002597 fp->fi_had_conflict = true;
2598 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2599 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002600 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603static
2604int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2605{
2606 if (arg & F_UNLCK)
2607 return lease_modify(onlist, arg);
2608 else
2609 return -EAGAIN;
2610}
2611
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002612static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002613 .lm_break = nfsd_break_deleg_cb,
2614 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615};
2616
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002617static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2618{
2619 if (nfsd4_has_session(cstate))
2620 return nfs_ok;
2621 if (seqid == so->so_seqid - 1)
2622 return nfserr_replay_me;
2623 if (seqid == so->so_seqid)
2624 return nfs_ok;
2625 return nfserr_bad_seqid;
2626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Al Virob37ad282006-10-19 23:28:59 -07002628__be32
Andy Adamson66689582009-04-03 08:28:45 +03002629nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03002630 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 clientid_t *clientid = &open->op_clientid;
2633 struct nfs4_client *clp = NULL;
2634 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002635 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002636 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002638 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002640 /*
2641 * In case we need it later, after we've already created the
2642 * file and don't want to risk a further failure:
2643 */
2644 open->op_file = nfsd4_alloc_file();
2645 if (open->op_file == NULL)
2646 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002648 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002649 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002650 open->op_openowner = oo;
2651 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002652 clp = find_confirmed_client(clientid, cstate->minorversion,
2653 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002655 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002656 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002658 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002659 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002660 clp = oo->oo_owner.so_client;
2661 release_openowner(oo);
2662 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002663 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002664 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002665 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2666 if (status)
2667 return status;
2668 clp = oo->oo_owner.so_client;
2669 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002670new_owner:
2671 oo = alloc_init_open_stateowner(strhashval, clp, open);
2672 if (oo == NULL)
2673 return nfserr_jukebox;
2674 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002675alloc_stateid:
2676 open->op_stp = nfs4_alloc_stateid(clp);
2677 if (!open->op_stp)
2678 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002679 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Al Virob37ad282006-10-19 23:28:59 -07002682static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002683nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2684{
2685 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2686 return nfserr_openmode;
2687 else
2688 return nfs_ok;
2689}
2690
Daniel Mackc47d8322011-05-16 16:38:14 +02002691static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002692{
J. Bruce Fields24a01112010-05-18 20:01:35 -04002693 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2694}
2695
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002696static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002697{
2698 struct nfs4_stid *ret;
2699
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002700 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002701 if (!ret)
2702 return NULL;
2703 return delegstateid(ret);
2704}
2705
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002706static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2707{
2708 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2709 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2710}
2711
Al Virob37ad282006-10-19 23:28:59 -07002712static __be32
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002713nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07002714 struct nfs4_delegation **dp)
2715{
2716 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002717 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002718
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002719 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002720 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002721 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002722 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002723 status = nfs4_check_delegmode(*dp, flags);
2724 if (status)
2725 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002726out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002727 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07002728 return nfs_ok;
2729 if (status)
2730 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002731 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002732 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002733}
2734
Al Virob37ad282006-10-19 23:28:59 -07002735static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002736nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002738 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002739 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
NeilBrown8beefa22005-06-23 22:03:08 -07002741 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* ignore lock owners */
2743 if (local->st_stateowner->so_is_open_owner == 0)
2744 continue;
2745 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002746 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 *stpp = local;
2748 /* check for conflicting share reservations */
2749 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002750 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002752 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002755static inline int nfs4_access_to_access(u32 nfs4_access)
2756{
2757 int flags = 0;
2758
2759 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2760 flags |= NFSD_MAY_READ;
2761 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2762 flags |= NFSD_MAY_WRITE;
2763 return flags;
2764}
2765
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002766static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2767 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002768{
2769 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002770 int oflag = nfs4_access_to_omode(open->op_share_access);
2771 int access = nfs4_access_to_access(open->op_share_access);
2772
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002773 if (!fp->fi_fds[oflag]) {
2774 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2775 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002776 if (status)
2777 return status;
2778 }
2779 nfs4_file_get_access(fp, oflag);
2780
2781 return nfs_ok;
2782}
2783
Al Virob37ad282006-10-19 23:28:59 -07002784static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2786 struct nfsd4_open *open)
2787{
2788 struct iattr iattr = {
2789 .ia_valid = ATTR_SIZE,
2790 .ia_size = 0,
2791 };
2792 if (!open->op_truncate)
2793 return 0;
2794 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002795 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2797}
2798
Al Virob37ad282006-10-19 23:28:59 -07002799static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002800nfs4_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 -07002801{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002802 u32 op_share_access = open->op_share_access;
J. Bruce Fields7d947842010-08-20 18:09:31 -04002803 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002804 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Jeff Layton82c5ff12012-05-11 09:45:13 -04002806 new_access = !test_access(op_share_access, stp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002807 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002808 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002809 if (status)
2810 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 status = nfsd4_truncate(rqstp, cur_fh, open);
2813 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002814 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002815 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002816 nfs4_file_put_access(fp, oflag);
2817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return status;
2819 }
2820 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04002821 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002822 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 return nfs_ok;
2825}
2826
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05002829nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002831 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002834/* Should we give out recallable state?: */
2835static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2836{
2837 if (clp->cl_cb_state == NFSD4_CB_UP)
2838 return true;
2839 /*
2840 * In the sessions case, since we don't have to establish a
2841 * separate connection for callbacks, we assume it's OK
2842 * until we hear otherwise:
2843 */
2844 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2845}
2846
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002847static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2848{
2849 struct file_lock *fl;
2850
2851 fl = locks_alloc_lock();
2852 if (!fl)
2853 return NULL;
2854 locks_init_lock(fl);
2855 fl->fl_lmops = &nfsd_lease_mng_ops;
2856 fl->fl_flags = FL_LEASE;
2857 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2858 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002859 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002860 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002861 return fl;
2862}
2863
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002864static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2865{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002866 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002867 struct file_lock *fl;
2868 int status;
2869
2870 fl = nfs4_alloc_init_lease(dp, flag);
2871 if (!fl)
2872 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002873 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002874 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002875 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002876 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002877 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002878 locks_free_lock(fl);
2879 return -ENOMEM;
2880 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002881 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04002882 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002883 atomic_set(&fp->fi_delegees, 1);
2884 list_add(&dp->dl_perfile, &fp->fi_delegations);
2885 return 0;
2886}
2887
2888static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2889{
2890 struct nfs4_file *fp = dp->dl_file;
2891
2892 if (!fp->fi_lease)
2893 return nfs4_setlease(dp, flag);
2894 spin_lock(&recall_lock);
2895 if (fp->fi_had_conflict) {
2896 spin_unlock(&recall_lock);
2897 return -EAGAIN;
2898 }
2899 atomic_inc(&fp->fi_delegees);
2900 list_add(&dp->dl_perfile, &fp->fi_delegations);
2901 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002902 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002903 return 0;
2904}
2905
Benny Halevy4aa89132012-02-21 14:16:44 -08002906static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2907{
2908 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2909 if (status == -EAGAIN)
2910 open->op_why_no_deleg = WND4_CONTENTION;
2911 else {
2912 open->op_why_no_deleg = WND4_RESOURCE;
2913 switch (open->op_deleg_want) {
2914 case NFS4_SHARE_WANT_READ_DELEG:
2915 case NFS4_SHARE_WANT_WRITE_DELEG:
2916 case NFS4_SHARE_WANT_ANY_DELEG:
2917 break;
2918 case NFS4_SHARE_WANT_CANCEL:
2919 open->op_why_no_deleg = WND4_CANCELLED;
2920 break;
2921 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002922 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08002923 }
2924 }
2925}
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927/*
2928 * Attempt to hand out a delegation.
2929 */
2930static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002931nfs4_open_delegation(struct net *net, struct svc_fh *fh,
2932 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
2934 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002935 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002936 int cb_up;
Benny Halevyd24433c2012-02-16 20:57:17 +02002937 int status = 0, flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002939 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002941 open->op_recall = 0;
2942 switch (open->op_claim_type) {
2943 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002944 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002945 open->op_recall = 1;
2946 flag = open->op_delegate_type;
2947 if (flag == NFS4_OPEN_DELEGATE_NONE)
2948 goto out;
2949 break;
2950 case NFS4_OPEN_CLAIM_NULL:
2951 /* Let's not give out any delegations till everyone's
2952 * had the chance to reclaim theirs.... */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002953 if (locks_in_grace(net))
NeilBrown7b190fe2005-06-23 22:03:23 -07002954 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002955 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002956 goto out;
2957 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2958 flag = NFS4_OPEN_DELEGATE_WRITE;
2959 else
2960 flag = NFS4_OPEN_DELEGATE_READ;
2961 break;
2962 default:
2963 goto out;
2964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002966 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002967 if (dp == NULL)
2968 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002969 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002970 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002971 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002973 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002975 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002976 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977out:
2978 open->op_delegate_type = flag;
Benny Halevyd24433c2012-02-16 20:57:17 +02002979 if (flag == NFS4_OPEN_DELEGATE_NONE) {
2980 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
2981 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2982 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2983
Benny Halevy4aa89132012-02-21 14:16:44 -08002984 /* 4.1 client asking for a delegation? */
2985 if (open->op_deleg_want)
2986 nfsd4_open_deleg_none_ext(open, status);
Benny Halevyd24433c2012-02-16 20:57:17 +02002987 }
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002988 return;
2989out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002990 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002991out_no_deleg:
2992 flag = NFS4_OPEN_DELEGATE_NONE;
2993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994}
2995
Benny Halevye27f49c2012-02-21 14:16:54 -08002996static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
2997 struct nfs4_delegation *dp)
2998{
2999 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3000 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3001 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3002 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3003 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3004 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3005 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3006 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3007 }
3008 /* Otherwise the client must be confused wanting a delegation
3009 * it already has, therefore we don't return
3010 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3011 */
3012}
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014/*
3015 * called with nfs4_lock_state() held.
3016 */
Al Virob37ad282006-10-19 23:28:59 -07003017__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3019{
Andy Adamson66689582009-04-03 08:28:45 +03003020 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003021 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 struct nfs4_file *fp = NULL;
3023 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003024 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003025 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003026 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 /*
3029 * Lookup file; if found, lookup stateid and check open request,
3030 * and check for delegations in the process of being recalled.
3031 * If not found, create the nfs4_file struct
3032 */
3033 fp = find_file(ino);
3034 if (fp) {
3035 if ((status = nfs4_check_open(fp, open, &stp)))
3036 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003037 status = nfs4_check_deleg(cl, fp, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003038 if (status)
3039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07003041 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003042 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003043 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003044 status = nfserr_jukebox;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003045 fp = open->op_file;
3046 open->op_file = NULL;
3047 nfsd4_init_file(fp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049
3050 /*
3051 * OPEN the file, or upgrade an existing OPEN.
3052 * If truncate fails, the OPEN fails.
3053 */
3054 if (stp) {
3055 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003056 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (status)
3058 goto out;
3059 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003060 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003061 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 goto out;
J. Bruce Fields4af82502012-06-07 17:30:45 -04003063 status = nfsd4_truncate(rqstp, current_fh, open);
3064 if (status)
3065 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003066 stp = open->op_stp;
3067 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003068 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003070 update_stateid(&stp->st_stid.sc_stateid);
3071 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Benny Halevyd24433c2012-02-16 20:57:17 +02003073 if (nfsd4_has_session(&resp->cstate)) {
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003074 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03003075
Benny Halevyd24433c2012-02-16 20:57:17 +02003076 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3077 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3078 open->op_why_no_deleg = WND4_NOT_WANTED;
3079 goto nodeleg;
3080 }
3081 }
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 /*
3084 * Attempt to hand out a delegation. No error return, because the
3085 * OPEN succeeds even if we fail.
3086 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003087 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003088nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 status = nfs_ok;
3090
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003091 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003092 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003094 /* 4.1 client trying to upgrade/downgrade delegation? */
3095 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003096 open->op_deleg_want)
3097 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003098
NeilBrown13cd2182005-06-23 22:03:10 -07003099 if (fp)
3100 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003101 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003102 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 /*
3104 * To finish the open response, we just need to set the rflags.
3105 */
3106 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003107 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003108 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3110
3111 return status;
3112}
3113
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003114void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3115{
3116 if (open->op_openowner) {
3117 struct nfs4_openowner *oo = open->op_openowner;
3118
3119 if (!list_empty(&oo->oo_owner.so_stateids))
3120 list_del_init(&oo->oo_close_lru);
3121 if (oo->oo_flags & NFS4_OO_NEW) {
3122 if (status) {
3123 release_openowner(oo);
3124 open->op_openowner = NULL;
3125 } else
3126 oo->oo_flags &= ~NFS4_OO_NEW;
3127 }
3128 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003129 if (open->op_file)
3130 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003131 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003132 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003133}
3134
Al Virob37ad282006-10-19 23:28:59 -07003135__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003136nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3137 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
3139 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003140 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003141 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
3143 nfs4_lock_state();
3144 dprintk("process_renew(%08x/%08x): starting\n",
3145 clid->cl_boot, clid->cl_id);
3146 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003147 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 goto out;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003149 clp = find_confirmed_client(clid, cstate->minorversion, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 status = nfserr_expired;
3151 if (clp == NULL) {
3152 /* We assume the client took too long to RENEW. */
3153 dprintk("nfsd4_renew: clientid not found!\n");
3154 goto out;
3155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003157 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003158 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 goto out;
3160 status = nfs_ok;
3161out:
3162 nfs4_unlock_state();
3163 return status;
3164}
3165
NeilBrowna76b4312005-06-23 22:04:01 -07003166static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003167nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003168{
Jeff Layton33dcc482012-04-10 11:08:48 -04003169 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003170 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003171 return;
3172
NeilBrowna76b4312005-06-23 22:04:01 -07003173 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003174 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003175 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003176 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003177 /*
3178 * Now that every NFSv4 client has had the chance to recover and
3179 * to see the (possibly new, possibly shorter) lease time, we
3180 * can safely set the next grace time to the current lease time:
3181 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003182 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003183}
3184
NeilBrownfd39ca92005-06-23 22:04:03 -07003185static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003186nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003189 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 struct nfs4_delegation *dp;
3191 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003192 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3193 time_t t, clientid_val = nn->nfsd4_lease;
3194 time_t u, test_val = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 nfs4_lock_state();
3197
3198 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003199 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003200 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003201 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003202 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 clp = list_entry(pos, struct nfs4_client, cl_lru);
3204 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3205 t = clp->cl_time - cutoff;
3206 if (clientid_val > t)
3207 clientid_val = t;
3208 break;
3209 }
Benny Halevyd7682982010-05-12 00:13:54 +03003210 if (atomic_read(&clp->cl_refcount)) {
3211 dprintk("NFSD: client in use (clientid %08x)\n",
3212 clp->cl_clientid.cl_id);
3213 continue;
3214 }
3215 unhash_client_locked(clp);
3216 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003217 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003218 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003219 list_for_each_safe(pos, next, &reaplist) {
3220 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 dprintk("NFSD: purging unused client (clientid %08x)\n",
3222 clp->cl_clientid.cl_id);
3223 expire_client(clp);
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 spin_lock(&recall_lock);
3226 list_for_each_safe(pos, next, &del_recall_lru) {
3227 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003228 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3229 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3231 u = dp->dl_time - cutoff;
3232 if (test_val > u)
3233 test_val = u;
3234 break;
3235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 list_move(&dp->dl_recall_lru, &reaplist);
3237 }
3238 spin_unlock(&recall_lock);
3239 list_for_each_safe(pos, next, &reaplist) {
3240 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 unhash_delegation(dp);
3242 }
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003243 test_val = nn->nfsd4_lease;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003244 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003245 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3246 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3247 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (test_val > u)
3249 test_val = u;
3250 break;
3251 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003252 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
3254 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3255 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3256 nfs4_unlock_state();
3257 return clientid_val;
3258}
3259
Harvey Harrisona254b242008-02-20 12:49:00 -08003260static struct workqueue_struct *laundry_wq;
3261static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003262
3263static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003264laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
3266 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003267 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3268 work);
3269 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3270 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003272 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003274 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003277static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003278{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003279 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3280 return nfserr_bad_stateid;
3281 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
3284static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003285STALE_STATEID(stateid_t *stateid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003287 if (stateid->si_opaque.so_clid.cl_boot == nn->boot_time)
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003288 return 0;
3289 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003290 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003291 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
3294static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003295access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003297 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3298 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3299 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
3302static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003303access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003305 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3306 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
3309static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003310__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Al Virob37ad282006-10-19 23:28:59 -07003312 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
J. Bruce Fields02921912010-07-29 15:16:59 -04003314 /* For lock stateid's, we test the parent open, not the lock: */
3315 if (stp->st_openstp)
3316 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003317 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003319 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 goto out;
3321 status = nfs_ok;
3322out:
3323 return status;
3324}
3325
Al Virob37ad282006-10-19 23:28:59 -07003326static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003327check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003329 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003331 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003332 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 * conflicting state; so we must wait out the grace period. */
3334 return nfserr_grace;
3335 } else if (flags & WR_STATE)
3336 return nfs4_share_conflict(current_fh,
3337 NFS4_SHARE_DENY_WRITE);
3338 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3339 return nfs4_share_conflict(current_fh,
3340 NFS4_SHARE_DENY_READ);
3341}
3342
3343/*
3344 * Allow READ/WRITE during grace period on recovered state only for files
3345 * that are not able to provide mandatory locking.
3346 */
3347static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003348grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003350 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
J. Bruce Fields81b82962011-08-23 11:03:29 -04003353/* Returns true iff a is later than b: */
3354static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3355{
3356 return (s32)a->si_generation - (s32)b->si_generation > 0;
3357}
3358
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003359static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003360{
Andy Adamson66689582009-04-03 08:28:45 +03003361 /*
3362 * When sessions are used the stateid generation number is ignored
3363 * when it is zero.
3364 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003365 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003366 return nfs_ok;
3367
3368 if (in->si_generation == ref->si_generation)
3369 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003370
J. Bruce Fields0836f582008-01-26 19:08:12 -05003371 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003372 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003373 return nfserr_bad_stateid;
3374 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003375 * However, we could see a stateid from the past, even from a
3376 * non-buggy client. For example, if the client sends a lock
3377 * while some IO is outstanding, the lock may bump si_generation
3378 * while the IO is still in flight. The client could avoid that
3379 * situation by waiting for responses on all the IO requests,
3380 * but better performance may result in retrying IO that
3381 * receives an old_stateid error if requests are rarely
3382 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003383 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003384 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003385}
3386
Chuck Lever7df302f2012-05-29 13:56:37 -04003387static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003388{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003389 struct nfs4_stid *s;
3390 struct nfs4_ol_stateid *ols;
3391 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003392
Chuck Lever7df302f2012-05-29 13:56:37 -04003393 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3394 return nfserr_bad_stateid;
3395 /* Client debugging aid. */
3396 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3397 char addr_str[INET6_ADDRSTRLEN];
3398 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3399 sizeof(addr_str));
3400 pr_warn_ratelimited("NFSD: client %s testing state ID "
3401 "with incorrect client ID\n", addr_str);
3402 return nfserr_bad_stateid;
3403 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003404 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003405 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003406 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003407 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003408 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003409 return status;
3410 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3411 return nfs_ok;
3412 ols = openlockstateid(s);
3413 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003414 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003415 return nfserr_bad_stateid;
3416 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003417}
3418
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003419static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3420 struct nfs4_stid **s, bool sessions,
3421 struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003422{
3423 struct nfs4_client *cl;
3424
3425 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3426 return nfserr_bad_stateid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003427 if (STALE_STATEID(stateid, nn))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003428 return nfserr_stale_stateid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003429 cl = find_confirmed_client(&stateid->si_opaque.so_clid, sessions, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003430 if (!cl)
3431 return nfserr_expired;
3432 *s = find_stateid_by_type(cl, stateid, typemask);
3433 if (!*s)
3434 return nfserr_bad_stateid;
3435 return nfs_ok;
3436
3437}
3438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439/*
3440* Checks for stateid operations
3441*/
Al Virob37ad282006-10-19 23:28:59 -07003442__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003443nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003444 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003446 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003447 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003449 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003451 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Al Virob37ad282006-10-19 23:28:59 -07003452 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 if (filpp)
3455 *filpp = NULL;
3456
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003457 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return nfserr_grace;
3459
3460 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003461 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003463 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3464 &s, cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003465 if (status)
3466 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003467 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3468 if (status)
3469 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003470 switch (s->sc_type) {
3471 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003472 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003473 status = nfs4_check_delegmode(dp, flags);
3474 if (status)
3475 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003476 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003477 *filpp = dp->dl_file->fi_deleg_file;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003478 if (!*filpp) {
3479 WARN_ON_ONCE(1);
3480 status = nfserr_serverfault;
3481 goto out;
3482 }
Dan Carpenter43b01782010-10-27 23:19:04 +02003483 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003484 break;
3485 case NFS4_OPEN_STID:
3486 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003487 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003488 status = nfs4_check_fh(current_fh, stp);
3489 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003491 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003492 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003494 status = nfs4_check_openmode(stp, flags);
3495 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003497 if (filpp) {
3498 if (flags & RD_STATE)
3499 *filpp = find_readable_file(stp->st_file);
3500 else
3501 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003502 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003503 break;
3504 default:
3505 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507 status = nfs_ok;
3508out:
3509 return status;
3510}
3511
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003512static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003513nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003514{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003515 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003516 return nfserr_locks_held;
3517 release_lock_stateid(stp);
3518 return nfs_ok;
3519}
3520
3521/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003522 * Test if the stateid is valid
3523 */
3524__be32
3525nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3526 struct nfsd4_test_stateid *test_stateid)
3527{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003528 struct nfsd4_test_stateid_id *stateid;
3529 struct nfs4_client *cl = cstate->session->se_client;
3530
3531 nfs4_lock_state();
3532 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003533 stateid->ts_id_status =
3534 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003535 nfs4_unlock_state();
3536
Bryan Schumaker17456802011-07-13 10:50:48 -04003537 return nfs_ok;
3538}
3539
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003540__be32
3541nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3542 struct nfsd4_free_stateid *free_stateid)
3543{
3544 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003545 struct nfs4_stid *s;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003546 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003547 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003548
3549 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003550 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003551 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003552 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003553 switch (s->sc_type) {
3554 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003555 ret = nfserr_locks_held;
3556 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003557 case NFS4_OPEN_STID:
3558 case NFS4_LOCK_STID:
3559 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3560 if (ret)
3561 goto out;
3562 if (s->sc_type == NFS4_LOCK_STID)
3563 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3564 else
3565 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003566 break;
3567 default:
3568 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003569 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003570out:
3571 nfs4_unlock_state();
3572 return ret;
3573}
3574
NeilBrown4c4cd222005-07-07 17:59:27 -07003575static inline int
3576setlkflg (int type)
3577{
3578 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3579 RD_STATE : WR_STATE;
3580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003582static __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 -04003583{
3584 struct svc_fh *current_fh = &cstate->current_fh;
3585 struct nfs4_stateowner *sop = stp->st_stateowner;
3586 __be32 status;
3587
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003588 status = nfsd4_check_seqid(cstate, sop, seqid);
3589 if (status)
3590 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003591 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3592 /*
3593 * "Closed" stateid's exist *only* to return
3594 * nfserr_replay_me from the previous step.
3595 */
3596 return nfserr_bad_stateid;
3597 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3598 if (status)
3599 return status;
3600 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003601}
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603/*
3604 * Checks for sequence id mutating operations.
3605 */
Al Virob37ad282006-10-19 23:28:59 -07003606static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003607nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003608 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003609 struct nfs4_ol_stateid **stpp,
3610 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003612 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003613 struct nfs4_stid *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003615 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3616 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 *stpp = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003619 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3620 cstate->minorversion, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003621 if (status)
3622 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003623 *stpp = openlockstateid(s);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003624 cstate->replay_owner = (*stpp)->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003626 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3627}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003628
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003629static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3630 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003631{
3632 __be32 status;
3633 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003635 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003636 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003637 if (status)
3638 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003639 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003640 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003641 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003642 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
Al Virob37ad282006-10-19 23:28:59 -07003645__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003646nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003647 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
Al Virob37ad282006-10-19 23:28:59 -07003649 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003650 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003651 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003652 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
3654 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003655 (int)cstate->current_fh.fh_dentry->d_name.len,
3656 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003658 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003659 if (status)
3660 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
3662 nfs4_lock_state();
3663
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003664 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003665 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003666 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003667 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003668 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003669 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003670 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003671 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003672 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003673 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003674 update_stateid(&stp->st_stid.sc_stateid);
3675 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003676 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003677 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003678
Jeff Layton2a4317c2012-03-21 16:42:43 -04003679 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003680 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003682 if (!cstate->replay_owner)
3683 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 return status;
3685}
3686
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003687static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003689 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003690 return;
3691 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04003692 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003693}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003694
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003695static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3696{
3697 switch (to_access) {
3698 case NFS4_SHARE_ACCESS_READ:
3699 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3700 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3701 break;
3702 case NFS4_SHARE_ACCESS_WRITE:
3703 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3704 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3705 break;
3706 case NFS4_SHARE_ACCESS_BOTH:
3707 break;
3708 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003709 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 }
3711}
3712
3713static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04003714reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
3716 int i;
3717 for (i = 0; i < 4; i++) {
3718 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04003719 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
3721}
3722
Al Virob37ad282006-10-19 23:28:59 -07003723__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003724nfsd4_open_downgrade(struct svc_rqst *rqstp,
3725 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003726 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
Al Virob37ad282006-10-19 23:28:59 -07003728 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003729 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003730 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
3732 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003733 (int)cstate->current_fh.fh_dentry->d_name.len,
3734 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04003736 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02003737 if (od->od_deleg_want)
3738 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3739 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
3741 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003742 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003743 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003744 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003747 if (!test_access(od->od_share_access, stp)) {
3748 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 stp->st_access_bmap, od->od_share_access);
3750 goto out;
3751 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04003752 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3754 stp->st_deny_bmap, od->od_share_deny);
3755 goto out;
3756 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003757 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Jeff Laytonce0fc432012-05-11 09:45:14 -04003759 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003761 update_stateid(&stp->st_stid.sc_stateid);
3762 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 status = nfs_ok;
3764out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003765 if (!cstate->replay_owner)
3766 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 return status;
3768}
3769
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003770void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3771{
3772 struct nfs4_openowner *oo;
3773 struct nfs4_ol_stateid *s;
3774
3775 if (!so->so_is_open_owner)
3776 return;
3777 oo = openowner(so);
3778 s = oo->oo_last_closed_stid;
3779 if (!s)
3780 return;
3781 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3782 /* Release the last_closed_stid on the next seqid bump: */
3783 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3784 return;
3785 }
3786 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003787 release_last_closed_stateid(oo);
3788}
3789
3790static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3791{
3792 unhash_open_stateid(s);
3793 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003794}
3795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796/*
3797 * nfs4_unlock_state() called after encode
3798 */
Al Virob37ad282006-10-19 23:28:59 -07003799__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003800nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003801 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
Al Virob37ad282006-10-19 23:28:59 -07003803 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003804 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003805 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003806 struct net *net = SVC_NET(rqstp);
3807 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
3809 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003810 (int)cstate->current_fh.fh_dentry->d_name.len,
3811 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003814 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3815 &close->cl_stateid,
3816 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003817 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003818 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003820 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003822 update_stateid(&stp->st_stid.sc_stateid);
3823 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003825 nfsd4_close_open_stateid(stp);
J. Bruce Fieldscf9182e2012-08-29 15:21:58 -07003826 release_last_closed_stateid(oo);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003827 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003828
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003829 if (list_empty(&oo->oo_owner.so_stateids)) {
3830 if (cstate->minorversion) {
3831 release_openowner(oo);
3832 cstate->replay_owner = NULL;
3833 } else {
3834 /*
3835 * In the 4.0 case we need to keep the owners around a
3836 * little while to handle CLOSE replay.
3837 */
3838 if (list_empty(&oo->oo_owner.so_stateids))
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003839 move_to_close_lru(oo, SVC_NET(rqstp));
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003840 }
3841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003843 if (!cstate->replay_owner)
3844 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 return status;
3846}
3847
Al Virob37ad282006-10-19 23:28:59 -07003848__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003849nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3850 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003852 struct nfs4_delegation *dp;
3853 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003854 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07003855 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003856 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003858 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003859 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
3861 nfs4_lock_state();
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003862 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3863 cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003864 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003865 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003866 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003867 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003868 if (status)
3869 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003870
3871 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003873 nfs4_unlock_state();
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 return status;
3876}
3877
3878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
J. Bruce Fields009673b2011-11-07 17:40:10 -05003881#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Benny Halevy87df4de2008-12-15 19:42:03 +02003883static inline u64
3884end_offset(u64 start, u64 len)
3885{
3886 u64 end;
3887
3888 end = start + len;
3889 return end >= start ? end: NFS4_MAX_UINT64;
3890}
3891
3892/* last octet in a range */
3893static inline u64
3894last_byte_offset(u64 start, u64 len)
3895{
3896 u64 end;
3897
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003898 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02003899 end = start + len;
3900 return end > start ? end - 1: NFS4_MAX_UINT64;
3901}
3902
J. Bruce Fields009673b2011-11-07 17:40:10 -05003903static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904{
3905 return (file_hashval(inode) + cl_id
3906 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05003907 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910/*
3911 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3912 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3913 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3914 * locking, this prevents us from being completely protocol-compliant. The
3915 * real solution to this problem is to start using unsigned file offsets in
3916 * the VFS, but this is a very deep change!
3917 */
3918static inline void
3919nfs4_transform_lock_offset(struct file_lock *lock)
3920{
3921 if (lock->fl_start < 0)
3922 lock->fl_start = OFFSET_MAX;
3923 if (lock->fl_end < 0)
3924 lock->fl_end = OFFSET_MAX;
3925}
3926
NeilBrownd5b90262006-04-10 22:55:22 -07003927/* Hack!: For now, we're defining this just so we can use a pointer to it
3928 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003929static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003930};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
3932static inline void
3933nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3934{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003935 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
NeilBrownd5b90262006-04-10 22:55:22 -07003937 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003938 lo = (struct nfs4_lockowner *) fl->fl_owner;
3939 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3940 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003941 if (!deny->ld_owner.data)
3942 /* We just don't care that much */
3943 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003944 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3945 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003946 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003947nevermind:
3948 deny->ld_owner.len = 0;
3949 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003950 deny->ld_clientid.cl_boot = 0;
3951 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
3953 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003954 deny->ld_length = NFS4_MAX_UINT64;
3955 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3957 deny->ld_type = NFS4_READ_LT;
3958 if (fl->fl_type != F_RDLCK)
3959 deny->ld_type = NFS4_WRITE_LT;
3960}
3961
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003962static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3963{
3964 struct nfs4_ol_stateid *lst;
3965
3966 if (!same_owner_str(&lo->lo_owner, owner, clid))
3967 return false;
3968 lst = list_first_entry(&lo->lo_owner.so_stateids,
3969 struct nfs4_ol_stateid, st_perstateowner);
3970 return lst->st_file->fi_inode == inode;
3971}
3972
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003973static struct nfs4_lockowner *
3974find_lockowner_str(struct inode *inode, clientid_t *clid,
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03003975 struct xdr_netobj *owner, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976{
J. Bruce Fields009673b2011-11-07 17:40:10 -05003977 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003978 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03003980 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003981 if (same_lockowner_ino(lo, inode, clid, owner))
3982 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 }
3984 return NULL;
3985}
3986
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003987static 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 -04003988{
J. Bruce Fields009673b2011-11-07 17:40:10 -05003989 struct inode *inode = open_stp->st_file->fi_inode;
3990 unsigned int inohash = lockowner_ino_hashval(inode,
3991 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003992 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields009673b2011-11-07 17:40:10 -05003993
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003994 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03003995 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003996 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003997}
3998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999/*
4000 * Alloc a lock owner structure.
4001 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004002 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004004 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 */
4006
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004007static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004008alloc_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 -04004009 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004011 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4012 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004014 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4015 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004016 /* It is the openowner seqid that will be incremented in encode in the
4017 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004018 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4019 hash_lockowner(lo, strhashval, clp, open_stp);
4020 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
4022
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004023static struct nfs4_ol_stateid *
4024alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004026 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004027 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
J. Bruce Fields996e0932011-10-17 11:14:48 -04004029 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004030 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004031 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004032 init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
NeilBrown8beefa22005-06-23 22:03:08 -07004033 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004034 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4035 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004036 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004038 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004040 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 return stp;
4042}
4043
NeilBrownfd39ca92005-06-23 22:04:03 -07004044static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045check_lock_length(u64 offset, u64 length)
4046{
Benny Halevy87df4de2008-12-15 19:42:03 +02004047 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 LOFF_OVERFLOW(offset, length)));
4049}
4050
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004051static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004052{
4053 struct nfs4_file *fp = lock_stp->st_file;
4054 int oflag = nfs4_access_to_omode(access);
4055
Jeff Layton82c5ff12012-05-11 09:45:13 -04004056 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004057 return;
4058 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004059 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004060}
4061
J. Bruce Fields2355c592012-04-25 16:56:22 -04004062static __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 -04004063{
4064 struct nfs4_file *fi = ost->st_file;
4065 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4066 struct nfs4_client *cl = oo->oo_owner.so_client;
4067 struct nfs4_lockowner *lo;
4068 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004069 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004070
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004071 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4072 &lock->v.new.owner, nn);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004073 if (lo) {
4074 if (!cstate->minorversion)
4075 return nfserr_bad_seqid;
4076 /* XXX: a lockowner always has exactly one stateid: */
4077 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4078 struct nfs4_ol_stateid, st_perstateowner);
4079 return nfs_ok;
4080 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004081 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004082 &lock->v.new.owner);
4083 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4084 if (lo == NULL)
4085 return nfserr_jukebox;
4086 *lst = alloc_init_lock_stateid(lo, fi, ost);
4087 if (*lst == NULL) {
4088 release_lockowner(lo);
4089 return nfserr_jukebox;
4090 }
4091 *new = true;
4092 return nfs_ok;
4093}
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095/*
4096 * LOCK operation
4097 */
Al Virob37ad282006-10-19 23:28:59 -07004098__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004099nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004100 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004102 struct nfs4_openowner *open_sop = NULL;
4103 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004104 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004105 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004106 struct file_lock *file_lock = NULL;
4107 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004108 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004109 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004110 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004111 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004112 struct net *net = SVC_NET(rqstp);
4113 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4116 (long long) lock->lk_offset,
4117 (long long) lock->lk_length);
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 if (check_lock_length(lock->lk_offset, lock->lk_length))
4120 return nfserr_inval;
4121
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004122 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004123 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004124 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4125 return status;
4126 }
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 nfs4_lock_state();
4129
4130 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004131 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004132
4133 if (nfsd4_has_session(cstate))
4134 /* See rfc 5661 18.10.3: given clientid is ignored: */
4135 memcpy(&lock->v.new.clientid,
4136 &cstate->session->se_client->cl_clientid,
4137 sizeof(clientid_t));
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004140 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004144 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 lock->lk_new_open_seqid,
4146 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004147 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004148 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004150 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004151 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004152 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004153 &lock->v.new.clientid))
4154 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004155 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4156 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004157 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004158 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004159 lock->lk_old_lock_seqid,
4160 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004161 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004162 if (status)
4163 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004164 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004166 lkflg = setlkflg(lock->lk_type);
4167 status = nfs4_check_openmode(lock_stp, lkflg);
4168 if (status)
4169 goto out;
4170
NeilBrown0dd395d2005-07-07 17:59:15 -07004171 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004172 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004173 goto out;
4174 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004175 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004176 goto out;
4177
Jeff Layton21179d82012-08-21 08:03:32 -04004178 file_lock = locks_alloc_lock();
4179 if (!file_lock) {
4180 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4181 status = nfserr_jukebox;
4182 goto out;
4183 }
4184
4185 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 switch (lock->lk_type) {
4187 case NFS4_READ_LT:
4188 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004189 filp = find_readable_file(lock_stp->st_file);
4190 if (filp)
4191 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004192 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 case NFS4_WRITE_LT:
4195 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004196 filp = find_writeable_file(lock_stp->st_file);
4197 if (filp)
4198 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004199 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 default:
4202 status = nfserr_inval;
4203 goto out;
4204 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004205 if (!filp) {
4206 status = nfserr_openmode;
4207 goto out;
4208 }
Jeff Layton21179d82012-08-21 08:03:32 -04004209 file_lock->fl_owner = (fl_owner_t)lock_sop;
4210 file_lock->fl_pid = current->tgid;
4211 file_lock->fl_file = filp;
4212 file_lock->fl_flags = FL_POSIX;
4213 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4214 file_lock->fl_start = lock->lk_offset;
4215 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4216 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Jeff Layton21179d82012-08-21 08:03:32 -04004218 conflock = locks_alloc_lock();
4219 if (!conflock) {
4220 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4221 status = nfserr_jukebox;
4222 goto out;
4223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Jeff Layton21179d82012-08-21 08:03:32 -04004225 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004226 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004228 update_stateid(&lock_stp->st_stid.sc_stateid);
4229 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004231 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004232 break;
4233 case (EAGAIN): /* conflock holds conflicting lock */
4234 status = nfserr_denied;
4235 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004236 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 case (EDEADLK):
4239 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004240 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004241 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004242 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004243 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004247 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004248 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004249 if (!cstate->replay_owner)
4250 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004251 if (file_lock)
4252 locks_free_lock(file_lock);
4253 if (conflock)
4254 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 return status;
4256}
4257
4258/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004259 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4260 * so we do a temporary open here just to get an open file to pass to
4261 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4262 * inode operation.)
4263 */
Al Viro04da6e92012-04-13 00:00:04 -04004264static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004265{
4266 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004267 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4268 if (!err) {
4269 err = nfserrno(vfs_test_lock(file, lock));
4270 nfsd_close(file);
4271 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004272 return err;
4273}
4274
4275/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 * LOCKT operation
4277 */
Al Virob37ad282006-10-19 23:28:59 -07004278__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004279nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4280 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
4282 struct inode *inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004283 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004284 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004285 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004286 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004288 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 return nfserr_grace;
4290
4291 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4292 return nfserr_inval;
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 nfs4_lock_state();
4295
4296 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004297 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004300 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004303 inode = cstate->current_fh.fh_dentry->d_inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004304 file_lock = locks_alloc_lock();
4305 if (!file_lock) {
4306 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4307 status = nfserr_jukebox;
4308 goto out;
4309 }
4310 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 switch (lockt->lt_type) {
4312 case NFS4_READ_LT:
4313 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004314 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 break;
4316 case NFS4_WRITE_LT:
4317 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004318 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 break;
4320 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004321 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 status = nfserr_inval;
4323 goto out;
4324 }
4325
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004326 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004327 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004328 file_lock->fl_owner = (fl_owner_t)lo;
4329 file_lock->fl_pid = current->tgid;
4330 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
Jeff Layton21179d82012-08-21 08:03:32 -04004332 file_lock->fl_start = lockt->lt_offset;
4333 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Jeff Layton21179d82012-08-21 08:03:32 -04004335 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Jeff Layton21179d82012-08-21 08:03:32 -04004337 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004338 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004339 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004340
Jeff Layton21179d82012-08-21 08:03:32 -04004341 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004343 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 }
4345out:
4346 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004347 if (file_lock)
4348 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return status;
4350}
4351
Al Virob37ad282006-10-19 23:28:59 -07004352__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004353nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004354 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004356 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004358 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004359 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004360 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004361 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4364 (long long) locku->lu_offset,
4365 (long long) locku->lu_length);
4366
4367 if (check_lock_length(locku->lu_offset, locku->lu_length))
4368 return nfserr_inval;
4369
4370 nfs4_lock_state();
4371
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004372 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004373 &locku->lu_stateid, NFS4_LOCK_STID,
4374 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004375 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004377 filp = find_any_file(stp->st_file);
4378 if (!filp) {
4379 status = nfserr_lock_range;
4380 goto out;
4381 }
Jeff Layton21179d82012-08-21 08:03:32 -04004382 file_lock = locks_alloc_lock();
4383 if (!file_lock) {
4384 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4385 status = nfserr_jukebox;
4386 goto out;
4387 }
4388 locks_init_lock(file_lock);
4389 file_lock->fl_type = F_UNLCK;
4390 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4391 file_lock->fl_pid = current->tgid;
4392 file_lock->fl_file = filp;
4393 file_lock->fl_flags = FL_POSIX;
4394 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4395 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Jeff Layton21179d82012-08-21 08:03:32 -04004397 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4398 locku->lu_length);
4399 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 /*
4402 * Try to unlock the file in the VFS.
4403 */
Jeff Layton21179d82012-08-21 08:03:32 -04004404 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004405 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004406 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 goto out_nfserr;
4408 }
4409 /*
4410 * OK, unlock succeeded; the only thing left to do is update the stateid.
4411 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004412 update_stateid(&stp->st_stid.sc_stateid);
4413 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415out:
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004416 if (!cstate->replay_owner)
4417 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004418 if (file_lock)
4419 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 return status;
4421
4422out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004423 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 goto out;
4425}
4426
4427/*
4428 * returns
4429 * 1: locks held by lockowner
4430 * 0: no locks held by lockowner
4431 */
4432static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004433check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
4435 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004436 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 int status = 0;
4438
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004439 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004441 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 status = 1;
4443 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
4446out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004447 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return status;
4449}
4450
Al Virob37ad282006-10-19 23:28:59 -07004451__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004452nfsd4_release_lockowner(struct svc_rqst *rqstp,
4453 struct nfsd4_compound_state *cstate,
4454 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
4456 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004457 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004458 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004459 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004461 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004462 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004463 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004464 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
4466 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4467 clid->cl_boot, clid->cl_id);
4468
4469 /* XXX check for lease expiration */
4470
4471 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004472 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
4475 nfs4_lock_state();
4476
NeilBrown3e9e3db2005-06-23 22:04:20 -07004477 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004478 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004479
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004480 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004481 if (sop->so_is_open_owner)
4482 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004483 if (!same_owner_str(sop, owner, clid))
4484 continue;
4485 list_for_each_entry(stp, &sop->so_stateids,
4486 st_perstateowner) {
4487 lo = lockowner(sop);
4488 if (check_for_locks(stp->st_file, lo))
4489 goto out;
4490 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004492 }
4493 /* Clients probably won't expect us to return with some (but not all)
4494 * of the lockowner state released; so don't release any until all
4495 * have been checked. */
4496 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004497 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004498 lo = list_entry(matches.next, struct nfs4_lockowner,
4499 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004500 /* unhash_stateowner deletes so_perclient only
4501 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004502 list_del(&lo->lo_list);
4503 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 }
4505out:
4506 nfs4_unlock_state();
4507 return status;
4508}
4509
4510static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004511alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
NeilBrowna55370a2005-06-23 22:03:52 -07004513 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004516bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004517nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004518{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004519 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004520
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004521 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004522 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004523}
4524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525/*
4526 * failure => all reset bets are off, nfserr_no_grace...
4527 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004528struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004529nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530{
4531 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004532 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
NeilBrowna55370a2005-06-23 22:03:52 -07004534 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4535 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004536 if (crp) {
4537 strhashval = clientstr_hashval(name);
4538 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004539 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004540 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004541 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004542 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004543 }
4544 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
Jeff Layton2a4317c2012-03-21 16:42:43 -04004547void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004548nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004549{
4550 list_del(&crp->cr_strhash);
4551 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004552 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004553}
4554
4555void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004556nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
4558 struct nfs4_client_reclaim *crp = NULL;
4559 int i;
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004562 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4563 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004565 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 }
4567 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004568 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569}
4570
4571/*
4572 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004573struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004574nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575{
4576 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 struct nfs4_client_reclaim *crp = NULL;
4578
Jeff Layton278c9312012-11-12 15:00:52 -05004579 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Jeff Layton278c9312012-11-12 15:00:52 -05004581 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004582 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004583 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 return crp;
4585 }
4586 }
4587 return NULL;
4588}
4589
4590/*
4591* Called from OPEN. Look for clientid in reclaim list.
4592*/
Al Virob37ad282006-10-19 23:28:59 -07004593__be32
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004594nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
Jeff Laytona52d7262012-03-21 09:52:02 -04004596 struct nfs4_client *clp;
4597
4598 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004599 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004600 if (clp == NULL)
4601 return nfserr_reclaim_bad;
4602
4603 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Bryan Schumaker65178db2011-11-01 13:35:21 -04004606#ifdef CONFIG_NFSD_FAULT_INJECTION
4607
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004608u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4609{
4610 expire_client(clp);
4611 return 1;
4612}
4613
Bryan Schumakerfc291712012-11-29 11:40:40 -05004614static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4615{
4616 struct nfs4_openowner *oop;
4617 struct nfs4_lockowner *lop, *lo_next;
4618 struct nfs4_ol_stateid *stp, *st_next;
4619 u64 count = 0;
4620
4621 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4622 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4623 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4624 if (func)
4625 func(lop);
4626 if (++count == max)
4627 return count;
4628 }
4629 }
4630 }
4631
4632 return count;
4633}
4634
4635u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4636{
4637 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4638}
4639
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05004640static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4641{
4642 struct nfs4_openowner *oop, *next;
4643 u64 count = 0;
4644
4645 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4646 if (func)
4647 func(oop);
4648 if (++count == max)
4649 break;
4650 }
4651
4652 return count;
4653}
4654
4655u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4656{
4657 return nfsd_foreach_client_open(clp, max, release_openowner);
4658}
4659
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004660u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004661{
4662 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004663 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004664 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004665
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004666 if (!nfsd_netns_ready(nn))
4667 return 0;
4668
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004669 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004670 count += func(clp, max - count);
4671 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04004672 break;
4673 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04004674
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004675 return count;
4676}
4677
4678void nfsd_forget_clients(u64 num)
4679{
4680 u64 count = nfsd_for_n_state(num, nfsd_forget_client);
4681 printk(KERN_INFO "NFSD: Forgot %llu clients", count);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004682}
4683
Bryan Schumaker65178db2011-11-01 13:35:21 -04004684void nfsd_forget_locks(u64 num)
4685{
Bryan Schumakerfc291712012-11-29 11:40:40 -05004686 u64 count = nfsd_for_n_state(num, nfsd_forget_client_locks);
4687 printk(KERN_INFO "NFSD: Forgot %llu locks", count);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004688}
4689
4690void nfsd_forget_openowners(u64 num)
4691{
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05004692 u64 count = nfsd_for_n_state(num, nfsd_forget_client_openowners);
4693 printk(KERN_INFO "NFSD: Forgot %llu open owners", count);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004694}
4695
J. Bruce Fieldsda5c80a2012-08-14 16:09:31 -04004696static int nfsd_process_n_delegations(u64 num, struct list_head *list)
Bryan Schumaker65178db2011-11-01 13:35:21 -04004697{
4698 int i, count = 0;
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004699 struct nfs4_file *fp, *fnext;
4700 struct nfs4_delegation *dp, *dnext;
Bryan Schumaker65178db2011-11-01 13:35:21 -04004701
4702 for (i = 0; i < FILE_HASH_SIZE; i++) {
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004703 list_for_each_entry_safe(fp, fnext, &file_hashtbl[i], fi_hash) {
4704 list_for_each_entry_safe(dp, dnext, &fp->fi_delegations, dl_perfile) {
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004705 list_move(&dp->dl_recall_lru, list);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004706 if (++count == num)
4707 return count;
4708 }
4709 }
4710 }
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004711
Bryan Schumaker65178db2011-11-01 13:35:21 -04004712 return count;
4713}
4714
4715void nfsd_forget_delegations(u64 num)
4716{
4717 unsigned int count;
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004718 LIST_HEAD(victims);
4719 struct nfs4_delegation *dp, *dnext;
4720
4721 spin_lock(&recall_lock);
4722 count = nfsd_process_n_delegations(num, &victims);
4723 spin_unlock(&recall_lock);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004724
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004725 list_for_each_entry_safe(dp, dnext, &victims, dl_recall_lru)
4726 unhash_delegation(dp);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004727
4728 printk(KERN_INFO "NFSD: Forgot %d delegations", count);
4729}
4730
4731void nfsd_recall_delegations(u64 num)
4732{
4733 unsigned int count;
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004734 LIST_HEAD(victims);
4735 struct nfs4_delegation *dp, *dnext;
Bryan Schumaker65178db2011-11-01 13:35:21 -04004736
Bryan Schumaker65178db2011-11-01 13:35:21 -04004737 spin_lock(&recall_lock);
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004738 count = nfsd_process_n_delegations(num, &victims);
4739 list_for_each_entry_safe(dp, dnext, &victims, dl_recall_lru) {
4740 list_del(&dp->dl_recall_lru);
4741 nfsd_break_one_deleg(dp);
4742 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04004743 spin_unlock(&recall_lock);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004744
4745 printk(KERN_INFO "NFSD: Recalled %d delegations", count);
4746}
4747
4748#endif /* CONFIG_NFSD_FAULT_INJECTION */
4749
NeilBrownac4d8ff22005-06-23 22:03:30 -07004750/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Bryan Schumaker72083392011-11-01 15:24:59 -04004752void
NeilBrownac4d8ff22005-06-23 22:03:30 -07004753nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Bryan Schumaker72083392011-11-01 15:24:59 -04004755 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 for (i = 0; i < FILE_HASH_SIZE; i++) {
4758 INIT_LIST_HEAD(&file_hashtbl[i]);
4759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004761}
4762
Meelap Shahc2f1a552007-07-17 04:04:39 -07004763/*
4764 * Since the lifetime of a delegation isn't limited to that of an open, a
4765 * client may quite reasonably hang on to a delegation as long as it has
4766 * the inode cached. This becomes an obvious problem the first time a
4767 * client's inode cache approaches the size of the server's total memory.
4768 *
4769 * For now we avoid this problem by imposing a hard limit on the number
4770 * of delegations, which varies according to the server's memory size.
4771 */
4772static void
4773set_max_delegations(void)
4774{
4775 /*
4776 * Allow at most 4 delegations per megabyte of RAM. Quick
4777 * estimates suggest that in the worst case (where every delegation
4778 * is for a different inode), a delegation could take about 1.5K,
4779 * giving a worst case usage of about 6% of memory.
4780 */
4781 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4782}
4783
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004784static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004785{
4786 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4787 int i;
4788
4789 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4790 CLIENT_HASH_SIZE, GFP_KERNEL);
4791 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004792 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004793 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4794 CLIENT_HASH_SIZE, GFP_KERNEL);
4795 if (!nn->unconf_id_hashtbl)
4796 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004797 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4798 OWNER_HASH_SIZE, GFP_KERNEL);
4799 if (!nn->ownerstr_hashtbl)
4800 goto err_ownerstr;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004801 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4802 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4803 if (!nn->lockowner_ino_hashtbl)
4804 goto err_lockowner_ino;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004805 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4806 SESSION_HASH_SIZE, GFP_KERNEL);
4807 if (!nn->sessionid_hashtbl)
4808 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004809
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004810 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004811 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004812 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004813 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004814 for (i = 0; i < OWNER_HASH_SIZE; i++)
4815 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004816 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4817 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004818 for (i = 0; i < SESSION_HASH_SIZE; i++)
4819 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004820 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004821 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004822 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004823 INIT_LIST_HEAD(&nn->close_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004824 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004825
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004826 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004827 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004828
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004829 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004830
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004831err_sessionid:
4832 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004833err_lockowner_ino:
4834 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004835err_ownerstr:
4836 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004837err_unconf_id:
4838 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004839err:
4840 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004841}
4842
4843static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004844nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004845{
4846 int i;
4847 struct nfs4_client *clp = NULL;
4848 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004849 struct rb_node *node, *tmp;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004850
4851 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4852 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4853 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4854 destroy_client(clp);
4855 }
4856 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004857
4858 node = rb_first(&nn->unconf_name_tree);
4859 while (node != NULL) {
4860 tmp = node;
4861 node = rb_next(tmp);
4862 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4863 rb_erase(tmp, &nn->unconf_name_tree);
4864 destroy_client(clp);
4865 }
4866
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03004867 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004868 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004869 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004870 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004871 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004872 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004873}
4874
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004875int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004876nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07004877{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004878 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004879 int ret;
4880
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004881 /*
4882 * FIXME: For now, we hang most of the pernet global stuff off of
4883 * init_net until nfsd is fully containerized. Eventually, we'll
4884 * need to pass a net pointer into this function, take a reference
4885 * to that instead and then do most of the rest of this on a per-net
4886 * basis.
4887 */
4888 if (net != &init_net)
4889 return -EINVAL;
4890
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004891 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004892 if (ret)
4893 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004894 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004895 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004896 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004897 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004898 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03004899 nn->nfsd4_grace, net);
4900 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004901 return 0;
4902}
4903
4904/* initialization to perform when the nfsd service is started: */
4905
4906int
4907nfs4_state_start(void)
4908{
4909 int ret;
4910
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004911 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004912 if (ret)
4913 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07004914 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05004915 if (laundry_wq == NULL) {
4916 ret = -ENOMEM;
4917 goto out_recovery;
4918 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004919 ret = nfsd4_create_callback_queue();
4920 if (ret)
4921 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004922
Meelap Shahc2f1a552007-07-17 04:04:39 -07004923 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004924
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004925 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03004926
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004927out_free_laundry:
4928 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05004929out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004930 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004933/* should be called with the state lock held */
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03004934void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004935nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004939 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004941 cancel_delayed_work_sync(&nn->laundromat_work);
4942 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 INIT_LIST_HEAD(&reaplist);
4945 spin_lock(&recall_lock);
4946 list_for_each_safe(pos, next, &del_recall_lru) {
4947 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03004948 if (dp->dl_stid.sc_client->net != net)
4949 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 list_move(&dp->dl_recall_lru, &reaplist);
4951 }
4952 spin_unlock(&recall_lock);
4953 list_for_each_safe(pos, next, &reaplist) {
4954 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 unhash_delegation(dp);
4956 }
4957
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004958 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03004959 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
4962void
4963nfs4_state_shutdown(void)
4964{
NeilBrown5e8d5c22006-04-10 22:55:37 -07004965 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004966 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004968
4969static void
4970get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4971{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01004972 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
4973 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004974}
4975
4976static void
4977put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4978{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01004979 if (cstate->minorversion) {
4980 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
4981 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
4982 }
4983}
4984
4985void
4986clear_current_stateid(struct nfsd4_compound_state *cstate)
4987{
4988 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004989}
4990
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004991/*
4992 * functions to set current state id
4993 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004994void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01004995nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
4996{
4997 put_stateid(cstate, &odp->od_stateid);
4998}
4999
5000void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005001nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5002{
5003 put_stateid(cstate, &open->op_stateid);
5004}
5005
5006void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005007nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5008{
5009 put_stateid(cstate, &close->cl_stateid);
5010}
5011
5012void
5013nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5014{
5015 put_stateid(cstate, &lock->lk_resp_stateid);
5016}
5017
5018/*
5019 * functions to consume current state id
5020 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005021
5022void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005023nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5024{
5025 get_stateid(cstate, &odp->od_stateid);
5026}
5027
5028void
5029nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5030{
5031 get_stateid(cstate, &drp->dr_stateid);
5032}
5033
5034void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005035nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5036{
5037 get_stateid(cstate, &fsp->fr_stateid);
5038}
5039
5040void
5041nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5042{
5043 get_stateid(cstate, &setattr->sa_stateid);
5044}
5045
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005046void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005047nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5048{
5049 get_stateid(cstate, &close->cl_stateid);
5050}
5051
5052void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005053nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005054{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005055 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005056}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005057
5058void
5059nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5060{
5061 get_stateid(cstate, &read->rd_stateid);
5062}
5063
5064void
5065nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5066{
5067 get_stateid(cstate, &write->wr_stateid);
5068}