blob: 91aec738e8fdd684d3bf9e75c31725e68113cac1 [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>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050041#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton363168b2009-08-14 12:57:56 -040042#include <linux/sunrpc/clnt.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020043#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050044#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040045#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define NFSDDBG_FACILITY NFSDDBG_PROC
48
49/* Globals */
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -050050time_t nfsd4_lease = 90; /* default lease time */
J. Bruce Fieldsefc4bb42010-03-02 11:04:06 -050051time_t nfsd4_grace = 90;
NeilBrownfd39ca92005-06-23 22:04:03 -070052static time_t boot_time;
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050053
54#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040073static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
J. Bruce Fields8b671b82009-02-22 14:51:34 -080075/* Locking: */
76
77/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080078static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
J. Bruce Fields8b671b82009-02-22 14:51:34 -080080/*
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
84 */
85static DEFINE_SPINLOCK(recall_lock);
86
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040087static struct kmem_cache *openowner_slab = NULL;
88static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080089static struct kmem_cache *file_slab = NULL;
90static struct kmem_cache *stateid_slab = NULL;
91static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void
94nfs4_lock_state(void)
95{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080096 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Benny Halevy508dc6e2012-02-23 17:40:52 -080099static void free_session(struct kref *);
100
101/* Must be called under the client_lock */
102static void nfsd4_put_session_locked(struct nfsd4_session *ses)
103{
104 kref_put(&ses->se_ref, free_session);
105}
106
107static void nfsd4_get_session(struct nfsd4_session *ses)
108{
109 kref_get(&ses->se_ref);
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void
113nfs4_unlock_state(void)
114{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800115 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static inline u32
119opaque_hashval(const void *ptr, int nbytes)
120{
121 unsigned char *cptr = (unsigned char *) ptr;
122
123 u32 x = 0;
124 while (nbytes--) {
125 x *= 37;
126 x += *cptr++;
127 }
128 return x;
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static struct list_head del_recall_lru;
132
J. Bruce Fields32513b42011-10-13 16:00:16 -0400133static void nfsd4_free_file(struct nfs4_file *f)
134{
135 kmem_cache_free(file_slab, f);
136}
137
NeilBrown13cd2182005-06-23 22:03:10 -0700138static inline void
139put_nfs4_file(struct nfs4_file *fi)
140{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800141 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
142 list_del(&fi->fi_hash);
143 spin_unlock(&recall_lock);
144 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400145 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800146 }
NeilBrown13cd2182005-06-23 22:03:10 -0700147}
148
149static inline void
150get_nfs4_file(struct nfs4_file *fi)
151{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800152 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700153}
154
NeilBrownef0f3392006-04-10 22:55:41 -0700155static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700156unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700157
158/*
159 * Open owner state (share locks)
160 */
161
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500162/* hash tables for lock and open owners */
163#define OWNER_HASH_BITS 8
164#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
165#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700166
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500167static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400168{
169 unsigned int ret;
170
171 ret = opaque_hashval(ownername->data, ownername->len);
172 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500173 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400174}
NeilBrownef0f3392006-04-10 22:55:41 -0700175
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500176static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700177
178/* hash table for nfs4_file */
179#define FILE_HASH_BITS 8
180#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800181
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400182static unsigned int file_hashval(struct inode *ino)
183{
184 /* XXX: why are we hashing on inode pointer, anyway? */
185 return hash_ptr(ino, FILE_HASH_BITS);
186}
187
NeilBrownef0f3392006-04-10 22:55:41 -0700188static struct list_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700189
J. Bruce Fields998db522010-08-07 09:21:41 -0400190static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400191{
192 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
193 atomic_inc(&fp->fi_access[oflag]);
194}
195
J. Bruce Fields998db522010-08-07 09:21:41 -0400196static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
197{
198 if (oflag == O_RDWR) {
199 __nfs4_file_get_access(fp, O_RDONLY);
200 __nfs4_file_get_access(fp, O_WRONLY);
201 } else
202 __nfs4_file_get_access(fp, oflag);
203}
204
205static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400206{
207 if (fp->fi_fds[oflag]) {
208 fput(fp->fi_fds[oflag]);
209 fp->fi_fds[oflag] = NULL;
210 }
211}
212
J. Bruce Fields998db522010-08-07 09:21:41 -0400213static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400214{
215 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400216 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400217 /*
218 * It's also safe to get rid of the RDWR open *if*
219 * we no longer have need of the other kind of access
220 * or if we already have the other kind of open:
221 */
222 if (fp->fi_fds[1-oflag]
223 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
224 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400225 }
226}
227
J. Bruce Fields998db522010-08-07 09:21:41 -0400228static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
229{
230 if (oflag == O_RDWR) {
231 __nfs4_file_put_access(fp, O_RDONLY);
232 __nfs4_file_put_access(fp, O_WRONLY);
233 } else
234 __nfs4_file_put_access(fp, oflag);
235}
236
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400237static inline int get_new_stid(struct nfs4_stid *stid)
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400238{
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400239 static int min_stateid = 0;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400240 struct idr *stateids = &stid->sc_client->cl_stateids;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400241 int new_stid;
242 int error;
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400243
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400244 error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400245 /*
J. Bruce Fields996e0932011-10-17 11:14:48 -0400246 * Note: the necessary preallocation was done in
247 * nfs4_alloc_stateid(). The idr code caps the number of
248 * preallocations that can exist at a time, but the state lock
249 * prevents anyone from using ours before we get here:
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400250 */
251 BUG_ON(error);
252 /*
253 * It shouldn't be a problem to reuse an opaque stateid value.
254 * I don't think it is for 4.1. But with 4.0 I worry that, for
255 * example, a stray write retransmission could be accepted by
256 * the server when it should have been rejected. Therefore,
257 * adopt a trick from the sctp code to attempt to maximize the
258 * amount of time until an id is reused, by ensuring they always
259 * "increase" (mod INT_MAX):
260 */
261
262 min_stateid = new_stid+1;
263 if (min_stateid == INT_MAX)
264 min_stateid = 0;
265 return new_stid;
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400266}
267
J. Bruce Fields996e0932011-10-17 11:14:48 -0400268static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400269{
270 stateid_t *s = &stid->sc_stateid;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400271 int new_id;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400272
273 stid->sc_type = type;
274 stid->sc_client = cl;
275 s->si_opaque.so_clid = cl->cl_clientid;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400276 new_id = get_new_stid(stid);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400277 s->si_opaque.so_id = (u32)new_id;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400278 /* Will be incremented before return to client: */
279 s->si_generation = 0;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400280}
281
282static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
283{
284 struct idr *stateids = &cl->cl_stateids;
285
286 if (!idr_pre_get(stateids, GFP_KERNEL))
287 return NULL;
288 /*
289 * Note: if we fail here (or any time between now and the time
290 * we actually get the new idr), we won't need to undo the idr
291 * preallocation, since the idr code caps the number of
292 * preallocated entries.
293 */
294 return kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400295}
296
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400297static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
298{
299 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400303alloc_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 -0700304{
305 struct nfs4_delegation *dp;
306 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400309 /*
310 * Major work on the lease subsystem (for example, to support
311 * calbacks on stat) will be required before we can support
312 * write delegations properly.
313 */
314 if (type != NFS4_OPEN_DELEGATE_READ)
315 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700316 if (fp->fi_had_conflict)
317 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700318 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700319 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400320 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700321 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return dp;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400323 init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400324 /*
325 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400326 * meaning of seqid 0 isn't meaningful, really, but let's avoid
327 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400328 */
329 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700330 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700331 INIT_LIST_HEAD(&dp->dl_perfile);
332 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 INIT_LIST_HEAD(&dp->dl_recall_lru);
NeilBrown13cd2182005-06-23 22:03:10 -0700334 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dp->dl_type = type;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500337 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dp->dl_time = 0;
339 atomic_set(&dp->dl_count, 1);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500340 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return dp;
342}
343
344void
345nfs4_put_delegation(struct nfs4_delegation *dp)
346{
347 if (atomic_dec_and_test(&dp->dl_count)) {
348 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700349 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700350 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700351 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353}
354
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500355static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500357 if (atomic_dec_and_test(&fp->fi_delegees)) {
358 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
359 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400360 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500361 fp->fi_deleg_file = NULL;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400365static void unhash_stid(struct nfs4_stid *s)
366{
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400367 struct idr *stateids = &s->sc_client->cl_stateids;
368
369 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/* Called under the state lock. */
373static void
374unhash_delegation(struct nfs4_delegation *dp)
375{
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400376 unhash_stid(&dp->dl_stid);
NeilBrownea1da632005-06-23 22:04:17 -0700377 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500379 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 list_del_init(&dp->dl_recall_lru);
381 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500382 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 nfs4_put_delegation(dp);
384}
385
386/*
387 * SETCLIENTID state
388 */
389
Benny Halevy36acb662010-05-12 00:13:04 +0300390/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300391static DEFINE_SPINLOCK(client_lock);
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/* Hash tables for nfs4_clientid state */
394#define CLIENT_HASH_BITS 4
395#define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
396#define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
397
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400398static unsigned int clientid_hashval(u32 id)
399{
400 return id & CLIENT_HASH_MASK;
401}
402
403static unsigned int clientstr_hashval(const char *name)
404{
405 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
409 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
410 * used in reboot/reset lease grace period processing
411 *
412 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
413 * setclientid_confirmed info.
414 *
415 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
416 * setclientid info.
417 *
418 * client_lru holds client queue ordered by nfs4_client.cl_time
419 * for lease renewal.
420 *
421 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
422 * for last close replay.
423 */
424static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
425static int reclaim_str_hashtbl_size = 0;
426static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
427static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
428static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
429static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
430static struct list_head client_lru;
431static struct list_head close_lru;
432
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400433/*
434 * We store the NONE, READ, WRITE, and BOTH bits separately in the
435 * st_{access,deny}_bmap field of the stateid, in order to track not
436 * only what share bits are currently in force, but also what
437 * combinations of share bits previous opens have used. This allows us
438 * to enforce the recommendation of rfc 3530 14.2.19 that the server
439 * return an error if the client attempt to downgrade to a combination
440 * of share bits not explicable by closing some of its previous opens.
441 *
442 * XXX: This enforcement is actually incomplete, since we don't keep
443 * track of access/deny bit combinations; so, e.g., we allow:
444 *
445 * OPEN allow read, deny write
446 * OPEN allow both, deny none
447 * DOWNGRADE allow read, deny none
448 *
449 * which we should reject.
450 */
451static void
452set_access(unsigned int *access, unsigned long bmap) {
453 int i;
454
455 *access = 0;
456 for (i = 1; i < 4; i++) {
457 if (test_bit(i, &bmap))
458 *access |= i;
459 }
460}
461
462static void
463set_deny(unsigned int *deny, unsigned long bmap) {
464 int i;
465
466 *deny = 0;
467 for (i = 0; i < 4; i++) {
468 if (test_bit(i, &bmap))
469 *deny |= i ;
470 }
471}
472
473static int
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400474test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400475 unsigned int access, deny;
476
477 set_access(&access, stp->st_access_bmap);
478 set_deny(&deny, stp->st_deny_bmap);
479 if ((access & open->op_share_deny) || (deny & open->op_share_access))
480 return 0;
481 return 1;
482}
483
484static int nfs4_access_to_omode(u32 access)
485{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400486 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400487 case NFS4_SHARE_ACCESS_READ:
488 return O_RDONLY;
489 case NFS4_SHARE_ACCESS_WRITE:
490 return O_WRONLY;
491 case NFS4_SHARE_ACCESS_BOTH:
492 return O_RDWR;
493 }
494 BUG();
495}
496
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400497static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500498{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500499 list_del(&stp->st_perfile);
500 list_del(&stp->st_perstateowner);
501}
502
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400503static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500504{
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400505 int i;
J. Bruce Fields0997b172011-03-02 18:01:35 -0500506
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800507 if (stp->st_access_bmap) {
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400508 for (i = 1; i < 4; i++) {
509 if (test_bit(i, &stp->st_access_bmap))
510 nfs4_file_put_access(stp->st_file,
511 nfs4_access_to_omode(i));
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400512 __clear_bit(i, &stp->st_access_bmap);
J. Bruce Fields499f3ed2011-06-27 16:57:12 -0400513 }
J. Bruce Fields23fcf2e2011-03-28 15:15:09 +0800514 }
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400515 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400516 stp->st_file = NULL;
517}
518
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400519static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400520{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500521 kmem_cache_free(stateid_slab, stp);
522}
523
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400524static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500525{
526 struct file *file;
527
528 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400529 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500530 file = find_any_file(stp->st_file);
531 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400532 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400533 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500534 free_generic_stateid(stp);
535}
536
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400537static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500538{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400539 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500540
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400541 list_del(&lo->lo_owner.so_strhash);
542 list_del(&lo->lo_perstateid);
J. Bruce Fields009673b2011-11-07 17:40:10 -0500543 list_del(&lo->lo_owner_ino_hash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400544 while (!list_empty(&lo->lo_owner.so_stateids)) {
545 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400546 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500547 release_lock_stateid(stp);
548 }
549}
550
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400551static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500552{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400553 unhash_lockowner(lo);
554 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500555}
556
557static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400558release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500559{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400560 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500561
562 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400563 lo = list_entry(open_stp->st_lockowners.next,
564 struct nfs4_lockowner, lo_perstateid);
565 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500566 }
567}
568
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400569static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500570{
571 unhash_generic_stateid(stp);
572 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400573 close_generic_stateid(stp);
574}
575
576static void release_open_stateid(struct nfs4_ol_stateid *stp)
577{
578 unhash_open_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400579 unhash_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500580 free_generic_stateid(stp);
581}
582
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400583static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500584{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400585 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500586
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400587 list_del(&oo->oo_owner.so_strhash);
588 list_del(&oo->oo_perclient);
589 while (!list_empty(&oo->oo_owner.so_stateids)) {
590 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400591 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500592 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500593 }
594}
595
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400596static void release_last_closed_stateid(struct nfs4_openowner *oo)
597{
598 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
599
600 if (s) {
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400601 unhash_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400602 free_generic_stateid(s);
603 oo->oo_last_closed_stid = NULL;
604 }
605}
606
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400607static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500608{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400609 unhash_openowner(oo);
610 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400611 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400612 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500613}
614
Marc Eshel5282fd72009-04-03 08:27:52 +0300615#define SESSION_HASH_SIZE 512
616static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
617
618static inline int
619hash_sessionid(struct nfs4_sessionid *sessionid)
620{
621 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
622
623 return sid->sequence % SESSION_HASH_SIZE;
624}
625
Trond Myklebust8f199b82012-03-20 15:11:17 -0400626#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300627static inline void
628dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
629{
630 u32 *ptr = (u32 *)(&sessionid->data[0]);
631 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
632}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400633#else
634static inline void
635dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
636{
637}
638#endif
639
Marc Eshel5282fd72009-04-03 08:27:52 +0300640
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300641static void
642gen_sessionid(struct nfsd4_session *ses)
643{
644 struct nfs4_client *clp = ses->se_client;
645 struct nfsd4_sessionid *sid;
646
647 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
648 sid->clientid = clp->cl_clientid;
649 sid->sequence = current_sessionid++;
650 sid->reserved = 0;
651}
652
653/*
Andy Adamsona6496372009-08-28 08:45:01 -0400654 * The protocol defines ca_maxresponssize_cached to include the size of
655 * the rpc header, but all we need to cache is the data starting after
656 * the end of the initial SEQUENCE operation--the rest we regenerate
657 * each time. Therefore we can advertise a ca_maxresponssize_cached
658 * value that is the number of bytes in our cache plus a few additional
659 * bytes. In order to stay on the safe side, and not promise more than
660 * we can cache, those additional bytes must be the minimum possible: 24
661 * bytes of rpc header (xid through accept state, with AUTH_NULL
662 * verifier), 12 for the compound header (with zero-length tag), and 44
663 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300664 */
Andy Adamsona6496372009-08-28 08:45:01 -0400665#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
666
Andy Adamson557ce262009-08-28 08:45:04 -0400667static void
668free_session_slots(struct nfsd4_session *ses)
669{
670 int i;
671
672 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
673 kfree(ses->se_slots[i]);
674}
675
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400676/*
677 * We don't actually need to cache the rpc and session headers, so we
678 * can allocate a little less for each slot:
679 */
680static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
681{
682 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
683}
684
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400685static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300686{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400687 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
688 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300689
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400690 return size;
691}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300692
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400693/*
694 * XXX: If we run out of reserved DRC memory we could (up to a point)
695 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800696 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400697 */
698static int nfsd4_get_drc_mem(int slotsize, u32 num)
699{
700 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300701
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400702 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400703
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400704 spin_lock(&nfsd_drc_lock);
705 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
706 nfsd_drc_max_mem - nfsd_drc_mem_used);
707 num = min_t(int, num, avail / slotsize);
708 nfsd_drc_mem_used += num * slotsize;
709 spin_unlock(&nfsd_drc_lock);
710
711 return num;
712}
713
714static void nfsd4_put_drc_mem(int slotsize, int num)
715{
716 spin_lock(&nfsd_drc_lock);
717 nfsd_drc_mem_used -= slotsize * num;
718 spin_unlock(&nfsd_drc_lock);
719}
720
721static struct nfsd4_session *alloc_session(int slotsize, int numslots)
722{
723 struct nfsd4_session *new;
724 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300725
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400726 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400727 + sizeof(struct nfsd4_session) > PAGE_SIZE);
728 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400729
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400730 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300731 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400732 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400733 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400734 for (i = 0; i < numslots; i++) {
735 mem = sizeof(struct nfsd4_slot) + slotsize;
736 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
737 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400738 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400739 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400740 return new;
741out_free:
742 while (i--)
743 kfree(new->se_slots[i]);
744 kfree(new);
745 return NULL;
746}
747
748static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
749{
750 u32 maxrpc = nfsd_serv->sv_max_mesg;
751
752 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800753 new->maxresp_cached = min_t(u32, req->maxresp_cached,
754 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400755 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
756 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
757 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
758}
759
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400760static void free_conn(struct nfsd4_conn *c)
761{
762 svc_xprt_put(c->cn_xprt);
763 kfree(c);
764}
765
766static void nfsd4_conn_lost(struct svc_xpt_user *u)
767{
768 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
769 struct nfs4_client *clp = c->cn_session->se_client;
770
771 spin_lock(&clp->cl_lock);
772 if (!list_empty(&c->cn_persession)) {
773 list_del(&c->cn_persession);
774 free_conn(c);
775 }
776 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500777 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400778}
779
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400780static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400781{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400782 struct nfsd4_conn *conn;
783
784 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
785 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400786 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400787 svc_xprt_get(rqstp->rq_xprt);
788 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400789 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400790 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
791 return conn;
792}
793
J. Bruce Fields328ead22010-09-29 16:11:06 -0400794static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
795{
796 conn->cn_session = ses;
797 list_add(&conn->cn_persession, &ses->se_conns);
798}
799
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400800static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
801{
802 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400803
804 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400805 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400806 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400807}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400808
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400809static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400810{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400811 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400812 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400813}
814
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400815static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400816{
817 struct nfsd4_conn *conn;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400818 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400819
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400820 conn = alloc_conn(rqstp, dir);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400821 if (!conn)
822 return nfserr_jukebox;
823 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400824 ret = nfsd4_register_conn(conn);
825 if (ret)
826 /* oops; xprt is already down: */
827 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400828 return nfs_ok;
829}
830
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400831static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
832{
833 u32 dir = NFS4_CDFC4_FORE;
834
835 if (ses->se_flags & SESSION4_BACK_CHAN)
836 dir |= NFS4_CDFC4_BACK;
837
838 return nfsd4_new_conn(rqstp, ses, dir);
839}
840
841/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400842static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400843{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400844 struct nfs4_client *clp = s->se_client;
845 struct nfsd4_conn *c;
846
847 spin_lock(&clp->cl_lock);
848 while (!list_empty(&s->se_conns)) {
849 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
850 list_del_init(&c->cn_persession);
851 spin_unlock(&clp->cl_lock);
852
853 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
854 free_conn(c);
855
856 spin_lock(&clp->cl_lock);
857 }
858 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400859}
860
Benny Halevy508dc6e2012-02-23 17:40:52 -0800861static void free_session(struct kref *kref)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400862{
863 struct nfsd4_session *ses;
864 int mem;
865
Benny Halevy508dc6e2012-02-23 17:40:52 -0800866 BUG_ON(!spin_is_locked(&client_lock));
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400867 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400868 nfsd4_del_conns(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400869 spin_lock(&nfsd_drc_lock);
870 mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
871 nfsd_drc_mem_used -= mem;
872 spin_unlock(&nfsd_drc_lock);
873 free_session_slots(ses);
874 kfree(ses);
875}
876
Benny Halevy508dc6e2012-02-23 17:40:52 -0800877void nfsd4_put_session(struct nfsd4_session *ses)
878{
879 spin_lock(&client_lock);
880 nfsd4_put_session_locked(ses);
881 spin_unlock(&client_lock);
882}
883
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400884static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400885{
886 struct nfsd4_session *new;
887 struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
888 int numslots, slotsize;
J. Bruce Fields57b7b432012-04-25 17:58:50 -0400889 __be32 status;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400890 int idx;
891
892 /*
893 * Note decreasing slot size below client's request may
894 * make it difficult for client to function correctly, whereas
895 * decreasing the number of slots will (just?) affect
896 * performance. When short on memory we therefore prefer to
897 * decrease number of slots instead of their size.
898 */
899 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
900 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800901 if (numslots < 1)
902 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400903
904 new = alloc_session(slotsize, numslots);
905 if (!new) {
906 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400907 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400908 }
909 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
Andy Adamson557ce262009-08-28 08:45:04 -0400910
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300911 new->se_client = clp;
912 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300913
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400914 INIT_LIST_HEAD(&new->se_conns);
915
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400916 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300917 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400918 new->se_cb_prog = cses->callback_prog;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300919 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400920 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300921 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300922 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400923 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300924 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400925 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300926 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300927
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400928 status = nfsd4_new_conn_from_crses(rqstp, new);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400929 /* whoops: benny points out, status is ignored! (err, or bogus) */
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400930 if (status) {
Benny Halevy508dc6e2012-02-23 17:40:52 -0800931 spin_lock(&client_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400932 free_session(&new->se_ref);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800933 spin_unlock(&client_lock);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400934 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400935 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400936 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400937 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400938 /*
939 * This is a little silly; with sessions there's no real
940 * use for the callback address. Use the peer address
941 * as a reasonable default for now, but consider fixing
942 * the rpc client not to require an address in the
943 * future:
944 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400945 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
946 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400947 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400948 nfsd4_probe_callback(clp);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400949 return new;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300950}
951
Benny Halevy9089f1b2010-05-12 00:12:26 +0300952/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300953static struct nfsd4_session *
954find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
955{
956 struct nfsd4_session *elem;
957 int idx;
958
959 dump_sessionid(__func__, sessionid);
960 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300961 /* Search in the appropriate list */
962 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300963 if (!memcmp(elem->se_sessionid.data, sessionid->data,
964 NFS4_MAX_SESSIONID_LEN)) {
965 return elem;
966 }
967 }
968
969 dprintk("%s: session not found\n", __func__);
970 return NULL;
971}
972
Benny Halevy9089f1b2010-05-12 00:12:26 +0300973/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300974static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300975unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300976{
977 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400978 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +0300979 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400980 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +0300981}
982
Benny Halevy36acb662010-05-12 00:13:04 +0300983/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static inline void
Benny Halevy36acb662010-05-12 00:13:04 +0300985renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Benny Halevy07cd4902010-05-12 00:13:41 +0300987 if (is_client_expired(clp)) {
988 dprintk("%s: client (clientid %08x/%08x) already expired\n",
989 __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);
998 list_move_tail(&clp->cl_lru, &client_lru);
999 clp->cl_time = get_seconds();
1000}
1001
Benny Halevy36acb662010-05-12 00:13:04 +03001002static inline void
1003renew_client(struct nfs4_client *clp)
1004{
1005 spin_lock(&client_lock);
1006 renew_client_locked(clp);
1007 spin_unlock(&client_lock);
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1011static int
1012STALE_CLIENTID(clientid_t *clid)
1013{
1014 if (clid->cl_boot == boot_time)
1015 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001016 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1017 clid->cl_boot, clid->cl_id, boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return 1;
1019}
1020
1021/*
1022 * XXX Should we use a slab cache ?
1023 * This type of memory management is somewhat inefficient, but we use it
1024 * anyway since SETCLIENTID is not a common operation.
1025 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001026static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 struct nfs4_client *clp;
1029
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001030 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1031 if (clp == NULL)
1032 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001033 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001034 if (clp->cl_name.data == NULL) {
1035 kfree(clp);
1036 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001038 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return clp;
1040}
1041
1042static inline void
1043free_client(struct nfs4_client *clp)
1044{
Benny Halevy508dc6e2012-02-23 17:40:52 -08001045 BUG_ON(!spin_is_locked(&client_lock));
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001046 while (!list_empty(&clp->cl_sessions)) {
1047 struct nfsd4_session *ses;
1048 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1049 se_perclnt);
1050 list_del(&ses->se_perclnt);
Benny Halevy508dc6e2012-02-23 17:40:52 -08001051 nfsd4_put_session_locked(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (clp->cl_cred.cr_group_info)
1054 put_group_info(clp->cl_cred.cr_group_info);
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -05001055 kfree(clp->cl_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 kfree(clp->cl_name.data);
1057 kfree(clp);
1058}
1059
Benny Halevyd7682982010-05-12 00:13:54 +03001060void
1061release_session_client(struct nfsd4_session *session)
1062{
1063 struct nfs4_client *clp = session->se_client;
1064
1065 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
1066 return;
1067 if (is_client_expired(clp)) {
1068 free_client(clp);
1069 session->se_client = NULL;
1070 } else
1071 renew_client_locked(clp);
1072 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +03001073}
1074
Benny Halevy84d38ac2010-05-12 00:13:16 +03001075/* must be called under the client_lock */
1076static inline void
1077unhash_client_locked(struct nfs4_client *clp)
1078{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001079 struct nfsd4_session *ses;
1080
Benny Halevy07cd4902010-05-12 00:13:41 +03001081 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001082 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001083 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001084 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1085 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001086 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089static void
1090expire_client(struct nfs4_client *clp)
1091{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001092 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct list_head reaplist;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 INIT_LIST_HEAD(&reaplist);
1097 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001098 while (!list_empty(&clp->cl_delegations)) {
1099 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001100 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 list_move(&dp->dl_recall_lru, &reaplist);
1102 }
1103 spin_unlock(&recall_lock);
1104 while (!list_empty(&reaplist)) {
1105 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 unhash_delegation(dp);
1107 }
NeilBrownea1da632005-06-23 22:04:17 -07001108 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001109 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1110 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001112 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001113 if (clp->cl_cb_conn.cb_xprt)
1114 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001115 list_del(&clp->cl_idhash);
1116 list_del(&clp->cl_strhash);
1117 spin_lock(&client_lock);
1118 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +03001119 if (atomic_read(&clp->cl_refcount) == 0)
1120 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001121 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001124static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1125{
1126 memcpy(target->cl_verifier.data, source->data,
1127 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001130static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1133 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1134}
1135
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001136static void copy_cred(struct svc_cred *target, struct svc_cred *source)
1137{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 target->cr_uid = source->cr_uid;
1139 target->cr_gid = source->cr_gid;
1140 target->cr_group_info = source->cr_group_info;
1141 get_group_info(target->cr_group_info);
1142}
1143
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001144static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001145{
NeilBrowna55370a2005-06-23 22:03:52 -07001146 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001150same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1151{
1152 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001156same_clid(clientid_t *cl1, clientid_t *cl2)
1157{
1158 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161/* XXX what about NGROUP */
1162static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001163same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1164{
1165 return cr1->cr_uid == cr2->cr_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001168static void gen_clid(struct nfs4_client *clp)
1169{
1170 static u32 current_clientid = 1;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 clp->cl_clientid.cl_boot = boot_time;
1173 clp->cl_clientid.cl_id = current_clientid++;
1174}
1175
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001176static void gen_confirm(struct nfs4_client *clp)
1177{
Chuck Leverab4684d2012-03-02 17:13:50 -05001178 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001179 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Chuck Leverab4684d2012-03-02 17:13:50 -05001181 verf[0] = (__be32)get_seconds();
1182 verf[1] = (__be32)i++;
1183 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001186static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001187{
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001188 return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001189}
1190
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001191static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001192{
1193 struct nfs4_stid *s;
1194
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001195 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001196 if (!s)
1197 return NULL;
1198 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001199 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001200 return NULL;
1201}
1202
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001203static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1204 struct svc_rqst *rqstp, nfs4_verifier *verf)
1205{
1206 struct nfs4_client *clp;
1207 struct sockaddr *sa = svc_addr(rqstp);
1208 char *princ;
1209
1210 clp = alloc_client(name);
1211 if (clp == NULL)
1212 return NULL;
1213
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001214 INIT_LIST_HEAD(&clp->cl_sessions);
1215
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001216 princ = svc_gss_principal(rqstp);
1217 if (princ) {
1218 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1219 if (clp->cl_principal == NULL) {
Benny Halevy508dc6e2012-02-23 17:40:52 -08001220 spin_lock(&client_lock);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001221 free_client(clp);
Benny Halevy508dc6e2012-02-23 17:40:52 -08001222 spin_unlock(&client_lock);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001223 return NULL;
1224 }
1225 }
1226
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001227 idr_init(&clp->cl_stateids);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001228 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
Benny Halevy46583e22010-05-12 00:13:29 +03001229 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001230 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001231 INIT_LIST_HEAD(&clp->cl_idhash);
1232 INIT_LIST_HEAD(&clp->cl_strhash);
1233 INIT_LIST_HEAD(&clp->cl_openowners);
1234 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001235 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba252011-01-10 16:44:41 -05001236 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001237 spin_lock_init(&clp->cl_lock);
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001238 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
Benny Halevy07cd4902010-05-12 00:13:41 +03001239 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001240 clear_bit(0, &clp->cl_cb_slot_busy);
1241 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1242 copy_verf(clp, verf);
1243 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1244 clp->cl_flavor = rqstp->rq_flavor;
1245 copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1246 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001247 clp->cl_cb_session = NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001248 return clp;
1249}
1250
NeilBrownfd39ca92005-06-23 22:04:03 -07001251static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1253{
1254 unsigned int idhashval;
1255
1256 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1257 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1258 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001259 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
NeilBrownfd39ca92005-06-23 22:04:03 -07001262static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263move_to_confirmed(struct nfs4_client *clp)
1264{
1265 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1266 unsigned int strhashval;
1267
1268 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Akinobu Mitaf1166292006-06-26 00:24:46 -07001269 list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07001270 strhashval = clientstr_hashval(clp->cl_recdir);
Benny Halevy328efba2010-05-12 00:12:51 +03001271 list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 renew_client(clp);
1273}
1274
1275static struct nfs4_client *
1276find_confirmed_client(clientid_t *clid)
1277{
1278 struct nfs4_client *clp;
1279 unsigned int idhashval = clientid_hashval(clid->cl_id);
1280
1281 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001282 if (same_clid(&clp->cl_clientid, clid)) {
1283 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 return NULL;
1288}
1289
1290static struct nfs4_client *
1291find_unconfirmed_client(clientid_t *clid)
1292{
1293 struct nfs4_client *clp;
1294 unsigned int idhashval = clientid_hashval(clid->cl_id);
1295
1296 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001297 if (same_clid(&clp->cl_clientid, clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return clp;
1299 }
1300 return NULL;
1301}
1302
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001303static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001304{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001305 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001306}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001307
NeilBrown28ce6052005-06-23 22:03:56 -07001308static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001309find_confirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001310{
1311 struct nfs4_client *clp;
1312
1313 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001314 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001315 return clp;
1316 }
1317 return NULL;
1318}
1319
1320static struct nfs4_client *
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001321find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
NeilBrown28ce6052005-06-23 22:03:56 -07001322{
1323 struct nfs4_client *clp;
1324
1325 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001326 if (same_name(clp->cl_recdir, dname))
NeilBrown28ce6052005-06-23 22:03:56 -07001327 return clp;
1328 }
1329 return NULL;
1330}
1331
NeilBrownfd39ca92005-06-23 22:04:03 -07001332static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001333gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001335 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001336 struct sockaddr *sa = svc_addr(rqstp);
1337 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001338 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Jeff Layton7077ecb2009-08-14 12:57:58 -04001340 /* Currently, we only support tcp and tcp6 for the callback channel */
1341 if (se->se_callback_netid_len == 3 &&
1342 !memcmp(se->se_callback_netid_val, "tcp", 3))
1343 expected_family = AF_INET;
1344 else if (se->se_callback_netid_len == 4 &&
1345 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1346 expected_family = AF_INET6;
1347 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto out_err;
1349
Stanislav Kinsburskyf2ac4dc2012-01-13 13:09:27 +04001350 conn->cb_addrlen = rpc_uaddr2sockaddr(&init_net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001351 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001352 (struct sockaddr *)&conn->cb_addr,
1353 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001354
J. Bruce Fields07263f12010-05-31 19:09:40 -04001355 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001357
J. Bruce Fields07263f12010-05-31 19:09:40 -04001358 if (conn->cb_addr.ss_family == AF_INET6)
1359 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001360
J. Bruce Fields07263f12010-05-31 19:09:40 -04001361 conn->cb_prog = se->se_callback_prog;
1362 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001363 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return;
1365out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001366 conn->cb_addr.ss_family = AF_UNSPEC;
1367 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001368 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 "will not receive delegations\n",
1370 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return;
1373}
1374
Andy Adamson074fe892009-04-03 08:28:15 +03001375/*
Andy Adamson557ce262009-08-28 08:45:04 -04001376 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001377 */
1378void
1379nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1380{
Andy Adamson557ce262009-08-28 08:45:04 -04001381 struct nfsd4_slot *slot = resp->cstate.slot;
1382 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001383
Andy Adamson557ce262009-08-28 08:45:04 -04001384 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001385
Andy Adamson557ce262009-08-28 08:45:04 -04001386 slot->sl_opcnt = resp->opcnt;
1387 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001388
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001389 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001390 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001391 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001392 return;
1393 }
Andy Adamson557ce262009-08-28 08:45:04 -04001394 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1395 base = (char *)resp->cstate.datap -
1396 (char *)resp->xbuf->head[0].iov_base;
1397 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1398 slot->sl_datalen))
1399 WARN("%s: sessions DRC could not cache compound\n", __func__);
1400 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001401}
1402
1403/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001404 * Encode the replay sequence operation from the slot values.
1405 * If cachethis is FALSE encode the uncached rep error on the next
1406 * operation which sets resp->p and increments resp->opcnt for
1407 * nfs4svc_encode_compoundres.
1408 *
Andy Adamson074fe892009-04-03 08:28:15 +03001409 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001410static __be32
1411nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1412 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001413{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001414 struct nfsd4_op *op;
1415 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001416
Andy Adamsonabfabf82009-07-23 19:02:18 -04001417 /* Encode the replayed sequence operation */
1418 op = &args->ops[resp->opcnt - 1];
1419 nfsd4_encode_operation(resp, op);
1420
1421 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001422 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001423 op = &args->ops[resp->opcnt++];
1424 op->status = nfserr_retry_uncached_rep;
1425 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001426 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001427 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001428}
1429
1430/*
Andy Adamson557ce262009-08-28 08:45:04 -04001431 * The sequence operation is not cached because we can use the slot and
1432 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001433 */
1434__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001435nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1436 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001437{
Andy Adamson557ce262009-08-28 08:45:04 -04001438 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001439 __be32 status;
1440
Andy Adamson557ce262009-08-28 08:45:04 -04001441 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001442
Andy Adamsonabfabf82009-07-23 19:02:18 -04001443 /* Either returns 0 or nfserr_retry_uncached */
1444 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1445 if (status == nfserr_retry_uncached_rep)
1446 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001447
Andy Adamson557ce262009-08-28 08:45:04 -04001448 /* The sequence operation has been encoded, cstate->datap set. */
1449 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001450
Andy Adamson557ce262009-08-28 08:45:04 -04001451 resp->opcnt = slot->sl_opcnt;
1452 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1453 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001454
1455 return status;
1456}
1457
Andy Adamson0733d212009-04-03 08:28:01 +03001458/*
1459 * Set the exchange_id flags returned by the server.
1460 */
1461static void
1462nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1463{
1464 /* pNFS is not supported */
1465 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1466
1467 /* Referrals are supported, Migration is not. */
1468 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1469
1470 /* set the wire flags to return to client. */
1471 clid->flags = new->cl_exchange_flags;
1472}
1473
Al Virob37ad282006-10-19 23:28:59 -07001474__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001475nfsd4_exchange_id(struct svc_rqst *rqstp,
1476 struct nfsd4_compound_state *cstate,
1477 struct nfsd4_exchange_id *exid)
1478{
Andy Adamson0733d212009-04-03 08:28:01 +03001479 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001480 __be32 status;
Andy Adamson0733d212009-04-03 08:28:01 +03001481 unsigned int strhashval;
1482 char dname[HEXDIR_LEN];
Jeff Layton363168b2009-08-14 12:57:56 -04001483 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001484 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001485 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamson0733d212009-04-03 08:28:01 +03001486
Jeff Layton363168b2009-08-14 12:57:56 -04001487 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001488 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001489 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001490 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001491 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001492
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001493 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001494 return nfserr_inval;
1495
1496 /* Currently only support SP4_NONE */
1497 switch (exid->spa_how) {
1498 case SP4_NONE:
1499 break;
1500 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001501 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001502 default:
1503 BUG(); /* checked by xdr code */
1504 case SP4_MACH_CRED:
1505 return nfserr_serverfault; /* no excuse :-/ */
1506 }
1507
1508 status = nfs4_make_rec_clidname(dname, &exid->clname);
1509
1510 if (status)
1511 goto error;
1512
1513 strhashval = clientstr_hashval(dname);
1514
1515 nfs4_lock_state();
1516 status = nfs_ok;
1517
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001518 conf = find_confirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001519 if (conf) {
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001520 if (!clp_used_exchangeid(conf)) {
1521 status = nfserr_clid_inuse; /* XXX: ? */
1522 goto out;
1523 }
Andy Adamson0733d212009-04-03 08:28:01 +03001524 if (!same_verf(&verf, &conf->cl_verifier)) {
1525 /* 18.35.4 case 8 */
1526 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1527 status = nfserr_not_same;
1528 goto out;
1529 }
1530 /* Client reboot: destroy old state */
1531 expire_client(conf);
1532 goto out_new;
1533 }
1534 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1535 /* 18.35.4 case 9 */
1536 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1537 status = nfserr_perm;
1538 goto out;
1539 }
1540 expire_client(conf);
1541 goto out_new;
1542 }
Andy Adamson0733d212009-04-03 08:28:01 +03001543 /*
1544 * Set bit when the owner id and verifier map to an already
1545 * confirmed client id (18.35.3).
1546 */
1547 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1548
1549 /*
1550 * Falling into 18.35.4 case 2, possible router replay.
1551 * Leave confirmed record intact and return same result.
1552 */
1553 copy_verf(conf, &verf);
1554 new = conf;
1555 goto out_copy;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001556 }
1557
1558 /* 18.35.4 case 7 */
1559 if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1560 status = nfserr_noent;
1561 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001562 }
1563
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001564 unconf = find_unconfirmed_client_by_str(dname, strhashval);
Andy Adamson0733d212009-04-03 08:28:01 +03001565 if (unconf) {
1566 /*
1567 * Possible retry or client restart. Per 18.35.4 case 4,
1568 * a new unconfirmed record should be generated regardless
1569 * of whether any properties have changed.
1570 */
1571 expire_client(unconf);
1572 }
1573
1574out_new:
1575 /* Normal case */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001576 new = create_client(exid->clname, dname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001577 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001578 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001579 goto out;
1580 }
1581
Andy Adamson0733d212009-04-03 08:28:01 +03001582 gen_clid(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001583 add_to_unconfirmed(new, strhashval);
1584out_copy:
1585 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1586 exid->clientid.cl_id = new->cl_clientid.cl_id;
1587
Andy Adamson38eb76a2009-04-03 08:28:32 +03001588 exid->seqid = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001589 nfsd4_set_ex_flags(new, exid);
1590
1591 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001592 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001593 status = nfs_ok;
1594
1595out:
1596 nfs4_unlock_state();
1597error:
1598 dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1599 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001600}
1601
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001602static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001603check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001604{
Andy Adamson88e588d2009-07-23 19:02:15 -04001605 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1606 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001607
1608 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001609 if (slot_inuse) {
1610 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001611 return nfserr_jukebox;
1612 else
1613 return nfserr_seq_misordered;
1614 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001615 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001616 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001617 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001618 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001619 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001620 return nfserr_seq_misordered;
1621}
1622
Andy Adamson49557cc2009-07-23 19:02:16 -04001623/*
1624 * Cache the create session result into the create session single DRC
1625 * slot cache by saving the xdr structure. sl_seqid has been set.
1626 * Do this for solo or embedded create session operations.
1627 */
1628static void
1629nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001630 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001631{
1632 slot->sl_status = nfserr;
1633 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1634}
1635
1636static __be32
1637nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1638 struct nfsd4_clid_slot *slot)
1639{
1640 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1641 return slot->sl_status;
1642}
1643
Mi Jinlong1b74c252011-07-14 14:50:17 +08001644#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1645 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1646 1 + /* MIN tag is length with zero, only length */ \
1647 3 + /* version, opcount, opcode */ \
1648 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1649 /* seqid, slotID, slotID, cache */ \
1650 4 ) * sizeof(__be32))
1651
1652#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1653 2 + /* verifier: AUTH_NULL, length 0 */\
1654 1 + /* status */ \
1655 1 + /* MIN tag is length with zero, only length */ \
1656 3 + /* opcount, opcode, opstatus*/ \
1657 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1658 /* seqid, slotID, slotID, slotID, status */ \
1659 5 ) * sizeof(__be32))
1660
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001661static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001662{
1663 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1664 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1665}
1666
Andy Adamson069b6ad2009-04-03 08:27:58 +03001667__be32
1668nfsd4_create_session(struct svc_rqst *rqstp,
1669 struct nfsd4_compound_state *cstate,
1670 struct nfsd4_create_session *cr_ses)
1671{
Jeff Layton363168b2009-08-14 12:57:56 -04001672 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001673 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001674 struct nfsd4_session *new;
Andy Adamson49557cc2009-07-23 19:02:16 -04001675 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001676 bool confirm_me = false;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001677 __be32 status = 0;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001678
Mi Jinlonga62573d2011-03-23 17:57:07 +08001679 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1680 return nfserr_inval;
1681
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001682 nfs4_lock_state();
1683 unconf = find_unconfirmed_client(&cr_ses->clientid);
1684 conf = find_confirmed_client(&cr_ses->clientid);
1685
1686 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001687 cs_slot = &conf->cl_cs_slot;
1688 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001689 if (status == nfserr_replay_cache) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001690 dprintk("Got a create_session replay! seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001691 cs_slot->sl_seqid);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001692 /* Return the cached reply status */
Andy Adamson49557cc2009-07-23 19:02:16 -04001693 status = nfsd4_replay_create_session(cr_ses, cs_slot);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001694 goto out;
Andy Adamson49557cc2009-07-23 19:02:16 -04001695 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001696 status = nfserr_seq_misordered;
1697 dprintk("Sequence misordered!\n");
1698 dprintk("Expected seqid= %d but got seqid= %d\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001699 cs_slot->sl_seqid, cr_ses->seqid);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001700 goto out;
1701 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001702 } else if (unconf) {
1703 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001704 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001705 status = nfserr_clid_inuse;
1706 goto out;
1707 }
1708
Andy Adamson49557cc2009-07-23 19:02:16 -04001709 cs_slot = &unconf->cl_cs_slot;
1710 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001711 if (status) {
1712 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001713 status = nfserr_seq_misordered;
J. Bruce Fieldscd5b8142010-10-02 17:03:35 -04001714 goto out;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001715 }
1716
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001717 confirm_me = true;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001718 conf = unconf;
1719 } else {
1720 status = nfserr_stale_clientid;
1721 goto out;
1722 }
1723
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001724 /*
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001725 * XXX: we should probably set this at creation time, and check
1726 * for consistent minorversion use throughout:
1727 */
1728 conf->cl_minorversion = 1;
1729 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001730 * We do not support RDMA or persistent sessions
1731 */
1732 cr_ses->flags &= ~SESSION4_PERSIST;
1733 cr_ses->flags &= ~SESSION4_RDMA;
1734
Mi Jinlong1b74c252011-07-14 14:50:17 +08001735 status = nfserr_toosmall;
1736 if (check_forechannel_attrs(cr_ses->fore_channel))
1737 goto out;
1738
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001739 status = nfserr_jukebox;
1740 new = alloc_init_session(rqstp, conf, cr_ses);
1741 if (!new)
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001742 goto out;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001743 status = nfs_ok;
1744 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001745 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001746 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1747 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001748 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001749 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001750
Andy Adamson49557cc2009-07-23 19:02:16 -04001751 /* cache solo and embedded create sessions under the state lock */
1752 nfsd4_cache_create_session(cr_ses, cs_slot, status);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001753 if (confirm_me)
1754 move_to_confirmed(conf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001755out:
1756 nfs4_unlock_state();
1757 dprintk("%s returns %d\n", __func__, ntohl(status));
1758 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001759}
1760
J. Bruce Fields57716352010-04-21 12:27:19 -04001761static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1762{
1763 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1764 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1765
1766 return argp->opcnt == resp->opcnt;
1767}
1768
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001769static __be32 nfsd4_map_bcts_dir(u32 *dir)
1770{
1771 switch (*dir) {
1772 case NFS4_CDFC4_FORE:
1773 case NFS4_CDFC4_BACK:
1774 return nfs_ok;
1775 case NFS4_CDFC4_FORE_OR_BOTH:
1776 case NFS4_CDFC4_BACK_OR_BOTH:
1777 *dir = NFS4_CDFC4_BOTH;
1778 return nfs_ok;
1779 };
1780 return nfserr_inval;
1781}
1782
1783__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1784 struct nfsd4_compound_state *cstate,
1785 struct nfsd4_bind_conn_to_session *bcts)
1786{
1787 __be32 status;
1788
1789 if (!nfsd4_last_compound_op(rqstp))
1790 return nfserr_not_only_op;
1791 spin_lock(&client_lock);
1792 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1793 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1794 * client_lock iself: */
1795 if (cstate->session) {
1796 nfsd4_get_session(cstate->session);
1797 atomic_inc(&cstate->session->se_client->cl_refcount);
1798 }
1799 spin_unlock(&client_lock);
1800 if (!cstate->session)
1801 return nfserr_badsession;
1802
1803 status = nfsd4_map_bcts_dir(&bcts->dir);
Bryan Schumaker1db2b9d2011-04-27 15:47:15 -04001804 if (!status)
1805 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1806 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001807}
1808
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001809static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1810{
1811 if (!session)
1812 return 0;
1813 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1814}
1815
Andy Adamson069b6ad2009-04-03 08:27:58 +03001816__be32
1817nfsd4_destroy_session(struct svc_rqst *r,
1818 struct nfsd4_compound_state *cstate,
1819 struct nfsd4_destroy_session *sessionid)
1820{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001821 struct nfsd4_session *ses;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001822 __be32 status = nfserr_badsession;
Benny Halevye10e0cf2009-04-03 08:28:38 +03001823
1824 /* Notes:
1825 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1826 * - Should we return nfserr_back_chan_busy if waiting for
1827 * callbacks on to-be-destroyed session?
1828 * - Do we need to clear any callback info from previous session?
1829 */
1830
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001831 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001832 if (!nfsd4_last_compound_op(r))
1833 return nfserr_not_only_op;
1834 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001835 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001836 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001837 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1838 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001839 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001840 goto out;
1841 }
1842
1843 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001844 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001845
Benny Halevyab707e152010-05-12 00:14:06 +03001846 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001847 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001848 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001849
Benny Halevy508dc6e2012-02-23 17:40:52 -08001850 spin_lock(&client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001851 nfsd4_del_conns(ses);
Benny Halevy508dc6e2012-02-23 17:40:52 -08001852 nfsd4_put_session_locked(ses);
1853 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001854 status = nfs_ok;
1855out:
1856 dprintk("%s returns %d\n", __func__, ntohl(status));
1857 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001858}
1859
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001860static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001861{
1862 struct nfsd4_conn *c;
1863
1864 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001865 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001866 return c;
1867 }
1868 }
1869 return NULL;
1870}
1871
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001872static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001873{
1874 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001875 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001876 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001877
1878 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001879 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001880 if (c) {
1881 spin_unlock(&clp->cl_lock);
1882 free_conn(new);
1883 return;
1884 }
1885 __nfsd4_hash_conn(new, ses);
1886 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001887 ret = nfsd4_register_conn(new);
1888 if (ret)
1889 /* oops; xprt is already down: */
1890 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001891 return;
1892}
1893
Mi Jinlong868b89c2011-04-27 09:09:58 +08001894static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1895{
1896 struct nfsd4_compoundargs *args = rqstp->rq_argp;
1897
1898 return args->opcnt > session->se_fchannel.maxops;
1899}
1900
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001901static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1902 struct nfsd4_session *session)
1903{
1904 struct xdr_buf *xb = &rqstp->rq_arg;
1905
1906 return xb->len > session->se_fchannel.maxreq_sz;
1907}
1908
Andy Adamson069b6ad2009-04-03 08:27:58 +03001909__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03001910nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03001911 struct nfsd4_compound_state *cstate,
1912 struct nfsd4_sequence *seq)
1913{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001914 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001915 struct nfsd4_session *session;
1916 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001917 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001918 __be32 status;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001919
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03001920 if (resp->opcnt != 1)
1921 return nfserr_sequence_pos;
1922
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001923 /*
1924 * Will be either used or freed by nfsd4_sequence_check_conn
1925 * below.
1926 */
1927 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1928 if (!conn)
1929 return nfserr_jukebox;
1930
Benny Halevy9089f1b2010-05-12 00:12:26 +03001931 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001932 status = nfserr_badsession;
1933 session = find_in_sessionid_hashtbl(&seq->sessionid);
1934 if (!session)
1935 goto out;
1936
Mi Jinlong868b89c2011-04-27 09:09:58 +08001937 status = nfserr_too_many_ops;
1938 if (nfsd4_session_too_many_ops(rqstp, session))
1939 goto out;
1940
Mi Jinlongae82a8d2011-07-14 14:56:02 +08001941 status = nfserr_req_too_big;
1942 if (nfsd4_request_too_big(rqstp, session))
1943 goto out;
1944
Benny Halevyb85d4c02009-04-03 08:28:08 +03001945 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03001946 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001947 goto out;
1948
Andy Adamson557ce262009-08-28 08:45:04 -04001949 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03001950 dprintk("%s: slotid %d\n", __func__, seq->slotid);
1951
Andy Adamsona8dfdae2009-08-28 08:45:02 -04001952 /* We do not negotiate the number of slots yet, so set the
1953 * maxslots to the session maxreqs which is used to encode
1954 * sr_highest_slotid and the sr_target_slot id to maxslots */
1955 seq->maxslots = session->se_fchannel.maxreqs;
1956
J. Bruce Fields73e79482012-02-13 16:39:00 -05001957 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
1958 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001959 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001960 status = nfserr_seq_misordered;
1961 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
1962 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001963 cstate->slot = slot;
1964 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03001965 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04001966 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03001967 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03001968 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001969 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001970 }
1971 if (status)
1972 goto out;
1973
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001974 nfsd4_sequence_check_conn(conn, session);
1975 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04001976
Benny Halevyb85d4c02009-04-03 08:28:08 +03001977 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03001978 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001979 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05001980 if (seq->cachethis)
1981 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001982 else
1983 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001984
1985 cstate->slot = slot;
1986 cstate->session = session;
1987
Benny Halevyb85d4c02009-04-03 08:28:08 +03001988out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04001989 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03001990 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001991 struct nfs4_client *clp = session->se_client;
1992
Benny Halevy36acb662010-05-12 00:13:04 +03001993 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05001994 atomic_inc(&clp->cl_refcount);
Benny Halevy54237322011-10-19 19:12:58 -07001995 switch (clp->cl_cb_state) {
1996 case NFSD4_CB_DOWN:
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07001997 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevy54237322011-10-19 19:12:58 -07001998 break;
1999 case NFSD4_CB_FAULT:
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07002000 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
Benny Halevy54237322011-10-19 19:12:58 -07002001 break;
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07002002 default:
2003 seq->status_flags = 0;
Benny Halevy54237322011-10-19 19:12:58 -07002004 }
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002005 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002006 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03002007 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002008 dprintk("%s: return %d\n", __func__, ntohl(status));
2009 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002010}
2011
Mi Jinlong345c2842011-10-20 17:51:39 +08002012static inline bool has_resources(struct nfs4_client *clp)
2013{
2014 return !list_empty(&clp->cl_openowners)
2015 || !list_empty(&clp->cl_delegations)
2016 || !list_empty(&clp->cl_sessions);
2017}
2018
2019__be32
2020nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2021{
2022 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002023 __be32 status = 0;
Mi Jinlong345c2842011-10-20 17:51:39 +08002024
2025 nfs4_lock_state();
2026 unconf = find_unconfirmed_client(&dc->clientid);
2027 conf = find_confirmed_client(&dc->clientid);
2028
2029 if (conf) {
2030 clp = conf;
2031
2032 if (!is_client_expired(conf) && has_resources(conf)) {
2033 status = nfserr_clientid_busy;
2034 goto out;
2035 }
2036
2037 /* rfc5661 18.50.3 */
2038 if (cstate->session && conf == cstate->session->se_client) {
2039 status = nfserr_clientid_busy;
2040 goto out;
2041 }
2042 } else if (unconf)
2043 clp = unconf;
2044 else {
2045 status = nfserr_stale_clientid;
2046 goto out;
2047 }
2048
2049 expire_client(clp);
2050out:
2051 nfs4_unlock_state();
2052 dprintk("%s return %d\n", __func__, ntohl(status));
2053 return status;
2054}
2055
Andy Adamson069b6ad2009-04-03 08:27:58 +03002056__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002057nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2058{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002059 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002060
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002061 if (rc->rca_one_fs) {
2062 if (!cstate->current_fh.fh_dentry)
2063 return nfserr_nofilehandle;
2064 /*
2065 * We don't take advantage of the rca_one_fs case.
2066 * That's OK, it's optional, we can safely ignore it.
2067 */
2068 return nfs_ok;
2069 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002070
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002071 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002072 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002073 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2074 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002075 goto out;
2076
2077 status = nfserr_stale_clientid;
2078 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002079 /*
2080 * The following error isn't really legal.
2081 * But we only get here if the client just explicitly
2082 * destroyed the client. Surely it no longer cares what
2083 * error it gets back on an operation for the dead
2084 * client.
2085 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002086 goto out;
2087
2088 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002089 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002090out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002091 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002092 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002093}
2094
2095__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002096nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2097 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002099 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 nfs4_verifier clverifier = setclid->se_verf;
2101 unsigned int strhashval;
NeilBrown28ce6052005-06-23 22:03:56 -07002102 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002103 __be32 status;
NeilBrowna55370a2005-06-23 22:03:52 -07002104 char dname[HEXDIR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
NeilBrowna55370a2005-06-23 22:03:52 -07002106 status = nfs4_make_rec_clidname(dname, &clname);
2107 if (status)
Neil Brown73aea4e2005-09-13 01:25:39 -07002108 return status;
NeilBrowna55370a2005-06-23 22:03:52 -07002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
2111 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2112 * We get here on a DRC miss.
2113 */
2114
NeilBrowna55370a2005-06-23 22:03:52 -07002115 strhashval = clientstr_hashval(dname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 nfs4_lock_state();
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002118 conf = find_confirmed_client_by_str(dname, strhashval);
NeilBrown28ce6052005-06-23 22:03:56 -07002119 if (conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002120 /* RFC 3530 14.2.33 CASE 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002122 if (clp_used_exchangeid(conf))
2123 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002124 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002125 char addr_str[INET6_ADDRSTRLEN];
2126 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2127 sizeof(addr_str));
2128 dprintk("NFSD: setclientid: string in use by client "
2129 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 goto out;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002133 /*
2134 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2135 * has a description of SETCLIENTID request processing consisting
2136 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2137 */
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002138 unconf = find_unconfirmed_client_by_str(dname, strhashval);
J. Bruce Fields3e772462011-08-10 19:07:33 -04002139 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (!conf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002141 /*
2142 * RFC 3530 14.2.33 CASE 4:
2143 * placed first, because it is the normal case
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
2145 if (unconf)
2146 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002147 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002148 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 gen_clid(new);
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002151 } else if (same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002153 * RFC 3530 14.2.33 CASE 1:
2154 * probable callback update
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 */
NeilBrown31f4a6c2005-06-23 22:04:06 -07002156 if (unconf) {
2157 /* Note this is removing unconfirmed {*x***},
2158 * which is stronger than RFC recommended {vxc**}.
2159 * This has the advantage that there is at most
2160 * one {*x***} in either list at any time.
2161 */
2162 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002164 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002165 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 copy_clid(new, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 } else if (!unconf) {
2169 /*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002170 * RFC 3530 14.2.33 CASE 2:
2171 * probable client reboot; state will be removed if
2172 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002174 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002175 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 gen_clid(new);
J. Bruce Fields49ba8782007-11-19 19:09:50 -05002178 } else {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002179 /*
2180 * RFC 3530 14.2.33 CASE 3:
2181 * probable client reboot; state will be removed if
2182 * confirmed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 */
2184 expire_client(unconf);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002185 new = create_client(clname, dname, rqstp, &clverifier);
NeilBrowna55370a2005-06-23 22:03:52 -07002186 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 gen_clid(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002190 /*
2191 * XXX: we should probably set this at creation time, and check
2192 * for consistent minorversion use throughout:
2193 */
2194 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002195 gen_callback(new, setclid, rqstp);
J. Bruce Fieldsc175b832007-08-09 18:34:32 -04002196 add_to_unconfirmed(new, strhashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2198 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2199 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2200 status = nfs_ok;
2201out:
2202 nfs4_unlock_state();
2203 return status;
2204}
2205
2206
2207/*
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002208 * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2209 * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2210 * bullets, labeled as CASE1 - CASE4 below.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 */
Al Virob37ad282006-10-19 23:28:59 -07002212__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002213nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2214 struct nfsd4_compound_state *cstate,
2215 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
NeilBrown21ab45a2005-06-23 22:04:14 -07002217 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2219 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002220 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 if (STALE_CLIENTID(clid))
2223 return nfserr_stale_clientid;
2224 /*
2225 * XXX The Duplicate Request Cache (DRC) has been checked (??)
2226 * We get here on a DRC miss.
2227 */
2228
2229 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002230
2231 conf = find_confirmed_client(clid);
2232 unconf = find_unconfirmed_client(clid);
2233
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002234 /*
2235 * section 14.2.34 of RFC 3530 has a description of
2236 * SETCLIENTID_CONFIRM request processing consisting
2237 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2238 */
Chuck Leverf07ea102012-05-15 17:42:08 -04002239 status = nfserr_clid_inuse;
J. Bruce Fields366e0c12007-11-20 15:54:10 -05002240 if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002241 /*
2242 * RFC 3530 14.2.34 CASE 1:
2243 * callback update
2244 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002245 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 status = nfserr_clid_inuse;
2247 else {
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002248 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2249 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002250 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 status = nfs_ok;
2252 }
J. Bruce Fieldsf3aba4e2007-11-20 16:52:07 -05002253 } else if (conf && !unconf) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002254 /*
2255 * RFC 3530 14.2.34 CASE 2:
2256 * probable retransmitted request; play it safe and
2257 * do nothing.
NeilBrown7c79f732005-06-23 22:04:13 -07002258 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002259 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 status = nfserr_clid_inuse;
NeilBrown21ab45a2005-06-23 22:04:14 -07002261 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 status = nfs_ok;
NeilBrown7c79f732005-06-23 22:04:13 -07002263 } else if (!conf && unconf
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002264 && same_verf(&unconf->cl_confirm, &confirm)) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002265 /*
2266 * RFC 3530 14.2.34 CASE 3:
2267 * Normal case; new or rebooted client:
NeilBrown7c79f732005-06-23 22:04:13 -07002268 */
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002269 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 status = nfserr_clid_inuse;
2271 } else {
NeilBrown1a69c172005-06-23 22:04:08 -07002272 unsigned int hash =
2273 clientstr_hashval(unconf->cl_recdir);
2274 conf = find_confirmed_client_by_str(unconf->cl_recdir,
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002275 hash);
NeilBrown1a69c172005-06-23 22:04:08 -07002276 if (conf) {
Jeff Layton2a4317c2012-03-21 16:42:43 -04002277 nfsd4_client_record_remove(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002278 expire_client(conf);
2279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 move_to_confirmed(unconf);
NeilBrown21ab45a2005-06-23 22:04:14 -07002281 conf = unconf;
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -04002282 nfsd4_probe_callback(conf);
NeilBrown1a69c172005-06-23 22:04:08 -07002283 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002285 } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2286 && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
NeilBrown7c79f732005-06-23 22:04:13 -07002287 &confirm)))) {
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002288 /*
2289 * RFC 3530 14.2.34 CASE 4:
2290 * Client probably hasn't noticed that we rebooted yet.
NeilBrown7c79f732005-06-23 22:04:13 -07002291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 status = nfserr_stale_clientid;
NeilBrown08e89872005-06-23 22:04:11 -07002293 }
Chuck Leverf07ea102012-05-15 17:42:08 -04002294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 nfs4_unlock_state();
2296 return status;
2297}
2298
J. Bruce Fields32513b42011-10-13 16:00:16 -04002299static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002301 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2302}
2303
2304/* OPEN Share state helper functions */
2305static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 unsigned int hashval = file_hashval(ino);
2308
J. Bruce Fields32513b42011-10-13 16:00:16 -04002309 atomic_set(&fp->fi_ref, 1);
2310 INIT_LIST_HEAD(&fp->fi_hash);
2311 INIT_LIST_HEAD(&fp->fi_stateids);
2312 INIT_LIST_HEAD(&fp->fi_delegations);
2313 fp->fi_inode = igrab(ino);
2314 fp->fi_had_conflict = false;
2315 fp->fi_lease = NULL;
2316 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2317 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2318 spin_lock(&recall_lock);
2319 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2320 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
2323static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002324nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002325{
NeilBrowne60d4392005-06-23 22:03:01 -07002326 if (*slab == NULL)
2327 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002328 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002329 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002330}
2331
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002332void
NeilBrowne60d4392005-06-23 22:03:01 -07002333nfsd4_free_slabs(void)
2334{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002335 nfsd4_free_slab(&openowner_slab);
2336 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002337 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002338 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002339 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Bryan Schumaker72083392011-11-01 15:24:59 -04002342int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343nfsd4_init_slabs(void)
2344{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002345 openowner_slab = kmem_cache_create("nfsd4_openowners",
2346 sizeof(struct nfs4_openowner), 0, 0, NULL);
2347 if (openowner_slab == NULL)
2348 goto out_nomem;
2349 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2350 sizeof(struct nfs4_openowner), 0, 0, NULL);
2351 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002352 goto out_nomem;
2353 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002354 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002355 if (file_slab == NULL)
2356 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002357 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002358 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002359 if (stateid_slab == NULL)
2360 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002361 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002362 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002363 if (deleg_slab == NULL)
2364 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002366out_nomem:
2367 nfsd4_free_slabs();
2368 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2369 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002372void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002374 kfree(oo->oo_owner.so_owner.data);
2375 kmem_cache_free(openowner_slab, oo);
2376}
2377
2378void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2379{
2380 kfree(lo->lo_owner.so_owner.data);
2381 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002384static void init_nfs4_replay(struct nfs4_replay *rp)
2385{
2386 rp->rp_status = nfserr_serverfault;
2387 rp->rp_buflen = 0;
2388 rp->rp_buf = rp->rp_ibuf;
2389}
2390
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002391static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 struct nfs4_stateowner *sop;
2394
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002395 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002396 if (!sop)
2397 return NULL;
2398
2399 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2400 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002401 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002402 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002404 sop->so_owner.len = owner->len;
2405
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002406 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002407 sop->so_client = clp;
2408 init_nfs4_replay(&sop->so_replay);
2409 return sop;
2410}
2411
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002412static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002413{
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002414 list_add(&oo->oo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002415 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002418static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002420 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002422 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2423 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002425 oo->oo_owner.so_is_open_owner = 1;
2426 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002427 oo->oo_flags = NFS4_OO_NEW;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002428 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002429 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002430 INIT_LIST_HEAD(&oo->oo_close_lru);
2431 hash_openowner(oo, clp, strhashval);
2432 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
J. Bruce Fields996e0932011-10-17 11:14:48 -04002435static 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 -04002436 struct nfs4_openowner *oo = open->op_openowner;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04002437 struct nfs4_client *clp = oo->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
J. Bruce Fields996e0932011-10-17 11:14:48 -04002439 init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
NeilBrownea1da632005-06-23 22:04:17 -07002440 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002441 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002442 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002443 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002444 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 stp->st_access_bmap = 0;
2447 stp->st_deny_bmap = 0;
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002448 __set_bit(open->op_share_access, &stp->st_access_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
NeilBrown4c4cd222005-07-07 17:59:27 -07002450 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
2453static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002454move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002456 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002458 list_move_tail(&oo->oo_close_lru, &close_lru);
2459 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002463same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2464 clientid_t *clid)
2465{
2466 return (sop->so_owner.len == owner->len) &&
2467 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2468 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002471static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2473{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002474 struct nfs4_stateowner *so;
2475 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002477 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
2478 if (!so->so_is_open_owner)
2479 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002480 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2481 oo = openowner(so);
2482 renew_client(oo->oo_owner.so_client);
2483 return oo;
2484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486 return NULL;
2487}
2488
2489/* search file_hashtbl[] for file */
2490static struct nfs4_file *
2491find_file(struct inode *ino)
2492{
2493 unsigned int hashval = file_hashval(ino);
2494 struct nfs4_file *fp;
2495
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002496 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002498 if (fp->fi_inode == ino) {
2499 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002500 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002504 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return NULL;
2506}
2507
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002508/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 * Called to check deny when READ with all zero stateid or
2510 * WRITE with all zero or all one stateid
2511 */
Al Virob37ad282006-10-19 23:28:59 -07002512static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2514{
2515 struct inode *ino = current_fh->fh_dentry->d_inode;
2516 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002517 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002518 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 dprintk("NFSD: nfs4_share_conflict\n");
2521
2522 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002523 if (!fp)
2524 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002525 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002527 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2528 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2529 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
NeilBrown13cd2182005-06-23 22:03:10 -07002532 ret = nfs_ok;
2533out:
2534 put_nfs4_file(fp);
2535 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002538static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /* We're assuming the state code never drops its reference
2541 * without first removing the lease. Since we're in this lease
2542 * callback (and since the lease code is serialized by the kernel
2543 * lock) we know the server hasn't removed the lease yet, we know
2544 * it's safe to take a reference: */
2545 atomic_inc(&dp->dl_count);
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Arnd Bergmann460781b2010-11-17 16:26:56 +01002549 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 dp->dl_time = get_seconds();
2551
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002552 nfsd4_cb_recall(dp);
2553}
2554
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002555/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002556static void nfsd_break_deleg_cb(struct file_lock *fl)
2557{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002558 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2559 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002560
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002561 BUG_ON(!fp);
2562 /* We assume break_lease is only called once per lease: */
2563 BUG_ON(fp->fi_had_conflict);
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002564 /*
2565 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002566 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002567 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002568 */
2569 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002571 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002572 fp->fi_had_conflict = true;
2573 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2574 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002575 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static
2579int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2580{
2581 if (arg & F_UNLCK)
2582 return lease_modify(onlist, arg);
2583 else
2584 return -EAGAIN;
2585}
2586
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002587static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002588 .lm_break = nfsd_break_deleg_cb,
2589 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590};
2591
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002592static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2593{
2594 if (nfsd4_has_session(cstate))
2595 return nfs_ok;
2596 if (seqid == so->so_seqid - 1)
2597 return nfserr_replay_me;
2598 if (seqid == so->so_seqid)
2599 return nfs_ok;
2600 return nfserr_bad_seqid;
2601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Al Virob37ad282006-10-19 23:28:59 -07002603__be32
Andy Adamson66689582009-04-03 08:28:45 +03002604nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2605 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 clientid_t *clientid = &open->op_clientid;
2608 struct nfs4_client *clp = NULL;
2609 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002610 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002611 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (STALE_CLIENTID(&open->op_clientid))
2614 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002615 /*
2616 * In case we need it later, after we've already created the
2617 * file and don't want to risk a further failure:
2618 */
2619 open->op_file = nfsd4_alloc_file();
2620 if (open->op_file == NULL)
2621 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002623 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002624 oo = find_openstateowner_str(strhashval, open);
2625 open->op_openowner = oo;
2626 if (!oo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 clp = find_confirmed_client(clientid);
2628 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002629 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002630 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002632 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002633 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002634 clp = oo->oo_owner.so_client;
2635 release_openowner(oo);
2636 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002637 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002638 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002639 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2640 if (status)
2641 return status;
2642 clp = oo->oo_owner.so_client;
2643 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002644new_owner:
2645 oo = alloc_init_open_stateowner(strhashval, clp, open);
2646 if (oo == NULL)
2647 return nfserr_jukebox;
2648 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002649alloc_stateid:
2650 open->op_stp = nfs4_alloc_stateid(clp);
2651 if (!open->op_stp)
2652 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002653 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
Al Virob37ad282006-10-19 23:28:59 -07002656static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002657nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2658{
2659 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2660 return nfserr_openmode;
2661 else
2662 return nfs_ok;
2663}
2664
Daniel Mackc47d8322011-05-16 16:38:14 +02002665static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002666{
J. Bruce Fields24a01112010-05-18 20:01:35 -04002667 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2668}
2669
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002670static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002671{
2672 struct nfs4_stid *ret;
2673
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002674 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002675 if (!ret)
2676 return NULL;
2677 return delegstateid(ret);
2678}
2679
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002680static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2681{
2682 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2683 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2684}
2685
Al Virob37ad282006-10-19 23:28:59 -07002686static __be32
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002687nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07002688 struct nfs4_delegation **dp)
2689{
2690 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002691 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002692
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002693 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002694 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002695 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002696 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002697 status = nfs4_check_delegmode(*dp, flags);
2698 if (status)
2699 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002700out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002701 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07002702 return nfs_ok;
2703 if (status)
2704 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002705 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002706 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002707}
2708
Al Virob37ad282006-10-19 23:28:59 -07002709static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002710nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002712 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002713 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
NeilBrown8beefa22005-06-23 22:03:08 -07002715 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 /* ignore lock owners */
2717 if (local->st_stateowner->so_is_open_owner == 0)
2718 continue;
2719 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002720 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 *stpp = local;
2722 /* check for conflicting share reservations */
2723 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002724 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002726 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
J. Bruce Fields996e0932011-10-17 11:14:48 -04002729static void nfs4_free_stateid(struct nfs4_ol_stateid *s)
2730{
2731 kmem_cache_free(stateid_slab, s);
NeilBrown5ac049a2005-06-23 22:03:03 -07002732}
2733
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002734static inline int nfs4_access_to_access(u32 nfs4_access)
2735{
2736 int flags = 0;
2737
2738 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2739 flags |= NFSD_MAY_READ;
2740 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2741 flags |= NFSD_MAY_WRITE;
2742 return flags;
2743}
2744
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002745static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2746 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002747{
2748 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002749 int oflag = nfs4_access_to_omode(open->op_share_access);
2750 int access = nfs4_access_to_access(open->op_share_access);
2751
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002752 if (!fp->fi_fds[oflag]) {
2753 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2754 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002755 if (status)
2756 return status;
2757 }
2758 nfs4_file_get_access(fp, oflag);
2759
2760 return nfs_ok;
2761}
2762
Al Virob37ad282006-10-19 23:28:59 -07002763static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2765 struct nfsd4_open *open)
2766{
2767 struct iattr iattr = {
2768 .ia_valid = ATTR_SIZE,
2769 .ia_size = 0,
2770 };
2771 if (!open->op_truncate)
2772 return 0;
2773 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002774 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2776}
2777
Al Virob37ad282006-10-19 23:28:59 -07002778static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002779nfs4_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 -07002780{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002781 u32 op_share_access = open->op_share_access;
J. Bruce Fields7d947842010-08-20 18:09:31 -04002782 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002783 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
J. Bruce Fields7d947842010-08-20 18:09:31 -04002785 new_access = !test_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002786 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002787 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002788 if (status)
2789 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 status = nfsd4_truncate(rqstp, cur_fh, open);
2792 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002793 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002794 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002795 nfs4_file_put_access(fp, oflag);
2796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return status;
2798 }
2799 /* remember the open */
J. Bruce Fields24a01112010-05-18 20:01:35 -04002800 __set_bit(op_share_access, &stp->st_access_bmap);
J. Bruce Fieldsb55e0ba2008-04-28 18:22:50 -04002801 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 return nfs_ok;
2804}
2805
2806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05002808nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002810 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
2812
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002813/* Should we give out recallable state?: */
2814static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2815{
2816 if (clp->cl_cb_state == NFSD4_CB_UP)
2817 return true;
2818 /*
2819 * In the sessions case, since we don't have to establish a
2820 * separate connection for callbacks, we assume it's OK
2821 * until we hear otherwise:
2822 */
2823 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2824}
2825
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002826static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2827{
2828 struct file_lock *fl;
2829
2830 fl = locks_alloc_lock();
2831 if (!fl)
2832 return NULL;
2833 locks_init_lock(fl);
2834 fl->fl_lmops = &nfsd_lease_mng_ops;
2835 fl->fl_flags = FL_LEASE;
2836 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2837 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002838 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002839 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002840 return fl;
2841}
2842
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002843static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2844{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002845 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002846 struct file_lock *fl;
2847 int status;
2848
2849 fl = nfs4_alloc_init_lease(dp, flag);
2850 if (!fl)
2851 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002852 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002853 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002854 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002855 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002856 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002857 locks_free_lock(fl);
2858 return -ENOMEM;
2859 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002860 fp->fi_lease = fl;
2861 fp->fi_deleg_file = fl->fl_file;
2862 get_file(fp->fi_deleg_file);
2863 atomic_set(&fp->fi_delegees, 1);
2864 list_add(&dp->dl_perfile, &fp->fi_delegations);
2865 return 0;
2866}
2867
2868static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2869{
2870 struct nfs4_file *fp = dp->dl_file;
2871
2872 if (!fp->fi_lease)
2873 return nfs4_setlease(dp, flag);
2874 spin_lock(&recall_lock);
2875 if (fp->fi_had_conflict) {
2876 spin_unlock(&recall_lock);
2877 return -EAGAIN;
2878 }
2879 atomic_inc(&fp->fi_delegees);
2880 list_add(&dp->dl_perfile, &fp->fi_delegations);
2881 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002882 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002883 return 0;
2884}
2885
Benny Halevy4aa89132012-02-21 14:16:44 -08002886static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2887{
2888 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2889 if (status == -EAGAIN)
2890 open->op_why_no_deleg = WND4_CONTENTION;
2891 else {
2892 open->op_why_no_deleg = WND4_RESOURCE;
2893 switch (open->op_deleg_want) {
2894 case NFS4_SHARE_WANT_READ_DELEG:
2895 case NFS4_SHARE_WANT_WRITE_DELEG:
2896 case NFS4_SHARE_WANT_ANY_DELEG:
2897 break;
2898 case NFS4_SHARE_WANT_CANCEL:
2899 open->op_why_no_deleg = WND4_CANCELLED;
2900 break;
2901 case NFS4_SHARE_WANT_NO_DELEG:
2902 BUG(); /* not supposed to get here */
2903 }
2904 }
2905}
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907/*
2908 * Attempt to hand out a delegation.
2909 */
2910static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002911nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002914 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002915 int cb_up;
Benny Halevyd24433c2012-02-16 20:57:17 +02002916 int status = 0, flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002918 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002920 open->op_recall = 0;
2921 switch (open->op_claim_type) {
2922 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002923 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002924 open->op_recall = 1;
2925 flag = open->op_delegate_type;
2926 if (flag == NFS4_OPEN_DELEGATE_NONE)
2927 goto out;
2928 break;
2929 case NFS4_OPEN_CLAIM_NULL:
2930 /* Let's not give out any delegations till everyone's
2931 * had the chance to reclaim theirs.... */
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04002932 if (locks_in_grace())
NeilBrown7b190fe2005-06-23 22:03:23 -07002933 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002934 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002935 goto out;
2936 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2937 flag = NFS4_OPEN_DELEGATE_WRITE;
2938 else
2939 flag = NFS4_OPEN_DELEGATE_READ;
2940 break;
2941 default:
2942 goto out;
2943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002945 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002946 if (dp == NULL)
2947 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002948 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002949 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002950 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002952 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002954 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002955 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956out:
2957 open->op_delegate_type = flag;
Benny Halevyd24433c2012-02-16 20:57:17 +02002958 if (flag == NFS4_OPEN_DELEGATE_NONE) {
2959 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
2960 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2961 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2962
Benny Halevy4aa89132012-02-21 14:16:44 -08002963 /* 4.1 client asking for a delegation? */
2964 if (open->op_deleg_want)
2965 nfsd4_open_deleg_none_ext(open, status);
Benny Halevyd24433c2012-02-16 20:57:17 +02002966 }
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002967 return;
2968out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002969 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002970out_no_deleg:
2971 flag = NFS4_OPEN_DELEGATE_NONE;
2972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
Benny Halevye27f49c2012-02-21 14:16:54 -08002975static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
2976 struct nfs4_delegation *dp)
2977{
2978 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
2979 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
2980 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2981 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
2982 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
2983 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
2984 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2985 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
2986 }
2987 /* Otherwise the client must be confused wanting a delegation
2988 * it already has, therefore we don't return
2989 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
2990 */
2991}
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993/*
2994 * called with nfs4_lock_state() held.
2995 */
Al Virob37ad282006-10-19 23:28:59 -07002996__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2998{
Andy Adamson66689582009-04-03 08:28:45 +03002999 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003000 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 struct nfs4_file *fp = NULL;
3002 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003003 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003004 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003005 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 /*
3008 * Lookup file; if found, lookup stateid and check open request,
3009 * and check for delegations in the process of being recalled.
3010 * If not found, create the nfs4_file struct
3011 */
3012 fp = find_file(ino);
3013 if (fp) {
3014 if ((status = nfs4_check_open(fp, open, &stp)))
3015 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003016 status = nfs4_check_deleg(cl, fp, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003017 if (status)
3018 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07003020 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003021 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003022 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003023 status = nfserr_jukebox;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003024 fp = open->op_file;
3025 open->op_file = NULL;
3026 nfsd4_init_file(fp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028
3029 /*
3030 * OPEN the file, or upgrade an existing OPEN.
3031 * If truncate fails, the OPEN fails.
3032 */
3033 if (stp) {
3034 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003035 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 if (status)
3037 goto out;
3038 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003039 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003040 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003042 stp = open->op_stp;
3043 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003044 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 status = nfsd4_truncate(rqstp, current_fh, open);
3046 if (status) {
J. Bruce Fields22839632009-01-11 14:27:17 -05003047 release_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 goto out;
3049 }
3050 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003051 update_stateid(&stp->st_stid.sc_stateid);
3052 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Benny Halevyd24433c2012-02-16 20:57:17 +02003054 if (nfsd4_has_session(&resp->cstate)) {
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003055 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03003056
Benny Halevyd24433c2012-02-16 20:57:17 +02003057 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3058 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3059 open->op_why_no_deleg = WND4_NOT_WANTED;
3060 goto nodeleg;
3061 }
3062 }
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 /*
3065 * Attempt to hand out a delegation. No error return, because the
3066 * OPEN succeeds even if we fail.
3067 */
3068 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003069nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 status = nfs_ok;
3071
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003072 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003073 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003075 /* 4.1 client trying to upgrade/downgrade delegation? */
3076 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003077 open->op_deleg_want)
3078 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003079
NeilBrown13cd2182005-06-23 22:03:10 -07003080 if (fp)
3081 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003082 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003083 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 /*
3085 * To finish the open response, we just need to set the rflags.
3086 */
3087 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003088 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003089 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3091
3092 return status;
3093}
3094
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003095void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3096{
3097 if (open->op_openowner) {
3098 struct nfs4_openowner *oo = open->op_openowner;
3099
3100 if (!list_empty(&oo->oo_owner.so_stateids))
3101 list_del_init(&oo->oo_close_lru);
3102 if (oo->oo_flags & NFS4_OO_NEW) {
3103 if (status) {
3104 release_openowner(oo);
3105 open->op_openowner = NULL;
3106 } else
3107 oo->oo_flags &= ~NFS4_OO_NEW;
3108 }
3109 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003110 if (open->op_file)
3111 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003112 if (open->op_stp)
3113 nfs4_free_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003114}
3115
Al Virob37ad282006-10-19 23:28:59 -07003116__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003117nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3118 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003121 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 nfs4_lock_state();
3124 dprintk("process_renew(%08x/%08x): starting\n",
3125 clid->cl_boot, clid->cl_id);
3126 status = nfserr_stale_clientid;
3127 if (STALE_CLIENTID(clid))
3128 goto out;
3129 clp = find_confirmed_client(clid);
3130 status = nfserr_expired;
3131 if (clp == NULL) {
3132 /* We assume the client took too long to RENEW. */
3133 dprintk("nfsd4_renew: clientid not found!\n");
3134 goto out;
3135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003137 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003138 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 goto out;
3140 status = nfs_ok;
3141out:
3142 nfs4_unlock_state();
3143 return status;
3144}
3145
Daniel Mackc47d8322011-05-16 16:38:14 +02003146static struct lock_manager nfsd4_manager = {
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003147};
3148
Jeff Layton33dcc482012-04-10 11:08:48 -04003149static bool grace_ended;
3150
NeilBrowna76b4312005-06-23 22:04:01 -07003151static void
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003152nfsd4_end_grace(void)
NeilBrowna76b4312005-06-23 22:04:01 -07003153{
Jeff Layton33dcc482012-04-10 11:08:48 -04003154 /* do nothing if grace period already ended */
3155 if (grace_ended)
3156 return;
3157
NeilBrowna76b4312005-06-23 22:04:01 -07003158 dprintk("NFSD: end of grace period\n");
Jeff Layton33dcc482012-04-10 11:08:48 -04003159 grace_ended = true;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003160 nfsd4_record_grace_done(&init_net, boot_time);
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003161 locks_end_grace(&nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003162 /*
3163 * Now that every NFSv4 client has had the chance to recover and
3164 * to see the (possibly new, possibly shorter) lease time, we
3165 * can safely set the next grace time to the current lease time:
3166 */
3167 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003168}
3169
NeilBrownfd39ca92005-06-23 22:04:03 -07003170static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171nfs4_laundromat(void)
3172{
3173 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003174 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 struct nfs4_delegation *dp;
3176 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003177 time_t cutoff = get_seconds() - nfsd4_lease;
3178 time_t t, clientid_val = nfsd4_lease;
3179 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 nfs4_lock_state();
3182
3183 dprintk("NFSD: laundromat service - starting\n");
Jeff Layton33dcc482012-04-10 11:08:48 -04003184 nfsd4_end_grace();
Benny Halevy36acb662010-05-12 00:13:04 +03003185 INIT_LIST_HEAD(&reaplist);
3186 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 list_for_each_safe(pos, next, &client_lru) {
3188 clp = list_entry(pos, struct nfs4_client, cl_lru);
3189 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3190 t = clp->cl_time - cutoff;
3191 if (clientid_val > t)
3192 clientid_val = t;
3193 break;
3194 }
Benny Halevyd7682982010-05-12 00:13:54 +03003195 if (atomic_read(&clp->cl_refcount)) {
3196 dprintk("NFSD: client in use (clientid %08x)\n",
3197 clp->cl_clientid.cl_id);
3198 continue;
3199 }
3200 unhash_client_locked(clp);
3201 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003202 }
3203 spin_unlock(&client_lock);
3204 list_for_each_safe(pos, next, &reaplist) {
3205 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 dprintk("NFSD: purging unused client (clientid %08x)\n",
3207 clp->cl_clientid.cl_id);
Jeff Layton2a4317c2012-03-21 16:42:43 -04003208 nfsd4_client_record_remove(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 expire_client(clp);
3210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 spin_lock(&recall_lock);
3212 list_for_each_safe(pos, next, &del_recall_lru) {
3213 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3214 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3215 u = dp->dl_time - cutoff;
3216 if (test_val > u)
3217 test_val = u;
3218 break;
3219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 list_move(&dp->dl_recall_lru, &reaplist);
3221 }
3222 spin_unlock(&recall_lock);
3223 list_for_each_safe(pos, next, &reaplist) {
3224 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 unhash_delegation(dp);
3226 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003227 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003229 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3230 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3231 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (test_val > u)
3233 test_val = u;
3234 break;
3235 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003236 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
3238 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3239 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3240 nfs4_unlock_state();
3241 return clientid_val;
3242}
3243
Harvey Harrisona254b242008-02-20 12:49:00 -08003244static struct workqueue_struct *laundry_wq;
3245static void laundromat_main(struct work_struct *);
3246static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3247
3248static void
David Howellsc4028952006-11-22 14:57:56 +00003249laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 time_t t;
3252
3253 t = nfs4_laundromat();
3254 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003255 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003258static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003259{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003260 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3261 return nfserr_bad_stateid;
3262 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
3265static int
3266STALE_STATEID(stateid_t *stateid)
3267{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04003268 if (stateid->si_opaque.so_clid.cl_boot == boot_time)
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003269 return 0;
3270 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003271 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003272 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
3275static inline int
3276access_permit_read(unsigned long access_bmap)
3277{
3278 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3279 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3280 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3281}
3282
3283static inline int
3284access_permit_write(unsigned long access_bmap)
3285{
3286 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3287 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3288}
3289
3290static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003291__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Al Virob37ad282006-10-19 23:28:59 -07003293 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
J. Bruce Fields02921912010-07-29 15:16:59 -04003295 /* For lock stateid's, we test the parent open, not the lock: */
3296 if (stp->st_openstp)
3297 stp = stp->st_openstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3299 goto out;
3300 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3301 goto out;
3302 status = nfs_ok;
3303out:
3304 return status;
3305}
3306
Al Virob37ad282006-10-19 23:28:59 -07003307static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3309{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003310 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 return nfs_ok;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04003312 else if (locks_in_grace()) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003313 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 * conflicting state; so we must wait out the grace period. */
3315 return nfserr_grace;
3316 } else if (flags & WR_STATE)
3317 return nfs4_share_conflict(current_fh,
3318 NFS4_SHARE_DENY_WRITE);
3319 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3320 return nfs4_share_conflict(current_fh,
3321 NFS4_SHARE_DENY_READ);
3322}
3323
3324/*
3325 * Allow READ/WRITE during grace period on recovered state only for files
3326 * that are not able to provide mandatory locking.
3327 */
3328static inline int
J. Bruce Fields18f82732009-02-21 15:23:01 -08003329grace_disallows_io(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003331 return locks_in_grace() && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
J. Bruce Fields81b82962011-08-23 11:03:29 -04003334/* Returns true iff a is later than b: */
3335static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3336{
3337 return (s32)a->si_generation - (s32)b->si_generation > 0;
3338}
3339
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003340static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003341{
Andy Adamson66689582009-04-03 08:28:45 +03003342 /*
3343 * When sessions are used the stateid generation number is ignored
3344 * when it is zero.
3345 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003346 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003347 return nfs_ok;
3348
3349 if (in->si_generation == ref->si_generation)
3350 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003351
J. Bruce Fields0836f582008-01-26 19:08:12 -05003352 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003353 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003354 return nfserr_bad_stateid;
3355 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003356 * However, we could see a stateid from the past, even from a
3357 * non-buggy client. For example, if the client sends a lock
3358 * while some IO is outstanding, the lock may bump si_generation
3359 * while the IO is still in flight. The client could avoid that
3360 * situation by waiting for responses on all the IO requests,
3361 * but better performance may result in retrying IO that
3362 * receives an old_stateid error if requests are rarely
3363 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003364 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003365 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003366}
3367
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003368__be32 nfs4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003369{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003370 struct nfs4_stid *s;
3371 struct nfs4_ol_stateid *ols;
3372 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003373
3374 if (STALE_STATEID(stateid))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003375 return nfserr_stale_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04003376
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003377 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003378 if (!s)
3379 return nfserr_stale_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003380 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003381 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003382 return status;
3383 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3384 return nfs_ok;
3385 ols = openlockstateid(s);
3386 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003387 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003388 return nfserr_bad_stateid;
3389 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003390}
3391
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003392static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, struct nfs4_stid **s)
3393{
3394 struct nfs4_client *cl;
3395
3396 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3397 return nfserr_bad_stateid;
3398 if (STALE_STATEID(stateid))
3399 return nfserr_stale_stateid;
3400 cl = find_confirmed_client(&stateid->si_opaque.so_clid);
3401 if (!cl)
3402 return nfserr_expired;
3403 *s = find_stateid_by_type(cl, stateid, typemask);
3404 if (!*s)
3405 return nfserr_bad_stateid;
3406 return nfs_ok;
3407
3408}
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410/*
3411* Checks for stateid operations
3412*/
Al Virob37ad282006-10-19 23:28:59 -07003413__be32
Benny Halevydd453df2009-04-03 08:28:41 +03003414nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3415 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003417 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003418 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003420 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003422 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 if (filpp)
3425 *filpp = NULL;
3426
J. Bruce Fields18f82732009-02-21 15:23:01 -08003427 if (grace_disallows_io(ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return nfserr_grace;
3429
3430 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3431 return check_special_stateids(current_fh, stateid, flags);
3432
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003433 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, &s);
3434 if (status)
3435 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003436 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3437 if (status)
3438 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003439 switch (s->sc_type) {
3440 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003441 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003442 status = nfs4_check_delegmode(dp, flags);
3443 if (status)
3444 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003445 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003446 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003447 BUG_ON(!*filpp);
3448 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003449 break;
3450 case NFS4_OPEN_STID:
3451 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003452 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003453 status = nfs4_check_fh(current_fh, stp);
3454 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003456 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003457 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003459 status = nfs4_check_openmode(stp, flags);
3460 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003462 if (filpp) {
3463 if (flags & RD_STATE)
3464 *filpp = find_readable_file(stp->st_file);
3465 else
3466 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003467 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003468 break;
3469 default:
3470 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 }
3472 status = nfs_ok;
3473out:
3474 return status;
3475}
3476
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003477static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003478nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003479{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003480 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003481 return nfserr_locks_held;
3482 release_lock_stateid(stp);
3483 return nfs_ok;
3484}
3485
3486/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003487 * Test if the stateid is valid
3488 */
3489__be32
3490nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3491 struct nfsd4_test_stateid *test_stateid)
3492{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003493 struct nfsd4_test_stateid_id *stateid;
3494 struct nfs4_client *cl = cstate->session->se_client;
3495
3496 nfs4_lock_state();
3497 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
3498 stateid->ts_id_status = nfs4_validate_stateid(cl, &stateid->ts_id_stateid);
3499 nfs4_unlock_state();
3500
Bryan Schumaker17456802011-07-13 10:50:48 -04003501 return nfs_ok;
3502}
3503
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003504__be32
3505nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3506 struct nfsd4_free_stateid *free_stateid)
3507{
3508 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003509 struct nfs4_stid *s;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003510 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003511 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003512
3513 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003514 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003515 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003516 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003517 switch (s->sc_type) {
3518 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003519 ret = nfserr_locks_held;
3520 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003521 case NFS4_OPEN_STID:
3522 case NFS4_LOCK_STID:
3523 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3524 if (ret)
3525 goto out;
3526 if (s->sc_type == NFS4_LOCK_STID)
3527 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3528 else
3529 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003530 break;
3531 default:
3532 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003533 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003534out:
3535 nfs4_unlock_state();
3536 return ret;
3537}
3538
NeilBrown4c4cd222005-07-07 17:59:27 -07003539static inline int
3540setlkflg (int type)
3541{
3542 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3543 RD_STATE : WR_STATE;
3544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003546static __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 -04003547{
3548 struct svc_fh *current_fh = &cstate->current_fh;
3549 struct nfs4_stateowner *sop = stp->st_stateowner;
3550 __be32 status;
3551
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003552 status = nfsd4_check_seqid(cstate, sop, seqid);
3553 if (status)
3554 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003555 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3556 /*
3557 * "Closed" stateid's exist *only* to return
3558 * nfserr_replay_me from the previous step.
3559 */
3560 return nfserr_bad_stateid;
3561 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3562 if (status)
3563 return status;
3564 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003565}
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567/*
3568 * Checks for sequence id mutating operations.
3569 */
Al Virob37ad282006-10-19 23:28:59 -07003570static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003571nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003572 stateid_t *stateid, char typemask,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003573 struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003575 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003576 struct nfs4_stid *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003578 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3579 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 *stpp = NULL;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003582 status = nfsd4_lookup_stateid(stateid, typemask, &s);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003583 if (status)
3584 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003585 *stpp = openlockstateid(s);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003586 cstate->replay_owner = (*stpp)->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003588 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3589}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003590
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003591static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003592{
3593 __be32 status;
3594 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003596 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003597 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003598 if (status)
3599 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003600 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003601 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003602 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003603 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
Al Virob37ad282006-10-19 23:28:59 -07003606__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003607nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003608 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Al Virob37ad282006-10-19 23:28:59 -07003610 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003611 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003612 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003615 (int)cstate->current_fh.fh_dentry->d_name.len,
3616 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003618 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003619 if (status)
3620 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
3622 nfs4_lock_state();
3623
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003624 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003625 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003626 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003627 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003628 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003629 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003630 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003631 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003632 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003633 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003634 update_stateid(&stp->st_stid.sc_stateid);
3635 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003636 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003637 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003638
Jeff Layton2a4317c2012-03-21 16:42:43 -04003639 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003640 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003642 if (!cstate->replay_owner)
3643 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return status;
3645}
3646
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003647static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003649 if (!test_bit(access, &stp->st_access_bmap))
3650 return;
3651 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
3652 __clear_bit(access, &stp->st_access_bmap);
3653}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003654
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003655static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3656{
3657 switch (to_access) {
3658 case NFS4_SHARE_ACCESS_READ:
3659 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3660 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3661 break;
3662 case NFS4_SHARE_ACCESS_WRITE:
3663 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3664 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3665 break;
3666 case NFS4_SHARE_ACCESS_BOTH:
3667 break;
3668 default:
3669 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
3671}
3672
3673static void
3674reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3675{
3676 int i;
3677 for (i = 0; i < 4; i++) {
3678 if ((i & deny) != i)
3679 __clear_bit(i, bmap);
3680 }
3681}
3682
Al Virob37ad282006-10-19 23:28:59 -07003683__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003684nfsd4_open_downgrade(struct svc_rqst *rqstp,
3685 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003686 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Al Virob37ad282006-10-19 23:28:59 -07003688 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003689 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
3691 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003692 (int)cstate->current_fh.fh_dentry->d_name.len,
3693 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04003695 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02003696 if (od->od_deleg_want)
3697 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3698 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
3700 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003701 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3702 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003703 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 status = nfserr_inval;
3706 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3707 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3708 stp->st_access_bmap, od->od_share_access);
3709 goto out;
3710 }
3711 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3712 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3713 stp->st_deny_bmap, od->od_share_deny);
3714 goto out;
3715 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003716 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3719
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003720 update_stateid(&stp->st_stid.sc_stateid);
3721 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 status = nfs_ok;
3723out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003724 if (!cstate->replay_owner)
3725 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return status;
3727}
3728
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003729void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3730{
3731 struct nfs4_openowner *oo;
3732 struct nfs4_ol_stateid *s;
3733
3734 if (!so->so_is_open_owner)
3735 return;
3736 oo = openowner(so);
3737 s = oo->oo_last_closed_stid;
3738 if (!s)
3739 return;
3740 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3741 /* Release the last_closed_stid on the next seqid bump: */
3742 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3743 return;
3744 }
3745 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003746 release_last_closed_stateid(oo);
3747}
3748
3749static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3750{
3751 unhash_open_stateid(s);
3752 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003753}
3754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755/*
3756 * nfs4_unlock_state() called after encode
3757 */
Al Virob37ad282006-10-19 23:28:59 -07003758__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003759nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003760 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Al Virob37ad282006-10-19 23:28:59 -07003762 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003763 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003764 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003767 (int)cstate->current_fh.fh_dentry->d_name.len,
3768 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003771 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3772 &close->cl_stateid,
3773 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3774 &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003775 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003777 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003779 update_stateid(&stp->st_stid.sc_stateid);
3780 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003782 nfsd4_close_open_stateid(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003783 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003784
3785 /* place unused nfs4_stateowners on so_close_lru list to be
3786 * released by the laundromat service after the lease period
3787 * to enable us to handle CLOSE replay
3788 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003789 if (list_empty(&oo->oo_owner.so_stateids))
3790 move_to_close_lru(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003792 if (!cstate->replay_owner)
3793 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return status;
3795}
3796
Al Virob37ad282006-10-19 23:28:59 -07003797__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003798nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3799 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003801 struct nfs4_delegation *dp;
3802 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003803 struct nfs4_stid *s;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003804 struct inode *inode;
Al Virob37ad282006-10-19 23:28:59 -07003805 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003807 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003808 return status;
3809 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003812 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s);
3813 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003814 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003815 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003816 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003817 if (status)
3818 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003819
3820 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003822 nfs4_unlock_state();
3823
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return status;
3825}
3826
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
J. Bruce Fields009673b2011-11-07 17:40:10 -05003830#define LOCKOWNER_INO_HASH_BITS 8
3831#define LOCKOWNER_INO_HASH_SIZE (1 << LOCKOWNER_INO_HASH_BITS)
3832#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Benny Halevy87df4de2008-12-15 19:42:03 +02003834static inline u64
3835end_offset(u64 start, u64 len)
3836{
3837 u64 end;
3838
3839 end = start + len;
3840 return end >= start ? end: NFS4_MAX_UINT64;
3841}
3842
3843/* last octet in a range */
3844static inline u64
3845last_byte_offset(u64 start, u64 len)
3846{
3847 u64 end;
3848
3849 BUG_ON(!len);
3850 end = start + len;
3851 return end > start ? end - 1: NFS4_MAX_UINT64;
3852}
3853
J. Bruce Fields009673b2011-11-07 17:40:10 -05003854static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
3856 return (file_hashval(inode) + cl_id
3857 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05003858 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859}
3860
J. Bruce Fields009673b2011-11-07 17:40:10 -05003861static struct list_head lockowner_ino_hashtbl[LOCKOWNER_INO_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863/*
3864 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3865 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3866 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3867 * locking, this prevents us from being completely protocol-compliant. The
3868 * real solution to this problem is to start using unsigned file offsets in
3869 * the VFS, but this is a very deep change!
3870 */
3871static inline void
3872nfs4_transform_lock_offset(struct file_lock *lock)
3873{
3874 if (lock->fl_start < 0)
3875 lock->fl_start = OFFSET_MAX;
3876 if (lock->fl_end < 0)
3877 lock->fl_end = OFFSET_MAX;
3878}
3879
NeilBrownd5b90262006-04-10 22:55:22 -07003880/* Hack!: For now, we're defining this just so we can use a pointer to it
3881 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003882static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003883};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885static inline void
3886nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3887{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003888 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
NeilBrownd5b90262006-04-10 22:55:22 -07003890 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003891 lo = (struct nfs4_lockowner *) fl->fl_owner;
3892 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3893 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003894 if (!deny->ld_owner.data)
3895 /* We just don't care that much */
3896 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003897 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3898 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003899 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003900nevermind:
3901 deny->ld_owner.len = 0;
3902 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003903 deny->ld_clientid.cl_boot = 0;
3904 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 }
3906 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003907 deny->ld_length = NFS4_MAX_UINT64;
3908 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3910 deny->ld_type = NFS4_READ_LT;
3911 if (fl->fl_type != F_RDLCK)
3912 deny->ld_type = NFS4_WRITE_LT;
3913}
3914
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003915static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3916{
3917 struct nfs4_ol_stateid *lst;
3918
3919 if (!same_owner_str(&lo->lo_owner, owner, clid))
3920 return false;
3921 lst = list_first_entry(&lo->lo_owner.so_stateids,
3922 struct nfs4_ol_stateid, st_perstateowner);
3923 return lst->st_file->fi_inode == inode;
3924}
3925
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003926static struct nfs4_lockowner *
3927find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 struct xdr_netobj *owner)
3929{
J. Bruce Fields009673b2011-11-07 17:40:10 -05003930 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003931 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
J. Bruce Fields009673b2011-11-07 17:40:10 -05003933 list_for_each_entry(lo, &lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003934 if (same_lockowner_ino(lo, inode, clid, owner))
3935 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 }
3937 return NULL;
3938}
3939
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003940static 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 -04003941{
J. Bruce Fields009673b2011-11-07 17:40:10 -05003942 struct inode *inode = open_stp->st_file->fi_inode;
3943 unsigned int inohash = lockowner_ino_hashval(inode,
3944 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
3945
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003946 list_add(&lo->lo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
J. Bruce Fields009673b2011-11-07 17:40:10 -05003947 list_add(&lo->lo_owner_ino_hash, &lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003948 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003949}
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951/*
3952 * Alloc a lock owner structure.
3953 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003954 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003956 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 */
3958
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003959static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003960alloc_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 -04003961 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003963 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3964 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003966 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3967 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07003968 /* It is the openowner seqid that will be incremented in encode in the
3969 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003970 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3971 hash_lockowner(lo, strhashval, clp, open_stp);
3972 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003975static struct nfs4_ol_stateid *
3976alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003978 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04003979 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
J. Bruce Fields996e0932011-10-17 11:14:48 -04003981 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07003982 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04003983 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003984 init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
NeilBrown8beefa22005-06-23 22:03:08 -07003985 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003986 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3987 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07003988 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05003990 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07003992 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 return stp;
3994}
3995
NeilBrownfd39ca92005-06-23 22:04:03 -07003996static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997check_lock_length(u64 offset, u64 length)
3998{
Benny Halevy87df4de2008-12-15 19:42:03 +02003999 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 LOFF_OVERFLOW(offset, length)));
4001}
4002
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004003static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004004{
4005 struct nfs4_file *fp = lock_stp->st_file;
4006 int oflag = nfs4_access_to_omode(access);
4007
4008 if (test_bit(access, &lock_stp->st_access_bmap))
4009 return;
4010 nfs4_file_get_access(fp, oflag);
4011 __set_bit(access, &lock_stp->st_access_bmap);
4012}
4013
J. Bruce Fields2355c592012-04-25 16:56:22 -04004014static __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 -04004015{
4016 struct nfs4_file *fi = ost->st_file;
4017 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4018 struct nfs4_client *cl = oo->oo_owner.so_client;
4019 struct nfs4_lockowner *lo;
4020 unsigned int strhashval;
4021
4022 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid, &lock->v.new.owner);
4023 if (lo) {
4024 if (!cstate->minorversion)
4025 return nfserr_bad_seqid;
4026 /* XXX: a lockowner always has exactly one stateid: */
4027 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4028 struct nfs4_ol_stateid, st_perstateowner);
4029 return nfs_ok;
4030 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004031 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004032 &lock->v.new.owner);
4033 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4034 if (lo == NULL)
4035 return nfserr_jukebox;
4036 *lst = alloc_init_lock_stateid(lo, fi, ost);
4037 if (*lst == NULL) {
4038 release_lockowner(lo);
4039 return nfserr_jukebox;
4040 }
4041 *new = true;
4042 return nfs_ok;
4043}
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045/*
4046 * LOCK operation
4047 */
Al Virob37ad282006-10-19 23:28:59 -07004048__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004049nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004050 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004052 struct nfs4_openowner *open_sop = NULL;
4053 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004054 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004055 struct file *filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 struct file_lock file_lock;
Andy Adamson8dc7c312006-03-20 13:44:26 -05004057 struct file_lock conflock;
Al Virob37ad282006-10-19 23:28:59 -07004058 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004059 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004060 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004061 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
4063 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4064 (long long) lock->lk_offset,
4065 (long long) lock->lk_length);
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 if (check_lock_length(lock->lk_offset, lock->lk_length))
4068 return nfserr_inval;
4069
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004070 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004071 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004072 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4073 return status;
4074 }
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 nfs4_lock_state();
4077
4078 if (lock->lk_is_new) {
NeilBrown893f8772005-07-07 17:59:17 -07004079 /*
4080 * Client indicates that this is a new lockowner.
4081 * Use open owner and open stateid to create lock owner and
4082 * lock stateid.
4083 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004084 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004085
4086 if (nfsd4_has_session(cstate))
4087 /* See rfc 5661 18.10.3: given clientid is ignored: */
4088 memcpy(&lock->v.new.clientid,
4089 &cstate->session->se_client->cl_clientid,
4090 sizeof(clientid_t));
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 status = nfserr_stale_clientid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004093 if (STALE_CLIENTID(&lock->lk_new_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004097 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 lock->lk_new_open_seqid,
4099 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004100 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07004101 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004103 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004104 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004105 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004106 &lock->v.new.clientid))
4107 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004108 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4109 &lock_stp, &new_state);
4110 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 } else {
4113 /* lock (lock owner + lock stateid) already exists */
Benny Halevydd453df2009-04-03 08:28:41 +03004114 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004115 lock->lk_old_lock_seqid,
4116 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004117 NFS4_LOCK_STID, &lock_stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (status)
4119 goto out;
4120 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004121 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004123 lkflg = setlkflg(lock->lk_type);
4124 status = nfs4_check_openmode(lock_stp, lkflg);
4125 if (status)
4126 goto out;
4127
NeilBrown0dd395d2005-07-07 17:59:15 -07004128 status = nfserr_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004129 if (locks_in_grace() && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004130 goto out;
4131 status = nfserr_no_grace;
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004132 if (!locks_in_grace() && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004133 goto out;
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 locks_init_lock(&file_lock);
4136 switch (lock->lk_type) {
4137 case NFS4_READ_LT:
4138 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004139 filp = find_readable_file(lock_stp->st_file);
4140 if (filp)
4141 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 file_lock.fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 case NFS4_WRITE_LT:
4145 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004146 filp = find_writeable_file(lock_stp->st_file);
4147 if (filp)
4148 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 file_lock.fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 default:
4152 status = nfserr_inval;
4153 goto out;
4154 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004155 if (!filp) {
4156 status = nfserr_openmode;
4157 goto out;
4158 }
Neil Brownb59e3c02005-09-13 01:25:38 -07004159 file_lock.fl_owner = (fl_owner_t)lock_sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 file_lock.fl_pid = current->tgid;
4161 file_lock.fl_file = filp;
4162 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004163 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
4165 file_lock.fl_start = lock->lk_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004166 file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 nfs4_transform_lock_offset(&file_lock);
4168
4169 /*
4170 * Try to lock the file in the VFS.
4171 * Note: locks.c uses the BKL to protect the inode's lock list.
4172 */
4173
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004174 err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004175 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004177 update_stateid(&lock_stp->st_stid.sc_stateid);
4178 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004180 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004181 break;
4182 case (EAGAIN): /* conflock holds conflicting lock */
4183 status = nfserr_denied;
4184 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4185 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 case (EDEADLK):
4188 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004189 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004190 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004191 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004192 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004196 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004197 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004198 if (!cstate->replay_owner)
4199 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 return status;
4201}
4202
4203/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004204 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4205 * so we do a temporary open here just to get an open file to pass to
4206 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4207 * inode operation.)
4208 */
4209static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4210{
4211 struct file *file;
4212 int err;
4213
4214 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4215 if (err)
4216 return err;
4217 err = vfs_test_lock(file, lock);
4218 nfsd_close(file);
4219 return err;
4220}
4221
4222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 * LOCKT operation
4224 */
Al Virob37ad282006-10-19 23:28:59 -07004225__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004226nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4227 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228{
4229 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 struct file_lock file_lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004231 struct nfs4_lockowner *lo;
Marc Eshelfd85b812006-11-28 16:26:41 -05004232 int error;
Al Virob37ad282006-10-19 23:28:59 -07004233 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004235 if (locks_in_grace())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 return nfserr_grace;
4237
4238 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4239 return nfserr_inval;
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 nfs4_lock_state();
4242
4243 status = nfserr_stale_clientid;
Andy Adamson60adfc52009-04-03 08:28:50 +03004244 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004247 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004250 inode = cstate->current_fh.fh_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 locks_init_lock(&file_lock);
4252 switch (lockt->lt_type) {
4253 case NFS4_READ_LT:
4254 case NFS4_READW_LT:
4255 file_lock.fl_type = F_RDLCK;
4256 break;
4257 case NFS4_WRITE_LT:
4258 case NFS4_WRITEW_LT:
4259 file_lock.fl_type = F_WRLCK;
4260 break;
4261 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004262 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 status = nfserr_inval;
4264 goto out;
4265 }
4266
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004267 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4268 if (lo)
4269 file_lock.fl_owner = (fl_owner_t)lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 file_lock.fl_pid = current->tgid;
4271 file_lock.fl_flags = FL_POSIX;
4272
4273 file_lock.fl_start = lockt->lt_offset;
Benny Halevy87df4de2008-12-15 19:42:03 +02004274 file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 nfs4_transform_lock_offset(&file_lock);
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 status = nfs_ok;
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004279 error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
Marc Eshelfd85b812006-11-28 16:26:41 -05004280 if (error) {
4281 status = nfserrno(error);
4282 goto out;
4283 }
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004284 if (file_lock.fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 status = nfserr_denied;
Marc Eshel9d6a8c52007-02-21 00:55:18 -05004286 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 }
4288out:
4289 nfs4_unlock_state();
4290 return status;
4291}
4292
Al Virob37ad282006-10-19 23:28:59 -07004293__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004294nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004295 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004297 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 struct file *filp = NULL;
4299 struct file_lock file_lock;
Al Virob37ad282006-10-19 23:28:59 -07004300 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004301 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
4303 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4304 (long long) locku->lu_offset,
4305 (long long) locku->lu_length);
4306
4307 if (check_lock_length(locku->lu_offset, locku->lu_length))
4308 return nfserr_inval;
4309
4310 nfs4_lock_state();
4311
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004312 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004313 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004314 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004316 filp = find_any_file(stp->st_file);
4317 if (!filp) {
4318 status = nfserr_lock_range;
4319 goto out;
4320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 BUG_ON(!filp);
4322 locks_init_lock(&file_lock);
4323 file_lock.fl_type = F_UNLCK;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004324 file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 file_lock.fl_pid = current->tgid;
4326 file_lock.fl_file = filp;
4327 file_lock.fl_flags = FL_POSIX;
NeilBrownd5b90262006-04-10 22:55:22 -07004328 file_lock.fl_lmops = &nfsd_posix_mng_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 file_lock.fl_start = locku->lu_offset;
4330
Benny Halevy87df4de2008-12-15 19:42:03 +02004331 file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 nfs4_transform_lock_offset(&file_lock);
4333
4334 /*
4335 * Try to unlock the file in the VFS.
4336 */
Marc Eshelfd85b812006-11-28 16:26:41 -05004337 err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004338 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004339 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 goto out_nfserr;
4341 }
4342 /*
4343 * OK, unlock succeeded; the only thing left to do is update the stateid.
4344 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004345 update_stateid(&stp->st_stid.sc_stateid);
4346 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
4348out:
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004349 if (!cstate->replay_owner)
4350 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 return status;
4352
4353out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004354 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 goto out;
4356}
4357
4358/*
4359 * returns
4360 * 1: locks held by lockowner
4361 * 0: no locks held by lockowner
4362 */
4363static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004364check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
4366 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004367 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 int status = 0;
4369
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004370 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004372 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 status = 1;
4374 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 }
4377out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004378 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return status;
4380}
4381
Al Virob37ad282006-10-19 23:28:59 -07004382__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004383nfsd4_release_lockowner(struct svc_rqst *rqstp,
4384 struct nfsd4_compound_state *cstate,
4385 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
4387 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004388 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004389 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004390 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004392 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004393 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004394 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
4396 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4397 clid->cl_boot, clid->cl_id);
4398
4399 /* XXX check for lease expiration */
4400
4401 status = nfserr_stale_clientid;
Neil Brown849823c2005-09-13 01:25:36 -07004402 if (STALE_CLIENTID(clid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 nfs4_lock_state();
4406
NeilBrown3e9e3db2005-06-23 22:04:20 -07004407 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004408 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004409
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004410 list_for_each_entry(sop, &ownerstr_hashtbl[hashval], so_strhash) {
4411 if (sop->so_is_open_owner)
4412 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004413 if (!same_owner_str(sop, owner, clid))
4414 continue;
4415 list_for_each_entry(stp, &sop->so_stateids,
4416 st_perstateowner) {
4417 lo = lockowner(sop);
4418 if (check_for_locks(stp->st_file, lo))
4419 goto out;
4420 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004422 }
4423 /* Clients probably won't expect us to return with some (but not all)
4424 * of the lockowner state released; so don't release any until all
4425 * have been checked. */
4426 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004427 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004428 lo = list_entry(matches.next, struct nfs4_lockowner,
4429 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004430 /* unhash_stateowner deletes so_perclient only
4431 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004432 list_del(&lo->lo_list);
4433 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
4435out:
4436 nfs4_unlock_state();
4437 return status;
4438}
4439
4440static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004441alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
NeilBrowna55370a2005-06-23 22:03:52 -07004443 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
4445
NeilBrownc7b9a452005-06-23 22:04:30 -07004446int
Andy Adamsona1bcecd2009-04-03 08:28:05 +03004447nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
NeilBrownc7b9a452005-06-23 22:04:30 -07004448{
4449 unsigned int strhashval = clientstr_hashval(name);
4450 struct nfs4_client *clp;
4451
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004452 clp = find_confirmed_client_by_str(name, strhashval);
J. Bruce Fields393d8ed2012-03-06 14:43:36 -05004453 if (!clp)
4454 return 0;
Jeff Laytona52d7262012-03-21 09:52:02 -04004455 return test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
NeilBrownc7b9a452005-06-23 22:04:30 -07004456}
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458/*
4459 * failure => all reset bets are off, nfserr_no_grace...
4460 */
NeilBrown190e4fb2005-06-23 22:04:25 -07004461int
4462nfs4_client_to_reclaim(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
4464 unsigned int strhashval;
4465 struct nfs4_client_reclaim *crp = NULL;
4466
NeilBrowna55370a2005-06-23 22:03:52 -07004467 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4468 crp = alloc_reclaim();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 if (!crp)
4470 return 0;
NeilBrowna55370a2005-06-23 22:03:52 -07004471 strhashval = clientstr_hashval(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 INIT_LIST_HEAD(&crp->cr_strhash);
4473 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
NeilBrowna55370a2005-06-23 22:03:52 -07004474 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 reclaim_str_hashtbl_size++;
4476 return 1;
4477}
4478
Jeff Layton2a4317c2012-03-21 16:42:43 -04004479void
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480nfs4_release_reclaim(void)
4481{
4482 struct nfs4_client_reclaim *crp = NULL;
4483 int i;
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4486 while (!list_empty(&reclaim_str_hashtbl[i])) {
4487 crp = list_entry(reclaim_str_hashtbl[i].next,
4488 struct nfs4_client_reclaim, cr_strhash);
4489 list_del(&crp->cr_strhash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 kfree(crp);
4491 reclaim_str_hashtbl_size--;
4492 }
4493 }
4494 BUG_ON(reclaim_str_hashtbl_size);
4495}
4496
4497/*
4498 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004499struct nfs4_client_reclaim *
Jeff Laytona52d7262012-03-21 09:52:02 -04004500nfsd4_find_reclaim_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
4502 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 struct nfs4_client_reclaim *crp = NULL;
4504
NeilBrowna55370a2005-06-23 22:03:52 -07004505 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4506 clp->cl_name.len, clp->cl_name.data,
4507 clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
4509 /* find clp->cl_name in reclaim_str_hashtbl */
NeilBrowna55370a2005-06-23 22:03:52 -07004510 strhashval = clientstr_hashval(clp->cl_recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
NeilBrowna55370a2005-06-23 22:03:52 -07004512 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 return crp;
4514 }
4515 }
4516 return NULL;
4517}
4518
4519/*
4520* Called from OPEN. Look for clientid in reclaim list.
4521*/
Al Virob37ad282006-10-19 23:28:59 -07004522__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523nfs4_check_open_reclaim(clientid_t *clid)
4524{
Jeff Laytona52d7262012-03-21 09:52:02 -04004525 struct nfs4_client *clp;
4526
4527 /* find clientid in conf_id_hashtbl */
4528 clp = find_confirmed_client(clid);
4529 if (clp == NULL)
4530 return nfserr_reclaim_bad;
4531
4532 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533}
4534
Bryan Schumaker65178db2011-11-01 13:35:21 -04004535#ifdef CONFIG_NFSD_FAULT_INJECTION
4536
4537void nfsd_forget_clients(u64 num)
4538{
4539 struct nfs4_client *clp, *next;
4540 int count = 0;
4541
4542 nfs4_lock_state();
4543 list_for_each_entry_safe(clp, next, &client_lru, cl_lru) {
Jeff Layton2a4317c2012-03-21 16:42:43 -04004544 nfsd4_client_record_remove(clp);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004545 expire_client(clp);
4546 if (++count == num)
4547 break;
4548 }
4549 nfs4_unlock_state();
4550
4551 printk(KERN_INFO "NFSD: Forgot %d clients", count);
4552}
4553
4554static void release_lockowner_sop(struct nfs4_stateowner *sop)
4555{
4556 release_lockowner(lockowner(sop));
4557}
4558
4559static void release_openowner_sop(struct nfs4_stateowner *sop)
4560{
4561 release_openowner(openowner(sop));
4562}
4563
J. Bruce Fields353de312011-11-15 19:25:03 -05004564static int nfsd_release_n_owners(u64 num, bool is_open_owner,
Bryan Schumaker65178db2011-11-01 13:35:21 -04004565 void (*release_sop)(struct nfs4_stateowner *))
4566{
4567 int i, count = 0;
4568 struct nfs4_stateowner *sop, *next;
4569
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004570 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4571 list_for_each_entry_safe(sop, next, &ownerstr_hashtbl[i], so_strhash) {
4572 if (sop->so_is_open_owner != is_open_owner)
4573 continue;
Bryan Schumaker65178db2011-11-01 13:35:21 -04004574 release_sop(sop);
4575 if (++count == num)
4576 return count;
4577 }
4578 }
4579 return count;
4580}
4581
4582void nfsd_forget_locks(u64 num)
4583{
4584 int count;
4585
4586 nfs4_lock_state();
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004587 count = nfsd_release_n_owners(num, false, release_lockowner_sop);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004588 nfs4_unlock_state();
4589
4590 printk(KERN_INFO "NFSD: Forgot %d locks", count);
4591}
4592
4593void nfsd_forget_openowners(u64 num)
4594{
4595 int count;
4596
4597 nfs4_lock_state();
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004598 count = nfsd_release_n_owners(num, true, release_openowner_sop);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004599 nfs4_unlock_state();
4600
4601 printk(KERN_INFO "NFSD: Forgot %d open owners", count);
4602}
4603
4604int nfsd_process_n_delegations(u64 num, void (*deleg_func)(struct nfs4_delegation *))
4605{
4606 int i, count = 0;
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004607 struct nfs4_file *fp, *fnext;
4608 struct nfs4_delegation *dp, *dnext;
Bryan Schumaker65178db2011-11-01 13:35:21 -04004609
4610 for (i = 0; i < FILE_HASH_SIZE; i++) {
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004611 list_for_each_entry_safe(fp, fnext, &file_hashtbl[i], fi_hash) {
4612 list_for_each_entry_safe(dp, dnext, &fp->fi_delegations, dl_perfile) {
Bryan Schumaker65178db2011-11-01 13:35:21 -04004613 deleg_func(dp);
4614 if (++count == num)
4615 return count;
4616 }
4617 }
4618 }
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004619
Bryan Schumaker65178db2011-11-01 13:35:21 -04004620 return count;
4621}
4622
4623void nfsd_forget_delegations(u64 num)
4624{
4625 unsigned int count;
4626
4627 nfs4_lock_state();
4628 count = nfsd_process_n_delegations(num, unhash_delegation);
4629 nfs4_unlock_state();
4630
4631 printk(KERN_INFO "NFSD: Forgot %d delegations", count);
4632}
4633
4634void nfsd_recall_delegations(u64 num)
4635{
4636 unsigned int count;
4637
4638 nfs4_lock_state();
4639 spin_lock(&recall_lock);
4640 count = nfsd_process_n_delegations(num, nfsd_break_one_deleg);
4641 spin_unlock(&recall_lock);
4642 nfs4_unlock_state();
4643
4644 printk(KERN_INFO "NFSD: Recalled %d delegations", count);
4645}
4646
4647#endif /* CONFIG_NFSD_FAULT_INJECTION */
4648
NeilBrownac4d8ff22005-06-23 22:03:30 -07004649/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Bryan Schumaker72083392011-11-01 15:24:59 -04004651void
NeilBrownac4d8ff22005-06-23 22:03:30 -07004652nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Bryan Schumaker72083392011-11-01 15:24:59 -04004654 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4657 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4658 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4659 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4660 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
Wang Chen02cb2852009-04-24 15:41:57 +08004661 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 }
Marc Eshel5282fd72009-04-03 08:27:52 +03004663 for (i = 0; i < SESSION_HASH_SIZE; i++)
4664 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 for (i = 0; i < FILE_HASH_SIZE; i++) {
4666 INIT_LIST_HEAD(&file_hashtbl[i]);
4667 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004668 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4669 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 }
J. Bruce Fields009673b2011-11-07 17:40:10 -05004671 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4672 INIT_LIST_HEAD(&lockowner_ino_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 INIT_LIST_HEAD(&close_lru);
4674 INIT_LIST_HEAD(&client_lru);
4675 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004676 reclaim_str_hashtbl_size = 0;
NeilBrownac4d8ff22005-06-23 22:03:30 -07004677}
4678
Meelap Shahc2f1a552007-07-17 04:04:39 -07004679/*
4680 * Since the lifetime of a delegation isn't limited to that of an open, a
4681 * client may quite reasonably hang on to a delegation as long as it has
4682 * the inode cached. This becomes an obvious problem the first time a
4683 * client's inode cache approaches the size of the server's total memory.
4684 *
4685 * For now we avoid this problem by imposing a hard limit on the number
4686 * of delegations, which varies according to the server's memory size.
4687 */
4688static void
4689set_max_delegations(void)
4690{
4691 /*
4692 * Allow at most 4 delegations per megabyte of RAM. Quick
4693 * estimates suggest that in the worst case (where every delegation
4694 * is for a different inode), a delegation could take about 1.5K,
4695 * giving a worst case usage of about 6% of memory.
4696 */
4697 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4698}
4699
NeilBrownac4d8ff22005-06-23 22:03:30 -07004700/* initialization to perform when the nfsd service is started: */
4701
Jeff Laytona6d6b782012-03-05 11:42:36 -05004702int
4703nfs4_state_start(void)
NeilBrownac4d8ff22005-06-23 22:03:30 -07004704{
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004705 int ret;
4706
Jeff Layton2a4317c2012-03-21 16:42:43 -04004707 /*
4708 * FIXME: For now, we hang most of the pernet global stuff off of
4709 * init_net until nfsd is fully containerized. Eventually, we'll
4710 * need to pass a net pointer into this function, take a reference
4711 * to that instead and then do most of the rest of this on a per-net
4712 * basis.
4713 */
4714 get_net(&init_net);
4715 nfsd4_client_tracking_init(&init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 boot_time = get_seconds();
J. Bruce Fieldsaf558e32007-09-06 12:34:25 -04004717 locks_start_grace(&nfsd4_manager);
Jeff Layton33dcc482012-04-10 11:08:48 -04004718 grace_ended = false;
Marc Eshel9a8db972007-07-17 04:04:35 -07004719 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004720 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004721 ret = set_callback_cred();
Jeff Laytona6d6b782012-03-05 11:42:36 -05004722 if (ret) {
4723 ret = -ENOMEM;
4724 goto out_recovery;
4725 }
NeilBrown58da2822005-06-23 22:03:19 -07004726 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05004727 if (laundry_wq == NULL) {
4728 ret = -ENOMEM;
4729 goto out_recovery;
4730 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004731 ret = nfsd4_create_callback_queue();
4732 if (ret)
4733 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004734 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004735 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004736 return 0;
4737out_free_laundry:
4738 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05004739out_recovery:
Jeff Layton2a4317c2012-03-21 16:42:43 -04004740 nfsd4_client_tracking_exit(&init_net);
4741 put_net(&init_net);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745static void
4746__nfs4_state_shutdown(void)
4747{
4748 int i;
4749 struct nfs4_client *clp = NULL;
4750 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 struct list_head *pos, *next, reaplist;
4752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4754 while (!list_empty(&conf_id_hashtbl[i])) {
4755 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4756 expire_client(clp);
4757 }
4758 while (!list_empty(&unconf_str_hashtbl[i])) {
4759 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4760 expire_client(clp);
4761 }
4762 }
4763 INIT_LIST_HEAD(&reaplist);
4764 spin_lock(&recall_lock);
4765 list_for_each_safe(pos, next, &del_recall_lru) {
4766 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4767 list_move(&dp->dl_recall_lru, &reaplist);
4768 }
4769 spin_unlock(&recall_lock);
4770 list_for_each_safe(pos, next, &reaplist) {
4771 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 unhash_delegation(dp);
4773 }
4774
Jeff Layton2a4317c2012-03-21 16:42:43 -04004775 nfsd4_client_tracking_exit(&init_net);
4776 put_net(&init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
4779void
4780nfs4_state_shutdown(void)
4781{
Tejun Heoafe2c512010-12-14 16:21:17 +01004782 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004783 destroy_workqueue(laundry_wq);
J. Bruce Fields2c5e7612008-11-20 14:36:17 -06004784 locks_end_grace(&nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 __nfs4_state_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004788 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004790
4791static void
4792get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4793{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01004794 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
4795 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004796}
4797
4798static void
4799put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4800{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01004801 if (cstate->minorversion) {
4802 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
4803 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
4804 }
4805}
4806
4807void
4808clear_current_stateid(struct nfsd4_compound_state *cstate)
4809{
4810 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004811}
4812
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004813/*
4814 * functions to set current state id
4815 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004816void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01004817nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
4818{
4819 put_stateid(cstate, &odp->od_stateid);
4820}
4821
4822void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004823nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
4824{
4825 put_stateid(cstate, &open->op_stateid);
4826}
4827
4828void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004829nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
4830{
4831 put_stateid(cstate, &close->cl_stateid);
4832}
4833
4834void
4835nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
4836{
4837 put_stateid(cstate, &lock->lk_resp_stateid);
4838}
4839
4840/*
4841 * functions to consume current state id
4842 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01004843
4844void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01004845nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
4846{
4847 get_stateid(cstate, &odp->od_stateid);
4848}
4849
4850void
4851nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
4852{
4853 get_stateid(cstate, &drp->dr_stateid);
4854}
4855
4856void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01004857nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
4858{
4859 get_stateid(cstate, &fsp->fr_stateid);
4860}
4861
4862void
4863nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
4864{
4865 get_stateid(cstate, &setattr->sa_stateid);
4866}
4867
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004868void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004869nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
4870{
4871 get_stateid(cstate, &close->cl_stateid);
4872}
4873
4874void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004875nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004876{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004877 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004878}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01004879
4880void
4881nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
4882{
4883 get_stateid(cstate, &read->rd_stateid);
4884}
4885
4886void
4887nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
4888{
4889 get_stateid(cstate, &write->wr_stateid);
4890}