blob: 6df427773d01de7865931c0901731489ab0bce5a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton363168b2009-08-14 12:57:56 -040043#include <linux/sunrpc/clnt.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020044#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050045#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040046#include "current_stateid.h"
J. Bruce Fields99dbb8f2012-07-27 16:30:12 -040047#include "fault_inject.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040049#include "netns.h"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define NFSDDBG_FACILITY NFSDDBG_PROC
52
53/* Globals */
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -050054time_t nfsd4_lease = 90; /* default lease time */
J. Bruce Fieldsefc4bb4f2010-03-02 11:04:06 -050055time_t nfsd4_grace = 90;
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050056
57#define all_ones {{~0,~0},~0}
58static const stateid_t one_stateid = {
59 .si_generation = ~0,
60 .si_opaque = all_ones,
61};
62static const stateid_t zero_stateid = {
63 /* all fields zero */
64};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010065static const stateid_t currentstateid = {
66 .si_generation = 1,
67};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068
Andy Adamsonec6b5d72009-04-03 08:28:28 +030069static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070070
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050071#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
72#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010073#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040076static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
J. Bruce Fields8b671b82009-02-22 14:51:34 -080078/* Locking: */
79
80/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080081static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
J. Bruce Fields8b671b82009-02-22 14:51:34 -080083/*
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
87 */
88static DEFINE_SPINLOCK(recall_lock);
89
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040090static struct kmem_cache *openowner_slab = NULL;
91static struct kmem_cache *lockowner_slab = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080092static struct kmem_cache *file_slab = NULL;
93static struct kmem_cache *stateid_slab = NULL;
94static struct kmem_cache *deleg_slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096void
97nfs4_lock_state(void)
98{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080099 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102static void free_session(struct kref *);
103
104/* Must be called under the client_lock */
105static void nfsd4_put_session_locked(struct nfsd4_session *ses)
106{
107 kref_put(&ses->se_ref, free_session);
108}
109
110static void nfsd4_get_session(struct nfsd4_session *ses)
111{
112 kref_get(&ses->se_ref);
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115void
116nfs4_unlock_state(void)
117{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800118 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static inline u32
122opaque_hashval(const void *ptr, int nbytes)
123{
124 unsigned char *cptr = (unsigned char *) ptr;
125
126 u32 x = 0;
127 while (nbytes--) {
128 x *= 37;
129 x += *cptr++;
130 }
131 return x;
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static struct list_head del_recall_lru;
135
J. Bruce Fields32513b42011-10-13 16:00:16 -0400136static void nfsd4_free_file(struct nfs4_file *f)
137{
138 kmem_cache_free(file_slab, f);
139}
140
NeilBrown13cd2182005-06-23 22:03:10 -0700141static inline void
142put_nfs4_file(struct nfs4_file *fi)
143{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800144 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
145 list_del(&fi->fi_hash);
146 spin_unlock(&recall_lock);
147 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400148 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800149 }
NeilBrown13cd2182005-06-23 22:03:10 -0700150}
151
152static inline void
153get_nfs4_file(struct nfs4_file *fi)
154{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800155 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700156}
157
NeilBrownef0f3392006-04-10 22:55:41 -0700158static int num_delegations;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700159unsigned int max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700160
161/*
162 * Open owner state (share locks)
163 */
164
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500165/* hash tables for lock and open owners */
166#define OWNER_HASH_BITS 8
167#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
168#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700169
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500170static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400171{
172 unsigned int ret;
173
174 ret = opaque_hashval(ownername->data, ownername->len);
175 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500176 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400177}
NeilBrownef0f3392006-04-10 22:55:41 -0700178
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500179static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700180
181/* hash table for nfs4_file */
182#define FILE_HASH_BITS 8
183#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800184
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400185static unsigned int file_hashval(struct inode *ino)
186{
187 /* XXX: why are we hashing on inode pointer, anyway? */
188 return hash_ptr(ino, FILE_HASH_BITS);
189}
190
NeilBrownef0f3392006-04-10 22:55:41 -0700191static struct list_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700192
J. Bruce Fields998db522010-08-07 09:21:41 -0400193static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400194{
195 BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
196 atomic_inc(&fp->fi_access[oflag]);
197}
198
J. Bruce Fields998db522010-08-07 09:21:41 -0400199static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
200{
201 if (oflag == O_RDWR) {
202 __nfs4_file_get_access(fp, O_RDONLY);
203 __nfs4_file_get_access(fp, O_WRONLY);
204 } else
205 __nfs4_file_get_access(fp, oflag);
206}
207
208static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400209{
210 if (fp->fi_fds[oflag]) {
211 fput(fp->fi_fds[oflag]);
212 fp->fi_fds[oflag] = NULL;
213 }
214}
215
J. Bruce Fields998db522010-08-07 09:21:41 -0400216static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400217{
218 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400219 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400220 /*
221 * It's also safe to get rid of the RDWR open *if*
222 * we no longer have need of the other kind of access
223 * or if we already have the other kind of open:
224 */
225 if (fp->fi_fds[1-oflag]
226 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
227 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400228 }
229}
230
J. Bruce Fields998db522010-08-07 09:21:41 -0400231static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
232{
233 if (oflag == O_RDWR) {
234 __nfs4_file_put_access(fp, O_RDONLY);
235 __nfs4_file_put_access(fp, O_WRONLY);
236 } else
237 __nfs4_file_put_access(fp, oflag);
238}
239
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400240static inline int get_new_stid(struct nfs4_stid *stid)
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400241{
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400242 static int min_stateid = 0;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400243 struct idr *stateids = &stid->sc_client->cl_stateids;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400244 int new_stid;
245 int error;
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400246
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400247 error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400248 /*
J. Bruce Fields996e0932011-10-17 11:14:48 -0400249 * Note: the necessary preallocation was done in
250 * nfs4_alloc_stateid(). The idr code caps the number of
251 * preallocations that can exist at a time, but the state lock
252 * prevents anyone from using ours before we get here:
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400253 */
254 BUG_ON(error);
255 /*
256 * It shouldn't be a problem to reuse an opaque stateid value.
257 * I don't think it is for 4.1. But with 4.0 I worry that, for
258 * example, a stray write retransmission could be accepted by
259 * the server when it should have been rejected. Therefore,
260 * adopt a trick from the sctp code to attempt to maximize the
261 * amount of time until an id is reused, by ensuring they always
262 * "increase" (mod INT_MAX):
263 */
264
265 min_stateid = new_stid+1;
266 if (min_stateid == INT_MAX)
267 min_stateid = 0;
268 return new_stid;
J. Bruce Fields36d44c62011-09-08 12:16:03 -0400269}
270
J. Bruce Fields996e0932011-10-17 11:14:48 -0400271static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400272{
273 stateid_t *s = &stid->sc_stateid;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400274 int new_id;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400275
276 stid->sc_type = type;
277 stid->sc_client = cl;
278 s->si_opaque.so_clid = cl->cl_clientid;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400279 new_id = get_new_stid(stid);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400280 s->si_opaque.so_id = (u32)new_id;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400281 /* Will be incremented before return to client: */
282 s->si_generation = 0;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400283}
284
285static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
286{
287 struct idr *stateids = &cl->cl_stateids;
288
289 if (!idr_pre_get(stateids, GFP_KERNEL))
290 return NULL;
291 /*
292 * Note: if we fail here (or any time between now and the time
293 * we actually get the new idr), we won't need to undo the idr
294 * preallocation, since the idr code caps the number of
295 * preallocated entries.
296 */
297 return kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400298}
299
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400300static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
301{
302 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static struct nfs4_delegation *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400306alloc_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 -0700307{
308 struct nfs4_delegation *dp;
309 struct nfs4_file *fp = stp->st_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 dprintk("NFSD alloc_init_deleg\n");
J. Bruce Fieldsc3e48082010-07-28 10:08:57 -0400312 /*
313 * Major work on the lease subsystem (for example, to support
314 * calbacks on stat) will be required before we can support
315 * write delegations properly.
316 */
317 if (type != NFS4_OPEN_DELEGATE_READ)
318 return NULL;
Meelap Shah47f99402007-07-17 04:04:40 -0700319 if (fp->fi_had_conflict)
320 return NULL;
Meelap Shahc2f1a552007-07-17 04:04:39 -0700321 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700322 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400323 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700324 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return dp;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400326 init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400327 /*
328 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400329 * meaning of seqid 0 isn't meaningful, really, but let's avoid
330 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400331 */
332 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700333 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700334 INIT_LIST_HEAD(&dp->dl_perfile);
335 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 INIT_LIST_HEAD(&dp->dl_recall_lru);
NeilBrown13cd2182005-06-23 22:03:10 -0700337 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dp->dl_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 dp->dl_type = type;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500340 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dp->dl_time = 0;
342 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500343 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return dp;
345}
346
347void
348nfs4_put_delegation(struct nfs4_delegation *dp)
349{
350 if (atomic_dec_and_test(&dp->dl_count)) {
351 dprintk("NFSD: freeing dp %p\n",dp);
NeilBrown13cd2182005-06-23 22:03:10 -0700352 put_nfs4_file(dp->dl_file);
NeilBrown5b2d21c2005-06-23 22:03:04 -0700353 kmem_cache_free(deleg_slab, dp);
NeilBrownef0f3392006-04-10 22:55:41 -0700354 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356}
357
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500358static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500360 if (atomic_dec_and_test(&fp->fi_delegees)) {
361 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
362 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400363 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500364 fp->fi_deleg_file = NULL;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400368static void unhash_stid(struct nfs4_stid *s)
369{
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -0400370 struct idr *stateids = &s->sc_client->cl_stateids;
371
372 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/* Called under the state lock. */
376static void
377unhash_delegation(struct nfs4_delegation *dp)
378{
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400379 unhash_stid(&dp->dl_stid);
NeilBrownea1da632005-06-23 22:04:17 -0700380 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 spin_lock(&recall_lock);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500382 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 list_del_init(&dp->dl_recall_lru);
384 spin_unlock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500385 nfs4_put_deleg_lease(dp->dl_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 nfs4_put_delegation(dp);
387}
388
389/*
390 * SETCLIENTID state
391 */
392
Benny Halevy36acb662010-05-12 00:13:04 +0300393/* client_lock protects the client lru list and session hash table */
Benny Halevy9089f1b2010-05-12 00:12:26 +0300394static DEFINE_SPINLOCK(client_lock);
395
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400396static unsigned int clientid_hashval(u32 id)
397{
398 return id & CLIENT_HASH_MASK;
399}
400
401static unsigned int clientstr_hashval(const char *name)
402{
403 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
Jeff Laytonac55fdc2012-11-12 15:00:56 -0500407 * conf_id_hashtbl[], and conf_name_tree hold confirmed
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +0300408 * setclientid_confirmed info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *
Jeff Laytonac55fdc2012-11-12 15:00:56 -0500410 * unconf_id_hashtbl[] and unconf_name_tree hold unconfirmed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * setclientid info.
412 *
413 * client_lru holds client queue ordered by nfs4_client.cl_time
414 * for lease renewal.
415 *
416 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
417 * for last close replay.
Jeff Laytonac55fdc2012-11-12 15:00:56 -0500418 *
419 * All of the above fields are protected by the client_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
Jeff Laytonac55fdc2012-11-12 15:00:56 -0500422static struct rb_root conf_name_tree;
423static struct rb_root unconf_name_tree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static struct list_head client_lru;
425static struct list_head close_lru;
426
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400427/*
428 * We store the NONE, READ, WRITE, and BOTH bits separately in the
429 * st_{access,deny}_bmap field of the stateid, in order to track not
430 * only what share bits are currently in force, but also what
431 * combinations of share bits previous opens have used. This allows us
432 * to enforce the recommendation of rfc 3530 14.2.19 that the server
433 * return an error if the client attempt to downgrade to a combination
434 * of share bits not explicable by closing some of its previous opens.
435 *
436 * XXX: This enforcement is actually incomplete, since we don't keep
437 * track of access/deny bit combinations; so, e.g., we allow:
438 *
439 * OPEN allow read, deny write
440 * OPEN allow both, deny none
441 * DOWNGRADE allow read, deny none
442 *
443 * which we should reject.
444 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400445static unsigned int
446bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400447 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400448 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400449
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400450 for (i = 1; i < 4; i++) {
451 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400452 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400453 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400454 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400455}
456
Jeff Layton3a328612012-05-11 09:45:12 -0400457static bool
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400458test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400459 unsigned int access, deny;
460
Jeff Layton5ae037e2012-05-11 09:45:11 -0400461 access = bmap_to_share_mode(stp->st_access_bmap);
462 deny = bmap_to_share_mode(stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400463 if ((access & open->op_share_deny) || (deny & open->op_share_access))
Jeff Layton3a328612012-05-11 09:45:12 -0400464 return false;
465 return true;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400466}
467
Jeff Layton82c5ff12012-05-11 09:45:13 -0400468/* set share access for a given stateid */
469static inline void
470set_access(u32 access, struct nfs4_ol_stateid *stp)
471{
472 __set_bit(access, &stp->st_access_bmap);
473}
474
475/* clear share access for a given stateid */
476static inline void
477clear_access(u32 access, struct nfs4_ol_stateid *stp)
478{
479 __clear_bit(access, &stp->st_access_bmap);
480}
481
482/* test whether a given stateid has access */
483static inline bool
484test_access(u32 access, struct nfs4_ol_stateid *stp)
485{
486 return test_bit(access, &stp->st_access_bmap);
487}
488
Jeff Laytonce0fc432012-05-11 09:45:14 -0400489/* set share deny for a given stateid */
490static inline void
491set_deny(u32 access, struct nfs4_ol_stateid *stp)
492{
493 __set_bit(access, &stp->st_deny_bmap);
494}
495
496/* clear share deny for a given stateid */
497static inline void
498clear_deny(u32 access, struct nfs4_ol_stateid *stp)
499{
500 __clear_bit(access, &stp->st_deny_bmap);
501}
502
503/* test whether a given stateid is denying specific access */
504static inline bool
505test_deny(u32 access, struct nfs4_ol_stateid *stp)
506{
507 return test_bit(access, &stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400508}
509
510static int nfs4_access_to_omode(u32 access)
511{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400512 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400513 case NFS4_SHARE_ACCESS_READ:
514 return O_RDONLY;
515 case NFS4_SHARE_ACCESS_WRITE:
516 return O_WRONLY;
517 case NFS4_SHARE_ACCESS_BOTH:
518 return O_RDWR;
519 }
520 BUG();
521}
522
Jeff Layton82c5ff12012-05-11 09:45:13 -0400523/* release all access and file references for a given stateid */
524static void
525release_all_access(struct nfs4_ol_stateid *stp)
526{
527 int i;
528
529 for (i = 1; i < 4; i++) {
530 if (test_access(i, stp))
531 nfs4_file_put_access(stp->st_file,
532 nfs4_access_to_omode(i));
533 clear_access(i, stp);
534 }
535}
536
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400537static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500538{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500539 list_del(&stp->st_perfile);
540 list_del(&stp->st_perstateowner);
541}
542
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400543static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500544{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400545 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400546 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400547 stp->st_file = NULL;
548}
549
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400550static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400551{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500552 kmem_cache_free(stateid_slab, stp);
553}
554
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400555static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500556{
557 struct file *file;
558
559 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400560 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500561 file = find_any_file(stp->st_file);
562 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400563 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400564 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500565 free_generic_stateid(stp);
566}
567
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400568static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500569{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400570 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500571
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400572 list_del(&lo->lo_owner.so_strhash);
573 list_del(&lo->lo_perstateid);
J. Bruce Fields009673b2011-11-07 17:40:10 -0500574 list_del(&lo->lo_owner_ino_hash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400575 while (!list_empty(&lo->lo_owner.so_stateids)) {
576 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400577 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500578 release_lock_stateid(stp);
579 }
580}
581
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400582static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500583{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400584 unhash_lockowner(lo);
585 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500586}
587
588static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400589release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500590{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400591 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500592
593 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400594 lo = list_entry(open_stp->st_lockowners.next,
595 struct nfs4_lockowner, lo_perstateid);
596 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500597 }
598}
599
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400600static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500601{
602 unhash_generic_stateid(stp);
603 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400604 close_generic_stateid(stp);
605}
606
607static void release_open_stateid(struct nfs4_ol_stateid *stp)
608{
609 unhash_open_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400610 unhash_stid(&stp->st_stid);
J. Bruce Fields22839632009-01-11 14:27:17 -0500611 free_generic_stateid(stp);
612}
613
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400614static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500615{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400616 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500617
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400618 list_del(&oo->oo_owner.so_strhash);
619 list_del(&oo->oo_perclient);
620 while (!list_empty(&oo->oo_owner.so_stateids)) {
621 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400622 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500623 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500624 }
625}
626
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400627static void release_last_closed_stateid(struct nfs4_openowner *oo)
628{
629 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
630
631 if (s) {
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400632 unhash_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400633 free_generic_stateid(s);
634 oo->oo_last_closed_stid = NULL;
635 }
636}
637
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400638static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500639{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400640 unhash_openowner(oo);
641 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400642 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400643 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500644}
645
Marc Eshel5282fd72009-04-03 08:27:52 +0300646#define SESSION_HASH_SIZE 512
647static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
648
649static inline int
650hash_sessionid(struct nfs4_sessionid *sessionid)
651{
652 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
653
654 return sid->sequence % SESSION_HASH_SIZE;
655}
656
Trond Myklebust8f199b82012-03-20 15:11:17 -0400657#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300658static inline void
659dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
660{
661 u32 *ptr = (u32 *)(&sessionid->data[0]);
662 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
663}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400664#else
665static inline void
666dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
667{
668}
669#endif
670
Marc Eshel5282fd72009-04-03 08:27:52 +0300671
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300672static void
673gen_sessionid(struct nfsd4_session *ses)
674{
675 struct nfs4_client *clp = ses->se_client;
676 struct nfsd4_sessionid *sid;
677
678 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
679 sid->clientid = clp->cl_clientid;
680 sid->sequence = current_sessionid++;
681 sid->reserved = 0;
682}
683
684/*
Andy Adamsona6496372009-08-28 08:45:01 -0400685 * The protocol defines ca_maxresponssize_cached to include the size of
686 * the rpc header, but all we need to cache is the data starting after
687 * the end of the initial SEQUENCE operation--the rest we regenerate
688 * each time. Therefore we can advertise a ca_maxresponssize_cached
689 * value that is the number of bytes in our cache plus a few additional
690 * bytes. In order to stay on the safe side, and not promise more than
691 * we can cache, those additional bytes must be the minimum possible: 24
692 * bytes of rpc header (xid through accept state, with AUTH_NULL
693 * verifier), 12 for the compound header (with zero-length tag), and 44
694 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300695 */
Andy Adamsona6496372009-08-28 08:45:01 -0400696#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
697
Andy Adamson557ce262009-08-28 08:45:04 -0400698static void
699free_session_slots(struct nfsd4_session *ses)
700{
701 int i;
702
703 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
704 kfree(ses->se_slots[i]);
705}
706
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400707/*
708 * We don't actually need to cache the rpc and session headers, so we
709 * can allocate a little less for each slot:
710 */
711static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
712{
713 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
714}
715
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400716static int nfsd4_sanitize_slot_size(u32 size)
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300717{
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400718 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
719 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300720
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400721 return size;
722}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300723
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400724/*
725 * XXX: If we run out of reserved DRC memory we could (up to a point)
726 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800727 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400728 */
729static int nfsd4_get_drc_mem(int slotsize, u32 num)
730{
731 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300732
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400733 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
Andy Adamson557ce262009-08-28 08:45:04 -0400734
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400735 spin_lock(&nfsd_drc_lock);
736 avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
737 nfsd_drc_max_mem - nfsd_drc_mem_used);
738 num = min_t(int, num, avail / slotsize);
739 nfsd_drc_mem_used += num * slotsize;
740 spin_unlock(&nfsd_drc_lock);
741
742 return num;
743}
744
745static void nfsd4_put_drc_mem(int slotsize, int num)
746{
747 spin_lock(&nfsd_drc_lock);
748 nfsd_drc_mem_used -= slotsize * num;
749 spin_unlock(&nfsd_drc_lock);
750}
751
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400752static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400753{
754 struct nfsd4_session *new;
755 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300756
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400757 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400758 + sizeof(struct nfsd4_session) > PAGE_SIZE);
759 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400760
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400761 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300762 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400763 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400764 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400765 for (i = 0; i < numslots; i++) {
766 mem = sizeof(struct nfsd4_slot) + slotsize;
767 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
768 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400769 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400770 }
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400771 return new;
772out_free:
773 while (i--)
774 kfree(new->se_slots[i]);
775 kfree(new);
776 return NULL;
777}
778
779static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
780{
781 u32 maxrpc = nfsd_serv->sv_max_mesg;
782
783 new->maxreqs = numslots;
Mi Jinlongd2b21742011-03-10 17:43:37 +0800784 new->maxresp_cached = min_t(u32, req->maxresp_cached,
785 slotsize + NFSD_MIN_HDR_SEQ_SZ);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400786 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
787 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
788 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
789}
790
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400791static void free_conn(struct nfsd4_conn *c)
792{
793 svc_xprt_put(c->cn_xprt);
794 kfree(c);
795}
796
797static void nfsd4_conn_lost(struct svc_xpt_user *u)
798{
799 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
800 struct nfs4_client *clp = c->cn_session->se_client;
801
802 spin_lock(&clp->cl_lock);
803 if (!list_empty(&c->cn_persession)) {
804 list_del(&c->cn_persession);
805 free_conn(c);
806 }
807 spin_unlock(&clp->cl_lock);
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500808 nfsd4_probe_callback(clp);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400809}
810
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400811static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400812{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400813 struct nfsd4_conn *conn;
814
815 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
816 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400817 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400818 svc_xprt_get(rqstp->rq_xprt);
819 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400820 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400821 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
822 return conn;
823}
824
J. Bruce Fields328ead22010-09-29 16:11:06 -0400825static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
826{
827 conn->cn_session = ses;
828 list_add(&conn->cn_persession, &ses->se_conns);
829}
830
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400831static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
832{
833 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400834
835 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -0400836 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400837 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400838}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400839
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400840static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400841{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400842 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400843 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400844}
845
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400846static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400847{
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400848 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400849
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400850 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400851 ret = nfsd4_register_conn(conn);
852 if (ret)
853 /* oops; xprt is already down: */
854 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -0400855 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -0400856 /* callback channel may be back up */
857 nfsd4_probe_callback(ses->se_client);
858 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400859}
860
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400861static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400862{
863 u32 dir = NFS4_CDFC4_FORE;
864
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400865 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400866 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -0400867 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400868}
869
870/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400871static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400872{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400873 struct nfs4_client *clp = s->se_client;
874 struct nfsd4_conn *c;
875
876 spin_lock(&clp->cl_lock);
877 while (!list_empty(&s->se_conns)) {
878 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
879 list_del_init(&c->cn_persession);
880 spin_unlock(&clp->cl_lock);
881
882 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
883 free_conn(c);
884
885 spin_lock(&clp->cl_lock);
886 }
887 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400888}
889
J. Bruce Fields1377b692012-09-11 21:42:40 -0400890static void __free_session(struct nfsd4_session *ses)
891{
892 nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
893 free_session_slots(ses);
894 kfree(ses);
895}
896
Benny Halevy508dc6e2012-02-23 17:40:52 -0800897static void free_session(struct kref *kref)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400898{
899 struct nfsd4_session *ses;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400900
J. Bruce Fieldsbc2df472012-06-12 08:28:48 -0400901 lockdep_assert_held(&client_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400902 ses = container_of(kref, struct nfsd4_session, se_ref);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400903 nfsd4_del_conns(ses);
J. Bruce Fields1377b692012-09-11 21:42:40 -0400904 __free_session(ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400905}
906
Benny Halevy508dc6e2012-02-23 17:40:52 -0800907void nfsd4_put_session(struct nfsd4_session *ses)
908{
909 spin_lock(&client_lock);
910 nfsd4_put_session_locked(ses);
911 spin_unlock(&client_lock);
912}
913
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400914static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400915{
916 struct nfsd4_session *new;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400917 int numslots, slotsize;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400918 /*
919 * Note decreasing slot size below client's request may
920 * make it difficult for client to function correctly, whereas
921 * decreasing the number of slots will (just?) affect
922 * performance. When short on memory we therefore prefer to
923 * decrease number of slots instead of their size.
924 */
925 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
926 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
Mi Jinlongced6dfe2010-11-11 18:03:50 +0800927 if (numslots < 1)
928 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400929
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400930 new = __alloc_session(slotsize, numslots);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400931 if (!new) {
932 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400933 return NULL;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400934 }
935 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400936 return new;
937}
Andy Adamson557ce262009-08-28 08:45:04 -0400938
Fengguang Wu135ae822012-11-10 07:20:25 -0500939static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400940{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -0400941 int idx;
942
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300943 new->se_client = clp;
944 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300945
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400946 INIT_LIST_HEAD(&new->se_conns);
947
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400948 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300949 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400950 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400951 new->se_cb_sec = cses->cb_sec;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300952 kref_init(&new->se_ref);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400953 idx = hash_sessionid(&new->se_sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300954 spin_lock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300955 list_add(&new->se_hash, &sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400956 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300957 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -0400958 spin_unlock(&clp->cl_lock);
Benny Halevy9089f1b2010-05-12 00:12:26 +0300959 spin_unlock(&client_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300960
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400961 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400962 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400963 /*
964 * This is a little silly; with sessions there's no real
965 * use for the callback address. Use the peer address
966 * as a reasonable default for now, but consider fixing
967 * the rpc client not to require an address in the
968 * future:
969 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400970 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
971 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -0400972 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300973}
974
Benny Halevy9089f1b2010-05-12 00:12:26 +0300975/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +0300976static struct nfsd4_session *
977find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
978{
979 struct nfsd4_session *elem;
980 int idx;
981
982 dump_sessionid(__func__, sessionid);
983 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +0300984 /* Search in the appropriate list */
985 list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +0300986 if (!memcmp(elem->se_sessionid.data, sessionid->data,
987 NFS4_MAX_SESSIONID_LEN)) {
988 return elem;
989 }
990 }
991
992 dprintk("%s: session not found\n", __func__);
993 return NULL;
994}
995
Benny Halevy9089f1b2010-05-12 00:12:26 +0300996/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +0300997static void
Marc Eshel5282fd72009-04-03 08:27:52 +0300998unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +0300999{
1000 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001001 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001002 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001003 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001004}
1005
Benny Halevy36acb662010-05-12 00:13:04 +03001006/* must be called under the client_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007static inline void
Benny Halevy36acb662010-05-12 00:13:04 +03001008renew_client_locked(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Benny Halevy07cd4902010-05-12 00:13:41 +03001010 if (is_client_expired(clp)) {
J. Bruce Fields74477582012-05-04 14:57:52 -04001011 WARN_ON(1);
1012 printk("%s: client (clientid %08x/%08x) already expired\n",
Benny Halevy07cd4902010-05-12 00:13:41 +03001013 __func__,
1014 clp->cl_clientid.cl_boot,
1015 clp->cl_clientid.cl_id);
1016 return;
1017 }
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 dprintk("renewing client (clientid %08x/%08x)\n",
1020 clp->cl_clientid.cl_boot,
1021 clp->cl_clientid.cl_id);
1022 list_move_tail(&clp->cl_lru, &client_lru);
1023 clp->cl_time = get_seconds();
1024}
1025
Benny Halevy36acb662010-05-12 00:13:04 +03001026static inline void
1027renew_client(struct nfs4_client *clp)
1028{
1029 spin_lock(&client_lock);
1030 renew_client_locked(clp);
1031 spin_unlock(&client_lock);
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1035static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001036STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001038 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001040 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001041 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 1;
1043}
1044
1045/*
1046 * XXX Should we use a slab cache ?
1047 * This type of memory management is somewhat inefficient, but we use it
1048 * anyway since SETCLIENTID is not a common operation.
1049 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001050static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct nfs4_client *clp;
1053
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001054 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1055 if (clp == NULL)
1056 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001057 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001058 if (clp->cl_name.data == NULL) {
1059 kfree(clp);
1060 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001062 clp->cl_name.len = name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return clp;
1064}
1065
1066static inline void
1067free_client(struct nfs4_client *clp)
1068{
J. Bruce Fieldsbc2df472012-06-12 08:28:48 -04001069 lockdep_assert_held(&client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001070 while (!list_empty(&clp->cl_sessions)) {
1071 struct nfsd4_session *ses;
1072 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1073 se_perclnt);
1074 list_del(&ses->se_perclnt);
Benny Halevy508dc6e2012-02-23 17:40:52 -08001075 nfsd4_put_session_locked(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001076 }
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001077 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 kfree(clp->cl_name.data);
1079 kfree(clp);
1080}
1081
Benny Halevyd7682982010-05-12 00:13:54 +03001082void
1083release_session_client(struct nfsd4_session *session)
1084{
1085 struct nfs4_client *clp = session->se_client;
1086
1087 if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
1088 return;
1089 if (is_client_expired(clp)) {
1090 free_client(clp);
1091 session->se_client = NULL;
1092 } else
1093 renew_client_locked(clp);
1094 spin_unlock(&client_lock);
Benny Halevyd7682982010-05-12 00:13:54 +03001095}
1096
Benny Halevy84d38ac2010-05-12 00:13:16 +03001097/* must be called under the client_lock */
1098static inline void
1099unhash_client_locked(struct nfs4_client *clp)
1100{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001101 struct nfsd4_session *ses;
1102
Benny Halevy07cd4902010-05-12 00:13:41 +03001103 mark_client_expired(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001104 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001105 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001106 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1107 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001108 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001112destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001114 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct list_head reaplist;
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 INIT_LIST_HEAD(&reaplist);
1119 spin_lock(&recall_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001120 while (!list_empty(&clp->cl_delegations)) {
1121 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001122 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 list_move(&dp->dl_recall_lru, &reaplist);
1124 }
1125 spin_unlock(&recall_lock);
1126 while (!list_empty(&reaplist)) {
1127 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 unhash_delegation(dp);
1129 }
NeilBrownea1da632005-06-23 22:04:17 -07001130 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001131 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1132 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001134 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001135 if (clp->cl_cb_conn.cb_xprt)
1136 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001137 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001138 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1139 rb_erase(&clp->cl_namenode, &conf_name_tree);
1140 else
1141 rb_erase(&clp->cl_namenode, &unconf_name_tree);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001142 spin_lock(&client_lock);
1143 unhash_client_locked(clp);
Benny Halevy46583e22010-05-12 00:13:29 +03001144 if (atomic_read(&clp->cl_refcount) == 0)
1145 free_client(clp);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001146 spin_unlock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001149static void expire_client(struct nfs4_client *clp)
1150{
1151 nfsd4_client_record_remove(clp);
1152 destroy_client(clp);
1153}
1154
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001155static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1156{
1157 memcpy(target->cl_verifier.data, source->data,
1158 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001161static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1164 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1165}
1166
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001167static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001168{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001169 if (source->cr_principal) {
1170 target->cr_principal =
1171 kstrdup(source->cr_principal, GFP_KERNEL);
1172 if (target->cr_principal == NULL)
1173 return -ENOMEM;
1174 } else
1175 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001176 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 target->cr_uid = source->cr_uid;
1178 target->cr_gid = source->cr_gid;
1179 target->cr_group_info = source->cr_group_info;
1180 get_group_info(target->cr_group_info);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001184static long long
1185compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1186{
1187 long long res;
1188
1189 res = o1->len - o2->len;
1190 if (res)
1191 return res;
1192 return (long long)memcmp(o1->data, o2->data, o1->len);
1193}
1194
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001195static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001196{
NeilBrowna55370a2005-06-23 22:03:52 -07001197 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
1200static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001201same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1202{
1203 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001207same_clid(clientid_t *cl1, clientid_t *cl2)
1208{
1209 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001212static bool groups_equal(struct group_info *g1, struct group_info *g2)
1213{
1214 int i;
1215
1216 if (g1->ngroups != g2->ngroups)
1217 return false;
1218 for (i=0; i<g1->ngroups; i++)
1219 if (GROUP_AT(g1, i) != GROUP_AT(g2, i))
1220 return false;
1221 return true;
1222}
1223
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001224/*
1225 * RFC 3530 language requires clid_inuse be returned when the
1226 * "principal" associated with a requests differs from that previously
1227 * used. We use uid, gid's, and gss principal string as our best
1228 * approximation. We also don't want to allow non-gss use of a client
1229 * established using gss: in theory cr_principal should catch that
1230 * change, but in practice cr_principal can be null even in the gss case
1231 * since gssd doesn't always pass down a principal string.
1232 */
1233static bool is_gss_cred(struct svc_cred *cr)
1234{
1235 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1236 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1237}
1238
1239
Vivek Trivedi5559b502012-07-24 21:18:20 +05301240static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001241same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1242{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001243 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001244 || (cr1->cr_uid != cr2->cr_uid)
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001245 || (cr1->cr_gid != cr2->cr_gid)
1246 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1247 return false;
1248 if (cr1->cr_principal == cr2->cr_principal)
1249 return true;
1250 if (!cr1->cr_principal || !cr2->cr_principal)
1251 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301252 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001255static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001256{
1257 static u32 current_clientid = 1;
1258
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001259 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 clp->cl_clientid.cl_id = current_clientid++;
1261}
1262
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001263static void gen_confirm(struct nfs4_client *clp)
1264{
Chuck Leverab4684d2012-03-02 17:13:50 -05001265 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001266 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Chuck Leverab4684d2012-03-02 17:13:50 -05001268 verf[0] = (__be32)get_seconds();
1269 verf[1] = (__be32)i++;
1270 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001273static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001274{
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001275 return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001276}
1277
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001278static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001279{
1280 struct nfs4_stid *s;
1281
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001282 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001283 if (!s)
1284 return NULL;
1285 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001286 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001287 return NULL;
1288}
1289
Jeff Layton2216d442012-11-12 15:00:57 -05001290static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001291 struct svc_rqst *rqstp, nfs4_verifier *verf)
1292{
1293 struct nfs4_client *clp;
1294 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001295 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001296 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001297
1298 clp = alloc_client(name);
1299 if (clp == NULL)
1300 return NULL;
1301
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001302 INIT_LIST_HEAD(&clp->cl_sessions);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001303 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1304 if (ret) {
1305 spin_lock(&client_lock);
1306 free_client(clp);
1307 spin_unlock(&client_lock);
1308 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001309 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001310 idr_init(&clp->cl_stateids);
Benny Halevy46583e22010-05-12 00:13:29 +03001311 atomic_set(&clp->cl_refcount, 0);
J. Bruce Fields77a35692010-04-30 18:51:44 -04001312 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001313 INIT_LIST_HEAD(&clp->cl_idhash);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001314 INIT_LIST_HEAD(&clp->cl_openowners);
1315 INIT_LIST_HEAD(&clp->cl_delegations);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001316 INIT_LIST_HEAD(&clp->cl_lru);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001317 INIT_LIST_HEAD(&clp->cl_callbacks);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001318 spin_lock_init(&clp->cl_lock);
J. Bruce Fields57725152012-11-05 15:10:26 -05001319 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001320 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001321 clear_bit(0, &clp->cl_cb_slot_busy);
1322 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1323 copy_verf(clp, verf);
1324 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001325 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001326 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001327 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001328 return clp;
1329}
1330
NeilBrownfd39ca92005-06-23 22:04:03 -07001331static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001332add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1333{
1334 struct rb_node **new = &(root->rb_node), *parent = NULL;
1335 struct nfs4_client *clp;
1336
1337 while (*new) {
1338 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1339 parent = *new;
1340
1341 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1342 new = &((*new)->rb_left);
1343 else
1344 new = &((*new)->rb_right);
1345 }
1346
1347 rb_link_node(&new_clp->cl_namenode, parent, new);
1348 rb_insert_color(&new_clp->cl_namenode, root);
1349}
1350
1351static struct nfs4_client *
1352find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1353{
1354 long long cmp;
1355 struct rb_node *node = root->rb_node;
1356 struct nfs4_client *clp;
1357
1358 while (node) {
1359 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1360 cmp = compare_blob(&clp->cl_name, name);
1361 if (cmp > 0)
1362 node = node->rb_left;
1363 else if (cmp < 0)
1364 node = node->rb_right;
1365 else
1366 return clp;
1367 }
1368 return NULL;
1369}
1370
1371static void
1372add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 unsigned int idhashval;
1375
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001376 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1377 add_clp_to_name_tree(clp, &unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1379 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001380 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
NeilBrownfd39ca92005-06-23 22:04:03 -07001383static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384move_to_confirmed(struct nfs4_client *clp)
1385{
1386 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001387 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001390 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001391 rb_erase(&clp->cl_namenode, &unconf_name_tree);
1392 add_clp_to_name_tree(clp, &conf_name_tree);
1393 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 renew_client(clp);
1395}
1396
1397static struct nfs4_client *
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001398find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct nfs4_client *clp;
1401 unsigned int idhashval = clientid_hashval(clid->cl_id);
1402
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001403 list_for_each_entry(clp, &nn->conf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001404 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001405 if ((bool)clp->cl_minorversion != sessions)
1406 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001407 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 return NULL;
1412}
1413
1414static struct nfs4_client *
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001415find_unconfirmed_client(clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct nfs4_client *clp;
1418 unsigned int idhashval = clientid_hashval(clid->cl_id);
1419
1420 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001421 if (same_clid(&clp->cl_clientid, clid)) {
1422 if ((bool)clp->cl_minorversion != sessions)
1423 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return clp;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427 return NULL;
1428}
1429
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001430static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001431{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001432 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001433}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001434
NeilBrown28ce6052005-06-23 22:03:56 -07001435static struct nfs4_client *
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001436find_confirmed_client_by_name(struct xdr_netobj *name)
NeilBrown28ce6052005-06-23 22:03:56 -07001437{
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001438 return find_clp_in_name_tree(name, &conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001439}
1440
1441static struct nfs4_client *
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001442find_unconfirmed_client_by_name(struct xdr_netobj *name)
NeilBrown28ce6052005-06-23 22:03:56 -07001443{
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001444 return find_clp_in_name_tree(name, &unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001445}
1446
NeilBrownfd39ca92005-06-23 22:04:03 -07001447static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001448gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001450 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001451 struct sockaddr *sa = svc_addr(rqstp);
1452 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001453 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Jeff Layton7077ecb2009-08-14 12:57:58 -04001455 /* Currently, we only support tcp and tcp6 for the callback channel */
1456 if (se->se_callback_netid_len == 3 &&
1457 !memcmp(se->se_callback_netid_val, "tcp", 3))
1458 expected_family = AF_INET;
1459 else if (se->se_callback_netid_len == 4 &&
1460 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1461 expected_family = AF_INET6;
1462 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 goto out_err;
1464
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001465 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001466 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001467 (struct sockaddr *)&conn->cb_addr,
1468 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001469
J. Bruce Fields07263f12010-05-31 19:09:40 -04001470 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001472
J. Bruce Fields07263f12010-05-31 19:09:40 -04001473 if (conn->cb_addr.ss_family == AF_INET6)
1474 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001475
J. Bruce Fields07263f12010-05-31 19:09:40 -04001476 conn->cb_prog = se->se_callback_prog;
1477 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001478 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return;
1480out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001481 conn->cb_addr.ss_family = AF_UNSPEC;
1482 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001483 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 "will not receive delegations\n",
1485 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return;
1488}
1489
Andy Adamson074fe892009-04-03 08:28:15 +03001490/*
Andy Adamson557ce262009-08-28 08:45:04 -04001491 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001492 */
1493void
1494nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1495{
Andy Adamson557ce262009-08-28 08:45:04 -04001496 struct nfsd4_slot *slot = resp->cstate.slot;
1497 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001498
Andy Adamson557ce262009-08-28 08:45:04 -04001499 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001500
Andy Adamson557ce262009-08-28 08:45:04 -04001501 slot->sl_opcnt = resp->opcnt;
1502 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001503
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001504 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001505 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001506 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001507 return;
1508 }
Andy Adamson557ce262009-08-28 08:45:04 -04001509 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1510 base = (char *)resp->cstate.datap -
1511 (char *)resp->xbuf->head[0].iov_base;
1512 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1513 slot->sl_datalen))
1514 WARN("%s: sessions DRC could not cache compound\n", __func__);
1515 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001516}
1517
1518/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001519 * Encode the replay sequence operation from the slot values.
1520 * If cachethis is FALSE encode the uncached rep error on the next
1521 * operation which sets resp->p and increments resp->opcnt for
1522 * nfs4svc_encode_compoundres.
1523 *
Andy Adamson074fe892009-04-03 08:28:15 +03001524 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001525static __be32
1526nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1527 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001528{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001529 struct nfsd4_op *op;
1530 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001531
Andy Adamsonabfabf82009-07-23 19:02:18 -04001532 /* Encode the replayed sequence operation */
1533 op = &args->ops[resp->opcnt - 1];
1534 nfsd4_encode_operation(resp, op);
1535
1536 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001537 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001538 op = &args->ops[resp->opcnt++];
1539 op->status = nfserr_retry_uncached_rep;
1540 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001541 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001542 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001543}
1544
1545/*
Andy Adamson557ce262009-08-28 08:45:04 -04001546 * The sequence operation is not cached because we can use the slot and
1547 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001548 */
1549__be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001550nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1551 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001552{
Andy Adamson557ce262009-08-28 08:45:04 -04001553 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001554 __be32 status;
1555
Andy Adamson557ce262009-08-28 08:45:04 -04001556 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001557
Andy Adamsonabfabf82009-07-23 19:02:18 -04001558 /* Either returns 0 or nfserr_retry_uncached */
1559 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1560 if (status == nfserr_retry_uncached_rep)
1561 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001562
Andy Adamson557ce262009-08-28 08:45:04 -04001563 /* The sequence operation has been encoded, cstate->datap set. */
1564 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
Andy Adamson074fe892009-04-03 08:28:15 +03001565
Andy Adamson557ce262009-08-28 08:45:04 -04001566 resp->opcnt = slot->sl_opcnt;
1567 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1568 status = slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001569
1570 return status;
1571}
1572
Andy Adamson0733d212009-04-03 08:28:01 +03001573/*
1574 * Set the exchange_id flags returned by the server.
1575 */
1576static void
1577nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1578{
1579 /* pNFS is not supported */
1580 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1581
1582 /* Referrals are supported, Migration is not. */
1583 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1584
1585 /* set the wire flags to return to client. */
1586 clid->flags = new->cl_exchange_flags;
1587}
1588
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001589static bool client_has_state(struct nfs4_client *clp)
1590{
1591 /*
1592 * Note clp->cl_openowners check isn't quite right: there's no
1593 * need to count owners without stateid's.
1594 *
1595 * Also note we should probably be using this in 4.0 case too.
1596 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001597 return !list_empty(&clp->cl_openowners)
1598 || !list_empty(&clp->cl_delegations)
1599 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001600}
1601
Al Virob37ad282006-10-19 23:28:59 -07001602__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001603nfsd4_exchange_id(struct svc_rqst *rqstp,
1604 struct nfsd4_compound_state *cstate,
1605 struct nfsd4_exchange_id *exid)
1606{
Andy Adamson0733d212009-04-03 08:28:01 +03001607 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001608 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001609 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001610 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001611 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001612 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001613 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001614
Jeff Layton363168b2009-08-14 12:57:56 -04001615 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001616 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001617 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001618 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001619 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001620
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001621 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001622 return nfserr_inval;
1623
1624 /* Currently only support SP4_NONE */
1625 switch (exid->spa_how) {
1626 case SP4_NONE:
1627 break;
1628 case SP4_SSV:
J. Bruce Fields044bc1d2010-11-12 14:36:06 -05001629 return nfserr_serverfault;
Andy Adamson0733d212009-04-03 08:28:01 +03001630 default:
1631 BUG(); /* checked by xdr code */
1632 case SP4_MACH_CRED:
1633 return nfserr_serverfault; /* no excuse :-/ */
1634 }
1635
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001636 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001637 nfs4_lock_state();
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001638 conf = find_confirmed_client_by_name(&exid->clname);
Andy Adamson0733d212009-04-03 08:28:01 +03001639 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001640 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1641 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1642
J. Bruce Fields136e6582012-05-12 20:37:23 -04001643 if (update) {
1644 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001645 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001646 goto out;
1647 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001648 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001649 status = nfserr_perm;
1650 goto out;
1651 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001652 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001653 status = nfserr_not_same;
1654 goto out;
1655 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001656 /* case 6 */
1657 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1658 new = conf;
1659 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001660 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001661 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001662 if (client_has_state(conf)) {
1663 status = nfserr_clid_inuse;
1664 goto out;
1665 }
Andy Adamson0733d212009-04-03 08:28:01 +03001666 expire_client(conf);
1667 goto out_new;
1668 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001669 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001670 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001671 new = conf;
1672 goto out_copy;
1673 }
1674 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001675 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001676 }
1677
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001678 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001679 status = nfserr_noent;
1680 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001681 }
1682
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001683 unconf = find_unconfirmed_client_by_name(&exid->clname);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001684 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001685 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001686
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001687 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001688out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001689 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001690 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001691 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001692 goto out;
1693 }
J. Bruce Fieldsc116a0a2012-09-11 14:53:09 -04001694 new->cl_minorversion = 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001695
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001696 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001697 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001698out_copy:
1699 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1700 exid->clientid.cl_id = new->cl_clientid.cl_id;
1701
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001702 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001703 nfsd4_set_ex_flags(new, exid);
1704
1705 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001706 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001707 status = nfs_ok;
1708
1709out:
1710 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001711 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001712}
1713
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001714static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001715check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001716{
Andy Adamson88e588d2009-07-23 19:02:15 -04001717 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1718 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001719
1720 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001721 if (slot_inuse) {
1722 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001723 return nfserr_jukebox;
1724 else
1725 return nfserr_seq_misordered;
1726 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001727 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001728 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001729 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001730 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001731 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001732 return nfserr_seq_misordered;
1733}
1734
Andy Adamson49557cc2009-07-23 19:02:16 -04001735/*
1736 * Cache the create session result into the create session single DRC
1737 * slot cache by saving the xdr structure. sl_seqid has been set.
1738 * Do this for solo or embedded create session operations.
1739 */
1740static void
1741nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001742 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001743{
1744 slot->sl_status = nfserr;
1745 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1746}
1747
1748static __be32
1749nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1750 struct nfsd4_clid_slot *slot)
1751{
1752 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1753 return slot->sl_status;
1754}
1755
Mi Jinlong1b74c252011-07-14 14:50:17 +08001756#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1757 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1758 1 + /* MIN tag is length with zero, only length */ \
1759 3 + /* version, opcount, opcode */ \
1760 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1761 /* seqid, slotID, slotID, cache */ \
1762 4 ) * sizeof(__be32))
1763
1764#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1765 2 + /* verifier: AUTH_NULL, length 0 */\
1766 1 + /* status */ \
1767 1 + /* MIN tag is length with zero, only length */ \
1768 3 + /* opcount, opcode, opstatus*/ \
1769 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1770 /* seqid, slotID, slotID, slotID, status */ \
1771 5 ) * sizeof(__be32))
1772
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001773static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001774{
1775 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1776 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1777}
1778
Andy Adamson069b6ad2009-04-03 08:27:58 +03001779__be32
1780nfsd4_create_session(struct svc_rqst *rqstp,
1781 struct nfsd4_compound_state *cstate,
1782 struct nfsd4_create_session *cr_ses)
1783{
Jeff Layton363168b2009-08-14 12:57:56 -04001784 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001785 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001786 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001787 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001788 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001789 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001790 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001791
Mi Jinlonga62573d2011-03-23 17:57:07 +08001792 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1793 return nfserr_inval;
J. Bruce Fields49730502012-09-11 15:33:21 -04001794 if (check_forechannel_attrs(cr_ses->fore_channel))
1795 return nfserr_toosmall;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001796 new = alloc_session(&cr_ses->fore_channel);
1797 if (!new)
1798 return nfserr_jukebox;
1799 status = nfserr_jukebox;
1800 conn = alloc_conn_from_crses(rqstp, cr_ses);
1801 if (!conn)
1802 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08001803
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001804 nfs4_lock_state();
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001805 unconf = find_unconfirmed_client(&cr_ses->clientid, true);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001806 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001807
1808 if (conf) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001809 cs_slot = &conf->cl_cs_slot;
1810 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001811 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04001812 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001813 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04001814 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001815 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001816 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001817 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001818 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001819 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001820 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04001821 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001822 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001823 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001824 }
Andy Adamson49557cc2009-07-23 19:02:16 -04001825 cs_slot = &unconf->cl_cs_slot;
1826 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03001827 if (status) {
1828 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001829 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001830 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001831 }
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001832 old = find_confirmed_client_by_name(&unconf->cl_name);
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04001833 if (old)
1834 expire_client(old);
1835 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001836 conf = unconf;
1837 } else {
1838 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001839 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001840 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001841 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04001842 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04001843 * We do not support RDMA or persistent sessions
1844 */
1845 cr_ses->flags &= ~SESSION4_PERSIST;
1846 cr_ses->flags &= ~SESSION4_RDMA;
1847
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001848 init_session(rqstp, new, conf, cr_ses);
1849 nfsd4_init_conn(rqstp, conn, new);
1850
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001851 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001852 NFS4_MAX_SESSIONID_LEN);
Mi Jinlong12050652010-11-11 18:03:40 +08001853 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1854 sizeof(struct nfsd4_channel_attrs));
J. Bruce Fields86c3e162010-10-02 17:04:00 -04001855 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04001856 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001857
Andy Adamson49557cc2009-07-23 19:02:16 -04001858 /* cache solo and embedded create sessions under the state lock */
1859 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001860out:
1861 nfs4_unlock_state();
1862 dprintk("%s returns %d\n", __func__, ntohl(status));
1863 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04001864out_free_conn:
1865 free_conn(conn);
1866out_free_session:
1867 __free_session(new);
1868 goto out;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001869}
1870
J. Bruce Fields57716352010-04-21 12:27:19 -04001871static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1872{
1873 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1874 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1875
1876 return argp->opcnt == resp->opcnt;
1877}
1878
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001879static __be32 nfsd4_map_bcts_dir(u32 *dir)
1880{
1881 switch (*dir) {
1882 case NFS4_CDFC4_FORE:
1883 case NFS4_CDFC4_BACK:
1884 return nfs_ok;
1885 case NFS4_CDFC4_FORE_OR_BOTH:
1886 case NFS4_CDFC4_BACK_OR_BOTH:
1887 *dir = NFS4_CDFC4_BOTH;
1888 return nfs_ok;
1889 };
1890 return nfserr_inval;
1891}
1892
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001893__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1894{
1895 struct nfsd4_session *session = cstate->session;
1896
1897 spin_lock(&client_lock);
1898 session->se_cb_prog = bc->bc_cb_program;
1899 session->se_cb_sec = bc->bc_cb_sec;
1900 spin_unlock(&client_lock);
1901
1902 nfsd4_probe_callback(session->se_client);
1903
1904 return nfs_ok;
1905}
1906
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001907__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1908 struct nfsd4_compound_state *cstate,
1909 struct nfsd4_bind_conn_to_session *bcts)
1910{
1911 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001912 struct nfsd4_conn *conn;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001913
1914 if (!nfsd4_last_compound_op(rqstp))
1915 return nfserr_not_only_op;
1916 spin_lock(&client_lock);
1917 cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1918 /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1919 * client_lock iself: */
1920 if (cstate->session) {
1921 nfsd4_get_session(cstate->session);
1922 atomic_inc(&cstate->session->se_client->cl_refcount);
1923 }
1924 spin_unlock(&client_lock);
1925 if (!cstate->session)
1926 return nfserr_badsession;
1927
1928 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001929 if (status)
1930 return status;
1931 conn = alloc_conn(rqstp, bcts->dir);
1932 if (!conn)
1933 return nfserr_jukebox;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001934 nfsd4_init_conn(rqstp, conn, cstate->session);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04001935 return nfs_ok;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001936}
1937
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001938static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1939{
1940 if (!session)
1941 return 0;
1942 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1943}
1944
Andy Adamson069b6ad2009-04-03 08:27:58 +03001945__be32
1946nfsd4_destroy_session(struct svc_rqst *r,
1947 struct nfsd4_compound_state *cstate,
1948 struct nfsd4_destroy_session *sessionid)
1949{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001950 struct nfsd4_session *ses;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001951 __be32 status = nfserr_badsession;
Benny Halevye10e0cf2009-04-03 08:28:38 +03001952
1953 /* Notes:
1954 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1955 * - Should we return nfserr_back_chan_busy if waiting for
1956 * callbacks on to-be-destroyed session?
1957 * - Do we need to clear any callback info from previous session?
1958 */
1959
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04001960 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04001961 if (!nfsd4_last_compound_op(r))
1962 return nfserr_not_only_op;
1963 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03001964 dump_sessionid(__func__, &sessionid->sessionid);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001965 spin_lock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001966 ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1967 if (!ses) {
Benny Halevy9089f1b2010-05-12 00:12:26 +03001968 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001969 goto out;
1970 }
1971
1972 unhash_session(ses);
Benny Halevy9089f1b2010-05-12 00:12:26 +03001973 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001974
Benny Halevyab707e152010-05-12 00:14:06 +03001975 nfs4_lock_state();
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05001976 nfsd4_probe_callback_sync(ses->se_client);
Benny Halevyab707e152010-05-12 00:14:06 +03001977 nfs4_unlock_state();
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001978
Benny Halevy508dc6e2012-02-23 17:40:52 -08001979 spin_lock(&client_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001980 nfsd4_del_conns(ses);
Benny Halevy508dc6e2012-02-23 17:40:52 -08001981 nfsd4_put_session_locked(ses);
1982 spin_unlock(&client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03001983 status = nfs_ok;
1984out:
1985 dprintk("%s returns %d\n", __func__, ntohl(status));
1986 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001987}
1988
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001989static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04001990{
1991 struct nfsd4_conn *c;
1992
1993 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04001994 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04001995 return c;
1996 }
1997 }
1998 return NULL;
1999}
2000
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002001static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002002{
2003 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002004 struct nfsd4_conn *c;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002005 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002006
2007 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002008 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002009 if (c) {
2010 spin_unlock(&clp->cl_lock);
2011 free_conn(new);
2012 return;
2013 }
2014 __nfsd4_hash_conn(new, ses);
2015 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002016 ret = nfsd4_register_conn(new);
2017 if (ret)
2018 /* oops; xprt is already down: */
2019 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields328ead22010-09-29 16:11:06 -04002020 return;
2021}
2022
Mi Jinlong868b89c2011-04-27 09:09:58 +08002023static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2024{
2025 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2026
2027 return args->opcnt > session->se_fchannel.maxops;
2028}
2029
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002030static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2031 struct nfsd4_session *session)
2032{
2033 struct xdr_buf *xb = &rqstp->rq_arg;
2034
2035 return xb->len > session->se_fchannel.maxreq_sz;
2036}
2037
Andy Adamson069b6ad2009-04-03 08:27:58 +03002038__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002039nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002040 struct nfsd4_compound_state *cstate,
2041 struct nfsd4_sequence *seq)
2042{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002043 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002044 struct nfsd4_session *session;
2045 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002046 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002047 __be32 status;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002048
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002049 if (resp->opcnt != 1)
2050 return nfserr_sequence_pos;
2051
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002052 /*
2053 * Will be either used or freed by nfsd4_sequence_check_conn
2054 * below.
2055 */
2056 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2057 if (!conn)
2058 return nfserr_jukebox;
2059
Benny Halevy9089f1b2010-05-12 00:12:26 +03002060 spin_lock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002061 status = nfserr_badsession;
2062 session = find_in_sessionid_hashtbl(&seq->sessionid);
2063 if (!session)
2064 goto out;
2065
Mi Jinlong868b89c2011-04-27 09:09:58 +08002066 status = nfserr_too_many_ops;
2067 if (nfsd4_session_too_many_ops(rqstp, session))
2068 goto out;
2069
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002070 status = nfserr_req_too_big;
2071 if (nfsd4_request_too_big(rqstp, session))
2072 goto out;
2073
Benny Halevyb85d4c02009-04-03 08:28:08 +03002074 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002075 if (seq->slotid >= session->se_fchannel.maxreqs)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002076 goto out;
2077
Andy Adamson557ce262009-08-28 08:45:04 -04002078 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002079 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2080
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002081 /* We do not negotiate the number of slots yet, so set the
2082 * maxslots to the session maxreqs which is used to encode
2083 * sr_highest_slotid and the sr_target_slot id to maxslots */
2084 seq->maxslots = session->se_fchannel.maxreqs;
2085
J. Bruce Fields73e79482012-02-13 16:39:00 -05002086 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2087 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002088 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002089 status = nfserr_seq_misordered;
2090 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2091 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002092 cstate->slot = slot;
2093 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002094 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002095 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002096 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002097 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002098 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002099 }
2100 if (status)
2101 goto out;
2102
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002103 nfsd4_sequence_check_conn(conn, session);
2104 conn = NULL;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002105
Benny Halevyb85d4c02009-04-03 08:28:08 +03002106 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002107 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002108 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002109 if (seq->cachethis)
2110 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002111 else
2112 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002113
2114 cstate->slot = slot;
2115 cstate->session = session;
2116
Benny Halevyb85d4c02009-04-03 08:28:08 +03002117out:
J. Bruce Fields26c0c752010-04-24 15:35:43 -04002118 /* Hold a session reference until done processing the compound. */
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002119 if (cstate->session) {
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05002120 struct nfs4_client *clp = session->se_client;
2121
Benny Halevy36acb662010-05-12 00:13:04 +03002122 nfsd4_get_session(cstate->session);
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05002123 atomic_inc(&clp->cl_refcount);
Benny Halevy54237322011-10-19 19:12:58 -07002124 switch (clp->cl_cb_state) {
2125 case NFSD4_CB_DOWN:
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07002126 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
Benny Halevy54237322011-10-19 19:12:58 -07002127 break;
2128 case NFSD4_CB_FAULT:
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07002129 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
Benny Halevy54237322011-10-19 19:12:58 -07002130 break;
Benny Halevyfc0c3dd2011-10-19 19:13:06 -07002131 default:
2132 seq->status_flags = 0;
Benny Halevy54237322011-10-19 19:12:58 -07002133 }
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002134 }
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002135 kfree(conn);
Benny Halevy36acb662010-05-12 00:13:04 +03002136 spin_unlock(&client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002137 dprintk("%s: return %d\n", __func__, ntohl(status));
2138 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002139}
2140
Mi Jinlong345c2842011-10-20 17:51:39 +08002141__be32
2142nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2143{
2144 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002145 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002146 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002147
2148 nfs4_lock_state();
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002149 unconf = find_unconfirmed_client(&dc->clientid, true);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002150 conf = find_confirmed_client(&dc->clientid, true, nn);
Mi Jinlong345c2842011-10-20 17:51:39 +08002151
2152 if (conf) {
2153 clp = conf;
2154
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002155 if (!is_client_expired(conf) && client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002156 status = nfserr_clientid_busy;
2157 goto out;
2158 }
2159
2160 /* rfc5661 18.50.3 */
2161 if (cstate->session && conf == cstate->session->se_client) {
2162 status = nfserr_clientid_busy;
2163 goto out;
2164 }
2165 } else if (unconf)
2166 clp = unconf;
2167 else {
2168 status = nfserr_stale_clientid;
2169 goto out;
2170 }
2171
2172 expire_client(clp);
2173out:
2174 nfs4_unlock_state();
2175 dprintk("%s return %d\n", __func__, ntohl(status));
2176 return status;
2177}
2178
Andy Adamson069b6ad2009-04-03 08:27:58 +03002179__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002180nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2181{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002182 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002183
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002184 if (rc->rca_one_fs) {
2185 if (!cstate->current_fh.fh_dentry)
2186 return nfserr_nofilehandle;
2187 /*
2188 * We don't take advantage of the rca_one_fs case.
2189 * That's OK, it's optional, we can safely ignore it.
2190 */
2191 return nfs_ok;
2192 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002193
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002194 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002195 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002196 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2197 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002198 goto out;
2199
2200 status = nfserr_stale_clientid;
2201 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002202 /*
2203 * The following error isn't really legal.
2204 * But we only get here if the client just explicitly
2205 * destroyed the client. Surely it no longer cares what
2206 * error it gets back on an operation for the dead
2207 * client.
2208 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002209 goto out;
2210
2211 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002212 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002213out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002214 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002215 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002216}
2217
2218__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002219nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2220 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002222 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002224 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002225 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002226 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2227
J. Bruce Fields63db4632012-05-18 21:54:19 -04002228 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 nfs4_lock_state();
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002230 conf = find_confirmed_client_by_name(&clname);
NeilBrown28ce6052005-06-23 22:03:56 -07002231 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002232 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002234 if (clp_used_exchangeid(conf))
2235 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002236 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002237 char addr_str[INET6_ADDRSTRLEN];
2238 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2239 sizeof(addr_str));
2240 dprintk("NFSD: setclientid: string in use by client "
2241 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 goto out;
2243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002245 unconf = find_unconfirmed_client_by_name(&clname);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002246 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002249 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002250 if (new == NULL)
2251 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002252 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002253 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002255 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002256 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002257 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002258 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002259 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2261 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2262 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2263 status = nfs_ok;
2264out:
2265 nfs4_unlock_state();
2266 return status;
2267}
2268
2269
Al Virob37ad282006-10-19 23:28:59 -07002270__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002271nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2272 struct nfsd4_compound_state *cstate,
2273 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
NeilBrown21ab45a2005-06-23 22:04:14 -07002275 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2277 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002278 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002279 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002281 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002284
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002285 conf = find_confirmed_client(clid, false, nn);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002286 unconf = find_unconfirmed_client(clid, false);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002287 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002288 * We try hard to give out unique clientid's, so if we get an
2289 * attempt to confirm the same clientid with a different cred,
2290 * there's a bug somewhere. Let's charitably assume it's our
2291 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002292 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002293 status = nfserr_serverfault;
2294 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2295 goto out;
2296 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2297 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002298 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002299 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2300 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002302 else /* case 4: client hasn't noticed we rebooted yet? */
2303 status = nfserr_stale_clientid;
2304 goto out;
2305 }
2306 status = nfs_ok;
2307 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002308 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2309 nfsd4_probe_callback(conf);
2310 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002311 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002312 conf = find_confirmed_client_by_name(&unconf->cl_name);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002313 if (conf)
J. Bruce Fields8695b902012-05-19 10:05:58 -04002314 expire_client(conf);
J. Bruce Fields8695b902012-05-19 10:05:58 -04002315 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002316 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 nfs4_unlock_state();
2320 return status;
2321}
2322
J. Bruce Fields32513b42011-10-13 16:00:16 -04002323static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002325 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2326}
2327
2328/* OPEN Share state helper functions */
2329static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2330{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 unsigned int hashval = file_hashval(ino);
2332
J. Bruce Fields32513b42011-10-13 16:00:16 -04002333 atomic_set(&fp->fi_ref, 1);
2334 INIT_LIST_HEAD(&fp->fi_hash);
2335 INIT_LIST_HEAD(&fp->fi_stateids);
2336 INIT_LIST_HEAD(&fp->fi_delegations);
2337 fp->fi_inode = igrab(ino);
2338 fp->fi_had_conflict = false;
2339 fp->fi_lease = NULL;
2340 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2341 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2342 spin_lock(&recall_lock);
2343 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2344 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
2347static void
Christoph Lametere18b8902006-12-06 20:33:20 -08002348nfsd4_free_slab(struct kmem_cache **slab)
NeilBrowne60d4392005-06-23 22:03:01 -07002349{
NeilBrowne60d4392005-06-23 22:03:01 -07002350 if (*slab == NULL)
2351 return;
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002352 kmem_cache_destroy(*slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002353 *slab = NULL;
NeilBrowne60d4392005-06-23 22:03:01 -07002354}
2355
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002356void
NeilBrowne60d4392005-06-23 22:03:01 -07002357nfsd4_free_slabs(void)
2358{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002359 nfsd4_free_slab(&openowner_slab);
2360 nfsd4_free_slab(&lockowner_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002361 nfsd4_free_slab(&file_slab);
NeilBrown5ac049a2005-06-23 22:03:03 -07002362 nfsd4_free_slab(&stateid_slab);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002363 nfsd4_free_slab(&deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Bryan Schumaker72083392011-11-01 15:24:59 -04002366int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367nfsd4_init_slabs(void)
2368{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002369 openowner_slab = kmem_cache_create("nfsd4_openowners",
2370 sizeof(struct nfs4_openowner), 0, 0, NULL);
2371 if (openowner_slab == NULL)
2372 goto out_nomem;
2373 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002374 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002375 if (lockowner_slab == NULL)
NeilBrowne60d4392005-06-23 22:03:01 -07002376 goto out_nomem;
2377 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002378 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002379 if (file_slab == NULL)
2380 goto out_nomem;
NeilBrown5ac049a2005-06-23 22:03:03 -07002381 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002382 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002383 if (stateid_slab == NULL)
2384 goto out_nomem;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002385 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002386 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002387 if (deleg_slab == NULL)
2388 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return 0;
NeilBrowne60d4392005-06-23 22:03:01 -07002390out_nomem:
2391 nfsd4_free_slabs();
2392 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2393 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002396void nfs4_free_openowner(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002398 kfree(oo->oo_owner.so_owner.data);
2399 kmem_cache_free(openowner_slab, oo);
2400}
2401
2402void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2403{
2404 kfree(lo->lo_owner.so_owner.data);
2405 kmem_cache_free(lockowner_slab, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002408static void init_nfs4_replay(struct nfs4_replay *rp)
2409{
2410 rp->rp_status = nfserr_serverfault;
2411 rp->rp_buflen = 0;
2412 rp->rp_buf = rp->rp_ibuf;
2413}
2414
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002415static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 struct nfs4_stateowner *sop;
2418
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002419 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002420 if (!sop)
2421 return NULL;
2422
2423 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2424 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002425 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002426 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002428 sop->so_owner.len = owner->len;
2429
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002430 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002431 sop->so_client = clp;
2432 init_nfs4_replay(&sop->so_replay);
2433 return sop;
2434}
2435
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002436static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002437{
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002438 list_add(&oo->oo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002439 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002442static struct nfs4_openowner *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002444 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002446 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2447 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002449 oo->oo_owner.so_is_open_owner = 1;
2450 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002451 oo->oo_flags = NFS4_OO_NEW;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002452 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002453 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002454 INIT_LIST_HEAD(&oo->oo_close_lru);
2455 hash_openowner(oo, clp, strhashval);
2456 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
J. Bruce Fields996e0932011-10-17 11:14:48 -04002459static 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 -04002460 struct nfs4_openowner *oo = open->op_openowner;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04002461 struct nfs4_client *clp = oo->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
J. Bruce Fields996e0932011-10-17 11:14:48 -04002463 init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
NeilBrownea1da632005-06-23 22:04:17 -07002464 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002465 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002466 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002467 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002468 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 stp->st_access_bmap = 0;
2471 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002472 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002473 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002474 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477static void
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002478move_to_close_lru(struct nfs4_openowner *oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002480 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002482 list_move_tail(&oo->oo_close_lru, &close_lru);
2483 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002487same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2488 clientid_t *clid)
2489{
2490 return (sop->so_owner.len == owner->len) &&
2491 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2492 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002495static struct nfs4_openowner *
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002496find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002498 struct nfs4_stateowner *so;
2499 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002500 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002502 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
2503 if (!so->so_is_open_owner)
2504 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002505 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2506 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002507 clp = oo->oo_owner.so_client;
2508 if ((bool)clp->cl_minorversion != sessions)
2509 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002510 renew_client(oo->oo_owner.so_client);
2511 return oo;
2512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514 return NULL;
2515}
2516
2517/* search file_hashtbl[] for file */
2518static struct nfs4_file *
2519find_file(struct inode *ino)
2520{
2521 unsigned int hashval = file_hashval(ino);
2522 struct nfs4_file *fp;
2523
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002524 spin_lock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002526 if (fp->fi_inode == ino) {
2527 get_nfs4_file(fp);
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002528 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
J. Bruce Fields8b671b82009-02-22 14:51:34 -08002532 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return NULL;
2534}
2535
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002536/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 * Called to check deny when READ with all zero stateid or
2538 * WRITE with all zero or all one stateid
2539 */
Al Virob37ad282006-10-19 23:28:59 -07002540static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2542{
2543 struct inode *ino = current_fh->fh_dentry->d_inode;
2544 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002545 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002546 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 dprintk("NFSD: nfs4_share_conflict\n");
2549
2550 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002551 if (!fp)
2552 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002553 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002555 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002556 if (test_deny(deny_type, stp) ||
2557 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
NeilBrown13cd2182005-06-23 22:03:10 -07002560 ret = nfs_ok;
2561out:
2562 put_nfs4_file(fp);
2563 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002566static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 /* We're assuming the state code never drops its reference
2569 * without first removing the lease. Since we're in this lease
2570 * callback (and since the lease code is serialized by the kernel
2571 * lock) we know the server hasn't removed the lease yet, we know
2572 * it's safe to take a reference: */
2573 atomic_inc(&dp->dl_count);
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Arnd Bergmann460781b2010-11-17 16:26:56 +01002577 /* only place dl_time is set. protected by lock_flocks*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 dp->dl_time = get_seconds();
2579
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002580 nfsd4_cb_recall(dp);
2581}
2582
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002583/* Called from break_lease() with lock_flocks() held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002584static void nfsd_break_deleg_cb(struct file_lock *fl)
2585{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002586 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2587 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002588
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002589 if (!fp) {
2590 WARN(1, "(%p)->fl_owner NULL\n", fl);
2591 return;
2592 }
2593 if (fp->fi_had_conflict) {
2594 WARN(1, "duplicate break on %p\n", fp);
2595 return;
2596 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002597 /*
2598 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002599 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002600 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002601 */
2602 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002604 spin_lock(&recall_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002605 fp->fi_had_conflict = true;
2606 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2607 nfsd_break_one_deleg(dp);
J. Bruce Fields5d926e82011-02-07 16:53:46 -05002608 spin_unlock(&recall_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611static
2612int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2613{
2614 if (arg & F_UNLCK)
2615 return lease_modify(onlist, arg);
2616 else
2617 return -EAGAIN;
2618}
2619
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002620static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002621 .lm_break = nfsd_break_deleg_cb,
2622 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623};
2624
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002625static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2626{
2627 if (nfsd4_has_session(cstate))
2628 return nfs_ok;
2629 if (seqid == so->so_seqid - 1)
2630 return nfserr_replay_me;
2631 if (seqid == so->so_seqid)
2632 return nfs_ok;
2633 return nfserr_bad_seqid;
2634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Al Virob37ad282006-10-19 23:28:59 -07002636__be32
Andy Adamson66689582009-04-03 08:28:45 +03002637nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2638 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 clientid_t *clientid = &open->op_clientid;
2641 struct nfs4_client *clp = NULL;
2642 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002643 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002644 __be32 status;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002645 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002647 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002649 /*
2650 * In case we need it later, after we've already created the
2651 * file and don't want to risk a further failure:
2652 */
2653 open->op_file = nfsd4_alloc_file();
2654 if (open->op_file == NULL)
2655 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002657 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002658 oo = find_openstateowner_str(strhashval, open, cstate->minorversion);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002659 open->op_openowner = oo;
2660 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002661 clp = find_confirmed_client(clientid, cstate->minorversion,
2662 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002664 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002665 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002667 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002668 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002669 clp = oo->oo_owner.so_client;
2670 release_openowner(oo);
2671 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002672 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002673 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002674 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2675 if (status)
2676 return status;
2677 clp = oo->oo_owner.so_client;
2678 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002679new_owner:
2680 oo = alloc_init_open_stateowner(strhashval, clp, open);
2681 if (oo == NULL)
2682 return nfserr_jukebox;
2683 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002684alloc_stateid:
2685 open->op_stp = nfs4_alloc_stateid(clp);
2686 if (!open->op_stp)
2687 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002688 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
Al Virob37ad282006-10-19 23:28:59 -07002691static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07002692nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2693{
2694 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2695 return nfserr_openmode;
2696 else
2697 return nfs_ok;
2698}
2699
Daniel Mackc47d8322011-05-16 16:38:14 +02002700static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04002701{
J. Bruce Fields24a01112010-05-18 20:01:35 -04002702 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2703}
2704
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002705static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002706{
2707 struct nfs4_stid *ret;
2708
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002709 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002710 if (!ret)
2711 return NULL;
2712 return delegstateid(ret);
2713}
2714
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002715static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2716{
2717 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2718 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2719}
2720
Al Virob37ad282006-10-19 23:28:59 -07002721static __be32
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002722nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07002723 struct nfs4_delegation **dp)
2724{
2725 int flags;
Al Virob37ad282006-10-19 23:28:59 -07002726 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07002727
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04002728 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07002729 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07002730 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04002731 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07002732 status = nfs4_check_delegmode(*dp, flags);
2733 if (status)
2734 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002735out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04002736 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07002737 return nfs_ok;
2738 if (status)
2739 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002740 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07002741 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07002742}
2743
Al Virob37ad282006-10-19 23:28:59 -07002744static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002745nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002747 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002748 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
NeilBrown8beefa22005-06-23 22:03:08 -07002750 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 /* ignore lock owners */
2752 if (local->st_stateowner->so_is_open_owner == 0)
2753 continue;
2754 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002755 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 *stpp = local;
2757 /* check for conflicting share reservations */
2758 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002759 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04002761 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
J. Bruce Fields21fb4012010-07-28 12:21:23 -04002764static inline int nfs4_access_to_access(u32 nfs4_access)
2765{
2766 int flags = 0;
2767
2768 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2769 flags |= NFSD_MAY_READ;
2770 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2771 flags |= NFSD_MAY_WRITE;
2772 return flags;
2773}
2774
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002775static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2776 struct svc_fh *cur_fh, struct nfsd4_open *open)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002777{
2778 __be32 status;
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002779 int oflag = nfs4_access_to_omode(open->op_share_access);
2780 int access = nfs4_access_to_access(open->op_share_access);
2781
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002782 if (!fp->fi_fds[oflag]) {
2783 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2784 &fp->fi_fds[oflag]);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002785 if (status)
2786 return status;
2787 }
2788 nfs4_file_get_access(fp, oflag);
2789
2790 return nfs_ok;
2791}
2792
Al Virob37ad282006-10-19 23:28:59 -07002793static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2795 struct nfsd4_open *open)
2796{
2797 struct iattr iattr = {
2798 .ia_valid = ATTR_SIZE,
2799 .ia_size = 0,
2800 };
2801 if (!open->op_truncate)
2802 return 0;
2803 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08002804 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2806}
2807
Al Virob37ad282006-10-19 23:28:59 -07002808static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002809nfs4_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 -07002810{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04002811 u32 op_share_access = open->op_share_access;
J. Bruce Fields7d947842010-08-20 18:09:31 -04002812 bool new_access;
Al Virob37ad282006-10-19 23:28:59 -07002813 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Jeff Layton82c5ff12012-05-11 09:45:13 -04002815 new_access = !test_access(op_share_access, stp);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002816 if (new_access) {
Casey Bodley0c12eaf2011-07-23 14:58:10 -04002817 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002818 if (status)
2819 return status;
J. Bruce Fields6c26d082006-01-18 17:43:38 -08002820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 status = nfsd4_truncate(rqstp, cur_fh, open);
2822 if (status) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002823 if (new_access) {
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04002824 int oflag = nfs4_access_to_omode(op_share_access);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04002825 nfs4_file_put_access(fp, oflag);
2826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return status;
2828 }
2829 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04002830 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002831 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 return nfs_ok;
2834}
2835
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05002838nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002840 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002843/* Should we give out recallable state?: */
2844static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2845{
2846 if (clp->cl_cb_state == NFSD4_CB_UP)
2847 return true;
2848 /*
2849 * In the sessions case, since we don't have to establish a
2850 * separate connection for callbacks, we assume it's OK
2851 * until we hear otherwise:
2852 */
2853 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2854}
2855
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002856static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2857{
2858 struct file_lock *fl;
2859
2860 fl = locks_alloc_lock();
2861 if (!fl)
2862 return NULL;
2863 locks_init_lock(fl);
2864 fl->fl_lmops = &nfsd_lease_mng_ops;
2865 fl->fl_flags = FL_LEASE;
2866 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2867 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002868 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002869 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05002870 return fl;
2871}
2872
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002873static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2874{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002875 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002876 struct file_lock *fl;
2877 int status;
2878
2879 fl = nfs4_alloc_init_lease(dp, flag);
2880 if (!fl)
2881 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002882 fl->fl_file = find_readable_file(fp);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002883 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002884 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002885 if (status) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002886 list_del_init(&dp->dl_perclnt);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002887 locks_free_lock(fl);
2888 return -ENOMEM;
2889 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002890 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04002891 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002892 atomic_set(&fp->fi_delegees, 1);
2893 list_add(&dp->dl_perfile, &fp->fi_delegations);
2894 return 0;
2895}
2896
2897static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2898{
2899 struct nfs4_file *fp = dp->dl_file;
2900
2901 if (!fp->fi_lease)
2902 return nfs4_setlease(dp, flag);
2903 spin_lock(&recall_lock);
2904 if (fp->fi_had_conflict) {
2905 spin_unlock(&recall_lock);
2906 return -EAGAIN;
2907 }
2908 atomic_inc(&fp->fi_delegees);
2909 list_add(&dp->dl_perfile, &fp->fi_delegations);
2910 spin_unlock(&recall_lock);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04002911 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002912 return 0;
2913}
2914
Benny Halevy4aa89132012-02-21 14:16:44 -08002915static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2916{
2917 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2918 if (status == -EAGAIN)
2919 open->op_why_no_deleg = WND4_CONTENTION;
2920 else {
2921 open->op_why_no_deleg = WND4_RESOURCE;
2922 switch (open->op_deleg_want) {
2923 case NFS4_SHARE_WANT_READ_DELEG:
2924 case NFS4_SHARE_WANT_WRITE_DELEG:
2925 case NFS4_SHARE_WANT_ANY_DELEG:
2926 break;
2927 case NFS4_SHARE_WANT_CANCEL:
2928 open->op_why_no_deleg = WND4_CANCELLED;
2929 break;
2930 case NFS4_SHARE_WANT_NO_DELEG:
2931 BUG(); /* not supposed to get here */
2932 }
2933 }
2934}
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936/*
2937 * Attempt to hand out a delegation.
2938 */
2939static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002940nfs4_open_delegation(struct net *net, struct svc_fh *fh,
2941 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002944 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05002945 int cb_up;
Benny Halevyd24433c2012-02-16 20:57:17 +02002946 int status = 0, flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002948 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 flag = NFS4_OPEN_DELEGATE_NONE;
NeilBrown7b190fe2005-06-23 22:03:23 -07002950 open->op_recall = 0;
2951 switch (open->op_claim_type) {
2952 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002953 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07002954 open->op_recall = 1;
2955 flag = open->op_delegate_type;
2956 if (flag == NFS4_OPEN_DELEGATE_NONE)
2957 goto out;
2958 break;
2959 case NFS4_OPEN_CLAIM_NULL:
2960 /* Let's not give out any delegations till everyone's
2961 * had the chance to reclaim theirs.... */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04002962 if (locks_in_grace(net))
NeilBrown7b190fe2005-06-23 22:03:23 -07002963 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002964 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown7b190fe2005-06-23 22:03:23 -07002965 goto out;
2966 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2967 flag = NFS4_OPEN_DELEGATE_WRITE;
2968 else
2969 flag = NFS4_OPEN_DELEGATE_READ;
2970 break;
2971 default:
2972 goto out;
2973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002975 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002976 if (dp == NULL)
2977 goto out_no_deleg;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002978 status = nfs4_set_delegation(dp, flag);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05002979 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002980 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002982 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Benny Halevy8c10cbd2009-10-19 12:04:53 +02002984 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04002985 STATEID_VAL(&dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986out:
2987 open->op_delegate_type = flag;
Benny Halevyd24433c2012-02-16 20:57:17 +02002988 if (flag == NFS4_OPEN_DELEGATE_NONE) {
2989 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
2990 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2991 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2992
Benny Halevy4aa89132012-02-21 14:16:44 -08002993 /* 4.1 client asking for a delegation? */
2994 if (open->op_deleg_want)
2995 nfsd4_open_deleg_none_ext(open, status);
Benny Halevyd24433c2012-02-16 20:57:17 +02002996 }
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05002997 return;
2998out_free:
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002999 nfs4_put_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003000out_no_deleg:
3001 flag = NFS4_OPEN_DELEGATE_NONE;
3002 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
Benny Halevye27f49c2012-02-21 14:16:54 -08003005static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3006 struct nfs4_delegation *dp)
3007{
3008 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3009 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3010 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3011 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3012 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3013 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3014 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3015 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3016 }
3017 /* Otherwise the client must be confused wanting a delegation
3018 * it already has, therefore we don't return
3019 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3020 */
3021}
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023/*
3024 * called with nfs4_lock_state() held.
3025 */
Al Virob37ad282006-10-19 23:28:59 -07003026__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3028{
Andy Adamson66689582009-04-03 08:28:45 +03003029 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003030 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 struct nfs4_file *fp = NULL;
3032 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003033 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003034 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003035 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 /*
3038 * Lookup file; if found, lookup stateid and check open request,
3039 * and check for delegations in the process of being recalled.
3040 * If not found, create the nfs4_file struct
3041 */
3042 fp = find_file(ino);
3043 if (fp) {
3044 if ((status = nfs4_check_open(fp, open, &stp)))
3045 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003046 status = nfs4_check_deleg(cl, fp, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003047 if (status)
3048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 } else {
NeilBrownc44c5ee2005-06-23 22:02:54 -07003050 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003051 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003052 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003053 status = nfserr_jukebox;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003054 fp = open->op_file;
3055 open->op_file = NULL;
3056 nfsd4_init_file(fp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
3058
3059 /*
3060 * OPEN the file, or upgrade an existing OPEN.
3061 * If truncate fails, the OPEN fails.
3062 */
3063 if (stp) {
3064 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003065 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (status)
3067 goto out;
3068 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003069 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003070 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 goto out;
J. Bruce Fields4af82502012-06-07 17:30:45 -04003072 status = nfsd4_truncate(rqstp, current_fh, open);
3073 if (status)
3074 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003075 stp = open->op_stp;
3076 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003077 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003079 update_stateid(&stp->st_stid.sc_stateid);
3080 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Benny Halevyd24433c2012-02-16 20:57:17 +02003082 if (nfsd4_has_session(&resp->cstate)) {
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003083 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Andy Adamson66689582009-04-03 08:28:45 +03003084
Benny Halevyd24433c2012-02-16 20:57:17 +02003085 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3086 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3087 open->op_why_no_deleg = WND4_NOT_WANTED;
3088 goto nodeleg;
3089 }
3090 }
3091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 /*
3093 * Attempt to hand out a delegation. No error return, because the
3094 * OPEN succeeds even if we fail.
3095 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003096 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003097nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 status = nfs_ok;
3099
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003100 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003101 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003103 /* 4.1 client trying to upgrade/downgrade delegation? */
3104 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003105 open->op_deleg_want)
3106 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003107
NeilBrown13cd2182005-06-23 22:03:10 -07003108 if (fp)
3109 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003110 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003111 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 /*
3113 * To finish the open response, we just need to set the rflags.
3114 */
3115 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003116 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003117 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3119
3120 return status;
3121}
3122
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003123void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3124{
3125 if (open->op_openowner) {
3126 struct nfs4_openowner *oo = open->op_openowner;
3127
3128 if (!list_empty(&oo->oo_owner.so_stateids))
3129 list_del_init(&oo->oo_close_lru);
3130 if (oo->oo_flags & NFS4_OO_NEW) {
3131 if (status) {
3132 release_openowner(oo);
3133 open->op_openowner = NULL;
3134 } else
3135 oo->oo_flags &= ~NFS4_OO_NEW;
3136 }
3137 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003138 if (open->op_file)
3139 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003140 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003141 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003142}
3143
Al Virob37ad282006-10-19 23:28:59 -07003144__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003145nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3146 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
3148 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003149 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003150 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 nfs4_lock_state();
3153 dprintk("process_renew(%08x/%08x): starting\n",
3154 clid->cl_boot, clid->cl_id);
3155 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003156 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 goto out;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003158 clp = find_confirmed_client(clid, cstate->minorversion, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 status = nfserr_expired;
3160 if (clp == NULL) {
3161 /* We assume the client took too long to RENEW. */
3162 dprintk("nfsd4_renew: clientid not found!\n");
3163 goto out;
3164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003166 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003167 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 goto out;
3169 status = nfs_ok;
3170out:
3171 nfs4_unlock_state();
3172 return status;
3173}
3174
NeilBrowna76b4312005-06-23 22:04:01 -07003175static void
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003176nfsd4_end_grace(struct net *net)
NeilBrowna76b4312005-06-23 22:04:01 -07003177{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003178 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3179
Jeff Layton33dcc482012-04-10 11:08:48 -04003180 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003181 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003182 return;
3183
NeilBrowna76b4312005-06-23 22:04:01 -07003184 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003185 nn->grace_ended = true;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003186 nfsd4_record_grace_done(net, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003187 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003188 /*
3189 * Now that every NFSv4 client has had the chance to recover and
3190 * to see the (possibly new, possibly shorter) lease time, we
3191 * can safely set the next grace time to the current lease time:
3192 */
3193 nfsd4_grace = nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003194}
3195
NeilBrownfd39ca92005-06-23 22:04:03 -07003196static time_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197nfs4_laundromat(void)
3198{
3199 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003200 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 struct nfs4_delegation *dp;
3202 struct list_head *pos, *next, reaplist;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003203 time_t cutoff = get_seconds() - nfsd4_lease;
3204 time_t t, clientid_val = nfsd4_lease;
3205 time_t u, test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 nfs4_lock_state();
3208
3209 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003210 nfsd4_end_grace(&init_net);
Benny Halevy36acb662010-05-12 00:13:04 +03003211 INIT_LIST_HEAD(&reaplist);
3212 spin_lock(&client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 list_for_each_safe(pos, next, &client_lru) {
3214 clp = list_entry(pos, struct nfs4_client, cl_lru);
3215 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3216 t = clp->cl_time - cutoff;
3217 if (clientid_val > t)
3218 clientid_val = t;
3219 break;
3220 }
Benny Halevyd7682982010-05-12 00:13:54 +03003221 if (atomic_read(&clp->cl_refcount)) {
3222 dprintk("NFSD: client in use (clientid %08x)\n",
3223 clp->cl_clientid.cl_id);
3224 continue;
3225 }
3226 unhash_client_locked(clp);
3227 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003228 }
3229 spin_unlock(&client_lock);
3230 list_for_each_safe(pos, next, &reaplist) {
3231 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 dprintk("NFSD: purging unused client (clientid %08x)\n",
3233 clp->cl_clientid.cl_id);
3234 expire_client(clp);
3235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 spin_lock(&recall_lock);
3237 list_for_each_safe(pos, next, &del_recall_lru) {
3238 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3239 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3240 u = dp->dl_time - cutoff;
3241 if (test_val > u)
3242 test_val = u;
3243 break;
3244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 list_move(&dp->dl_recall_lru, &reaplist);
3246 }
3247 spin_unlock(&recall_lock);
3248 list_for_each_safe(pos, next, &reaplist) {
3249 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 unhash_delegation(dp);
3251 }
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05003252 test_val = nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 list_for_each_safe(pos, next, &close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003254 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3255 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3256 u = oo->oo_time - cutoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (test_val > u)
3258 test_val = u;
3259 break;
3260 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003261 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
3263 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3264 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3265 nfs4_unlock_state();
3266 return clientid_val;
3267}
3268
Harvey Harrisona254b242008-02-20 12:49:00 -08003269static struct workqueue_struct *laundry_wq;
3270static void laundromat_main(struct work_struct *);
3271static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3272
3273static void
David Howellsc4028952006-11-22 14:57:56 +00003274laundromat_main(struct work_struct *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
3276 time_t t;
3277
3278 t = nfs4_laundromat();
3279 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
NeilBrown58da2822005-06-23 22:03:19 -07003280 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003283static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003284{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003285 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3286 return nfserr_bad_stateid;
3287 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288}
3289
3290static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003291STALE_STATEID(stateid_t *stateid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003293 if (stateid->si_opaque.so_clid.cl_boot == nn->boot_time)
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003294 return 0;
3295 dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003296 STATEID_VAL(stateid));
J. Bruce Fieldse4e83ea2010-04-22 16:21:39 -04003297 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003301access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003303 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3304 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3305 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
3308static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003309access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003311 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3312 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
3315static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003316__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
Al Virob37ad282006-10-19 23:28:59 -07003318 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
J. Bruce Fields02921912010-07-29 15:16:59 -04003320 /* For lock stateid's, we test the parent open, not the lock: */
3321 if (stp->st_openstp)
3322 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003323 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003325 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 goto out;
3327 status = nfs_ok;
3328out:
3329 return status;
3330}
3331
Al Virob37ad282006-10-19 23:28:59 -07003332static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003333check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003335 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003337 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003338 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 * conflicting state; so we must wait out the grace period. */
3340 return nfserr_grace;
3341 } else if (flags & WR_STATE)
3342 return nfs4_share_conflict(current_fh,
3343 NFS4_SHARE_DENY_WRITE);
3344 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3345 return nfs4_share_conflict(current_fh,
3346 NFS4_SHARE_DENY_READ);
3347}
3348
3349/*
3350 * Allow READ/WRITE during grace period on recovered state only for files
3351 * that are not able to provide mandatory locking.
3352 */
3353static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003354grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003356 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
J. Bruce Fields81b82962011-08-23 11:03:29 -04003359/* Returns true iff a is later than b: */
3360static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3361{
3362 return (s32)a->si_generation - (s32)b->si_generation > 0;
3363}
3364
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003365static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003366{
Andy Adamson66689582009-04-03 08:28:45 +03003367 /*
3368 * When sessions are used the stateid generation number is ignored
3369 * when it is zero.
3370 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003371 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003372 return nfs_ok;
3373
3374 if (in->si_generation == ref->si_generation)
3375 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003376
J. Bruce Fields0836f582008-01-26 19:08:12 -05003377 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003378 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003379 return nfserr_bad_stateid;
3380 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003381 * However, we could see a stateid from the past, even from a
3382 * non-buggy client. For example, if the client sends a lock
3383 * while some IO is outstanding, the lock may bump si_generation
3384 * while the IO is still in flight. The client could avoid that
3385 * situation by waiting for responses on all the IO requests,
3386 * but better performance may result in retrying IO that
3387 * receives an old_stateid error if requests are rarely
3388 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003389 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003390 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003391}
3392
Chuck Lever7df302f2012-05-29 13:56:37 -04003393static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003394{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003395 struct nfs4_stid *s;
3396 struct nfs4_ol_stateid *ols;
3397 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003398
Chuck Lever7df302f2012-05-29 13:56:37 -04003399 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3400 return nfserr_bad_stateid;
3401 /* Client debugging aid. */
3402 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3403 char addr_str[INET6_ADDRSTRLEN];
3404 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3405 sizeof(addr_str));
3406 pr_warn_ratelimited("NFSD: client %s testing state ID "
3407 "with incorrect client ID\n", addr_str);
3408 return nfserr_bad_stateid;
3409 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003410 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003411 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003412 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003413 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003414 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003415 return status;
3416 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3417 return nfs_ok;
3418 ols = openlockstateid(s);
3419 if (ols->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003420 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003421 return nfserr_bad_stateid;
3422 return nfs_ok;
Bryan Schumaker17456802011-07-13 10:50:48 -04003423}
3424
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003425static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, struct nfs4_stid **s, bool sessions)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003426{
3427 struct nfs4_client *cl;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003428 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003429
3430 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3431 return nfserr_bad_stateid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003432 if (STALE_STATEID(stateid, nn))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003433 return nfserr_stale_stateid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003434 cl = find_confirmed_client(&stateid->si_opaque.so_clid, sessions, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003435 if (!cl)
3436 return nfserr_expired;
3437 *s = find_stateid_by_type(cl, stateid, typemask);
3438 if (!*s)
3439 return nfserr_bad_stateid;
3440 return nfs_ok;
3441
3442}
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444/*
3445* Checks for stateid operations
3446*/
Al Virob37ad282006-10-19 23:28:59 -07003447__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003448nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003449 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003451 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003452 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003454 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 struct inode *ino = current_fh->fh_dentry->d_inode;
Al Virob37ad282006-10-19 23:28:59 -07003456 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 if (filpp)
3459 *filpp = NULL;
3460
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003461 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return nfserr_grace;
3463
3464 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003465 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003467 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, &s, cstate->minorversion);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003468 if (status)
3469 return status;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003470 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3471 if (status)
3472 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003473 switch (s->sc_type) {
3474 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003475 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003476 status = nfs4_check_delegmode(dp, flags);
3477 if (status)
3478 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003479 if (filpp) {
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003480 *filpp = dp->dl_file->fi_deleg_file;
Dan Carpenter43b01782010-10-27 23:19:04 +02003481 BUG_ON(!*filpp);
3482 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003483 break;
3484 case NFS4_OPEN_STID:
3485 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003486 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003487 status = nfs4_check_fh(current_fh, stp);
3488 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003490 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003491 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003493 status = nfs4_check_openmode(stp, flags);
3494 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003496 if (filpp) {
3497 if (flags & RD_STATE)
3498 *filpp = find_readable_file(stp->st_file);
3499 else
3500 *filpp = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003501 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003502 break;
3503 default:
3504 return nfserr_bad_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
3506 status = nfs_ok;
3507out:
3508 return status;
3509}
3510
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003511static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003512nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003513{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003514 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003515 return nfserr_locks_held;
3516 release_lock_stateid(stp);
3517 return nfs_ok;
3518}
3519
3520/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003521 * Test if the stateid is valid
3522 */
3523__be32
3524nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3525 struct nfsd4_test_stateid *test_stateid)
3526{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003527 struct nfsd4_test_stateid_id *stateid;
3528 struct nfs4_client *cl = cstate->session->se_client;
3529
3530 nfs4_lock_state();
3531 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003532 stateid->ts_id_status =
3533 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003534 nfs4_unlock_state();
3535
Bryan Schumaker17456802011-07-13 10:50:48 -04003536 return nfs_ok;
3537}
3538
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003539__be32
3540nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3541 struct nfsd4_free_stateid *free_stateid)
3542{
3543 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003544 struct nfs4_stid *s;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003545 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003546 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003547
3548 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003549 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003550 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003551 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003552 switch (s->sc_type) {
3553 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003554 ret = nfserr_locks_held;
3555 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003556 case NFS4_OPEN_STID:
3557 case NFS4_LOCK_STID:
3558 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3559 if (ret)
3560 goto out;
3561 if (s->sc_type == NFS4_LOCK_STID)
3562 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3563 else
3564 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003565 break;
3566 default:
3567 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003568 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003569out:
3570 nfs4_unlock_state();
3571 return ret;
3572}
3573
NeilBrown4c4cd222005-07-07 17:59:27 -07003574static inline int
3575setlkflg (int type)
3576{
3577 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3578 RD_STATE : WR_STATE;
3579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003581static __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 -04003582{
3583 struct svc_fh *current_fh = &cstate->current_fh;
3584 struct nfs4_stateowner *sop = stp->st_stateowner;
3585 __be32 status;
3586
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003587 status = nfsd4_check_seqid(cstate, sop, seqid);
3588 if (status)
3589 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003590 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3591 /*
3592 * "Closed" stateid's exist *only* to return
3593 * nfserr_replay_me from the previous step.
3594 */
3595 return nfserr_bad_stateid;
3596 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3597 if (status)
3598 return status;
3599 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003600}
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602/*
3603 * Checks for sequence id mutating operations.
3604 */
Al Virob37ad282006-10-19 23:28:59 -07003605static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003606nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003607 stateid_t *stateid, char typemask,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003608 struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003610 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003611 struct nfs4_stid *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003613 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3614 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 *stpp = NULL;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003617 status = nfsd4_lookup_stateid(stateid, typemask, &s, cstate->minorversion);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003618 if (status)
3619 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003620 *stpp = openlockstateid(s);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003621 cstate->replay_owner = (*stpp)->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003623 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3624}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003625
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003626static __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 -04003627{
3628 __be32 status;
3629 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003631 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003632 NFS4_OPEN_STID, stpp);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003633 if (status)
3634 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003635 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003636 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07003637 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07003638 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Al Virob37ad282006-10-19 23:28:59 -07003641__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003642nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003643 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
Al Virob37ad282006-10-19 23:28:59 -07003645 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003646 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003647 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
3649 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003650 (int)cstate->current_fh.fh_dentry->d_name.len,
3651 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003653 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07003654 if (status)
3655 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 nfs4_lock_state();
3658
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003659 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003660 oc->oc_seqid, &oc->oc_req_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003661 NFS4_OPEN_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003662 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003663 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003664 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003665 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003666 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003667 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003668 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003669 update_stateid(&stp->st_stid.sc_stateid);
3670 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003671 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003672 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07003673
Jeff Layton2a4317c2012-03-21 16:42:43 -04003674 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04003675 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003677 if (!cstate->replay_owner)
3678 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return status;
3680}
3681
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003682static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003684 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003685 return;
3686 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04003687 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003688}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04003689
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003690static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3691{
3692 switch (to_access) {
3693 case NFS4_SHARE_ACCESS_READ:
3694 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3695 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3696 break;
3697 case NFS4_SHARE_ACCESS_WRITE:
3698 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3699 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3700 break;
3701 case NFS4_SHARE_ACCESS_BOTH:
3702 break;
3703 default:
3704 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
3706}
3707
3708static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04003709reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
3711 int i;
3712 for (i = 0; i < 4; i++) {
3713 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04003714 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 }
3716}
3717
Al Virob37ad282006-10-19 23:28:59 -07003718__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003719nfsd4_open_downgrade(struct svc_rqst *rqstp,
3720 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003721 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
Al Virob37ad282006-10-19 23:28:59 -07003723 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003724 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
3726 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003727 (int)cstate->current_fh.fh_dentry->d_name.len,
3728 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04003730 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02003731 if (od->od_deleg_want)
3732 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3733 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
3735 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003736 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3737 &od->od_stateid, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003738 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003741 if (!test_access(od->od_share_access, stp)) {
3742 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 stp->st_access_bmap, od->od_share_access);
3744 goto out;
3745 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04003746 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3748 stp->st_deny_bmap, od->od_share_deny);
3749 goto out;
3750 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04003751 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Jeff Laytonce0fc432012-05-11 09:45:14 -04003753 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003755 update_stateid(&stp->st_stid.sc_stateid);
3756 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 status = nfs_ok;
3758out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003759 if (!cstate->replay_owner)
3760 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return status;
3762}
3763
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003764void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3765{
3766 struct nfs4_openowner *oo;
3767 struct nfs4_ol_stateid *s;
3768
3769 if (!so->so_is_open_owner)
3770 return;
3771 oo = openowner(so);
3772 s = oo->oo_last_closed_stid;
3773 if (!s)
3774 return;
3775 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3776 /* Release the last_closed_stid on the next seqid bump: */
3777 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3778 return;
3779 }
3780 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003781 release_last_closed_stateid(oo);
3782}
3783
3784static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3785{
3786 unhash_open_stateid(s);
3787 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003788}
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790/*
3791 * nfs4_unlock_state() called after encode
3792 */
Al Virob37ad282006-10-19 23:28:59 -07003793__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003794nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08003795 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
Al Virob37ad282006-10-19 23:28:59 -07003797 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003798 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003799 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
3801 dprintk("NFSD: nfsd4_close on file %.*s\n",
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003802 (int)cstate->current_fh.fh_dentry->d_name.len,
3803 cstate->current_fh.fh_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003806 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3807 &close->cl_stateid,
3808 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3809 &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04003810 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003812 oo = openowner(stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 status = nfs_ok;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003814 update_stateid(&stp->st_stid.sc_stateid);
3815 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003817 nfsd4_close_open_stateid(stp);
J. Bruce Fieldscf9182e2012-08-29 15:21:58 -07003818 release_last_closed_stateid(oo);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003819 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08003820
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04003821 if (list_empty(&oo->oo_owner.so_stateids)) {
3822 if (cstate->minorversion) {
3823 release_openowner(oo);
3824 cstate->replay_owner = NULL;
3825 } else {
3826 /*
3827 * In the 4.0 case we need to keep the owners around a
3828 * little while to handle CLOSE replay.
3829 */
3830 if (list_empty(&oo->oo_owner.so_stateids))
3831 move_to_close_lru(oo);
3832 }
3833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04003835 if (!cstate->replay_owner)
3836 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return status;
3838}
3839
Al Virob37ad282006-10-19 23:28:59 -07003840__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003841nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3842 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003844 struct nfs4_delegation *dp;
3845 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003846 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07003847 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
J.Bruce Fieldsca364312006-12-13 00:35:27 -08003849 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003850 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
3852 nfs4_lock_state();
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04003853 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s, cstate->minorversion);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003854 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003855 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003856 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003857 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003858 if (status)
3859 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003860
3861 unhash_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003863 nfs4_unlock_state();
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return status;
3866}
3867
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
J. Bruce Fields009673b2011-11-07 17:40:10 -05003871#define LOCKOWNER_INO_HASH_BITS 8
3872#define LOCKOWNER_INO_HASH_SIZE (1 << LOCKOWNER_INO_HASH_BITS)
3873#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Benny Halevy87df4de2008-12-15 19:42:03 +02003875static inline u64
3876end_offset(u64 start, u64 len)
3877{
3878 u64 end;
3879
3880 end = start + len;
3881 return end >= start ? end: NFS4_MAX_UINT64;
3882}
3883
3884/* last octet in a range */
3885static inline u64
3886last_byte_offset(u64 start, u64 len)
3887{
3888 u64 end;
3889
3890 BUG_ON(!len);
3891 end = start + len;
3892 return end > start ? end - 1: NFS4_MAX_UINT64;
3893}
3894
J. Bruce Fields009673b2011-11-07 17:40:10 -05003895static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
3897 return (file_hashval(inode) + cl_id
3898 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05003899 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900}
3901
J. Bruce Fields009673b2011-11-07 17:40:10 -05003902static struct list_head lockowner_ino_hashtbl[LOCKOWNER_INO_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904/*
3905 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3906 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3907 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3908 * locking, this prevents us from being completely protocol-compliant. The
3909 * real solution to this problem is to start using unsigned file offsets in
3910 * the VFS, but this is a very deep change!
3911 */
3912static inline void
3913nfs4_transform_lock_offset(struct file_lock *lock)
3914{
3915 if (lock->fl_start < 0)
3916 lock->fl_start = OFFSET_MAX;
3917 if (lock->fl_end < 0)
3918 lock->fl_end = OFFSET_MAX;
3919}
3920
NeilBrownd5b90262006-04-10 22:55:22 -07003921/* Hack!: For now, we're defining this just so we can use a pointer to it
3922 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003923static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07003924};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
3926static inline void
3927nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3928{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003929 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
NeilBrownd5b90262006-04-10 22:55:22 -07003931 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003932 lo = (struct nfs4_lockowner *) fl->fl_owner;
3933 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3934 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003935 if (!deny->ld_owner.data)
3936 /* We just don't care that much */
3937 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003938 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3939 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07003940 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003941nevermind:
3942 deny->ld_owner.len = 0;
3943 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07003944 deny->ld_clientid.cl_boot = 0;
3945 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 }
3947 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02003948 deny->ld_length = NFS4_MAX_UINT64;
3949 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3951 deny->ld_type = NFS4_READ_LT;
3952 if (fl->fl_type != F_RDLCK)
3953 deny->ld_type = NFS4_WRITE_LT;
3954}
3955
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003956static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3957{
3958 struct nfs4_ol_stateid *lst;
3959
3960 if (!same_owner_str(&lo->lo_owner, owner, clid))
3961 return false;
3962 lst = list_first_entry(&lo->lo_owner.so_stateids,
3963 struct nfs4_ol_stateid, st_perstateowner);
3964 return lst->st_file->fi_inode == inode;
3965}
3966
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003967static struct nfs4_lockowner *
3968find_lockowner_str(struct inode *inode, clientid_t *clid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 struct xdr_netobj *owner)
3970{
J. Bruce Fields009673b2011-11-07 17:40:10 -05003971 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003972 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
J. Bruce Fields009673b2011-11-07 17:40:10 -05003974 list_for_each_entry(lo, &lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05003975 if (same_lockowner_ino(lo, inode, clid, owner))
3976 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
3978 return NULL;
3979}
3980
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003981static 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 -04003982{
J. Bruce Fields009673b2011-11-07 17:40:10 -05003983 struct inode *inode = open_stp->st_file->fi_inode;
3984 unsigned int inohash = lockowner_ino_hashval(inode,
3985 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
3986
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003987 list_add(&lo->lo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
J. Bruce Fields009673b2011-11-07 17:40:10 -05003988 list_add(&lo->lo_owner_ino_hash, &lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003989 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003990}
3991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992/*
3993 * Alloc a lock owner structure.
3994 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003995 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05003997 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 */
3999
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004000static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004001alloc_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 -04004002 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004004 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4005 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004007 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4008 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004009 /* It is the openowner seqid that will be incremented in encode in the
4010 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004011 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4012 hash_lockowner(lo, strhashval, clp, open_stp);
4013 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
4015
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004016static struct nfs4_ol_stateid *
4017alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004019 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004020 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
J. Bruce Fields996e0932011-10-17 11:14:48 -04004022 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004023 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004024 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04004025 init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
NeilBrown8beefa22005-06-23 22:03:08 -07004026 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004027 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4028 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004029 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004031 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004033 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 return stp;
4035}
4036
NeilBrownfd39ca92005-06-23 22:04:03 -07004037static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038check_lock_length(u64 offset, u64 length)
4039{
Benny Halevy87df4de2008-12-15 19:42:03 +02004040 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 LOFF_OVERFLOW(offset, length)));
4042}
4043
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004044static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004045{
4046 struct nfs4_file *fp = lock_stp->st_file;
4047 int oflag = nfs4_access_to_omode(access);
4048
Jeff Layton82c5ff12012-05-11 09:45:13 -04004049 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004050 return;
4051 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004052 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004053}
4054
J. Bruce Fields2355c592012-04-25 16:56:22 -04004055static __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 -04004056{
4057 struct nfs4_file *fi = ost->st_file;
4058 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4059 struct nfs4_client *cl = oo->oo_owner.so_client;
4060 struct nfs4_lockowner *lo;
4061 unsigned int strhashval;
4062
4063 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid, &lock->v.new.owner);
4064 if (lo) {
4065 if (!cstate->minorversion)
4066 return nfserr_bad_seqid;
4067 /* XXX: a lockowner always has exactly one stateid: */
4068 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4069 struct nfs4_ol_stateid, st_perstateowner);
4070 return nfs_ok;
4071 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004072 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004073 &lock->v.new.owner);
4074 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4075 if (lo == NULL)
4076 return nfserr_jukebox;
4077 *lst = alloc_init_lock_stateid(lo, fi, ost);
4078 if (*lst == NULL) {
4079 release_lockowner(lo);
4080 return nfserr_jukebox;
4081 }
4082 *new = true;
4083 return nfs_ok;
4084}
4085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086/*
4087 * LOCK operation
4088 */
Al Virob37ad282006-10-19 23:28:59 -07004089__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004090nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004091 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004093 struct nfs4_openowner *open_sop = NULL;
4094 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004095 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004096 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004097 struct file_lock *file_lock = NULL;
4098 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004099 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004100 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004101 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004102 int err;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004103 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
4105 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4106 (long long) lock->lk_offset,
4107 (long long) lock->lk_length);
4108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 if (check_lock_length(lock->lk_offset, lock->lk_length))
4110 return nfserr_inval;
4111
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004112 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004113 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004114 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4115 return status;
4116 }
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 nfs4_lock_state();
4119
4120 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004121 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004122
4123 if (nfsd4_has_session(cstate))
4124 /* See rfc 5661 18.10.3: given clientid is ignored: */
4125 memcpy(&lock->v.new.clientid,
4126 &cstate->session->se_client->cl_clientid,
4127 sizeof(clientid_t));
4128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004130 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004134 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 lock->lk_new_open_seqid,
4136 &lock->lk_new_open_stateid,
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004137 &open_stp);
NeilBrown37515172005-07-07 17:59:16 -07004138 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004140 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004141 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004142 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004143 &lock->v.new.clientid))
4144 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004145 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4146 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004147 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004148 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004149 lock->lk_old_lock_seqid,
4150 &lock->lk_old_lock_stateid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004151 NFS4_LOCK_STID, &lock_stp);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004152 if (status)
4153 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004154 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004156 lkflg = setlkflg(lock->lk_type);
4157 status = nfs4_check_openmode(lock_stp, lkflg);
4158 if (status)
4159 goto out;
4160
NeilBrown0dd395d2005-07-07 17:59:15 -07004161 status = nfserr_grace;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004162 if (locks_in_grace(SVC_NET(rqstp)) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004163 goto out;
4164 status = nfserr_no_grace;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004165 if (!locks_in_grace(SVC_NET(rqstp)) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004166 goto out;
4167
Jeff Layton21179d82012-08-21 08:03:32 -04004168 file_lock = locks_alloc_lock();
4169 if (!file_lock) {
4170 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4171 status = nfserr_jukebox;
4172 goto out;
4173 }
4174
4175 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 switch (lock->lk_type) {
4177 case NFS4_READ_LT:
4178 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004179 filp = find_readable_file(lock_stp->st_file);
4180 if (filp)
4181 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004182 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 case NFS4_WRITE_LT:
4185 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004186 filp = find_writeable_file(lock_stp->st_file);
4187 if (filp)
4188 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004189 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 default:
4192 status = nfserr_inval;
4193 goto out;
4194 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004195 if (!filp) {
4196 status = nfserr_openmode;
4197 goto out;
4198 }
Jeff Layton21179d82012-08-21 08:03:32 -04004199 file_lock->fl_owner = (fl_owner_t)lock_sop;
4200 file_lock->fl_pid = current->tgid;
4201 file_lock->fl_file = filp;
4202 file_lock->fl_flags = FL_POSIX;
4203 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4204 file_lock->fl_start = lock->lk_offset;
4205 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4206 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Jeff Layton21179d82012-08-21 08:03:32 -04004208 conflock = locks_alloc_lock();
4209 if (!conflock) {
4210 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4211 status = nfserr_jukebox;
4212 goto out;
4213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Jeff Layton21179d82012-08-21 08:03:32 -04004215 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004216 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004218 update_stateid(&lock_stp->st_stid.sc_stateid);
4219 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004221 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004222 break;
4223 case (EAGAIN): /* conflock holds conflicting lock */
4224 status = nfserr_denied;
4225 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004226 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 case (EDEADLK):
4229 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004230 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004231 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004232 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004233 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004237 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004238 release_lockowner(lock_sop);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004239 if (!cstate->replay_owner)
4240 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004241 if (file_lock)
4242 locks_free_lock(file_lock);
4243 if (conflock)
4244 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 return status;
4246}
4247
4248/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004249 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4250 * so we do a temporary open here just to get an open file to pass to
4251 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4252 * inode operation.)
4253 */
Al Viro04da6e92012-04-13 00:00:04 -04004254static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004255{
4256 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004257 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4258 if (!err) {
4259 err = nfserrno(vfs_test_lock(file, lock));
4260 nfsd_close(file);
4261 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004262 return err;
4263}
4264
4265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 * LOCKT operation
4267 */
Al Virob37ad282006-10-19 23:28:59 -07004268__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004269nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4270 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
4272 struct inode *inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004273 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004274 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004275 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004276 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004278 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return nfserr_grace;
4280
4281 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4282 return nfserr_inval;
4283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 nfs4_lock_state();
4285
4286 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004287 if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004290 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004293 inode = cstate->current_fh.fh_dentry->d_inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004294 file_lock = locks_alloc_lock();
4295 if (!file_lock) {
4296 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4297 status = nfserr_jukebox;
4298 goto out;
4299 }
4300 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 switch (lockt->lt_type) {
4302 case NFS4_READ_LT:
4303 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004304 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 break;
4306 case NFS4_WRITE_LT:
4307 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004308 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 break;
4310 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004311 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 status = nfserr_inval;
4313 goto out;
4314 }
4315
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004316 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4317 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004318 file_lock->fl_owner = (fl_owner_t)lo;
4319 file_lock->fl_pid = current->tgid;
4320 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Jeff Layton21179d82012-08-21 08:03:32 -04004322 file_lock->fl_start = lockt->lt_offset;
4323 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Jeff Layton21179d82012-08-21 08:03:32 -04004325 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Jeff Layton21179d82012-08-21 08:03:32 -04004327 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004328 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004329 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004330
Jeff Layton21179d82012-08-21 08:03:32 -04004331 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004333 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 }
4335out:
4336 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004337 if (file_lock)
4338 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 return status;
4340}
4341
Al Virob37ad282006-10-19 23:28:59 -07004342__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004343nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004344 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004346 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004348 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004349 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004350 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4353 (long long) locku->lu_offset,
4354 (long long) locku->lu_length);
4355
4356 if (check_lock_length(locku->lu_offset, locku->lu_length))
4357 return nfserr_inval;
4358
4359 nfs4_lock_state();
4360
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004361 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04004362 &locku->lu_stateid, NFS4_LOCK_STID, &stp);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004363 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004365 filp = find_any_file(stp->st_file);
4366 if (!filp) {
4367 status = nfserr_lock_range;
4368 goto out;
4369 }
Jeff Layton21179d82012-08-21 08:03:32 -04004370 file_lock = locks_alloc_lock();
4371 if (!file_lock) {
4372 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4373 status = nfserr_jukebox;
4374 goto out;
4375 }
4376 locks_init_lock(file_lock);
4377 file_lock->fl_type = F_UNLCK;
4378 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4379 file_lock->fl_pid = current->tgid;
4380 file_lock->fl_file = filp;
4381 file_lock->fl_flags = FL_POSIX;
4382 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4383 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Jeff Layton21179d82012-08-21 08:03:32 -04004385 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4386 locku->lu_length);
4387 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 /*
4390 * Try to unlock the file in the VFS.
4391 */
Jeff Layton21179d82012-08-21 08:03:32 -04004392 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004393 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004394 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 goto out_nfserr;
4396 }
4397 /*
4398 * OK, unlock succeeded; the only thing left to do is update the stateid.
4399 */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004400 update_stateid(&stp->st_stid.sc_stateid);
4401 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
4403out:
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004404 if (!cstate->replay_owner)
4405 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004406 if (file_lock)
4407 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 return status;
4409
4410out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004411 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 goto out;
4413}
4414
4415/*
4416 * returns
4417 * 1: locks held by lockowner
4418 * 0: no locks held by lockowner
4419 */
4420static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004421check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422{
4423 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004424 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 int status = 0;
4426
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004427 lock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004429 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 status = 1;
4431 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
4434out:
Arnd Bergmannb89f4322010-09-18 15:09:31 +02004435 unlock_flocks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return status;
4437}
4438
Al Virob37ad282006-10-19 23:28:59 -07004439__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004440nfsd4_release_lockowner(struct svc_rqst *rqstp,
4441 struct nfsd4_compound_state *cstate,
4442 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
4444 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004445 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004446 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004447 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004449 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004450 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004451 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004452 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
4454 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4455 clid->cl_boot, clid->cl_id);
4456
4457 /* XXX check for lease expiration */
4458
4459 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004460 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 nfs4_lock_state();
4464
NeilBrown3e9e3db2005-06-23 22:04:20 -07004465 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004466 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004467
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004468 list_for_each_entry(sop, &ownerstr_hashtbl[hashval], so_strhash) {
4469 if (sop->so_is_open_owner)
4470 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004471 if (!same_owner_str(sop, owner, clid))
4472 continue;
4473 list_for_each_entry(stp, &sop->so_stateids,
4474 st_perstateowner) {
4475 lo = lockowner(sop);
4476 if (check_for_locks(stp->st_file, lo))
4477 goto out;
4478 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004480 }
4481 /* Clients probably won't expect us to return with some (but not all)
4482 * of the lockowner state released; so don't release any until all
4483 * have been checked. */
4484 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004485 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004486 lo = list_entry(matches.next, struct nfs4_lockowner,
4487 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004488 /* unhash_stateowner deletes so_perclient only
4489 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004490 list_del(&lo->lo_list);
4491 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 }
4493out:
4494 nfs4_unlock_state();
4495 return status;
4496}
4497
4498static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004499alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
NeilBrowna55370a2005-06-23 22:03:52 -07004501 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
4503
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004504bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004505nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004506{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004507 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004508
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004509 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004510 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004511}
4512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513/*
4514 * failure => all reset bets are off, nfserr_no_grace...
4515 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004516struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004517nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
4519 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004520 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
NeilBrowna55370a2005-06-23 22:03:52 -07004522 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4523 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004524 if (crp) {
4525 strhashval = clientstr_hashval(name);
4526 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004527 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004528 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004529 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004530 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004531 }
4532 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533}
4534
Jeff Layton2a4317c2012-03-21 16:42:43 -04004535void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004536nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004537{
4538 list_del(&crp->cr_strhash);
4539 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004540 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004541}
4542
4543void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004544nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
4546 struct nfs4_client_reclaim *crp = NULL;
4547 int i;
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004550 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4551 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004553 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 }
4555 }
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004556 BUG_ON(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557}
4558
4559/*
4560 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004561struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004562nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
4564 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 struct nfs4_client_reclaim *crp = NULL;
4566
Jeff Layton278c9312012-11-12 15:00:52 -05004567 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Jeff Layton278c9312012-11-12 15:00:52 -05004569 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004570 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004571 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return crp;
4573 }
4574 }
4575 return NULL;
4576}
4577
4578/*
4579* Called from OPEN. Look for clientid in reclaim list.
4580*/
Al Virob37ad282006-10-19 23:28:59 -07004581__be32
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04004582nfs4_check_open_reclaim(clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Jeff Laytona52d7262012-03-21 09:52:02 -04004584 struct nfs4_client *clp;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004585 struct nfsd_net *nn = net_generic(&init_net, nfsd_net_id);
Jeff Laytona52d7262012-03-21 09:52:02 -04004586
4587 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004588 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004589 if (clp == NULL)
4590 return nfserr_reclaim_bad;
4591
4592 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593}
4594
Bryan Schumaker65178db2011-11-01 13:35:21 -04004595#ifdef CONFIG_NFSD_FAULT_INJECTION
4596
4597void nfsd_forget_clients(u64 num)
4598{
4599 struct nfs4_client *clp, *next;
4600 int count = 0;
4601
4602 nfs4_lock_state();
4603 list_for_each_entry_safe(clp, next, &client_lru, cl_lru) {
Bryan Schumaker65178db2011-11-01 13:35:21 -04004604 expire_client(clp);
4605 if (++count == num)
4606 break;
4607 }
4608 nfs4_unlock_state();
4609
4610 printk(KERN_INFO "NFSD: Forgot %d clients", count);
4611}
4612
4613static void release_lockowner_sop(struct nfs4_stateowner *sop)
4614{
4615 release_lockowner(lockowner(sop));
4616}
4617
4618static void release_openowner_sop(struct nfs4_stateowner *sop)
4619{
4620 release_openowner(openowner(sop));
4621}
4622
J. Bruce Fields353de312011-11-15 19:25:03 -05004623static int nfsd_release_n_owners(u64 num, bool is_open_owner,
Bryan Schumaker65178db2011-11-01 13:35:21 -04004624 void (*release_sop)(struct nfs4_stateowner *))
4625{
4626 int i, count = 0;
4627 struct nfs4_stateowner *sop, *next;
4628
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004629 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4630 list_for_each_entry_safe(sop, next, &ownerstr_hashtbl[i], so_strhash) {
4631 if (sop->so_is_open_owner != is_open_owner)
4632 continue;
Bryan Schumaker65178db2011-11-01 13:35:21 -04004633 release_sop(sop);
4634 if (++count == num)
4635 return count;
4636 }
4637 }
4638 return count;
4639}
4640
4641void nfsd_forget_locks(u64 num)
4642{
4643 int count;
4644
4645 nfs4_lock_state();
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004646 count = nfsd_release_n_owners(num, false, release_lockowner_sop);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004647 nfs4_unlock_state();
4648
4649 printk(KERN_INFO "NFSD: Forgot %d locks", count);
4650}
4651
4652void nfsd_forget_openowners(u64 num)
4653{
4654 int count;
4655
4656 nfs4_lock_state();
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004657 count = nfsd_release_n_owners(num, true, release_openowner_sop);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004658 nfs4_unlock_state();
4659
4660 printk(KERN_INFO "NFSD: Forgot %d open owners", count);
4661}
4662
J. Bruce Fieldsda5c80a2012-08-14 16:09:31 -04004663static int nfsd_process_n_delegations(u64 num, struct list_head *list)
Bryan Schumaker65178db2011-11-01 13:35:21 -04004664{
4665 int i, count = 0;
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004666 struct nfs4_file *fp, *fnext;
4667 struct nfs4_delegation *dp, *dnext;
Bryan Schumaker65178db2011-11-01 13:35:21 -04004668
4669 for (i = 0; i < FILE_HASH_SIZE; i++) {
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004670 list_for_each_entry_safe(fp, fnext, &file_hashtbl[i], fi_hash) {
4671 list_for_each_entry_safe(dp, dnext, &fp->fi_delegations, dl_perfile) {
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004672 list_move(&dp->dl_recall_lru, list);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004673 if (++count == num)
4674 return count;
4675 }
4676 }
4677 }
Bryan Schumaker2d3475c2011-12-14 14:39:56 -05004678
Bryan Schumaker65178db2011-11-01 13:35:21 -04004679 return count;
4680}
4681
4682void nfsd_forget_delegations(u64 num)
4683{
4684 unsigned int count;
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004685 LIST_HEAD(victims);
4686 struct nfs4_delegation *dp, *dnext;
4687
4688 spin_lock(&recall_lock);
4689 count = nfsd_process_n_delegations(num, &victims);
4690 spin_unlock(&recall_lock);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004691
4692 nfs4_lock_state();
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004693 list_for_each_entry_safe(dp, dnext, &victims, dl_recall_lru)
4694 unhash_delegation(dp);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004695 nfs4_unlock_state();
4696
4697 printk(KERN_INFO "NFSD: Forgot %d delegations", count);
4698}
4699
4700void nfsd_recall_delegations(u64 num)
4701{
4702 unsigned int count;
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004703 LIST_HEAD(victims);
4704 struct nfs4_delegation *dp, *dnext;
Bryan Schumaker65178db2011-11-01 13:35:21 -04004705
Bryan Schumaker65178db2011-11-01 13:35:21 -04004706 spin_lock(&recall_lock);
Stanislav Kinsburskya6d88f22012-05-25 18:38:50 +04004707 count = nfsd_process_n_delegations(num, &victims);
4708 list_for_each_entry_safe(dp, dnext, &victims, dl_recall_lru) {
4709 list_del(&dp->dl_recall_lru);
4710 nfsd_break_one_deleg(dp);
4711 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04004712 spin_unlock(&recall_lock);
Bryan Schumaker65178db2011-11-01 13:35:21 -04004713
4714 printk(KERN_INFO "NFSD: Recalled %d delegations", count);
4715}
4716
4717#endif /* CONFIG_NFSD_FAULT_INJECTION */
4718
NeilBrownac4d8ff22005-06-23 22:03:30 -07004719/* initialization to perform at module load time: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Bryan Schumaker72083392011-11-01 15:24:59 -04004721void
NeilBrownac4d8ff22005-06-23 22:03:30 -07004722nfs4_state_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
Bryan Schumaker72083392011-11-01 15:24:59 -04004724 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4728 }
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004729 conf_name_tree = RB_ROOT;
4730 unconf_name_tree = RB_ROOT;
Marc Eshel5282fd72009-04-03 08:27:52 +03004731 for (i = 0; i < SESSION_HASH_SIZE; i++)
4732 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 for (i = 0; i < FILE_HASH_SIZE; i++) {
4734 INIT_LIST_HEAD(&file_hashtbl[i]);
4735 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004736 for (i = 0; i < OWNER_HASH_SIZE; i++) {
4737 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 }
J. Bruce Fields009673b2011-11-07 17:40:10 -05004739 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4740 INIT_LIST_HEAD(&lockowner_ino_hashtbl[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 INIT_LIST_HEAD(&close_lru);
4742 INIT_LIST_HEAD(&client_lru);
4743 INIT_LIST_HEAD(&del_recall_lru);
NeilBrownac4d8ff22005-06-23 22:03:30 -07004744}
4745
Meelap Shahc2f1a552007-07-17 04:04:39 -07004746/*
4747 * Since the lifetime of a delegation isn't limited to that of an open, a
4748 * client may quite reasonably hang on to a delegation as long as it has
4749 * the inode cached. This becomes an obvious problem the first time a
4750 * client's inode cache approaches the size of the server's total memory.
4751 *
4752 * For now we avoid this problem by imposing a hard limit on the number
4753 * of delegations, which varies according to the server's memory size.
4754 */
4755static void
4756set_max_delegations(void)
4757{
4758 /*
4759 * Allow at most 4 delegations per megabyte of RAM. Quick
4760 * estimates suggest that in the worst case (where every delegation
4761 * is for a different inode), a delegation could take about 1.5K,
4762 * giving a worst case usage of about 6% of memory.
4763 */
4764 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4765}
4766
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004767static int nfs4_state_start_net(struct net *net)
4768{
4769 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4770 int i;
4771
4772 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4773 CLIENT_HASH_SIZE, GFP_KERNEL);
4774 if (!nn->conf_id_hashtbl)
4775 return -ENOMEM;
4776
4777 for (i = 0; i < CLIENT_HASH_SIZE; i++)
4778 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
4779
4780 return 0;
4781}
4782
4783static void
4784__nfs4_state_shutdown_net(struct net *net)
4785{
4786 int i;
4787 struct nfs4_client *clp = NULL;
4788 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4789
4790 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4791 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4792 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4793 destroy_client(clp);
4794 }
4795 }
4796 kfree(nn->conf_id_hashtbl);
4797}
4798
NeilBrownac4d8ff22005-06-23 22:03:30 -07004799/* initialization to perform when the nfsd service is started: */
4800
Jeff Laytona6d6b782012-03-05 11:42:36 -05004801int
4802nfs4_state_start(void)
NeilBrownac4d8ff22005-06-23 22:03:30 -07004803{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004804 struct net *net = &init_net;
4805 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004806 int ret;
4807
Jeff Layton2a4317c2012-03-21 16:42:43 -04004808 /*
4809 * FIXME: For now, we hang most of the pernet global stuff off of
4810 * init_net until nfsd is fully containerized. Eventually, we'll
4811 * need to pass a net pointer into this function, take a reference
4812 * to that instead and then do most of the rest of this on a per-net
4813 * basis.
4814 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004815 get_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004816 ret = nfs4_state_start_net(net);
4817 if (ret)
4818 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004819 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004820 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004821 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004822 nn->grace_ended = false;
Marc Eshel9a8db972007-07-17 04:04:35 -07004823 printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004824 nfsd4_grace);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004825 ret = set_callback_cred();
Jeff Laytona6d6b782012-03-05 11:42:36 -05004826 if (ret) {
4827 ret = -ENOMEM;
4828 goto out_recovery;
4829 }
NeilBrown58da2822005-06-23 22:03:19 -07004830 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05004831 if (laundry_wq == NULL) {
4832 ret = -ENOMEM;
4833 goto out_recovery;
4834 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004835 ret = nfsd4_create_callback_queue();
4836 if (ret)
4837 goto out_free_laundry;
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05004838 queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
Meelap Shahc2f1a552007-07-17 04:04:39 -07004839 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004840 return 0;
4841out_free_laundry:
4842 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05004843out_recovery:
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004844 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004845 __nfs4_state_shutdown_net(net);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004846 put_net(net);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05004847 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004850/* should be called with the state lock held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851static void
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004852__nfs4_state_shutdown(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 struct nfs4_client *clp = NULL;
4855 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 struct list_head *pos, *next, reaplist;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004857 struct rb_node *node, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004859 __nfs4_state_shutdown_net(net);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004860
4861 node = rb_first(&unconf_name_tree);
4862 while (node != NULL) {
4863 tmp = node;
4864 node = rb_next(tmp);
4865 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4866 rb_erase(tmp, &unconf_name_tree);
4867 destroy_client(clp);
4868 }
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 INIT_LIST_HEAD(&reaplist);
4871 spin_lock(&recall_lock);
4872 list_for_each_safe(pos, next, &del_recall_lru) {
4873 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4874 list_move(&dp->dl_recall_lru, &reaplist);
4875 }
4876 spin_unlock(&recall_lock);
4877 list_for_each_safe(pos, next, &reaplist) {
4878 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 unhash_delegation(dp);
4880 }
4881
Jeff Layton2a4317c2012-03-21 16:42:43 -04004882 nfsd4_client_tracking_exit(&init_net);
4883 put_net(&init_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884}
4885
4886void
4887nfs4_state_shutdown(void)
4888{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004889 struct net *net = &init_net;
4890 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4891
Tejun Heoafe2c512010-12-14 16:21:17 +01004892 cancel_delayed_work_sync(&laundromat_work);
NeilBrown5e8d5c22006-04-10 22:55:37 -07004893 destroy_workqueue(laundry_wq);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004894 locks_end_grace(&nn->nfsd4_manager);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 nfs4_lock_state();
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004896 __nfs4_state_shutdown(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 nfs4_unlock_state();
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04004898 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004900
4901static void
4902get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4903{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01004904 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
4905 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004906}
4907
4908static void
4909put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
4910{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01004911 if (cstate->minorversion) {
4912 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
4913 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
4914 }
4915}
4916
4917void
4918clear_current_stateid(struct nfsd4_compound_state *cstate)
4919{
4920 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004921}
4922
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004923/*
4924 * functions to set current state id
4925 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004926void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01004927nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
4928{
4929 put_stateid(cstate, &odp->od_stateid);
4930}
4931
4932void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004933nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
4934{
4935 put_stateid(cstate, &open->op_stateid);
4936}
4937
4938void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004939nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
4940{
4941 put_stateid(cstate, &close->cl_stateid);
4942}
4943
4944void
4945nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
4946{
4947 put_stateid(cstate, &lock->lk_resp_stateid);
4948}
4949
4950/*
4951 * functions to consume current state id
4952 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01004953
4954void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01004955nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
4956{
4957 get_stateid(cstate, &odp->od_stateid);
4958}
4959
4960void
4961nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
4962{
4963 get_stateid(cstate, &drp->dr_stateid);
4964}
4965
4966void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01004967nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
4968{
4969 get_stateid(cstate, &fsp->fr_stateid);
4970}
4971
4972void
4973nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
4974{
4975 get_stateid(cstate, &setattr->sa_stateid);
4976}
4977
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004978void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004979nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
4980{
4981 get_stateid(cstate, &close->cl_stateid);
4982}
4983
4984void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004985nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004986{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01004987 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01004988}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01004989
4990void
4991nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
4992{
4993 get_stateid(cstate, &read->rd_stateid);
4994}
4995
4996void
4997nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
4998{
4999 get_stateid(cstate, &write->wr_stateid);
5000}