blob: 106db71e0eef94828d4ab0a2ef910ba016a9ede7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
NeilBrown6282cd52014-06-04 17:39:26 +100044#include <linux/hash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define NFSDDBG_FACILITY NFSDDBG_PROC
53
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050054#define all_ones {{~0,~0},~0}
55static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
58};
59static const stateid_t zero_stateid = {
60 /* all fields zero */
61};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010062static const stateid_t currentstateid = {
63 .si_generation = 1,
64};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050065
Andy Adamsonec6b5d72009-04-03 08:28:28 +030066static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070067
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050068#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010070#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* forward declarations */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -040073static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
J. Bruce Fields8b671b82009-02-22 14:51:34 -080075/* Locking: */
76
77/* Currently used for almost all code touching nfsv4 state: */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080078static DEFINE_MUTEX(client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
J. Bruce Fields8b671b82009-02-22 14:51:34 -080080/*
81 * Currently used for the del_recall_lru and file hash table. In an
82 * effort to decrease the scope of the client_mutex, this spinlock may
83 * eventually cover more:
84 */
Benny Halevycdc97502014-05-30 09:09:30 -040085static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080086
Christoph Hellwigabf11352014-05-21 07:43:03 -070087static struct kmem_cache *openowner_slab;
88static struct kmem_cache *lockowner_slab;
89static struct kmem_cache *file_slab;
90static struct kmem_cache *stateid_slab;
91static struct kmem_cache *deleg_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093void
94nfs4_lock_state(void)
95{
Ingo Molnar353ab6e2006-03-26 01:37:12 -080096 mutex_lock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400101static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800102{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103 return ses->se_flags & NFS4_SESSION_DEAD;
104}
105
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400106void nfsd4_put_session(struct nfsd4_session *ses)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400107{
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400108 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
109 free_session(ses);
110}
111
112static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
113{
114 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400115 return nfserr_jukebox;
116 ses->se_flags |= NFS4_SESSION_DEAD;
117 return nfs_ok;
118}
119
120static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
121{
122 if (is_session_dead(ses))
123 return nfserr_badsession;
124 atomic_inc(&ses->se_ref);
125 return nfs_ok;
Benny Halevy508dc6e2012-02-23 17:40:52 -0800126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128void
129nfs4_unlock_state(void)
130{
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800131 mutex_unlock(&client_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
J. Bruce Fields221a6872013-04-01 22:23:49 -0400134static bool is_client_expired(struct nfs4_client *clp)
135{
136 return clp->cl_time == 0;
137}
138
139static __be32 mark_client_expired_locked(struct nfs4_client *clp)
140{
141 if (atomic_read(&clp->cl_refcount))
142 return nfserr_jukebox;
143 clp->cl_time = 0;
144 return nfs_ok;
145}
146
147static __be32 mark_client_expired(struct nfs4_client *clp)
148{
149 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
150 __be32 ret;
151
152 spin_lock(&nn->client_lock);
153 ret = mark_client_expired_locked(clp);
154 spin_unlock(&nn->client_lock);
155 return ret;
156}
157
158static __be32 get_client_locked(struct nfs4_client *clp)
159{
160 if (is_client_expired(clp))
161 return nfserr_expired;
162 atomic_inc(&clp->cl_refcount);
163 return nfs_ok;
164}
165
166/* must be called under the client_lock */
167static inline void
168renew_client_locked(struct nfs4_client *clp)
169{
170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
171
172 if (is_client_expired(clp)) {
173 WARN_ON(1);
174 printk("%s: client (clientid %08x/%08x) already expired\n",
175 __func__,
176 clp->cl_clientid.cl_boot,
177 clp->cl_clientid.cl_id);
178 return;
179 }
180
181 dprintk("renewing client (clientid %08x/%08x)\n",
182 clp->cl_clientid.cl_boot,
183 clp->cl_clientid.cl_id);
184 list_move_tail(&clp->cl_lru, &nn->client_lru);
185 clp->cl_time = get_seconds();
186}
187
188static inline void
189renew_client(struct nfs4_client *clp)
190{
191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192
193 spin_lock(&nn->client_lock);
194 renew_client_locked(clp);
195 spin_unlock(&nn->client_lock);
196}
197
Fengguang Wuba138432013-04-16 22:14:15 -0400198static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400199{
200 if (!atomic_dec_and_test(&clp->cl_refcount))
201 return;
202 if (!is_client_expired(clp))
203 renew_client_locked(clp);
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static inline u32
207opaque_hashval(const void *ptr, int nbytes)
208{
209 unsigned char *cptr = (unsigned char *) ptr;
210
211 u32 x = 0;
212 while (nbytes--) {
213 x *= 37;
214 x += *cptr++;
215 }
216 return x;
217}
218
J. Bruce Fields32513b42011-10-13 16:00:16 -0400219static void nfsd4_free_file(struct nfs4_file *f)
220{
221 kmem_cache_free(file_slab, f);
222}
223
NeilBrown13cd2182005-06-23 22:03:10 -0700224static inline void
225put_nfs4_file(struct nfs4_file *fi)
226{
Benny Halevycdc97502014-05-30 09:09:30 -0400227 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton89876f82013-04-02 09:01:59 -0400228 hlist_del(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400229 spin_unlock(&state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800230 iput(fi->fi_inode);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400231 nfsd4_free_file(fi);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800232 }
NeilBrown13cd2182005-06-23 22:03:10 -0700233}
234
235static inline void
236get_nfs4_file(struct nfs4_file *fi)
237{
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800238 atomic_inc(&fi->fi_ref);
NeilBrown13cd2182005-06-23 22:03:10 -0700239}
240
NeilBrownef0f3392006-04-10 22:55:41 -0700241static int num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800242unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700243
244/*
245 * Open owner state (share locks)
246 */
247
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500248/* hash tables for lock and open owners */
249#define OWNER_HASH_BITS 8
250#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
251#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700252
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500253static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400254{
255 unsigned int ret;
256
257 ret = opaque_hashval(ownername->data, ownername->len);
258 ret += clientid;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500259 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400260}
NeilBrownef0f3392006-04-10 22:55:41 -0700261
NeilBrownef0f3392006-04-10 22:55:41 -0700262/* hash table for nfs4_file */
263#define FILE_HASH_BITS 8
264#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800265
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400266static unsigned int file_hashval(struct inode *ino)
267{
268 /* XXX: why are we hashing on inode pointer, anyway? */
269 return hash_ptr(ino, FILE_HASH_BITS);
270}
271
Jeff Layton89876f82013-04-02 09:01:59 -0400272static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700273
J. Bruce Fields34775652013-08-23 17:55:18 -0400274static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
275{
276 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
277 atomic_inc(&fp->fi_access[oflag]);
278}
279
J. Bruce Fields998db522010-08-07 09:21:41 -0400280static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
281{
282 if (oflag == O_RDWR) {
J. Bruce Fields34775652013-08-23 17:55:18 -0400283 __nfs4_file_get_access(fp, O_RDONLY);
284 __nfs4_file_get_access(fp, O_WRONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400285 } else
J. Bruce Fields34775652013-08-23 17:55:18 -0400286 __nfs4_file_get_access(fp, oflag);
J. Bruce Fields998db522010-08-07 09:21:41 -0400287}
288
289static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400290{
291 if (fp->fi_fds[oflag]) {
292 fput(fp->fi_fds[oflag]);
293 fp->fi_fds[oflag] = NULL;
294 }
295}
296
J. Bruce Fields998db522010-08-07 09:21:41 -0400297static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400298{
299 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400300 nfs4_file_put_fd(fp, oflag);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400301 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
J. Bruce Fields3d02fa22011-09-19 15:07:41 -0400302 nfs4_file_put_fd(fp, O_RDWR);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400303 }
304}
305
J. Bruce Fields998db522010-08-07 09:21:41 -0400306static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
307{
308 if (oflag == O_RDWR) {
309 __nfs4_file_put_access(fp, O_RDONLY);
310 __nfs4_file_put_access(fp, O_WRONLY);
311 } else
312 __nfs4_file_put_access(fp, oflag);
313}
314
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500315static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
316kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400317{
318 struct idr *stateids = &cl->cl_stateids;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500319 struct nfs4_stid *stid;
320 int new_id;
321
322 stid = kmem_cache_alloc(slab, GFP_KERNEL);
323 if (!stid)
324 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400325
Jeff Layton398c33a2013-04-29 16:21:20 -0700326 new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
Tejun Heoebd6c702013-03-13 14:59:37 -0700327 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500328 goto out_free;
329 stid->sc_client = cl;
330 stid->sc_type = 0;
331 stid->sc_stateid.si_opaque.so_id = new_id;
332 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
333 /* Will be incremented before return to client: */
334 stid->sc_stateid.si_generation = 0;
335
J. Bruce Fields996e0932011-10-17 11:14:48 -0400336 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500337 * It shouldn't be a problem to reuse an opaque stateid value.
338 * I don't think it is for 4.1. But with 4.0 I worry that, for
339 * example, a stray write retransmission could be accepted by
340 * the server when it should have been rejected. Therefore,
341 * adopt a trick from the sctp code to attempt to maximize the
342 * amount of time until an id is reused, by ensuring they always
343 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400344 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500345 return stid;
346out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800347 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500348 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400349}
350
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400351static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
352{
353 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
354}
355
NeilBrown6282cd52014-06-04 17:39:26 +1000356/*
357 * When we recall a delegation, we should be careful not to hand it
358 * out again straight away.
359 * To ensure this we keep a pair of bloom filters ('new' and 'old')
360 * in which the filehandles of recalled delegations are "stored".
361 * If a filehandle appear in either filter, a delegation is blocked.
362 * When a delegation is recalled, the filehandle is stored in the "new"
363 * filter.
364 * Every 30 seconds we swap the filters and clear the "new" one,
365 * unless both are empty of course.
366 *
367 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
368 * low 3 bytes as hash-table indices.
369 *
370 * 'state_lock', which is always held when block_delegations() is called,
371 * is used to manage concurrent access. Testing does not need the lock
372 * except when swapping the two filters.
373 */
374static struct bloom_pair {
375 int entries, old_entries;
376 time_t swap_time;
377 int new; /* index into 'set' */
378 DECLARE_BITMAP(set[2], 256);
379} blocked_delegations;
380
381static int delegation_blocked(struct knfsd_fh *fh)
382{
383 u32 hash;
384 struct bloom_pair *bd = &blocked_delegations;
385
386 if (bd->entries == 0)
387 return 0;
388 if (seconds_since_boot() - bd->swap_time > 30) {
389 spin_lock(&state_lock);
390 if (seconds_since_boot() - bd->swap_time > 30) {
391 bd->entries -= bd->old_entries;
392 bd->old_entries = bd->entries;
393 memset(bd->set[bd->new], 0,
394 sizeof(bd->set[0]));
395 bd->new = 1-bd->new;
396 bd->swap_time = seconds_since_boot();
397 }
398 spin_unlock(&state_lock);
399 }
400 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
401 if (test_bit(hash&255, bd->set[0]) &&
402 test_bit((hash>>8)&255, bd->set[0]) &&
403 test_bit((hash>>16)&255, bd->set[0]))
404 return 1;
405
406 if (test_bit(hash&255, bd->set[1]) &&
407 test_bit((hash>>8)&255, bd->set[1]) &&
408 test_bit((hash>>16)&255, bd->set[1]))
409 return 1;
410
411 return 0;
412}
413
414static void block_delegations(struct knfsd_fh *fh)
415{
416 u32 hash;
417 struct bloom_pair *bd = &blocked_delegations;
418
419 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
420
421 __set_bit(hash&255, bd->set[bd->new]);
422 __set_bit((hash>>8)&255, bd->set[bd->new]);
423 __set_bit((hash>>16)&255, bd->set[bd->new]);
424 if (bd->entries == 0)
425 bd->swap_time = seconds_since_boot();
426 bd->entries += 1;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static struct nfs4_delegation *
J. Bruce Fields99c41512013-05-21 16:21:25 -0400430alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 dprintk("NFSD alloc_init_deleg\n");
Meelap Shahc2f1a552007-07-17 04:04:39 -0700435 if (num_delegations > max_delegations)
NeilBrownef0f3392006-04-10 22:55:41 -0700436 return NULL;
NeilBrown6282cd52014-06-04 17:39:26 +1000437 if (delegation_blocked(&current_fh->fh_handle))
438 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400439 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700440 if (dp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return dp;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400442 /*
443 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400444 * meaning of seqid 0 isn't meaningful, really, but let's avoid
445 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400446 */
447 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownef0f3392006-04-10 22:55:41 -0700448 num_delegations++;
NeilBrownea1da632005-06-23 22:04:17 -0700449 INIT_LIST_HEAD(&dp->dl_perfile);
450 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 INIT_LIST_HEAD(&dp->dl_recall_lru);
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -0400452 dp->dl_file = NULL;
J. Bruce Fields99c41512013-05-21 16:21:25 -0400453 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fields6c02eaa2009-02-02 17:30:51 -0500454 fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 dp->dl_time = 0;
456 atomic_set(&dp->dl_count, 1);
J. Bruce Fields57725152012-11-05 15:10:26 -0500457 nfsd4_init_callback(&dp->dl_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return dp;
459}
460
J. Bruce Fields68a33962013-03-21 11:21:50 -0400461static void remove_stid(struct nfs4_stid *s)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500462{
463 struct idr *stateids = &s->sc_client->cl_stateids;
464
465 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500466}
467
Benny Halevy9857df82013-10-14 13:44:52 +0300468static void nfs4_free_stid(struct kmem_cache *slab, struct nfs4_stid *s)
469{
470 kmem_cache_free(slab, s);
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473void
474nfs4_put_delegation(struct nfs4_delegation *dp)
475{
476 if (atomic_dec_and_test(&dp->dl_count)) {
Benny Halevy9857df82013-10-14 13:44:52 +0300477 nfs4_free_stid(deleg_slab, &dp->dl_stid);
NeilBrownef0f3392006-04-10 22:55:41 -0700478 num_delegations--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480}
481
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500482static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500484 if (!fp->fi_lease)
485 return;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500486 if (atomic_dec_and_test(&fp->fi_delegees)) {
487 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
488 fp->fi_lease = NULL;
J. Bruce Fields4ee63622011-04-15 18:08:26 -0400489 fput(fp->fi_deleg_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500490 fp->fi_deleg_file = NULL;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400494static void unhash_stid(struct nfs4_stid *s)
495{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500496 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400497}
498
Benny Halevy931ee562014-05-30 09:09:27 -0400499static void
500hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
501{
Benny Halevycdc97502014-05-30 09:09:30 -0400502 lockdep_assert_held(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400503
Benny Halevy3fb87d12014-05-30 09:09:31 -0400504 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400505 list_add(&dp->dl_perfile, &fp->fi_delegations);
506 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/* Called under the state lock. */
510static void
511unhash_delegation(struct nfs4_delegation *dp)
512{
Benny Halevycdc97502014-05-30 09:09:30 -0400513 spin_lock(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400514 list_del_init(&dp->dl_perclnt);
J. Bruce Fields5d926e82011-02-07 16:53:46 -0500515 list_del_init(&dp->dl_perfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 list_del_init(&dp->dl_recall_lru);
Benny Halevycdc97502014-05-30 09:09:30 -0400517 spin_unlock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -0500518 if (dp->dl_file) {
519 nfs4_put_deleg_lease(dp->dl_file);
520 put_nfs4_file(dp->dl_file);
521 dp->dl_file = NULL;
522 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400523}
524
525
526
527static void destroy_revoked_delegation(struct nfs4_delegation *dp)
528{
529 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields68a33962013-03-21 11:21:50 -0400530 remove_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 nfs4_put_delegation(dp);
532}
533
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400534static void destroy_delegation(struct nfs4_delegation *dp)
535{
536 unhash_delegation(dp);
537 remove_stid(&dp->dl_stid);
538 nfs4_put_delegation(dp);
539}
540
541static void revoke_delegation(struct nfs4_delegation *dp)
542{
543 struct nfs4_client *clp = dp->dl_stid.sc_client;
544
545 if (clp->cl_minorversion == 0)
546 destroy_delegation(dp);
547 else {
548 unhash_delegation(dp);
549 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
550 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
551 }
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/*
555 * SETCLIENTID state
556 */
557
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400558static unsigned int clientid_hashval(u32 id)
559{
560 return id & CLIENT_HASH_MASK;
561}
562
563static unsigned int clientstr_hashval(const char *name)
564{
565 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400569 * We store the NONE, READ, WRITE, and BOTH bits separately in the
570 * st_{access,deny}_bmap field of the stateid, in order to track not
571 * only what share bits are currently in force, but also what
572 * combinations of share bits previous opens have used. This allows us
573 * to enforce the recommendation of rfc 3530 14.2.19 that the server
574 * return an error if the client attempt to downgrade to a combination
575 * of share bits not explicable by closing some of its previous opens.
576 *
577 * XXX: This enforcement is actually incomplete, since we don't keep
578 * track of access/deny bit combinations; so, e.g., we allow:
579 *
580 * OPEN allow read, deny write
581 * OPEN allow both, deny none
582 * DOWNGRADE allow read, deny none
583 *
584 * which we should reject.
585 */
Jeff Layton5ae037e2012-05-11 09:45:11 -0400586static unsigned int
587bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400588 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -0400589 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400590
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400591 for (i = 1; i < 4; i++) {
592 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -0400593 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400594 }
Jeff Layton5ae037e2012-05-11 09:45:11 -0400595 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400596}
597
Jeff Layton3a328612012-05-11 09:45:12 -0400598static bool
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400599test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400600 unsigned int access, deny;
601
Jeff Layton5ae037e2012-05-11 09:45:11 -0400602 access = bmap_to_share_mode(stp->st_access_bmap);
603 deny = bmap_to_share_mode(stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400604 if ((access & open->op_share_deny) || (deny & open->op_share_access))
Jeff Layton3a328612012-05-11 09:45:12 -0400605 return false;
606 return true;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400607}
608
Jeff Layton82c5ff12012-05-11 09:45:13 -0400609/* set share access for a given stateid */
610static inline void
611set_access(u32 access, struct nfs4_ol_stateid *stp)
612{
613 __set_bit(access, &stp->st_access_bmap);
614}
615
616/* clear share access for a given stateid */
617static inline void
618clear_access(u32 access, struct nfs4_ol_stateid *stp)
619{
620 __clear_bit(access, &stp->st_access_bmap);
621}
622
623/* test whether a given stateid has access */
624static inline bool
625test_access(u32 access, struct nfs4_ol_stateid *stp)
626{
627 return test_bit(access, &stp->st_access_bmap);
628}
629
Jeff Laytonce0fc432012-05-11 09:45:14 -0400630/* set share deny for a given stateid */
631static inline void
632set_deny(u32 access, struct nfs4_ol_stateid *stp)
633{
634 __set_bit(access, &stp->st_deny_bmap);
635}
636
637/* clear share deny for a given stateid */
638static inline void
639clear_deny(u32 access, struct nfs4_ol_stateid *stp)
640{
641 __clear_bit(access, &stp->st_deny_bmap);
642}
643
644/* test whether a given stateid is denying specific access */
645static inline bool
646test_deny(u32 access, struct nfs4_ol_stateid *stp)
647{
648 return test_bit(access, &stp->st_deny_bmap);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400649}
650
651static int nfs4_access_to_omode(u32 access)
652{
J. Bruce Fields8f34a432010-09-02 15:23:16 -0400653 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400654 case NFS4_SHARE_ACCESS_READ:
655 return O_RDONLY;
656 case NFS4_SHARE_ACCESS_WRITE:
657 return O_WRONLY;
658 case NFS4_SHARE_ACCESS_BOTH:
659 return O_RDWR;
660 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -0500661 WARN_ON_ONCE(1);
662 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400663}
664
Jeff Layton82c5ff12012-05-11 09:45:13 -0400665/* release all access and file references for a given stateid */
666static void
667release_all_access(struct nfs4_ol_stateid *stp)
668{
669 int i;
670
671 for (i = 1; i < 4; i++) {
672 if (test_access(i, stp))
673 nfs4_file_put_access(stp->st_file,
674 nfs4_access_to_omode(i));
675 clear_access(i, stp);
676 }
677}
678
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400679static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500680{
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500681 list_del(&stp->st_perfile);
682 list_del(&stp->st_perstateowner);
683}
684
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400685static void close_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500686{
Jeff Layton82c5ff12012-05-11 09:45:13 -0400687 release_all_access(stp);
OGAWA Hirofumia96e5b92011-04-18 11:48:55 -0400688 put_nfs4_file(stp->st_file);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400689 stp->st_file = NULL;
690}
691
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400692static void free_generic_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields4665e2b2011-09-06 14:50:49 -0400693{
J. Bruce Fields68a33962013-03-21 11:21:50 -0400694 remove_stid(&stp->st_stid);
Benny Halevy9857df82013-10-14 13:44:52 +0300695 nfs4_free_stid(stateid_slab, &stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500696}
697
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400698static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500699{
700 struct file *file;
701
702 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400703 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500704 file = find_any_file(stp->st_file);
705 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400706 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400707 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500708 free_generic_stateid(stp);
709}
710
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400711static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500712{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400713 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500714
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400715 list_del(&lo->lo_owner.so_strhash);
716 list_del(&lo->lo_perstateid);
J. Bruce Fields009673b2011-11-07 17:40:10 -0500717 list_del(&lo->lo_owner_ino_hash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400718 while (!list_empty(&lo->lo_owner.so_stateids)) {
719 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400720 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500721 release_lock_stateid(stp);
722 }
723}
724
Trond Myklebust50cc6232014-04-18 14:44:03 -0400725static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
726{
727 kfree(lo->lo_owner.so_owner.data);
728 kmem_cache_free(lockowner_slab, lo);
729}
730
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400731static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500732{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400733 unhash_lockowner(lo);
734 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500735}
736
737static void
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400738release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500739{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400740 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500741
742 while (!list_empty(&open_stp->st_lockowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400743 lo = list_entry(open_stp->st_lockowners.next,
744 struct nfs4_lockowner, lo_perstateid);
745 release_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500746 }
747}
748
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400749static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500750{
751 unhash_generic_stateid(stp);
752 release_stateid_lockowners(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400753 close_generic_stateid(stp);
754}
755
756static void release_open_stateid(struct nfs4_ol_stateid *stp)
757{
758 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500759 free_generic_stateid(stp);
760}
761
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400762static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500763{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400764 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500765
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400766 list_del(&oo->oo_owner.so_strhash);
767 list_del(&oo->oo_perclient);
768 while (!list_empty(&oo->oo_owner.so_stateids)) {
769 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400770 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500771 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500772 }
773}
774
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400775static void release_last_closed_stateid(struct nfs4_openowner *oo)
776{
777 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
778
779 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400780 free_generic_stateid(s);
781 oo->oo_last_closed_stid = NULL;
782 }
783}
784
Trond Myklebust50cc6232014-04-18 14:44:03 -0400785static void nfs4_free_openowner(struct nfs4_openowner *oo)
786{
787 kfree(oo->oo_owner.so_owner.data);
788 kmem_cache_free(openowner_slab, oo);
789}
790
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400791static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500792{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400793 unhash_openowner(oo);
794 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400795 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400796 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500797}
798
Marc Eshel5282fd72009-04-03 08:27:52 +0300799static inline int
800hash_sessionid(struct nfs4_sessionid *sessionid)
801{
802 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
803
804 return sid->sequence % SESSION_HASH_SIZE;
805}
806
Trond Myklebust8f199b82012-03-20 15:11:17 -0400807#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300808static inline void
809dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
810{
811 u32 *ptr = (u32 *)(&sessionid->data[0]);
812 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
813}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400814#else
815static inline void
816dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
817{
818}
819#endif
820
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400821/*
822 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
823 * won't be used for replay.
824 */
825void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
826{
827 struct nfs4_stateowner *so = cstate->replay_owner;
828
829 if (nfserr == nfserr_replay_me)
830 return;
831
832 if (!seqid_mutating_err(ntohl(nfserr))) {
833 cstate->replay_owner = NULL;
834 return;
835 }
836 if (!so)
837 return;
838 if (so->so_is_open_owner)
839 release_last_closed_stateid(openowner(so));
840 so->so_seqid++;
841 return;
842}
Marc Eshel5282fd72009-04-03 08:27:52 +0300843
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300844static void
845gen_sessionid(struct nfsd4_session *ses)
846{
847 struct nfs4_client *clp = ses->se_client;
848 struct nfsd4_sessionid *sid;
849
850 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
851 sid->clientid = clp->cl_clientid;
852 sid->sequence = current_sessionid++;
853 sid->reserved = 0;
854}
855
856/*
Andy Adamsona6496372009-08-28 08:45:01 -0400857 * The protocol defines ca_maxresponssize_cached to include the size of
858 * the rpc header, but all we need to cache is the data starting after
859 * the end of the initial SEQUENCE operation--the rest we regenerate
860 * each time. Therefore we can advertise a ca_maxresponssize_cached
861 * value that is the number of bytes in our cache plus a few additional
862 * bytes. In order to stay on the safe side, and not promise more than
863 * we can cache, those additional bytes must be the minimum possible: 24
864 * bytes of rpc header (xid through accept state, with AUTH_NULL
865 * verifier), 12 for the compound header (with zero-length tag), and 44
866 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300867 */
Andy Adamsona6496372009-08-28 08:45:01 -0400868#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
869
Andy Adamson557ce262009-08-28 08:45:04 -0400870static void
871free_session_slots(struct nfsd4_session *ses)
872{
873 int i;
874
875 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
876 kfree(ses->se_slots[i]);
877}
878
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400879/*
880 * We don't actually need to cache the rpc and session headers, so we
881 * can allocate a little less for each slot:
882 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400883static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400884{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400885 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400886
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400887 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
888 size = 0;
889 else
890 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
891 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400892}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300893
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400894/*
895 * XXX: If we run out of reserved DRC memory we could (up to a point)
896 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800897 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400898 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400899static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400900{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400901 u32 slotsize = slot_bytes(ca);
902 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400903 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300904
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400905 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800906 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
907 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400908 num = min_t(int, num, avail / slotsize);
909 nfsd_drc_mem_used += num * slotsize;
910 spin_unlock(&nfsd_drc_lock);
911
912 return num;
913}
914
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400915static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400916{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400917 int slotsize = slot_bytes(ca);
918
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400919 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400920 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400921 spin_unlock(&nfsd_drc_lock);
922}
923
Kinglong Mee60810e52014-01-01 00:35:47 +0800924static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
925 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400926{
Kinglong Mee60810e52014-01-01 00:35:47 +0800927 int numslots = fattrs->maxreqs;
928 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400929 struct nfsd4_session *new;
930 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300931
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400932 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400933 + sizeof(struct nfsd4_session) > PAGE_SIZE);
934 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400935
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400936 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300937 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400938 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400939 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400940 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400941 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400942 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400943 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400944 }
Kinglong Mee60810e52014-01-01 00:35:47 +0800945
946 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
947 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
948
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400949 return new;
950out_free:
951 while (i--)
952 kfree(new->se_slots[i]);
953 kfree(new);
954 return NULL;
955}
956
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400957static void free_conn(struct nfsd4_conn *c)
958{
959 svc_xprt_put(c->cn_xprt);
960 kfree(c);
961}
962
963static void nfsd4_conn_lost(struct svc_xpt_user *u)
964{
965 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
966 struct nfs4_client *clp = c->cn_session->se_client;
967
968 spin_lock(&clp->cl_lock);
969 if (!list_empty(&c->cn_persession)) {
970 list_del(&c->cn_persession);
971 free_conn(c);
972 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500973 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -0500974 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400975}
976
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400977static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400978{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400979 struct nfsd4_conn *conn;
980
981 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
982 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400983 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400984 svc_xprt_get(rqstp->rq_xprt);
985 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400986 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400987 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
988 return conn;
989}
990
J. Bruce Fields328ead22010-09-29 16:11:06 -0400991static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
992{
993 conn->cn_session = ses;
994 list_add(&conn->cn_persession, &ses->se_conns);
995}
996
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400997static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
998{
999 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001000
1001 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001002 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001003 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001004}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001005
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001006static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001007{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001008 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001009 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001010}
1011
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001012static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001013{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001014 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001015
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001016 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001017 ret = nfsd4_register_conn(conn);
1018 if (ret)
1019 /* oops; xprt is already down: */
1020 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -04001021 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -04001022 /* callback channel may be back up */
1023 nfsd4_probe_callback(ses->se_client);
1024 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001025}
1026
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001027static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001028{
1029 u32 dir = NFS4_CDFC4_FORE;
1030
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001031 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001032 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001033 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001034}
1035
1036/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001037static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001038{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001039 struct nfs4_client *clp = s->se_client;
1040 struct nfsd4_conn *c;
1041
1042 spin_lock(&clp->cl_lock);
1043 while (!list_empty(&s->se_conns)) {
1044 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1045 list_del_init(&c->cn_persession);
1046 spin_unlock(&clp->cl_lock);
1047
1048 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1049 free_conn(c);
1050
1051 spin_lock(&clp->cl_lock);
1052 }
1053 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001054}
1055
J. Bruce Fields1377b692012-09-11 21:42:40 -04001056static void __free_session(struct nfsd4_session *ses)
1057{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001058 free_session_slots(ses);
1059 kfree(ses);
1060}
1061
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001062static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001063{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001064 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1065
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001066 lockdep_assert_held(&nn->client_lock);
1067 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001068 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001069 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001070}
Andy Adamson557ce262009-08-28 08:45:04 -04001071
Fengguang Wu135ae822012-11-10 07:20:25 -05001072static 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 -04001073{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001074 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001075 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001076
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001077 new->se_client = clp;
1078 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001079
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001080 INIT_LIST_HEAD(&new->se_conns);
1081
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001082 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001083 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001084 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001085 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001086 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001087 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001088 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001089 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001090 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001091 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001092 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001093 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001094
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001095 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001096 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001097 /*
1098 * This is a little silly; with sessions there's no real
1099 * use for the callback address. Use the peer address
1100 * as a reasonable default for now, but consider fixing
1101 * the rpc client not to require an address in the
1102 * future:
1103 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001104 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1105 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001106 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001107}
1108
Benny Halevy9089f1b2010-05-12 00:12:26 +03001109/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001110static struct nfsd4_session *
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001111find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001112{
1113 struct nfsd4_session *elem;
1114 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001115 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001116
1117 dump_sessionid(__func__, sessionid);
1118 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001119 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001120 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001121 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1122 NFS4_MAX_SESSIONID_LEN)) {
1123 return elem;
1124 }
1125 }
1126
1127 dprintk("%s: session not found\n", __func__);
1128 return NULL;
1129}
1130
Benny Halevy9089f1b2010-05-12 00:12:26 +03001131/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001132static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001133unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001134{
1135 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001136 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001137 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001138 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1142static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001143STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001145 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001147 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001148 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return 1;
1150}
1151
1152/*
1153 * XXX Should we use a slab cache ?
1154 * This type of memory management is somewhat inefficient, but we use it
1155 * anyway since SETCLIENTID is not a common operation.
1156 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001157static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct nfs4_client *clp;
1160
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001161 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1162 if (clp == NULL)
1163 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001164 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001165 if (clp->cl_name.data == NULL) {
1166 kfree(clp);
1167 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001169 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001170 INIT_LIST_HEAD(&clp->cl_sessions);
1171 idr_init(&clp->cl_stateids);
1172 atomic_set(&clp->cl_refcount, 0);
1173 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1174 INIT_LIST_HEAD(&clp->cl_idhash);
1175 INIT_LIST_HEAD(&clp->cl_openowners);
1176 INIT_LIST_HEAD(&clp->cl_delegations);
1177 INIT_LIST_HEAD(&clp->cl_lru);
1178 INIT_LIST_HEAD(&clp->cl_callbacks);
1179 INIT_LIST_HEAD(&clp->cl_revoked);
1180 spin_lock_init(&clp->cl_lock);
1181 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return clp;
1183}
1184
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001185static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186free_client(struct nfs4_client *clp)
1187{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001188 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001189
1190 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001191 while (!list_empty(&clp->cl_sessions)) {
1192 struct nfsd4_session *ses;
1193 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1194 se_perclnt);
1195 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001196 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1197 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001198 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001199 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001200 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001202 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 kfree(clp);
1204}
1205
Benny Halevy84d38ac2010-05-12 00:13:16 +03001206/* must be called under the client_lock */
1207static inline void
1208unhash_client_locked(struct nfs4_client *clp)
1209{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001210 struct nfsd4_session *ses;
1211
Benny Halevy84d38ac2010-05-12 00:13:16 +03001212 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001213 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001214 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1215 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001216 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001220destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001222 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001225 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001228 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001229 while (!list_empty(&clp->cl_delegations)) {
1230 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001231 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 list_move(&dp->dl_recall_lru, &reaplist);
1233 }
Benny Halevycdc97502014-05-30 09:09:30 -04001234 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 while (!list_empty(&reaplist)) {
1236 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001237 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
Benny Halevy956c4fe2013-10-29 11:39:12 +02001239 list_splice_init(&clp->cl_revoked, &reaplist);
1240 while (!list_empty(&reaplist)) {
1241 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1242 destroy_revoked_delegation(dp);
1243 }
NeilBrownea1da632005-06-23 22:04:17 -07001244 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001245 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1246 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001248 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001249 if (clp->cl_cb_conn.cb_xprt)
1250 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001251 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001252 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001253 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001254 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001255 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001256 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001257 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001258 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1259 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001260 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001263static void expire_client(struct nfs4_client *clp)
1264{
1265 nfsd4_client_record_remove(clp);
1266 destroy_client(clp);
1267}
1268
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001269static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1270{
1271 memcpy(target->cl_verifier.data, source->data,
1272 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001275static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1278 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1279}
1280
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001281static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001282{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001283 if (source->cr_principal) {
1284 target->cr_principal =
1285 kstrdup(source->cr_principal, GFP_KERNEL);
1286 if (target->cr_principal == NULL)
1287 return -ENOMEM;
1288 } else
1289 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001290 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 target->cr_uid = source->cr_uid;
1292 target->cr_gid = source->cr_gid;
1293 target->cr_group_info = source->cr_group_info;
1294 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001295 target->cr_gss_mech = source->cr_gss_mech;
1296 if (source->cr_gss_mech)
1297 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001301static long long
1302compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1303{
1304 long long res;
1305
1306 res = o1->len - o2->len;
1307 if (res)
1308 return res;
1309 return (long long)memcmp(o1->data, o2->data, o1->len);
1310}
1311
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001312static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001313{
NeilBrowna55370a2005-06-23 22:03:52 -07001314 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001318same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1319{
1320 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001324same_clid(clientid_t *cl1, clientid_t *cl2)
1325{
1326 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001329static bool groups_equal(struct group_info *g1, struct group_info *g2)
1330{
1331 int i;
1332
1333 if (g1->ngroups != g2->ngroups)
1334 return false;
1335 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001336 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001337 return false;
1338 return true;
1339}
1340
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001341/*
1342 * RFC 3530 language requires clid_inuse be returned when the
1343 * "principal" associated with a requests differs from that previously
1344 * used. We use uid, gid's, and gss principal string as our best
1345 * approximation. We also don't want to allow non-gss use of a client
1346 * established using gss: in theory cr_principal should catch that
1347 * change, but in practice cr_principal can be null even in the gss case
1348 * since gssd doesn't always pass down a principal string.
1349 */
1350static bool is_gss_cred(struct svc_cred *cr)
1351{
1352 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1353 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1354}
1355
1356
Vivek Trivedi5559b502012-07-24 21:18:20 +05301357static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001358same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1359{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001360 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001361 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1362 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001363 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1364 return false;
1365 if (cr1->cr_principal == cr2->cr_principal)
1366 return true;
1367 if (!cr1->cr_principal || !cr2->cr_principal)
1368 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301369 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
J. Bruce Fields57266a62013-04-13 14:27:29 -04001372static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1373{
1374 struct svc_cred *cr = &rqstp->rq_cred;
1375 u32 service;
1376
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001377 if (!cr->cr_gss_mech)
1378 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001379 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1380 return service == RPC_GSS_SVC_INTEGRITY ||
1381 service == RPC_GSS_SVC_PRIVACY;
1382}
1383
1384static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1385{
1386 struct svc_cred *cr = &rqstp->rq_cred;
1387
1388 if (!cl->cl_mach_cred)
1389 return true;
1390 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1391 return false;
1392 if (!svc_rqst_integrity_protected(rqstp))
1393 return false;
1394 if (!cr->cr_principal)
1395 return false;
1396 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1397}
1398
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001399static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001400{
1401 static u32 current_clientid = 1;
1402
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001403 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 clp->cl_clientid.cl_id = current_clientid++;
1405}
1406
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001407static void gen_confirm(struct nfs4_client *clp)
1408{
Chuck Leverab4684d2012-03-02 17:13:50 -05001409 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001410 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Jeff Laytonf4199922014-06-17 07:44:11 -04001412 /*
1413 * This is opaque to client, so no need to byte-swap. Use
1414 * __force to keep sparse happy
1415 */
1416 verf[0] = (__force __be32)get_seconds();
1417 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001418 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001421static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001422{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001423 struct nfs4_stid *ret;
1424
1425 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1426 if (!ret || !ret->sc_type)
1427 return NULL;
1428 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001429}
1430
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001431static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001432{
1433 struct nfs4_stid *s;
1434
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001435 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001436 if (!s)
1437 return NULL;
1438 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001439 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001440 return NULL;
1441}
1442
Jeff Layton2216d442012-11-12 15:00:57 -05001443static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001444 struct svc_rqst *rqstp, nfs4_verifier *verf)
1445{
1446 struct nfs4_client *clp;
1447 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001448 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001449 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001450 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001451
1452 clp = alloc_client(name);
1453 if (clp == NULL)
1454 return NULL;
1455
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001456 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1457 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001458 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001459 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001460 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001461 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001462 }
J. Bruce Fields57725152012-11-05 15:10:26 -05001463 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001464 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001465 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001466 copy_verf(clp, verf);
1467 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001468 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001469 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001470 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001471 return clp;
1472}
1473
NeilBrownfd39ca92005-06-23 22:04:03 -07001474static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001475add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1476{
1477 struct rb_node **new = &(root->rb_node), *parent = NULL;
1478 struct nfs4_client *clp;
1479
1480 while (*new) {
1481 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1482 parent = *new;
1483
1484 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1485 new = &((*new)->rb_left);
1486 else
1487 new = &((*new)->rb_right);
1488 }
1489
1490 rb_link_node(&new_clp->cl_namenode, parent, new);
1491 rb_insert_color(&new_clp->cl_namenode, root);
1492}
1493
1494static struct nfs4_client *
1495find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1496{
1497 long long cmp;
1498 struct rb_node *node = root->rb_node;
1499 struct nfs4_client *clp;
1500
1501 while (node) {
1502 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1503 cmp = compare_blob(&clp->cl_name, name);
1504 if (cmp > 0)
1505 node = node->rb_left;
1506 else if (cmp < 0)
1507 node = node->rb_right;
1508 else
1509 return clp;
1510 }
1511 return NULL;
1512}
1513
1514static void
1515add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001518 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001520 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001521 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001523 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001524 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
NeilBrownfd39ca92005-06-23 22:04:03 -07001527static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528move_to_confirmed(struct nfs4_client *clp)
1529{
1530 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001531 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001534 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001535 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001536 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001537 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 renew_client(clp);
1539}
1540
1541static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001542find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 struct nfs4_client *clp;
1545 unsigned int idhashval = clientid_hashval(clid->cl_id);
1546
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001547 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001548 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001549 if ((bool)clp->cl_minorversion != sessions)
1550 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001551 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555 return NULL;
1556}
1557
1558static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001559find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1560{
1561 struct list_head *tbl = nn->conf_id_hashtbl;
1562
1563 return find_client_in_id_table(tbl, clid, sessions);
1564}
1565
1566static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001567find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001569 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001571 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001574static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001575{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001576 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001577}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001578
NeilBrown28ce6052005-06-23 22:03:56 -07001579static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001580find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001581{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001582 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001583}
1584
1585static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001586find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001587{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001588 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001589}
1590
NeilBrownfd39ca92005-06-23 22:04:03 -07001591static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001592gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001594 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001595 struct sockaddr *sa = svc_addr(rqstp);
1596 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001597 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Jeff Layton7077ecb2009-08-14 12:57:58 -04001599 /* Currently, we only support tcp and tcp6 for the callback channel */
1600 if (se->se_callback_netid_len == 3 &&
1601 !memcmp(se->se_callback_netid_val, "tcp", 3))
1602 expected_family = AF_INET;
1603 else if (se->se_callback_netid_len == 4 &&
1604 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1605 expected_family = AF_INET6;
1606 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 goto out_err;
1608
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001609 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001610 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001611 (struct sockaddr *)&conn->cb_addr,
1612 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001613
J. Bruce Fields07263f12010-05-31 19:09:40 -04001614 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001616
J. Bruce Fields07263f12010-05-31 19:09:40 -04001617 if (conn->cb_addr.ss_family == AF_INET6)
1618 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001619
J. Bruce Fields07263f12010-05-31 19:09:40 -04001620 conn->cb_prog = se->se_callback_prog;
1621 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001622 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return;
1624out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001625 conn->cb_addr.ss_family = AF_UNSPEC;
1626 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001627 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 "will not receive delegations\n",
1629 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return;
1632}
1633
Andy Adamson074fe892009-04-03 08:28:15 +03001634/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001635 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001636 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001637static void
Andy Adamson074fe892009-04-03 08:28:15 +03001638nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1639{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001640 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001641 struct nfsd4_slot *slot = resp->cstate.slot;
1642 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001643
Andy Adamson557ce262009-08-28 08:45:04 -04001644 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001645
Andy Adamson557ce262009-08-28 08:45:04 -04001646 slot->sl_opcnt = resp->opcnt;
1647 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001648
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001649 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001650 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001651 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001652 return;
1653 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001654 base = resp->cstate.data_offset;
1655 slot->sl_datalen = buf->len - base;
1656 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001657 WARN("%s: sessions DRC could not cache compound\n", __func__);
1658 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001659}
1660
1661/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001662 * Encode the replay sequence operation from the slot values.
1663 * If cachethis is FALSE encode the uncached rep error on the next
1664 * operation which sets resp->p and increments resp->opcnt for
1665 * nfs4svc_encode_compoundres.
1666 *
Andy Adamson074fe892009-04-03 08:28:15 +03001667 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001668static __be32
1669nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1670 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001671{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001672 struct nfsd4_op *op;
1673 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001674
Andy Adamsonabfabf82009-07-23 19:02:18 -04001675 /* Encode the replayed sequence operation */
1676 op = &args->ops[resp->opcnt - 1];
1677 nfsd4_encode_operation(resp, op);
1678
1679 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001680 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001681 op = &args->ops[resp->opcnt++];
1682 op->status = nfserr_retry_uncached_rep;
1683 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001684 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001685 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001686}
1687
1688/*
Andy Adamson557ce262009-08-28 08:45:04 -04001689 * The sequence operation is not cached because we can use the slot and
1690 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001691 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001692static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001693nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1694 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001695{
Andy Adamson557ce262009-08-28 08:45:04 -04001696 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001697 struct xdr_stream *xdr = &resp->xdr;
1698 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001699 __be32 status;
1700
Andy Adamson557ce262009-08-28 08:45:04 -04001701 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001702
Andy Adamsonabfabf82009-07-23 19:02:18 -04001703 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001704 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001705 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001706
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001707 p = xdr_reserve_space(xdr, slot->sl_datalen);
1708 if (!p) {
1709 WARN_ON_ONCE(1);
1710 return nfserr_serverfault;
1711 }
1712 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1713 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001714
Andy Adamson557ce262009-08-28 08:45:04 -04001715 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001716 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001717}
1718
Andy Adamson0733d212009-04-03 08:28:01 +03001719/*
1720 * Set the exchange_id flags returned by the server.
1721 */
1722static void
1723nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1724{
1725 /* pNFS is not supported */
1726 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1727
1728 /* Referrals are supported, Migration is not. */
1729 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1730
1731 /* set the wire flags to return to client. */
1732 clid->flags = new->cl_exchange_flags;
1733}
1734
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001735static bool client_has_state(struct nfs4_client *clp)
1736{
1737 /*
1738 * Note clp->cl_openowners check isn't quite right: there's no
1739 * need to count owners without stateid's.
1740 *
1741 * Also note we should probably be using this in 4.0 case too.
1742 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001743 return !list_empty(&clp->cl_openowners)
1744 || !list_empty(&clp->cl_delegations)
1745 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001746}
1747
Al Virob37ad282006-10-19 23:28:59 -07001748__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001749nfsd4_exchange_id(struct svc_rqst *rqstp,
1750 struct nfsd4_compound_state *cstate,
1751 struct nfsd4_exchange_id *exid)
1752{
Andy Adamson0733d212009-04-03 08:28:01 +03001753 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001754 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001755 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001756 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001757 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001758 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001759 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001760
Jeff Layton363168b2009-08-14 12:57:56 -04001761 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001762 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001763 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001764 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001765 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001766
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001767 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001768 return nfserr_inval;
1769
Andy Adamson0733d212009-04-03 08:28:01 +03001770 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04001771 case SP4_MACH_CRED:
1772 if (!svc_rqst_integrity_protected(rqstp))
1773 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001774 case SP4_NONE:
1775 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001776 default: /* checked by xdr code */
1777 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001778 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04001779 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03001780 }
1781
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001782 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001783 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001784 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001785 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001786 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1787 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1788
J. Bruce Fields136e6582012-05-12 20:37:23 -04001789 if (update) {
1790 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001791 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001792 goto out;
1793 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04001794 if (!mach_creds_match(conf, rqstp)) {
1795 status = nfserr_wrong_cred;
1796 goto out;
1797 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001798 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001799 status = nfserr_perm;
1800 goto out;
1801 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001802 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001803 status = nfserr_not_same;
1804 goto out;
1805 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001806 /* case 6 */
1807 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1808 new = conf;
1809 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001810 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001811 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001812 if (client_has_state(conf)) {
1813 status = nfserr_clid_inuse;
1814 goto out;
1815 }
Andy Adamson0733d212009-04-03 08:28:01 +03001816 expire_client(conf);
1817 goto out_new;
1818 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001819 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001820 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001821 new = conf;
1822 goto out_copy;
1823 }
1824 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001825 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001826 }
1827
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001828 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001829 status = nfserr_noent;
1830 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001831 }
1832
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001833 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001834 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001835 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001836
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001837 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001838out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001839 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001840 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001841 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001842 goto out;
1843 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04001844 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001845 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03001846
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001847 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001848 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001849out_copy:
1850 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1851 exid->clientid.cl_id = new->cl_clientid.cl_id;
1852
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001853 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001854 nfsd4_set_ex_flags(new, exid);
1855
1856 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001857 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001858 status = nfs_ok;
1859
1860out:
1861 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001862 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001863}
1864
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001865static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001866check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001867{
Andy Adamson88e588d2009-07-23 19:02:15 -04001868 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1869 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001870
1871 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001872 if (slot_inuse) {
1873 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001874 return nfserr_jukebox;
1875 else
1876 return nfserr_seq_misordered;
1877 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001878 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001879 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001880 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001881 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001882 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001883 return nfserr_seq_misordered;
1884}
1885
Andy Adamson49557cc2009-07-23 19:02:16 -04001886/*
1887 * Cache the create session result into the create session single DRC
1888 * slot cache by saving the xdr structure. sl_seqid has been set.
1889 * Do this for solo or embedded create session operations.
1890 */
1891static void
1892nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001893 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001894{
1895 slot->sl_status = nfserr;
1896 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1897}
1898
1899static __be32
1900nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1901 struct nfsd4_clid_slot *slot)
1902{
1903 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1904 return slot->sl_status;
1905}
1906
Mi Jinlong1b74c252011-07-14 14:50:17 +08001907#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1908 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1909 1 + /* MIN tag is length with zero, only length */ \
1910 3 + /* version, opcount, opcode */ \
1911 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1912 /* seqid, slotID, slotID, cache */ \
1913 4 ) * sizeof(__be32))
1914
1915#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1916 2 + /* verifier: AUTH_NULL, length 0 */\
1917 1 + /* status */ \
1918 1 + /* MIN tag is length with zero, only length */ \
1919 3 + /* opcount, opcode, opstatus*/ \
1920 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1921 /* seqid, slotID, slotID, slotID, status */ \
1922 5 ) * sizeof(__be32))
1923
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001924static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001925{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001926 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
1927
J. Bruce Fields373cd402013-04-08 15:42:12 -04001928 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
1929 return nfserr_toosmall;
1930 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
1931 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001932 ca->headerpadsz = 0;
1933 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
1934 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
1935 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
1936 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
1937 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
1938 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
1939 /*
1940 * Note decreasing slot size below client's request may make it
1941 * difficult for client to function correctly, whereas
1942 * decreasing the number of slots will (just?) affect
1943 * performance. When short on memory we therefore prefer to
1944 * decrease number of slots instead of their size. Clients that
1945 * request larger slots than they need will get poor results:
1946 */
1947 ca->maxreqs = nfsd4_get_drc_mem(ca);
1948 if (!ca->maxreqs)
1949 return nfserr_jukebox;
1950
J. Bruce Fields373cd402013-04-08 15:42:12 -04001951 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08001952}
1953
Kinglong Mee8a891632013-12-23 18:11:02 +08001954#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
1955 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
1956#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
1957 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
1958
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001959static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
1960{
1961 ca->headerpadsz = 0;
1962
1963 /*
1964 * These RPC_MAX_HEADER macros are overkill, especially since we
1965 * don't even do gss on the backchannel yet. But this is still
1966 * less than 1k. Tighten up this estimate in the unlikely event
1967 * it turns out to be a problem for some client:
1968 */
Kinglong Mee8a891632013-12-23 18:11:02 +08001969 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001970 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08001971 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001972 return nfserr_toosmall;
1973 ca->maxresp_cached = 0;
1974 if (ca->maxops < 2)
1975 return nfserr_toosmall;
1976
1977 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001978}
1979
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04001980static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
1981{
1982 switch (cbs->flavor) {
1983 case RPC_AUTH_NULL:
1984 case RPC_AUTH_UNIX:
1985 return nfs_ok;
1986 default:
1987 /*
1988 * GSS case: the spec doesn't allow us to return this
1989 * error. But it also doesn't allow us not to support
1990 * GSS.
1991 * I'd rather this fail hard than return some error the
1992 * client might think it can already handle:
1993 */
1994 return nfserr_encr_alg_unsupp;
1995 }
1996}
1997
Andy Adamson069b6ad2009-04-03 08:27:58 +03001998__be32
1999nfsd4_create_session(struct svc_rqst *rqstp,
2000 struct nfsd4_compound_state *cstate,
2001 struct nfsd4_create_session *cr_ses)
2002{
Jeff Layton363168b2009-08-14 12:57:56 -04002003 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002004 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002005 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002006 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002007 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002008 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002009 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002010
Mi Jinlonga62573d2011-03-23 17:57:07 +08002011 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2012 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002013 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2014 if (status)
2015 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002016 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002017 if (status)
2018 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002019 status = check_backchannel_attrs(&cr_ses->back_channel);
2020 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002021 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002022 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002023 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002024 if (!new)
2025 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002026 conn = alloc_conn_from_crses(rqstp, cr_ses);
2027 if (!conn)
2028 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002029
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002030 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002031 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002032 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002033 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002034
2035 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002036 status = nfserr_wrong_cred;
2037 if (!mach_creds_match(conf, rqstp))
2038 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002039 cs_slot = &conf->cl_cs_slot;
2040 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002041 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002042 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002043 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002044 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002045 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002046 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002047 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002048 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002049 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002050 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002051 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002052 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002053 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002054 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002055 status = nfserr_wrong_cred;
2056 if (!mach_creds_match(unconf, rqstp))
2057 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002058 cs_slot = &unconf->cl_cs_slot;
2059 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002060 if (status) {
2061 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002062 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002063 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002064 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002065 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002066 if (old) {
2067 status = mark_client_expired(old);
2068 if (status)
2069 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002070 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002071 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002072 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002073 conf = unconf;
2074 } else {
2075 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002076 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002077 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002078 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002079 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002080 * We do not support RDMA or persistent sessions
2081 */
2082 cr_ses->flags &= ~SESSION4_PERSIST;
2083 cr_ses->flags &= ~SESSION4_RDMA;
2084
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002085 init_session(rqstp, new, conf, cr_ses);
2086 nfsd4_init_conn(rqstp, conn, new);
2087
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002088 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002089 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002090 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002091 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002092
Andy Adamson49557cc2009-07-23 19:02:16 -04002093 /* cache solo and embedded create sessions under the state lock */
2094 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002095 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002096 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002097out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002098 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002099 free_conn(conn);
2100out_free_session:
2101 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002102out_release_drc_mem:
2103 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002104 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002105}
2106
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002107static __be32 nfsd4_map_bcts_dir(u32 *dir)
2108{
2109 switch (*dir) {
2110 case NFS4_CDFC4_FORE:
2111 case NFS4_CDFC4_BACK:
2112 return nfs_ok;
2113 case NFS4_CDFC4_FORE_OR_BOTH:
2114 case NFS4_CDFC4_BACK_OR_BOTH:
2115 *dir = NFS4_CDFC4_BOTH;
2116 return nfs_ok;
2117 };
2118 return nfserr_inval;
2119}
2120
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002121__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2122{
2123 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002124 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002125 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002126
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002127 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2128 if (status)
2129 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002130 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002131 session->se_cb_prog = bc->bc_cb_program;
2132 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002133 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002134
2135 nfsd4_probe_callback(session->se_client);
2136
2137 return nfs_ok;
2138}
2139
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002140__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2141 struct nfsd4_compound_state *cstate,
2142 struct nfsd4_bind_conn_to_session *bcts)
2143{
2144 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002145 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002146 struct nfsd4_session *session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002147 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002148
2149 if (!nfsd4_last_compound_op(rqstp))
2150 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002151 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002152 spin_lock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002153 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002154 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002155 status = nfserr_badsession;
2156 if (!session)
2157 goto out;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002158 status = nfserr_wrong_cred;
2159 if (!mach_creds_match(session->se_client, rqstp))
2160 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002161 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002162 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002163 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002164 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002165 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002166 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002167 goto out;
2168 nfsd4_init_conn(rqstp, conn, session);
2169 status = nfs_ok;
2170out:
2171 nfs4_unlock_state();
2172 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002173}
2174
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002175static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2176{
2177 if (!session)
2178 return 0;
2179 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2180}
2181
Andy Adamson069b6ad2009-04-03 08:27:58 +03002182__be32
2183nfsd4_destroy_session(struct svc_rqst *r,
2184 struct nfsd4_compound_state *cstate,
2185 struct nfsd4_destroy_session *sessionid)
2186{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002187 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002188 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002189 int ref_held_by_me = 0;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002190 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002191
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002192 nfs4_lock_state();
2193 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002194 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002195 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002196 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002197 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002198 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002199 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002200 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002201 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002202 status = nfserr_badsession;
2203 if (!ses)
2204 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002205 status = nfserr_wrong_cred;
2206 if (!mach_creds_match(ses->se_client, r))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002207 goto out_client_lock;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002208 nfsd4_get_session_locked(ses);
2209 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002210 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002211 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002212 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002213 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002214
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002215 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002216
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002217 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002218 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002219out_put_session:
2220 nfsd4_put_session(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002221out_client_lock:
2222 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002223out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002224 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002225 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002226}
2227
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002228static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002229{
2230 struct nfsd4_conn *c;
2231
2232 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002233 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002234 return c;
2235 }
2236 }
2237 return NULL;
2238}
2239
J. Bruce Fields57266a62013-04-13 14:27:29 -04002240static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002241{
2242 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002243 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002244 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002245 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002246
2247 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002248 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002249 if (c)
2250 goto out_free;
2251 status = nfserr_conn_not_bound_to_session;
2252 if (clp->cl_mach_cred)
2253 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002254 __nfsd4_hash_conn(new, ses);
2255 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002256 ret = nfsd4_register_conn(new);
2257 if (ret)
2258 /* oops; xprt is already down: */
2259 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002260 return nfs_ok;
2261out_free:
2262 spin_unlock(&clp->cl_lock);
2263 free_conn(new);
2264 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002265}
2266
Mi Jinlong868b89c2011-04-27 09:09:58 +08002267static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2268{
2269 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2270
2271 return args->opcnt > session->se_fchannel.maxops;
2272}
2273
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002274static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2275 struct nfsd4_session *session)
2276{
2277 struct xdr_buf *xb = &rqstp->rq_arg;
2278
2279 return xb->len > session->se_fchannel.maxreq_sz;
2280}
2281
Andy Adamson069b6ad2009-04-03 08:27:58 +03002282__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002283nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002284 struct nfsd4_compound_state *cstate,
2285 struct nfsd4_sequence *seq)
2286{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002287 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002288 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002289 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002290 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002291 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002292 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002293 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002294 int buflen;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002295 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002296
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002297 if (resp->opcnt != 1)
2298 return nfserr_sequence_pos;
2299
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002300 /*
2301 * Will be either used or freed by nfsd4_sequence_check_conn
2302 * below.
2303 */
2304 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2305 if (!conn)
2306 return nfserr_jukebox;
2307
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002308 spin_lock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002309 status = nfserr_badsession;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002310 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
Benny Halevyb85d4c02009-04-03 08:28:08 +03002311 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002312 goto out_no_session;
2313 clp = session->se_client;
2314 status = get_client_locked(clp);
2315 if (status)
2316 goto out_no_session;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002317 status = nfsd4_get_session_locked(session);
2318 if (status)
2319 goto out_put_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002320
Mi Jinlong868b89c2011-04-27 09:09:58 +08002321 status = nfserr_too_many_ops;
2322 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002323 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002324
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002325 status = nfserr_req_too_big;
2326 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002327 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002328
Benny Halevyb85d4c02009-04-03 08:28:08 +03002329 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002330 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002331 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002332
Andy Adamson557ce262009-08-28 08:45:04 -04002333 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002334 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2335
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002336 /* We do not negotiate the number of slots yet, so set the
2337 * maxslots to the session maxreqs which is used to encode
2338 * sr_highest_slotid and the sr_target_slot id to maxslots */
2339 seq->maxslots = session->se_fchannel.maxreqs;
2340
J. Bruce Fields73e79482012-02-13 16:39:00 -05002341 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2342 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002343 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002344 status = nfserr_seq_misordered;
2345 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002346 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002347 cstate->slot = slot;
2348 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002349 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002350 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002351 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002352 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002353 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002354 }
2355 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002356 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002357
J. Bruce Fields57266a62013-04-13 14:27:29 -04002358 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002359 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002360 if (status)
2361 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002362
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002363 buflen = (seq->cachethis) ?
2364 session->se_fchannel.maxresp_cached :
2365 session->se_fchannel.maxresp_sz;
2366 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2367 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002368 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002369 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002370 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002371
2372 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002373 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002374 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002375 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002376 if (seq->cachethis)
2377 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002378 else
2379 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002380
2381 cstate->slot = slot;
2382 cstate->session = session;
2383
Benny Halevyb85d4c02009-04-03 08:28:08 +03002384out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002385 switch (clp->cl_cb_state) {
2386 case NFSD4_CB_DOWN:
2387 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2388 break;
2389 case NFSD4_CB_FAULT:
2390 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2391 break;
2392 default:
2393 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002394 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002395 if (!list_empty(&clp->cl_revoked))
2396 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002397out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002398 if (conn)
2399 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002400 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002401 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002402out_put_session:
2403 nfsd4_put_session(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002404out_put_client:
2405 put_client_renew_locked(clp);
2406 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002407}
2408
Trond Myklebustb6076642014-06-30 11:48:35 -04002409void
2410nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2411{
2412 struct nfsd4_compound_state *cs = &resp->cstate;
2413
2414 if (nfsd4_has_session(cs)) {
2415 struct nfs4_client *clp = cs->session->se_client;
2416 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2417
2418 if (cs->status != nfserr_replay_cache) {
2419 nfsd4_store_cache_entry(resp);
2420 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2421 }
2422 /* Renew the clientid on success and on replay */
2423 spin_lock(&nn->client_lock);
2424 nfsd4_put_session(cs->session);
2425 put_client_renew_locked(clp);
2426 spin_unlock(&nn->client_lock);
2427 }
2428}
2429
Mi Jinlong345c2842011-10-20 17:51:39 +08002430__be32
2431nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2432{
2433 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002434 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002435 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002436
2437 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002438 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002439 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002440 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002441
2442 if (conf) {
2443 clp = conf;
2444
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002445 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002446 status = nfserr_clientid_busy;
2447 goto out;
2448 }
2449 } else if (unconf)
2450 clp = unconf;
2451 else {
2452 status = nfserr_stale_clientid;
2453 goto out;
2454 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002455 if (!mach_creds_match(clp, rqstp)) {
2456 status = nfserr_wrong_cred;
2457 goto out;
2458 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002459 expire_client(clp);
2460out:
2461 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002462 return status;
2463}
2464
Andy Adamson069b6ad2009-04-03 08:27:58 +03002465__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002466nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2467{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002468 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002469
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002470 if (rc->rca_one_fs) {
2471 if (!cstate->current_fh.fh_dentry)
2472 return nfserr_nofilehandle;
2473 /*
2474 * We don't take advantage of the rca_one_fs case.
2475 * That's OK, it's optional, we can safely ignore it.
2476 */
2477 return nfs_ok;
2478 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002479
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002480 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002481 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002482 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2483 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002484 goto out;
2485
2486 status = nfserr_stale_clientid;
2487 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002488 /*
2489 * The following error isn't really legal.
2490 * But we only get here if the client just explicitly
2491 * destroyed the client. Surely it no longer cares what
2492 * error it gets back on an operation for the dead
2493 * client.
2494 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002495 goto out;
2496
2497 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002498 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002499out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002500 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002501 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002502}
2503
2504__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002505nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2506 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002508 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002510 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002511 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002512 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2513
J. Bruce Fields63db4632012-05-18 21:54:19 -04002514 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002516 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002517 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002518 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002520 if (clp_used_exchangeid(conf))
2521 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002522 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002523 char addr_str[INET6_ADDRSTRLEN];
2524 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2525 sizeof(addr_str));
2526 dprintk("NFSD: setclientid: string in use by client "
2527 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 goto out;
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002531 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002532 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002535 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002536 if (new == NULL)
2537 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002538 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002539 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002541 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002542 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002543 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002544 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002545 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2547 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2548 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2549 status = nfs_ok;
2550out:
2551 nfs4_unlock_state();
2552 return status;
2553}
2554
2555
Al Virob37ad282006-10-19 23:28:59 -07002556__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002557nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2558 struct nfsd4_compound_state *cstate,
2559 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
NeilBrown21ab45a2005-06-23 22:04:14 -07002561 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2563 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002564 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002565 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002567 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002570
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002571 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002572 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002573 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002574 * We try hard to give out unique clientid's, so if we get an
2575 * attempt to confirm the same clientid with a different cred,
2576 * there's a bug somewhere. Let's charitably assume it's our
2577 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002578 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002579 status = nfserr_serverfault;
2580 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2581 goto out;
2582 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2583 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002584 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002585 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2586 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002588 else /* case 4: client hasn't noticed we rebooted yet? */
2589 status = nfserr_stale_clientid;
2590 goto out;
2591 }
2592 status = nfs_ok;
2593 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002594 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2595 nfsd4_probe_callback(conf);
2596 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002597 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002598 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002599 if (conf) {
2600 status = mark_client_expired(conf);
2601 if (status)
2602 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002603 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002604 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002605 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002606 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 nfs4_unlock_state();
2610 return status;
2611}
2612
J. Bruce Fields32513b42011-10-13 16:00:16 -04002613static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002615 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2616}
2617
2618/* OPEN Share state helper functions */
2619static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2620{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 unsigned int hashval = file_hashval(ino);
2622
Trond Myklebust950e0112014-06-30 11:48:31 -04002623 lockdep_assert_held(&state_lock);
2624
J. Bruce Fields32513b42011-10-13 16:00:16 -04002625 atomic_set(&fp->fi_ref, 1);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002626 INIT_LIST_HEAD(&fp->fi_stateids);
2627 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebust950e0112014-06-30 11:48:31 -04002628 ihold(ino);
2629 fp->fi_inode = ino;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002630 fp->fi_had_conflict = false;
2631 fp->fi_lease = NULL;
2632 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2633 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002634 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002637void
NeilBrowne60d4392005-06-23 22:03:01 -07002638nfsd4_free_slabs(void)
2639{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002640 kmem_cache_destroy(openowner_slab);
2641 kmem_cache_destroy(lockowner_slab);
2642 kmem_cache_destroy(file_slab);
2643 kmem_cache_destroy(stateid_slab);
2644 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Bryan Schumaker72083392011-11-01 15:24:59 -04002647int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648nfsd4_init_slabs(void)
2649{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002650 openowner_slab = kmem_cache_create("nfsd4_openowners",
2651 sizeof(struct nfs4_openowner), 0, 0, NULL);
2652 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002653 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002654 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002655 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002656 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002657 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002658 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002659 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002660 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002661 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002662 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002663 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002664 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002665 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002666 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002667 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002668 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002669 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002671
2672out_free_stateid_slab:
2673 kmem_cache_destroy(stateid_slab);
2674out_free_file_slab:
2675 kmem_cache_destroy(file_slab);
2676out_free_lockowner_slab:
2677 kmem_cache_destroy(lockowner_slab);
2678out_free_openowner_slab:
2679 kmem_cache_destroy(openowner_slab);
2680out:
NeilBrowne60d4392005-06-23 22:03:01 -07002681 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2682 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002685static void init_nfs4_replay(struct nfs4_replay *rp)
2686{
2687 rp->rp_status = nfserr_serverfault;
2688 rp->rp_buflen = 0;
2689 rp->rp_buf = rp->rp_ibuf;
2690}
2691
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002692static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 struct nfs4_stateowner *sop;
2695
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002696 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002697 if (!sop)
2698 return NULL;
2699
2700 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2701 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002702 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002703 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002705 sop->so_owner.len = owner->len;
2706
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002707 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002708 sop->so_client = clp;
2709 init_nfs4_replay(&sop->so_replay);
2710 return sop;
2711}
2712
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002713static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002714{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002715 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2716
2717 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002718 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002721static struct nfs4_openowner *
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002722alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp,
2723 struct nfsd4_open *open,
2724 struct nfsd4_compound_state *cstate)
2725{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002726 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002728 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2729 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002731 oo->oo_owner.so_is_open_owner = 1;
2732 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002733 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002734 if (nfsd4_has_session(cstate))
2735 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002736 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002737 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002738 INIT_LIST_HEAD(&oo->oo_close_lru);
2739 hash_openowner(oo, clp, strhashval);
2740 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
J. Bruce Fields996e0932011-10-17 11:14:48 -04002743static 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 -04002744 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002746 stp->st_stid.sc_type = NFS4_OPEN_STID;
NeilBrownea1da632005-06-23 22:04:17 -07002747 INIT_LIST_HEAD(&stp->st_lockowners);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002748 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002749 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002750 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002751 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 stp->st_access_bmap = 0;
2754 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002755 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002756 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002757 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
2760static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002761move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002763 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2764
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002765 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002767 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002768 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002772same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2773 clientid_t *clid)
2774{
2775 return (sop->so_owner.len == owner->len) &&
2776 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2777 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002780static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002781find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2782 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002784 struct nfs4_stateowner *so;
2785 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002786 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002788 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002789 if (!so->so_is_open_owner)
2790 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002791 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2792 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002793 clp = oo->oo_owner.so_client;
2794 if ((bool)clp->cl_minorversion != sessions)
2795 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002796 renew_client(oo->oo_owner.so_client);
2797 return oo;
2798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
2800 return NULL;
2801}
2802
2803/* search file_hashtbl[] for file */
2804static struct nfs4_file *
Trond Myklebust950e0112014-06-30 11:48:31 -04002805find_file_locked(struct inode *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 unsigned int hashval = file_hashval(ino);
2808 struct nfs4_file *fp;
2809
Trond Myklebust950e0112014-06-30 11:48:31 -04002810 lockdep_assert_held(&state_lock);
2811
Jeff Layton89876f82013-04-02 09:01:59 -04002812 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002813 if (fp->fi_inode == ino) {
2814 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
2818 return NULL;
2819}
2820
Trond Myklebust950e0112014-06-30 11:48:31 -04002821static struct nfs4_file *
2822find_file(struct inode *ino)
2823{
2824 struct nfs4_file *fp;
2825
2826 spin_lock(&state_lock);
2827 fp = find_file_locked(ino);
2828 spin_unlock(&state_lock);
2829 return fp;
2830}
2831
2832static struct nfs4_file *
2833find_or_add_file(struct inode *ino, struct nfs4_file *new)
2834{
2835 struct nfs4_file *fp;
2836
2837 spin_lock(&state_lock);
2838 fp = find_file_locked(ino);
2839 if (fp == NULL) {
2840 nfsd4_init_file(new, ino);
2841 fp = new;
2842 }
2843 spin_unlock(&state_lock);
2844
2845 return fp;
2846}
2847
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002848/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 * Called to check deny when READ with all zero stateid or
2850 * WRITE with all zero or all one stateid
2851 */
Al Virob37ad282006-10-19 23:28:59 -07002852static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2854{
2855 struct inode *ino = current_fh->fh_dentry->d_inode;
2856 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002857 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002858 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002861 if (!fp)
2862 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002863 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002865 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002866 if (test_deny(deny_type, stp) ||
2867 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
NeilBrown13cd2182005-06-23 22:03:10 -07002870 ret = nfs_ok;
2871out:
2872 put_nfs4_file(fp);
2873 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874}
2875
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002876static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002878 struct nfs4_client *clp = dp->dl_stid.sc_client;
2879 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2880
Benny Halevycdc97502014-05-30 09:09:30 -04002881 lockdep_assert_held(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 /* We're assuming the state code never drops its reference
2883 * without first removing the lease. Since we're in this lease
2884 * callback (and since the lease code is serialized by the kernel
2885 * lock) we know the server hasn't removed the lease yet, we know
2886 * it's safe to take a reference: */
2887 atomic_inc(&dp->dl_count);
2888
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002889 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Jeff Layton1c8c6012013-06-21 08:58:15 -04002891 /* Only place dl_time is set; protected by i_lock: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 dp->dl_time = get_seconds();
2893
NeilBrown6282cd52014-06-04 17:39:26 +10002894 block_delegations(&dp->dl_fh);
2895
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002896 nfsd4_cb_recall(dp);
2897}
2898
Jeff Layton1c8c6012013-06-21 08:58:15 -04002899/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002900static void nfsd_break_deleg_cb(struct file_lock *fl)
2901{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002902 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2903 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002904
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002905 if (!fp) {
2906 WARN(1, "(%p)->fl_owner NULL\n", fl);
2907 return;
2908 }
2909 if (fp->fi_had_conflict) {
2910 WARN(1, "duplicate break on %p\n", fp);
2911 return;
2912 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002913 /*
2914 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002915 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002916 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002917 */
2918 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Benny Halevycdc97502014-05-30 09:09:30 -04002920 spin_lock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002921 fp->fi_had_conflict = true;
2922 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2923 nfsd_break_one_deleg(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04002924 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927static
2928int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2929{
2930 if (arg & F_UNLCK)
2931 return lease_modify(onlist, arg);
2932 else
2933 return -EAGAIN;
2934}
2935
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002936static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002937 .lm_break = nfsd_break_deleg_cb,
2938 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939};
2940
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002941static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2942{
2943 if (nfsd4_has_session(cstate))
2944 return nfs_ok;
2945 if (seqid == so->so_seqid - 1)
2946 return nfserr_replay_me;
2947 if (seqid == so->so_seqid)
2948 return nfs_ok;
2949 return nfserr_bad_seqid;
2950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Al Virob37ad282006-10-19 23:28:59 -07002952__be32
Andy Adamson66689582009-04-03 08:28:45 +03002953nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03002954 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 clientid_t *clientid = &open->op_clientid;
2957 struct nfs4_client *clp = NULL;
2958 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002959 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002960 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002962 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002964 /*
2965 * In case we need it later, after we've already created the
2966 * file and don't want to risk a further failure:
2967 */
2968 open->op_file = nfsd4_alloc_file();
2969 if (open->op_file == NULL)
2970 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002972 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002973 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002974 open->op_openowner = oo;
2975 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002976 clp = find_confirmed_client(clientid, cstate->minorversion,
2977 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002979 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002980 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002982 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002983 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002984 clp = oo->oo_owner.so_client;
2985 release_openowner(oo);
2986 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002987 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002988 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002989 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2990 if (status)
2991 return status;
2992 clp = oo->oo_owner.so_client;
2993 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002994new_owner:
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002995 oo = alloc_init_open_stateowner(strhashval, clp, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002996 if (oo == NULL)
2997 return nfserr_jukebox;
2998 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002999alloc_stateid:
3000 open->op_stp = nfs4_alloc_stateid(clp);
3001 if (!open->op_stp)
3002 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003003 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
Al Virob37ad282006-10-19 23:28:59 -07003006static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003007nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3008{
3009 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3010 return nfserr_openmode;
3011 else
3012 return nfs_ok;
3013}
3014
Daniel Mackc47d8322011-05-16 16:38:14 +02003015static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003016{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003017 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3018}
3019
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003020static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003021{
3022 struct nfs4_stid *ret;
3023
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003024 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003025 if (!ret)
3026 return NULL;
3027 return delegstateid(ret);
3028}
3029
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003030static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3031{
3032 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3033 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3034}
3035
Al Virob37ad282006-10-19 23:28:59 -07003036static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003037nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003038 struct nfs4_delegation **dp)
3039{
3040 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003041 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003042
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003043 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003044 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003045 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003046 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003047 status = nfs4_check_delegmode(*dp, flags);
3048 if (status)
3049 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003050out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003051 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003052 return nfs_ok;
3053 if (status)
3054 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003055 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003056 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003057}
3058
Al Virob37ad282006-10-19 23:28:59 -07003059static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003060nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003062 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003063 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
NeilBrown8beefa22005-06-23 22:03:08 -07003065 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 /* ignore lock owners */
3067 if (local->st_stateowner->so_is_open_owner == 0)
3068 continue;
3069 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003070 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 *stpp = local;
3072 /* check for conflicting share reservations */
3073 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003074 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003076 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003079static inline int nfs4_access_to_access(u32 nfs4_access)
3080{
3081 int flags = 0;
3082
3083 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3084 flags |= NFSD_MAY_READ;
3085 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3086 flags |= NFSD_MAY_WRITE;
3087 return flags;
3088}
3089
Al Virob37ad282006-10-19 23:28:59 -07003090static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3092 struct nfsd4_open *open)
3093{
3094 struct iattr iattr = {
3095 .ia_valid = ATTR_SIZE,
3096 .ia_size = 0,
3097 };
3098 if (!open->op_truncate)
3099 return 0;
3100 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003101 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3103}
3104
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003105static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3106 struct svc_fh *cur_fh, struct nfsd4_open *open)
3107{
3108 __be32 status;
3109 int oflag = nfs4_access_to_omode(open->op_share_access);
3110 int access = nfs4_access_to_access(open->op_share_access);
3111
3112 if (!fp->fi_fds[oflag]) {
3113 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
3114 &fp->fi_fds[oflag]);
3115 if (status)
3116 goto out;
3117 }
3118 nfs4_file_get_access(fp, oflag);
3119
3120 status = nfsd4_truncate(rqstp, cur_fh, open);
3121 if (status)
3122 goto out_put_access;
3123
3124 return nfs_ok;
3125
3126out_put_access:
3127 nfs4_file_put_access(fp, oflag);
3128out:
3129 return status;
3130}
3131
Al Virob37ad282006-10-19 23:28:59 -07003132static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003133nfs4_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 -07003134{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04003135 u32 op_share_access = open->op_share_access;
Al Virob37ad282006-10-19 23:28:59 -07003136 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003138 if (!test_access(op_share_access, stp))
Casey Bodley0c12eaf2011-07-23 14:58:10 -04003139 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003140 else
3141 status = nfsd4_truncate(rqstp, cur_fh, open);
3142
3143 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return status;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04003147 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04003148 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return nfs_ok;
3150}
3151
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003154nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003156 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157}
3158
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003159/* Should we give out recallable state?: */
3160static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3161{
3162 if (clp->cl_cb_state == NFSD4_CB_UP)
3163 return true;
3164 /*
3165 * In the sessions case, since we don't have to establish a
3166 * separate connection for callbacks, we assume it's OK
3167 * until we hear otherwise:
3168 */
3169 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3170}
3171
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003172static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
3173{
3174 struct file_lock *fl;
3175
3176 fl = locks_alloc_lock();
3177 if (!fl)
3178 return NULL;
3179 locks_init_lock(fl);
3180 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003181 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003182 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3183 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003184 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003185 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003186 return fl;
3187}
3188
J. Bruce Fields99c41512013-05-21 16:21:25 -04003189static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003190{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003191 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003192 struct file_lock *fl;
3193 int status;
3194
J. Bruce Fields99c41512013-05-21 16:21:25 -04003195 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003196 if (!fl)
3197 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003198 fl->fl_file = find_readable_file(fp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003199 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003200 if (status)
3201 goto out_free;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003202 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04003203 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003204 atomic_set(&fp->fi_delegees, 1);
Benny Halevycdc97502014-05-30 09:09:30 -04003205 spin_lock(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04003206 hash_delegation_locked(dp, fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003207 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003208 return 0;
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003209out_free:
3210 locks_free_lock(fl);
3211 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003212}
3213
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003214static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003215{
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003216 if (fp->fi_had_conflict)
3217 return -EAGAIN;
3218 get_nfs4_file(fp);
3219 dp->dl_file = fp;
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003220 if (!fp->fi_lease)
3221 return nfs4_setlease(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04003222 spin_lock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003223 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003224 if (fp->fi_had_conflict) {
Benny Halevycdc97502014-05-30 09:09:30 -04003225 spin_unlock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003226 return -EAGAIN;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003227 }
Benny Halevy931ee562014-05-30 09:09:27 -04003228 hash_delegation_locked(dp, fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003229 spin_unlock(&state_lock);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003230 return 0;
3231}
3232
Benny Halevy4aa89132012-02-21 14:16:44 -08003233static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3234{
3235 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3236 if (status == -EAGAIN)
3237 open->op_why_no_deleg = WND4_CONTENTION;
3238 else {
3239 open->op_why_no_deleg = WND4_RESOURCE;
3240 switch (open->op_deleg_want) {
3241 case NFS4_SHARE_WANT_READ_DELEG:
3242 case NFS4_SHARE_WANT_WRITE_DELEG:
3243 case NFS4_SHARE_WANT_ANY_DELEG:
3244 break;
3245 case NFS4_SHARE_WANT_CANCEL:
3246 open->op_why_no_deleg = WND4_CANCELLED;
3247 break;
3248 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003249 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003250 }
3251 }
3252}
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254/*
3255 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003256 *
3257 * Note we don't support write delegations, and won't until the vfs has
3258 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 */
3260static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003261nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3262 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
3264 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003265 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003266 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003267 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003269 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003270 open->op_recall = 0;
3271 switch (open->op_claim_type) {
3272 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003273 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003274 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003275 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3276 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003277 break;
3278 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003279 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003280 /*
3281 * Let's not give out any delegations till everyone's
3282 * had the chance to reclaim theirs....
3283 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003284 if (locks_in_grace(net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003285 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003286 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003287 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003288 /*
3289 * Also, if the file was opened for write or
3290 * create, there's a good chance the client's
3291 * about to write to it, resulting in an
3292 * immediate recall (since we don't support
3293 * write delegations):
3294 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003295 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003296 goto out_no_deleg;
3297 if (open->op_create == NFS4_OPEN_CREATE)
3298 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003299 break;
3300 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003301 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003302 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003303 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003304 if (dp == NULL)
3305 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003306 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003307 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003308 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003310 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003312 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003313 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003314 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003315 return;
3316out_free:
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003317 destroy_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003318out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003319 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3320 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003321 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003322 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003323 open->op_recall = 1;
3324 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003325
3326 /* 4.1 client asking for a delegation? */
3327 if (open->op_deleg_want)
3328 nfsd4_open_deleg_none_ext(open, status);
3329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
Benny Halevye27f49c2012-02-21 14:16:54 -08003332static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3333 struct nfs4_delegation *dp)
3334{
3335 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3336 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3337 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3338 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3339 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3340 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3341 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3342 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3343 }
3344 /* Otherwise the client must be confused wanting a delegation
3345 * it already has, therefore we don't return
3346 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3347 */
3348}
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350/*
3351 * called with nfs4_lock_state() held.
3352 */
Al Virob37ad282006-10-19 23:28:59 -07003353__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3355{
Andy Adamson66689582009-04-03 08:28:45 +03003356 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003357 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 struct nfs4_file *fp = NULL;
3359 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003360 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003361 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003362 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /*
3365 * Lookup file; if found, lookup stateid and check open request,
3366 * and check for delegations in the process of being recalled.
3367 * If not found, create the nfs4_file struct
3368 */
Trond Myklebust950e0112014-06-30 11:48:31 -04003369 fp = find_or_add_file(ino, open->op_file);
3370 if (fp != open->op_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if ((status = nfs4_check_open(fp, open, &stp)))
3372 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003373 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003374 if (status)
3375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003377 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003378 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003379 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003380 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003381 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 }
3383
3384 /*
3385 * OPEN the file, or upgrade an existing OPEN.
3386 * If truncate fails, the OPEN fails.
3387 */
3388 if (stp) {
3389 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003390 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 if (status)
3392 goto out;
3393 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003394 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003395 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003397 stp = open->op_stp;
3398 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003399 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003401 update_stateid(&stp->st_stid.sc_stateid);
3402 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Benny Halevyd24433c2012-02-16 20:57:17 +02003404 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003405 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3406 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3407 open->op_why_no_deleg = WND4_NOT_WANTED;
3408 goto nodeleg;
3409 }
3410 }
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 /*
3413 * Attempt to hand out a delegation. No error return, because the
3414 * OPEN succeeds even if we fail.
3415 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003416 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003417nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 status = nfs_ok;
3419
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003420 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003421 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003423 /* 4.1 client trying to upgrade/downgrade delegation? */
3424 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003425 open->op_deleg_want)
3426 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003427
NeilBrown13cd2182005-06-23 22:03:10 -07003428 if (fp)
3429 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003430 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003431 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 /*
3433 * To finish the open response, we just need to set the rflags.
3434 */
3435 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003436 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003437 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3439
3440 return status;
3441}
3442
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003443void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3444{
3445 if (open->op_openowner) {
3446 struct nfs4_openowner *oo = open->op_openowner;
3447
3448 if (!list_empty(&oo->oo_owner.so_stateids))
3449 list_del_init(&oo->oo_close_lru);
3450 if (oo->oo_flags & NFS4_OO_NEW) {
3451 if (status) {
3452 release_openowner(oo);
3453 open->op_openowner = NULL;
3454 } else
3455 oo->oo_flags &= ~NFS4_OO_NEW;
3456 }
3457 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003458 if (open->op_file)
3459 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003460 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003461 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003462}
3463
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003464static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3465{
3466 struct nfs4_client *found;
3467
3468 if (STALE_CLIENTID(clid, nn))
3469 return nfserr_stale_clientid;
3470 found = find_confirmed_client(clid, session, nn);
3471 if (clp)
3472 *clp = found;
3473 return found ? nfs_ok : nfserr_expired;
3474}
3475
Al Virob37ad282006-10-19 23:28:59 -07003476__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003477nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3478 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
3480 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003481 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003482 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
3484 nfs4_lock_state();
3485 dprintk("process_renew(%08x/%08x): starting\n",
3486 clid->cl_boot, clid->cl_id);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003487 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3488 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003491 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003492 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 goto out;
3494 status = nfs_ok;
3495out:
3496 nfs4_unlock_state();
3497 return status;
3498}
3499
NeilBrowna76b4312005-06-23 22:04:01 -07003500static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003501nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003502{
Jeff Layton33dcc482012-04-10 11:08:48 -04003503 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003504 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003505 return;
3506
NeilBrowna76b4312005-06-23 22:04:01 -07003507 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003508 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003509 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003510 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003511 /*
3512 * Now that every NFSv4 client has had the chance to recover and
3513 * to see the (possibly new, possibly shorter) lease time, we
3514 * can safely set the next grace time to the current lease time:
3515 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003516 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003517}
3518
NeilBrownfd39ca92005-06-23 22:04:03 -07003519static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003520nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
3522 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003523 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 struct nfs4_delegation *dp;
3525 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003526 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003527 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 nfs4_lock_state();
3530
3531 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003532 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003533 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003534 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003535 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 clp = list_entry(pos, struct nfs4_client, cl_lru);
3537 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3538 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003539 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 break;
3541 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003542 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003543 dprintk("NFSD: client in use (clientid %08x)\n",
3544 clp->cl_clientid.cl_id);
3545 continue;
3546 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003547 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003548 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003549 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003550 list_for_each_safe(pos, next, &reaplist) {
3551 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 dprintk("NFSD: purging unused client (clientid %08x)\n",
3553 clp->cl_clientid.cl_id);
3554 expire_client(clp);
3555 }
Benny Halevycdc97502014-05-30 09:09:30 -04003556 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003557 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003559 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3560 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003562 t = dp->dl_time - cutoff;
3563 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 break;
3565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 list_move(&dp->dl_recall_lru, &reaplist);
3567 }
Benny Halevycdc97502014-05-30 09:09:30 -04003568 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 list_for_each_safe(pos, next, &reaplist) {
3570 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003571 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003573 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003574 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3575 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003576 t = oo->oo_time - cutoff;
3577 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 break;
3579 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003580 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003582 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003584 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
3586
Harvey Harrisona254b242008-02-20 12:49:00 -08003587static struct workqueue_struct *laundry_wq;
3588static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003589
3590static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003591laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003594 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3595 work);
3596 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3597 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003599 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003601 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003604static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003605{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003606 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3607 return nfserr_bad_stateid;
3608 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003612access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003614 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3615 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3616 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
3619static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003620access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003622 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3623 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
3626static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003627__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
Al Virob37ad282006-10-19 23:28:59 -07003629 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
J. Bruce Fields02921912010-07-29 15:16:59 -04003631 /* For lock stateid's, we test the parent open, not the lock: */
3632 if (stp->st_openstp)
3633 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003634 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003636 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 goto out;
3638 status = nfs_ok;
3639out:
3640 return status;
3641}
3642
Al Virob37ad282006-10-19 23:28:59 -07003643static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003644check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003646 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003648 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003649 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 * conflicting state; so we must wait out the grace period. */
3651 return nfserr_grace;
3652 } else if (flags & WR_STATE)
3653 return nfs4_share_conflict(current_fh,
3654 NFS4_SHARE_DENY_WRITE);
3655 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3656 return nfs4_share_conflict(current_fh,
3657 NFS4_SHARE_DENY_READ);
3658}
3659
3660/*
3661 * Allow READ/WRITE during grace period on recovered state only for files
3662 * that are not able to provide mandatory locking.
3663 */
3664static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003665grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003667 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
J. Bruce Fields81b82962011-08-23 11:03:29 -04003670/* Returns true iff a is later than b: */
3671static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3672{
Jim Rees1a9357f2013-05-17 17:33:00 -04003673 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04003674}
3675
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003676static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003677{
Andy Adamson66689582009-04-03 08:28:45 +03003678 /*
3679 * When sessions are used the stateid generation number is ignored
3680 * when it is zero.
3681 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003682 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003683 return nfs_ok;
3684
3685 if (in->si_generation == ref->si_generation)
3686 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003687
J. Bruce Fields0836f582008-01-26 19:08:12 -05003688 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003689 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003690 return nfserr_bad_stateid;
3691 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003692 * However, we could see a stateid from the past, even from a
3693 * non-buggy client. For example, if the client sends a lock
3694 * while some IO is outstanding, the lock may bump si_generation
3695 * while the IO is still in flight. The client could avoid that
3696 * situation by waiting for responses on all the IO requests,
3697 * but better performance may result in retrying IO that
3698 * receives an old_stateid error if requests are rarely
3699 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003700 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003701 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003702}
3703
Chuck Lever7df302f2012-05-29 13:56:37 -04003704static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003705{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003706 struct nfs4_stid *s;
3707 struct nfs4_ol_stateid *ols;
3708 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003709
Chuck Lever7df302f2012-05-29 13:56:37 -04003710 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3711 return nfserr_bad_stateid;
3712 /* Client debugging aid. */
3713 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3714 char addr_str[INET6_ADDRSTRLEN];
3715 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3716 sizeof(addr_str));
3717 pr_warn_ratelimited("NFSD: client %s testing state ID "
3718 "with incorrect client ID\n", addr_str);
3719 return nfserr_bad_stateid;
3720 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003721 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003722 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003723 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003724 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003725 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003726 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04003727 switch (s->sc_type) {
3728 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003729 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003730 case NFS4_REVOKED_DELEG_STID:
3731 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04003732 case NFS4_OPEN_STID:
3733 case NFS4_LOCK_STID:
3734 ols = openlockstateid(s);
3735 if (ols->st_stateowner->so_is_open_owner
3736 && !(openowner(ols->st_stateowner)->oo_flags
3737 & NFS4_OO_CONFIRMED))
3738 return nfserr_bad_stateid;
3739 return nfs_ok;
3740 default:
3741 printk("unknown stateid type %x\n", s->sc_type);
3742 case NFS4_CLOSED_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003743 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04003744 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003745}
3746
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003747static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3748 struct nfs4_stid **s, bool sessions,
3749 struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003750{
3751 struct nfs4_client *cl;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003752 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003753
3754 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3755 return nfserr_bad_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003756 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3757 nn, &cl);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003758 if (status == nfserr_stale_clientid) {
3759 if (sessions)
3760 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003761 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003762 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003763 if (status)
3764 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003765 *s = find_stateid_by_type(cl, stateid, typemask);
3766 if (!*s)
3767 return nfserr_bad_stateid;
3768 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003769}
3770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771/*
3772* Checks for stateid operations
3773*/
Al Virob37ad282006-10-19 23:28:59 -07003774__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003775nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003776 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003778 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003779 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003781 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003783 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04003784 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003785 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 if (filpp)
3788 *filpp = NULL;
3789
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003790 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return nfserr_grace;
3792
3793 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003794 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Trond Myklebust14bcab12014-04-18 14:44:07 -04003796 nfs4_lock_state();
3797
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003798 status = nfsd4_lookup_stateid(stateid,
3799 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3800 &s, cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003801 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04003802 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003803 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3804 if (status)
3805 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003806 switch (s->sc_type) {
3807 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003808 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003809 status = nfs4_check_delegmode(dp, flags);
3810 if (status)
3811 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003812 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04003813 file = dp->dl_file->fi_deleg_file;
3814 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003815 WARN_ON_ONCE(1);
3816 status = nfserr_serverfault;
3817 goto out;
3818 }
Dan Carpenter43b01782010-10-27 23:19:04 +02003819 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003820 break;
3821 case NFS4_OPEN_STID:
3822 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003823 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003824 status = nfs4_check_fh(current_fh, stp);
3825 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003827 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003828 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003830 status = nfs4_check_openmode(stp, flags);
3831 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003833 if (filpp) {
3834 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04003835 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003836 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04003837 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003838 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003839 break;
3840 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04003841 status = nfserr_bad_stateid;
3842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
3844 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04003845 if (file)
3846 *filpp = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04003848 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 return status;
3850}
3851
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003852static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003853nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003854{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04003855 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
3856
3857 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003858 return nfserr_locks_held;
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04003859 /*
3860 * Currently there's a 1-1 lock stateid<->lockowner
3861 * correspondance, and we have to delete the lockowner when we
3862 * delete the lock stateid:
3863 */
J. Bruce Fields48385402014-05-27 11:14:26 -04003864 release_lockowner(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003865 return nfs_ok;
3866}
3867
3868/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003869 * Test if the stateid is valid
3870 */
3871__be32
3872nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3873 struct nfsd4_test_stateid *test_stateid)
3874{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003875 struct nfsd4_test_stateid_id *stateid;
3876 struct nfs4_client *cl = cstate->session->se_client;
3877
3878 nfs4_lock_state();
3879 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003880 stateid->ts_id_status =
3881 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003882 nfs4_unlock_state();
3883
Bryan Schumaker17456802011-07-13 10:50:48 -04003884 return nfs_ok;
3885}
3886
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003887__be32
3888nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3889 struct nfsd4_free_stateid *free_stateid)
3890{
3891 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003892 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003893 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003894 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003895 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003896
3897 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003898 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003899 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003900 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003901 switch (s->sc_type) {
3902 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003903 ret = nfserr_locks_held;
3904 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003905 case NFS4_OPEN_STID:
3906 case NFS4_LOCK_STID:
3907 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3908 if (ret)
3909 goto out;
3910 if (s->sc_type == NFS4_LOCK_STID)
3911 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3912 else
3913 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003914 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003915 case NFS4_REVOKED_DELEG_STID:
3916 dp = delegstateid(s);
3917 destroy_revoked_delegation(dp);
3918 ret = nfs_ok;
3919 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003920 default:
3921 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003922 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003923out:
3924 nfs4_unlock_state();
3925 return ret;
3926}
3927
NeilBrown4c4cd222005-07-07 17:59:27 -07003928static inline int
3929setlkflg (int type)
3930{
3931 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3932 RD_STATE : WR_STATE;
3933}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003935static __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 -04003936{
3937 struct svc_fh *current_fh = &cstate->current_fh;
3938 struct nfs4_stateowner *sop = stp->st_stateowner;
3939 __be32 status;
3940
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003941 status = nfsd4_check_seqid(cstate, sop, seqid);
3942 if (status)
3943 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003944 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
3945 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003946 /*
3947 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003948 * nfserr_replay_me from the previous step, and
3949 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003950 */
3951 return nfserr_bad_stateid;
3952 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3953 if (status)
3954 return status;
3955 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003956}
3957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958/*
3959 * Checks for sequence id mutating operations.
3960 */
Al Virob37ad282006-10-19 23:28:59 -07003961static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003962nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003963 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003964 struct nfs4_ol_stateid **stpp,
3965 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003967 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003968 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04003969 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003971 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3972 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 *stpp = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003975 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3976 cstate->minorversion, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003977 if (status)
3978 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04003979 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003980 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04003981 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Trond Myklebuste17f99b2014-06-30 11:48:34 -04003983 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
3984 if (!status)
3985 *stpp = stp;
3986 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003987}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003988
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003989static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3990 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003991{
3992 __be32 status;
3993 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003995 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003996 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003997 if (status)
3998 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003999 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004000 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004001 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004002 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
Al Virob37ad282006-10-19 23:28:59 -07004005__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004006nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004007 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
Al Virob37ad282006-10-19 23:28:59 -07004009 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004010 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004011 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004012 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Al Viroa6a9f182013-09-16 10:57:01 -04004014 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4015 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004017 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004018 if (status)
4019 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 nfs4_lock_state();
4022
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004023 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004024 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004025 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004026 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004027 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004028 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004029 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004030 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004031 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004032 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004033 update_stateid(&stp->st_stid.sc_stateid);
4034 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004035 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004036 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004037
Jeff Layton2a4317c2012-03-21 16:42:43 -04004038 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004039 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004041 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004042 if (!cstate->replay_owner)
4043 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 return status;
4045}
4046
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004047static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004049 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004050 return;
4051 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04004052 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004053}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004054
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004055static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4056{
4057 switch (to_access) {
4058 case NFS4_SHARE_ACCESS_READ:
4059 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4060 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4061 break;
4062 case NFS4_SHARE_ACCESS_WRITE:
4063 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4064 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4065 break;
4066 case NFS4_SHARE_ACCESS_BOTH:
4067 break;
4068 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004069 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 }
4071}
4072
4073static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04004074reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075{
4076 int i;
4077 for (i = 0; i < 4; i++) {
4078 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04004079 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
4081}
4082
Al Virob37ad282006-10-19 23:28:59 -07004083__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004084nfsd4_open_downgrade(struct svc_rqst *rqstp,
4085 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004086 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087{
Al Virob37ad282006-10-19 23:28:59 -07004088 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004089 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004090 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Al Viroa6a9f182013-09-16 10:57:01 -04004092 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4093 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004095 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004096 if (od->od_deleg_want)
4097 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4098 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004101 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004102 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004103 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004106 if (!test_access(od->od_share_access, stp)) {
4107 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 stp->st_access_bmap, od->od_share_access);
4109 goto out;
4110 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004111 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
4113 stp->st_deny_bmap, od->od_share_deny);
4114 goto out;
4115 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004116 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Jeff Laytonce0fc432012-05-11 09:45:14 -04004118 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004120 update_stateid(&stp->st_stid.sc_stateid);
4121 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 status = nfs_ok;
4123out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004124 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004125 if (!cstate->replay_owner)
4126 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return status;
4128}
4129
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004130static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4131{
4132 unhash_open_stateid(s);
4133 s->st_stid.sc_type = NFS4_CLOSED_STID;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004134}
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136/*
4137 * nfs4_unlock_state() called after encode
4138 */
Al Virob37ad282006-10-19 23:28:59 -07004139__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004140nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004141 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
Al Virob37ad282006-10-19 23:28:59 -07004143 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004144 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004145 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004146 struct net *net = SVC_NET(rqstp);
4147 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Al Viroa6a9f182013-09-16 10:57:01 -04004149 dprintk("NFSD: nfsd4_close on file %pd\n",
4150 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
4152 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004153 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4154 &close->cl_stateid,
4155 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004156 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004157 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004158 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004160 oo = openowner(stp->st_stateowner);
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004161 update_stateid(&stp->st_stid.sc_stateid);
4162 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004164 nfsd4_close_open_stateid(stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004165
Benny Halevy01a87d92013-10-14 09:01:55 +03004166 if (cstate->minorversion)
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004167 free_generic_stateid(stp);
Benny Halevy01a87d92013-10-14 09:01:55 +03004168 else
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004169 oo->oo_last_closed_stid = stp;
J. Bruce Fields04ef5952006-01-18 17:43:21 -08004170
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04004171 if (list_empty(&oo->oo_owner.so_stateids)) {
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004172 if (cstate->minorversion)
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04004173 release_openowner(oo);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04004174 else {
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04004175 /*
4176 * In the 4.0 case we need to keep the owners around a
4177 * little while to handle CLOSE replay.
4178 */
fanchaoting53584f62013-04-11 21:24:13 +08004179 move_to_close_lru(oo, SVC_NET(rqstp));
J. Bruce Fields74dbafa2012-06-06 12:53:48 -04004180 }
4181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004183 if (!cstate->replay_owner)
4184 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 return status;
4186}
4187
Al Virob37ad282006-10-19 23:28:59 -07004188__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004189nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4190 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004192 struct nfs4_delegation *dp;
4193 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004194 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004195 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004196 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004198 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004199 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
4201 nfs4_lock_state();
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004202 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
4203 cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004204 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004205 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004206 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004207 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004208 if (status)
4209 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004210
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004211 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004213 nfs4_unlock_state();
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return status;
4216}
4217
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
J. Bruce Fields009673b2011-11-07 17:40:10 -05004221#define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Benny Halevy87df4de2008-12-15 19:42:03 +02004223static inline u64
4224end_offset(u64 start, u64 len)
4225{
4226 u64 end;
4227
4228 end = start + len;
4229 return end >= start ? end: NFS4_MAX_UINT64;
4230}
4231
4232/* last octet in a range */
4233static inline u64
4234last_byte_offset(u64 start, u64 len)
4235{
4236 u64 end;
4237
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004238 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004239 end = start + len;
4240 return end > start ? end - 1: NFS4_MAX_UINT64;
4241}
4242
J. Bruce Fields009673b2011-11-07 17:40:10 -05004243static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
4245 return (file_hashval(inode) + cl_id
4246 + opaque_hashval(ownername->data, ownername->len))
J. Bruce Fields009673b2011-11-07 17:40:10 -05004247 & LOCKOWNER_INO_HASH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250/*
4251 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4252 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4253 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4254 * locking, this prevents us from being completely protocol-compliant. The
4255 * real solution to this problem is to start using unsigned file offsets in
4256 * the VFS, but this is a very deep change!
4257 */
4258static inline void
4259nfs4_transform_lock_offset(struct file_lock *lock)
4260{
4261 if (lock->fl_start < 0)
4262 lock->fl_start = OFFSET_MAX;
4263 if (lock->fl_end < 0)
4264 lock->fl_end = OFFSET_MAX;
4265}
4266
NeilBrownd5b90262006-04-10 22:55:22 -07004267/* Hack!: For now, we're defining this just so we can use a pointer to it
4268 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004269static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004270};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272static inline void
4273nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4274{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004275 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
NeilBrownd5b90262006-04-10 22:55:22 -07004277 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004278 lo = (struct nfs4_lockowner *) fl->fl_owner;
4279 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4280 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004281 if (!deny->ld_owner.data)
4282 /* We just don't care that much */
4283 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004284 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4285 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004286 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004287nevermind:
4288 deny->ld_owner.len = 0;
4289 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004290 deny->ld_clientid.cl_boot = 0;
4291 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 }
4293 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004294 deny->ld_length = NFS4_MAX_UINT64;
4295 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4297 deny->ld_type = NFS4_READ_LT;
4298 if (fl->fl_type != F_RDLCK)
4299 deny->ld_type = NFS4_WRITE_LT;
4300}
4301
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004302static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
4303{
4304 struct nfs4_ol_stateid *lst;
4305
4306 if (!same_owner_str(&lo->lo_owner, owner, clid))
4307 return false;
J. Bruce Fields27b11422014-05-08 11:19:41 -04004308 if (list_empty(&lo->lo_owner.so_stateids)) {
4309 WARN_ON_ONCE(1);
4310 return false;
4311 }
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004312 lst = list_first_entry(&lo->lo_owner.so_stateids,
4313 struct nfs4_ol_stateid, st_perstateowner);
4314 return lst->st_file->fi_inode == inode;
4315}
4316
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004317static struct nfs4_lockowner *
4318find_lockowner_str(struct inode *inode, clientid_t *clid,
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004319 struct xdr_netobj *owner, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004321 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004322 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004324 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
J. Bruce Fieldsb93d87c2011-11-07 16:37:57 -05004325 if (same_lockowner_ino(lo, inode, clid, owner))
4326 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
4328 return NULL;
4329}
4330
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004331static 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 -04004332{
J. Bruce Fields009673b2011-11-07 17:40:10 -05004333 struct inode *inode = open_stp->st_file->fi_inode;
4334 unsigned int inohash = lockowner_ino_hashval(inode,
4335 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004336 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields009673b2011-11-07 17:40:10 -05004337
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004338 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004339 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004340 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004341}
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343/*
4344 * Alloc a lock owner structure.
4345 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004346 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004348 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 */
4350
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004351static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004352alloc_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 -04004353 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004355 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4356 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004358 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4359 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004360 /* It is the openowner seqid that will be incremented in encode in the
4361 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004362 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4363 hash_lockowner(lo, strhashval, clp, open_stp);
4364 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004367static struct nfs4_ol_stateid *
4368alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004370 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004371 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
J. Bruce Fields996e0932011-10-17 11:14:48 -04004373 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004374 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004375 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004376 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown8beefa22005-06-23 22:03:08 -07004377 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004378 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4379 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004380 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004382 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004384 stp->st_openstp = open_stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return stp;
4386}
4387
NeilBrownfd39ca92005-06-23 22:04:03 -07004388static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389check_lock_length(u64 offset, u64 length)
4390{
Benny Halevy87df4de2008-12-15 19:42:03 +02004391 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 LOFF_OVERFLOW(offset, length)));
4393}
4394
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004395static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004396{
4397 struct nfs4_file *fp = lock_stp->st_file;
4398 int oflag = nfs4_access_to_omode(access);
4399
Jeff Layton82c5ff12012-05-11 09:45:13 -04004400 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004401 return;
4402 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004403 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004404}
4405
J. Bruce Fields2355c592012-04-25 16:56:22 -04004406static __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 -04004407{
4408 struct nfs4_file *fi = ost->st_file;
4409 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4410 struct nfs4_client *cl = oo->oo_owner.so_client;
4411 struct nfs4_lockowner *lo;
4412 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004413 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004414
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004415 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4416 &lock->v.new.owner, nn);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004417 if (lo) {
4418 if (!cstate->minorversion)
4419 return nfserr_bad_seqid;
4420 /* XXX: a lockowner always has exactly one stateid: */
4421 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4422 struct nfs4_ol_stateid, st_perstateowner);
4423 return nfs_ok;
4424 }
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004425 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004426 &lock->v.new.owner);
4427 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4428 if (lo == NULL)
4429 return nfserr_jukebox;
4430 *lst = alloc_init_lock_stateid(lo, fi, ost);
4431 if (*lst == NULL) {
4432 release_lockowner(lo);
4433 return nfserr_jukebox;
4434 }
4435 *new = true;
4436 return nfs_ok;
4437}
4438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439/*
4440 * LOCK operation
4441 */
Al Virob37ad282006-10-19 23:28:59 -07004442__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004443nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004444 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004446 struct nfs4_openowner *open_sop = NULL;
4447 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004448 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004449 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004450 struct file_lock *file_lock = NULL;
4451 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004452 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004453 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004454 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004455 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004456 struct net *net = SVC_NET(rqstp);
4457 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
4459 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4460 (long long) lock->lk_offset,
4461 (long long) lock->lk_length);
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (check_lock_length(lock->lk_offset, lock->lk_length))
4464 return nfserr_inval;
4465
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004466 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004467 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004468 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4469 return status;
4470 }
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 nfs4_lock_state();
4473
4474 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004475 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004476
4477 if (nfsd4_has_session(cstate))
4478 /* See rfc 5661 18.10.3: given clientid is ignored: */
4479 memcpy(&lock->v.new.clientid,
4480 &cstate->session->se_client->cl_clientid,
4481 sizeof(clientid_t));
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004484 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004488 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 lock->lk_new_open_seqid,
4490 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004491 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004492 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004494 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004495 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004496 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004497 &lock->v.new.clientid))
4498 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004499 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4500 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004501 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004502 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004503 lock->lk_old_lock_seqid,
4504 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004505 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004506 if (status)
4507 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004508 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004510 lkflg = setlkflg(lock->lk_type);
4511 status = nfs4_check_openmode(lock_stp, lkflg);
4512 if (status)
4513 goto out;
4514
NeilBrown0dd395d2005-07-07 17:59:15 -07004515 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004516 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004517 goto out;
4518 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004519 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004520 goto out;
4521
Jeff Layton21179d82012-08-21 08:03:32 -04004522 file_lock = locks_alloc_lock();
4523 if (!file_lock) {
4524 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4525 status = nfserr_jukebox;
4526 goto out;
4527 }
4528
4529 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 switch (lock->lk_type) {
4531 case NFS4_READ_LT:
4532 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004533 filp = find_readable_file(lock_stp->st_file);
4534 if (filp)
4535 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004536 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 case NFS4_WRITE_LT:
4539 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004540 filp = find_writeable_file(lock_stp->st_file);
4541 if (filp)
4542 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004543 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 default:
4546 status = nfserr_inval;
4547 goto out;
4548 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004549 if (!filp) {
4550 status = nfserr_openmode;
4551 goto out;
4552 }
Jeff Layton21179d82012-08-21 08:03:32 -04004553 file_lock->fl_owner = (fl_owner_t)lock_sop;
4554 file_lock->fl_pid = current->tgid;
4555 file_lock->fl_file = filp;
4556 file_lock->fl_flags = FL_POSIX;
4557 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4558 file_lock->fl_start = lock->lk_offset;
4559 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4560 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
Jeff Layton21179d82012-08-21 08:03:32 -04004562 conflock = locks_alloc_lock();
4563 if (!conflock) {
4564 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4565 status = nfserr_jukebox;
4566 goto out;
4567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Jeff Layton21179d82012-08-21 08:03:32 -04004569 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004570 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004572 update_stateid(&lock_stp->st_stid.sc_stateid);
4573 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004575 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004576 break;
4577 case (EAGAIN): /* conflock holds conflicting lock */
4578 status = nfserr_denied;
4579 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004580 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004581 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 case (EDEADLK):
4583 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004584 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004585 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004586 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004587 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004591 if (status && new_state)
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -05004592 release_lockowner(lock_sop);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004593 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004594 if (!cstate->replay_owner)
4595 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004596 if (file_lock)
4597 locks_free_lock(file_lock);
4598 if (conflock)
4599 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 return status;
4601}
4602
4603/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004604 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4605 * so we do a temporary open here just to get an open file to pass to
4606 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4607 * inode operation.)
4608 */
Al Viro04da6e92012-04-13 00:00:04 -04004609static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004610{
4611 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004612 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4613 if (!err) {
4614 err = nfserrno(vfs_test_lock(file, lock));
4615 nfsd_close(file);
4616 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004617 return err;
4618}
4619
4620/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 * LOCKT operation
4622 */
Al Virob37ad282006-10-19 23:28:59 -07004623__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004624nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4625 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626{
4627 struct inode *inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004628 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004629 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004630 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004631 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004633 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 return nfserr_grace;
4635
4636 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4637 return nfserr_inval;
4638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 nfs4_lock_state();
4640
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004641 if (!nfsd4_has_session(cstate)) {
4642 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4643 if (status)
4644 goto out;
4645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004647 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004650 inode = cstate->current_fh.fh_dentry->d_inode;
Jeff Layton21179d82012-08-21 08:03:32 -04004651 file_lock = locks_alloc_lock();
4652 if (!file_lock) {
4653 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4654 status = nfserr_jukebox;
4655 goto out;
4656 }
4657 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 switch (lockt->lt_type) {
4659 case NFS4_READ_LT:
4660 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004661 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 break;
4663 case NFS4_WRITE_LT:
4664 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004665 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 break;
4667 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004668 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 status = nfserr_inval;
4670 goto out;
4671 }
4672
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004673 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004674 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004675 file_lock->fl_owner = (fl_owner_t)lo;
4676 file_lock->fl_pid = current->tgid;
4677 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Jeff Layton21179d82012-08-21 08:03:32 -04004679 file_lock->fl_start = lockt->lt_offset;
4680 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Jeff Layton21179d82012-08-21 08:03:32 -04004682 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Jeff Layton21179d82012-08-21 08:03:32 -04004684 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004685 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004686 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004687
Jeff Layton21179d82012-08-21 08:03:32 -04004688 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004690 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 }
4692out:
4693 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004694 if (file_lock)
4695 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return status;
4697}
4698
Al Virob37ad282006-10-19 23:28:59 -07004699__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004700nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004701 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004703 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004705 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004706 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004707 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004708 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4711 (long long) locku->lu_offset,
4712 (long long) locku->lu_length);
4713
4714 if (check_lock_length(locku->lu_offset, locku->lu_length))
4715 return nfserr_inval;
4716
4717 nfs4_lock_state();
4718
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004719 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004720 &locku->lu_stateid, NFS4_LOCK_STID,
4721 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004722 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004724 filp = find_any_file(stp->st_file);
4725 if (!filp) {
4726 status = nfserr_lock_range;
4727 goto out;
4728 }
Jeff Layton21179d82012-08-21 08:03:32 -04004729 file_lock = locks_alloc_lock();
4730 if (!file_lock) {
4731 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4732 status = nfserr_jukebox;
4733 goto out;
4734 }
4735 locks_init_lock(file_lock);
4736 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04004737 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04004738 file_lock->fl_pid = current->tgid;
4739 file_lock->fl_file = filp;
4740 file_lock->fl_flags = FL_POSIX;
4741 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4742 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Jeff Layton21179d82012-08-21 08:03:32 -04004744 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4745 locku->lu_length);
4746 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Jeff Layton21179d82012-08-21 08:03:32 -04004748 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004749 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004750 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 goto out_nfserr;
4752 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004753 update_stateid(&stp->st_stid.sc_stateid);
4754 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
4756out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004757 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004758 if (!cstate->replay_owner)
4759 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004760 if (file_lock)
4761 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 return status;
4763
4764out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004765 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 goto out;
4767}
4768
4769/*
4770 * returns
4771 * 1: locks held by lockowner
4772 * 0: no locks held by lockowner
4773 */
4774static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004775check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
4777 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004778 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 int status = 0;
4780
Jeff Layton1c8c6012013-06-21 08:58:15 -04004781 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004783 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 status = 1;
4785 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 }
4788out:
Jeff Layton1c8c6012013-06-21 08:58:15 -04004789 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 return status;
4791}
4792
Al Virob37ad282006-10-19 23:28:59 -07004793__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004794nfsd4_release_lockowner(struct svc_rqst *rqstp,
4795 struct nfsd4_compound_state *cstate,
4796 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
4798 clientid_t *clid = &rlockowner->rl_clientid;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004799 struct nfs4_stateowner *sop;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004800 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004801 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 struct xdr_netobj *owner = &rlockowner->rl_owner;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004803 struct list_head matches;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004804 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004805 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004806 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4809 clid->cl_boot, clid->cl_id);
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 nfs4_lock_state();
4812
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004813 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4814 if (status)
4815 goto out;
4816
NeilBrown3e9e3db2005-06-23 22:04:20 -07004817 status = nfserr_locks_held;
NeilBrown3e9e3db2005-06-23 22:04:20 -07004818 INIT_LIST_HEAD(&matches);
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004819
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004820 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004821 if (sop->so_is_open_owner)
4822 continue;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004823 if (!same_owner_str(sop, owner, clid))
4824 continue;
4825 list_for_each_entry(stp, &sop->so_stateids,
4826 st_perstateowner) {
4827 lo = lockowner(sop);
4828 if (check_for_locks(stp->st_file, lo))
4829 goto out;
4830 list_add(&lo->lo_list, &matches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004832 }
4833 /* Clients probably won't expect us to return with some (but not all)
4834 * of the lockowner state released; so don't release any until all
4835 * have been checked. */
4836 status = nfs_ok;
NeilBrown0fa822e2005-07-07 17:59:14 -07004837 while (!list_empty(&matches)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004838 lo = list_entry(matches.next, struct nfs4_lockowner,
4839 lo_list);
NeilBrown0fa822e2005-07-07 17:59:14 -07004840 /* unhash_stateowner deletes so_perclient only
4841 * for openowners. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004842 list_del(&lo->lo_list);
4843 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 }
4845out:
4846 nfs4_unlock_state();
4847 return status;
4848}
4849
4850static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004851alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
NeilBrowna55370a2005-06-23 22:03:52 -07004853 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004856bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004857nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004858{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004859 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004860
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004861 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004862 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004863}
4864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865/*
4866 * failure => all reset bets are off, nfserr_no_grace...
4867 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004868struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004869nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870{
4871 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004872 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
NeilBrowna55370a2005-06-23 22:03:52 -07004874 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4875 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004876 if (crp) {
4877 strhashval = clientstr_hashval(name);
4878 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004879 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004880 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004881 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004882 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004883 }
4884 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885}
4886
Jeff Layton2a4317c2012-03-21 16:42:43 -04004887void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004888nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004889{
4890 list_del(&crp->cr_strhash);
4891 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004892 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004893}
4894
4895void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004896nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897{
4898 struct nfs4_client_reclaim *crp = NULL;
4899 int i;
4900
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004902 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4903 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004905 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 }
4907 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004908 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909}
4910
4911/*
4912 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004913struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004914nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915{
4916 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 struct nfs4_client_reclaim *crp = NULL;
4918
Jeff Layton278c9312012-11-12 15:00:52 -05004919 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Jeff Layton278c9312012-11-12 15:00:52 -05004921 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004922 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004923 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return crp;
4925 }
4926 }
4927 return NULL;
4928}
4929
4930/*
4931* Called from OPEN. Look for clientid in reclaim list.
4932*/
Al Virob37ad282006-10-19 23:28:59 -07004933__be32
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004934nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
Jeff Laytona52d7262012-03-21 09:52:02 -04004936 struct nfs4_client *clp;
4937
4938 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004939 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004940 if (clp == NULL)
4941 return nfserr_reclaim_bad;
4942
4943 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
Bryan Schumaker65178db2011-11-01 13:35:21 -04004946#ifdef CONFIG_NFSD_FAULT_INJECTION
4947
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004948u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4949{
J. Bruce Fields221a6872013-04-01 22:23:49 -04004950 if (mark_client_expired(clp))
4951 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004952 expire_client(clp);
4953 return 1;
4954}
4955
Bryan Schumaker184c1842012-11-29 11:40:44 -05004956u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4957{
4958 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004959 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004960 printk(KERN_INFO "NFS Client: %s\n", buf);
4961 return 1;
4962}
4963
4964static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4965 const char *type)
4966{
4967 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004968 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004969 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4970}
4971
Bryan Schumakerfc291712012-11-29 11:40:40 -05004972static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4973{
4974 struct nfs4_openowner *oop;
4975 struct nfs4_lockowner *lop, *lo_next;
4976 struct nfs4_ol_stateid *stp, *st_next;
4977 u64 count = 0;
4978
4979 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4980 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4981 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4982 if (func)
4983 func(lop);
4984 if (++count == max)
4985 return count;
4986 }
4987 }
4988 }
4989
4990 return count;
4991}
4992
4993u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4994{
4995 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4996}
4997
Bryan Schumaker184c1842012-11-29 11:40:44 -05004998u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4999{
5000 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5001 nfsd_print_count(clp, count, "locked files");
5002 return count;
5003}
5004
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05005005static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5006{
5007 struct nfs4_openowner *oop, *next;
5008 u64 count = 0;
5009
5010 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5011 if (func)
5012 func(oop);
5013 if (++count == max)
5014 break;
5015 }
5016
5017 return count;
5018}
5019
5020u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5021{
5022 return nfsd_foreach_client_open(clp, max, release_openowner);
5023}
5024
Bryan Schumaker184c1842012-11-29 11:40:44 -05005025u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5026{
5027 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5028 nfsd_print_count(clp, count, "open files");
5029 return count;
5030}
5031
Bryan Schumaker269de302012-11-29 11:40:42 -05005032static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5033 struct list_head *victims)
5034{
5035 struct nfs4_delegation *dp, *next;
5036 u64 count = 0;
5037
Benny Halevycdc97502014-05-30 09:09:30 -04005038 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005039 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5040 if (victims)
5041 list_move(&dp->dl_recall_lru, victims);
5042 if (++count == max)
5043 break;
5044 }
5045 return count;
5046}
5047
5048u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5049{
5050 struct nfs4_delegation *dp, *next;
5051 LIST_HEAD(victims);
5052 u64 count;
5053
Benny Halevycdc97502014-05-30 09:09:30 -04005054 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005055 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005056 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005057
5058 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005059 revoke_delegation(dp);
Bryan Schumaker269de302012-11-29 11:40:42 -05005060
5061 return count;
5062}
5063
5064u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5065{
5066 struct nfs4_delegation *dp, *next;
5067 LIST_HEAD(victims);
5068 u64 count;
5069
Benny Halevycdc97502014-05-30 09:09:30 -04005070 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005071 count = nfsd_find_all_delegations(clp, max, &victims);
5072 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
5073 nfsd_break_one_deleg(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04005074 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005075
5076 return count;
5077}
5078
Bryan Schumaker184c1842012-11-29 11:40:44 -05005079u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5080{
5081 u64 count = 0;
5082
Benny Halevycdc97502014-05-30 09:09:30 -04005083 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005084 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005085 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005086
5087 nfsd_print_count(clp, count, "delegations");
5088 return count;
5089}
5090
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005091u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005092{
5093 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005094 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005095 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005096
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005097 if (!nfsd_netns_ready(nn))
5098 return 0;
5099
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005100 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005101 count += func(clp, max - count);
5102 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005103 break;
5104 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005105
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005106 return count;
5107}
5108
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005109struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5110{
5111 struct nfs4_client *clp;
5112 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5113
5114 if (!nfsd_netns_ready(nn))
5115 return NULL;
5116
5117 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5118 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5119 return clp;
5120 }
5121 return NULL;
5122}
5123
Bryan Schumaker65178db2011-11-01 13:35:21 -04005124#endif /* CONFIG_NFSD_FAULT_INJECTION */
5125
Meelap Shahc2f1a552007-07-17 04:04:39 -07005126/*
5127 * Since the lifetime of a delegation isn't limited to that of an open, a
5128 * client may quite reasonably hang on to a delegation as long as it has
5129 * the inode cached. This becomes an obvious problem the first time a
5130 * client's inode cache approaches the size of the server's total memory.
5131 *
5132 * For now we avoid this problem by imposing a hard limit on the number
5133 * of delegations, which varies according to the server's memory size.
5134 */
5135static void
5136set_max_delegations(void)
5137{
5138 /*
5139 * Allow at most 4 delegations per megabyte of RAM. Quick
5140 * estimates suggest that in the worst case (where every delegation
5141 * is for a different inode), a delegation could take about 1.5K,
5142 * giving a worst case usage of about 6% of memory.
5143 */
5144 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5145}
5146
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005147static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005148{
5149 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5150 int i;
5151
5152 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5153 CLIENT_HASH_SIZE, GFP_KERNEL);
5154 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005155 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005156 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5157 CLIENT_HASH_SIZE, GFP_KERNEL);
5158 if (!nn->unconf_id_hashtbl)
5159 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005160 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5161 OWNER_HASH_SIZE, GFP_KERNEL);
5162 if (!nn->ownerstr_hashtbl)
5163 goto err_ownerstr;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005164 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
5165 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
5166 if (!nn->lockowner_ino_hashtbl)
5167 goto err_lockowner_ino;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005168 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5169 SESSION_HASH_SIZE, GFP_KERNEL);
5170 if (!nn->sessionid_hashtbl)
5171 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005172
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005173 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005174 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005175 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005176 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005177 for (i = 0; i < OWNER_HASH_SIZE; i++)
5178 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005179 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
5180 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005181 for (i = 0; i < SESSION_HASH_SIZE; i++)
5182 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005183 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005184 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005185 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005186 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005187 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005188 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005189
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005190 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005191 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005192
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005193 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005194
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005195err_sessionid:
5196 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005197err_lockowner_ino:
5198 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005199err_ownerstr:
5200 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005201err_unconf_id:
5202 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005203err:
5204 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005205}
5206
5207static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005208nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005209{
5210 int i;
5211 struct nfs4_client *clp = NULL;
5212 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5213
5214 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5215 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5216 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5217 destroy_client(clp);
5218 }
5219 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005220
Kinglong Mee2b905632014-03-26 22:09:30 +08005221 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5222 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5223 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5224 destroy_client(clp);
5225 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005226 }
5227
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005228 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005229 kfree(nn->lockowner_ino_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005230 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005231 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005232 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005233 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005234}
5235
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005236int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005237nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005238{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005239 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005240 int ret;
5241
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005242 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005243 if (ret)
5244 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005245 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005246 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005247 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005248 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005249 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005250 nn->nfsd4_grace, net);
5251 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005252 return 0;
5253}
5254
5255/* initialization to perform when the nfsd service is started: */
5256
5257int
5258nfs4_state_start(void)
5259{
5260 int ret;
5261
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005262 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005263 if (ret)
5264 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005265 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005266 if (laundry_wq == NULL) {
5267 ret = -ENOMEM;
5268 goto out_recovery;
5269 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005270 ret = nfsd4_create_callback_queue();
5271 if (ret)
5272 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005273
Meelap Shahc2f1a552007-07-17 04:04:39 -07005274 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005275
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005276 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005277
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005278out_free_laundry:
5279 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005280out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282}
5283
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005284void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005285nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005289 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005291 cancel_delayed_work_sync(&nn->laundromat_work);
5292 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005293
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005294 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005296 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005297 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5299 list_move(&dp->dl_recall_lru, &reaplist);
5300 }
Benny Halevycdc97502014-05-30 09:09:30 -04005301 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 list_for_each_safe(pos, next, &reaplist) {
5303 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005304 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 }
5306
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005307 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005308 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005309 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310}
5311
5312void
5313nfs4_state_shutdown(void)
5314{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005315 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005316 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005318
5319static void
5320get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5321{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005322 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5323 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005324}
5325
5326static void
5327put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5328{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005329 if (cstate->minorversion) {
5330 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5331 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5332 }
5333}
5334
5335void
5336clear_current_stateid(struct nfsd4_compound_state *cstate)
5337{
5338 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005339}
5340
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005341/*
5342 * functions to set current state id
5343 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005344void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005345nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5346{
5347 put_stateid(cstate, &odp->od_stateid);
5348}
5349
5350void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005351nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5352{
5353 put_stateid(cstate, &open->op_stateid);
5354}
5355
5356void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005357nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5358{
5359 put_stateid(cstate, &close->cl_stateid);
5360}
5361
5362void
5363nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5364{
5365 put_stateid(cstate, &lock->lk_resp_stateid);
5366}
5367
5368/*
5369 * functions to consume current state id
5370 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005371
5372void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005373nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5374{
5375 get_stateid(cstate, &odp->od_stateid);
5376}
5377
5378void
5379nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5380{
5381 get_stateid(cstate, &drp->dr_stateid);
5382}
5383
5384void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005385nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5386{
5387 get_stateid(cstate, &fsp->fr_stateid);
5388}
5389
5390void
5391nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5392{
5393 get_stateid(cstate, &setattr->sa_stateid);
5394}
5395
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005396void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005397nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5398{
5399 get_stateid(cstate, &close->cl_stateid);
5400}
5401
5402void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005403nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005404{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005405 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005406}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005407
5408void
5409nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5410{
5411 get_stateid(cstate, &read->rd_stateid);
5412}
5413
5414void
5415nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5416{
5417 get_stateid(cstate, &write->wr_stateid);
5418}