blob: 86ec359349c62508935508e3e4f36b9d01a077dd [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
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400698static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500699{
700 struct file *file;
701
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400702 list_del(&stp->st_locks);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500703 unhash_generic_stateid(stp);
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400704 unhash_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500705 file = find_any_file(stp->st_file);
706 if (file)
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400707 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400708 close_generic_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500709 free_generic_stateid(stp);
710}
711
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400712static void unhash_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500713{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400714 struct nfs4_ol_stateid *stp;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500715
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400716 list_del(&lo->lo_owner.so_strhash);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400717 while (!list_empty(&lo->lo_owner.so_stateids)) {
718 stp = list_first_entry(&lo->lo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400719 struct nfs4_ol_stateid, st_perstateowner);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400720 __release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500721 }
722}
723
Trond Myklebust50cc6232014-04-18 14:44:03 -0400724static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
725{
726 kfree(lo->lo_owner.so_owner.data);
727 kmem_cache_free(lockowner_slab, lo);
728}
729
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400730static void release_lockowner(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500731{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400732 unhash_lockowner(lo);
733 nfs4_free_lockowner(lo);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500734}
735
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400736static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
737{
738 if (list_empty(&lo->lo_owner.so_stateids))
739 release_lockowner(lo);
740}
741
742static void release_lock_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500743{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400744 struct nfs4_lockowner *lo;
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500745
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400746 lo = lockowner(stp->st_stateowner);
747 __release_lock_stateid(stp);
748 release_lockowner_if_empty(lo);
749}
750
751static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
752{
753 struct nfs4_ol_stateid *stp;
754
755 while (!list_empty(&open_stp->st_locks)) {
756 stp = list_entry(open_stp->st_locks.next,
757 struct nfs4_ol_stateid, st_locks);
758 release_lock_stateid(stp);
J. Bruce Fields529d7b22011-03-02 23:48:33 -0500759 }
760}
761
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400762static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields22839632009-01-11 14:27:17 -0500763{
764 unhash_generic_stateid(stp);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -0400765 release_open_stateid_locks(stp);
J. Bruce Fields38c387b2011-09-16 17:42:48 -0400766 close_generic_stateid(stp);
767}
768
769static void release_open_stateid(struct nfs4_ol_stateid *stp)
770{
771 unhash_open_stateid(stp);
J. Bruce Fields22839632009-01-11 14:27:17 -0500772 free_generic_stateid(stp);
773}
774
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400775static void unhash_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500776{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400777 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500778
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400779 list_del(&oo->oo_owner.so_strhash);
780 list_del(&oo->oo_perclient);
781 while (!list_empty(&oo->oo_owner.so_stateids)) {
782 stp = list_first_entry(&oo->oo_owner.so_stateids,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -0400783 struct nfs4_ol_stateid, st_perstateowner);
J. Bruce Fieldsf044ff82009-01-11 15:24:04 -0500784 release_open_stateid(stp);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500785 }
786}
787
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400788static void release_last_closed_stateid(struct nfs4_openowner *oo)
789{
790 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
791
792 if (s) {
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400793 free_generic_stateid(s);
794 oo->oo_last_closed_stid = NULL;
795 }
796}
797
Trond Myklebust50cc6232014-04-18 14:44:03 -0400798static void nfs4_free_openowner(struct nfs4_openowner *oo)
799{
800 kfree(oo->oo_owner.so_owner.data);
801 kmem_cache_free(openowner_slab, oo);
802}
803
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400804static void release_openowner(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500805{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400806 unhash_openowner(oo);
807 list_del(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -0400808 release_last_closed_stateid(oo);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400809 nfs4_free_openowner(oo);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -0500810}
811
Marc Eshel5282fd72009-04-03 08:27:52 +0300812static inline int
813hash_sessionid(struct nfs4_sessionid *sessionid)
814{
815 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
816
817 return sid->sequence % SESSION_HASH_SIZE;
818}
819
Trond Myklebust8f199b82012-03-20 15:11:17 -0400820#ifdef NFSD_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +0300821static inline void
822dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
823{
824 u32 *ptr = (u32 *)(&sessionid->data[0]);
825 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
826}
Trond Myklebust8f199b82012-03-20 15:11:17 -0400827#else
828static inline void
829dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
830{
831}
832#endif
833
J. Bruce Fields9411b1d2013-04-01 16:37:12 -0400834/*
835 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
836 * won't be used for replay.
837 */
838void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
839{
840 struct nfs4_stateowner *so = cstate->replay_owner;
841
842 if (nfserr == nfserr_replay_me)
843 return;
844
845 if (!seqid_mutating_err(ntohl(nfserr))) {
846 cstate->replay_owner = NULL;
847 return;
848 }
849 if (!so)
850 return;
851 if (so->so_is_open_owner)
852 release_last_closed_stateid(openowner(so));
853 so->so_seqid++;
854 return;
855}
Marc Eshel5282fd72009-04-03 08:27:52 +0300856
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300857static void
858gen_sessionid(struct nfsd4_session *ses)
859{
860 struct nfs4_client *clp = ses->se_client;
861 struct nfsd4_sessionid *sid;
862
863 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
864 sid->clientid = clp->cl_clientid;
865 sid->sequence = current_sessionid++;
866 sid->reserved = 0;
867}
868
869/*
Andy Adamsona6496372009-08-28 08:45:01 -0400870 * The protocol defines ca_maxresponssize_cached to include the size of
871 * the rpc header, but all we need to cache is the data starting after
872 * the end of the initial SEQUENCE operation--the rest we regenerate
873 * each time. Therefore we can advertise a ca_maxresponssize_cached
874 * value that is the number of bytes in our cache plus a few additional
875 * bytes. In order to stay on the safe side, and not promise more than
876 * we can cache, those additional bytes must be the minimum possible: 24
877 * bytes of rpc header (xid through accept state, with AUTH_NULL
878 * verifier), 12 for the compound header (with zero-length tag), and 44
879 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300880 */
Andy Adamsona6496372009-08-28 08:45:01 -0400881#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
882
Andy Adamson557ce262009-08-28 08:45:04 -0400883static void
884free_session_slots(struct nfsd4_session *ses)
885{
886 int i;
887
888 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
889 kfree(ses->se_slots[i]);
890}
891
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400892/*
893 * We don't actually need to cache the rpc and session headers, so we
894 * can allocate a little less for each slot:
895 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400896static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400897{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400898 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -0400899
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400900 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
901 size = 0;
902 else
903 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
904 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400905}
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300906
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400907/*
908 * XXX: If we run out of reserved DRC memory we could (up to a point)
909 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800910 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400911 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400912static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400913{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400914 u32 slotsize = slot_bytes(ca);
915 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400916 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300917
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400918 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800919 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
920 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400921 num = min_t(int, num, avail / slotsize);
922 nfsd_drc_mem_used += num * slotsize;
923 spin_unlock(&nfsd_drc_lock);
924
925 return num;
926}
927
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400928static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400929{
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400930 int slotsize = slot_bytes(ca);
931
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400932 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400933 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400934 spin_unlock(&nfsd_drc_lock);
935}
936
Kinglong Mee60810e52014-01-01 00:35:47 +0800937static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
938 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400939{
Kinglong Mee60810e52014-01-01 00:35:47 +0800940 int numslots = fattrs->maxreqs;
941 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400942 struct nfsd4_session *new;
943 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300944
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -0400945 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400946 + sizeof(struct nfsd4_session) > PAGE_SIZE);
947 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -0400948
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400949 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +0300950 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400951 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -0400952 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400953 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -0400954 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400955 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -0400956 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -0400957 }
Kinglong Mee60810e52014-01-01 00:35:47 +0800958
959 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
960 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
961
J. Bruce Fields5b6feee2010-09-27 17:12:05 -0400962 return new;
963out_free:
964 while (i--)
965 kfree(new->se_slots[i]);
966 kfree(new);
967 return NULL;
968}
969
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400970static void free_conn(struct nfsd4_conn *c)
971{
972 svc_xprt_put(c->cn_xprt);
973 kfree(c);
974}
975
976static void nfsd4_conn_lost(struct svc_xpt_user *u)
977{
978 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
979 struct nfs4_client *clp = c->cn_session->se_client;
980
981 spin_lock(&clp->cl_lock);
982 if (!list_empty(&c->cn_persession)) {
983 list_del(&c->cn_persession);
984 free_conn(c);
985 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -0500986 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -0500987 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -0400988}
989
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400990static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400991{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400992 struct nfsd4_conn *conn;
993
994 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
995 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -0400996 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -0400997 svc_xprt_get(rqstp->rq_xprt);
998 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -0400999 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001000 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1001 return conn;
1002}
1003
J. Bruce Fields328ead22010-09-29 16:11:06 -04001004static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1005{
1006 conn->cn_session = ses;
1007 list_add(&conn->cn_persession, &ses->se_conns);
1008}
1009
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001010static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1011{
1012 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001013
1014 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001015 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001016 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001017}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001018
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001019static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001020{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001021 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001022 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001023}
1024
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001025static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001026{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001027 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001028
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001029 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001030 ret = nfsd4_register_conn(conn);
1031 if (ret)
1032 /* oops; xprt is already down: */
1033 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields6a3b1562012-09-12 09:59:17 -04001034 if (conn->cn_flags & NFS4_CDFC4_BACK) {
Weston Andros Adamson24119672012-05-24 15:42:17 -04001035 /* callback channel may be back up */
1036 nfsd4_probe_callback(ses->se_client);
1037 }
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001038}
1039
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001040static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001041{
1042 u32 dir = NFS4_CDFC4_FORE;
1043
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001044 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001045 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001046 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001047}
1048
1049/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001050static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001051{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001052 struct nfs4_client *clp = s->se_client;
1053 struct nfsd4_conn *c;
1054
1055 spin_lock(&clp->cl_lock);
1056 while (!list_empty(&s->se_conns)) {
1057 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1058 list_del_init(&c->cn_persession);
1059 spin_unlock(&clp->cl_lock);
1060
1061 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1062 free_conn(c);
1063
1064 spin_lock(&clp->cl_lock);
1065 }
1066 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001067}
1068
J. Bruce Fields1377b692012-09-11 21:42:40 -04001069static void __free_session(struct nfsd4_session *ses)
1070{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001071 free_session_slots(ses);
1072 kfree(ses);
1073}
1074
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001075static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001076{
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001077 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1078
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001079 lockdep_assert_held(&nn->client_lock);
1080 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001081 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001082 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001083}
Andy Adamson557ce262009-08-28 08:45:04 -04001084
Fengguang Wu135ae822012-11-10 07:20:25 -05001085static 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 -04001086{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001087 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001088 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001089
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001090 new->se_client = clp;
1091 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001092
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001093 INIT_LIST_HEAD(&new->se_conns);
1094
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001095 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001096 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001097 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001098 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001099 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001100 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001101 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001102 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001103 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001104 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001105 spin_unlock(&clp->cl_lock);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001106 spin_unlock(&nn->client_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001107
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001108 if (cses->flags & SESSION4_BACK_CHAN) {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001109 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001110 /*
1111 * This is a little silly; with sessions there's no real
1112 * use for the callback address. Use the peer address
1113 * as a reasonable default for now, but consider fixing
1114 * the rpc client not to require an address in the
1115 * future:
1116 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001117 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1118 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001119 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001120}
1121
Benny Halevy9089f1b2010-05-12 00:12:26 +03001122/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001123static struct nfsd4_session *
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001124find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001125{
1126 struct nfsd4_session *elem;
1127 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001128 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001129
1130 dump_sessionid(__func__, sessionid);
1131 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001132 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001133 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001134 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1135 NFS4_MAX_SESSIONID_LEN)) {
1136 return elem;
1137 }
1138 }
1139
1140 dprintk("%s: session not found\n", __func__);
1141 return NULL;
1142}
1143
Benny Halevy9089f1b2010-05-12 00:12:26 +03001144/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001145static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001146unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001147{
1148 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001149 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001150 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001151 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1155static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001156STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001158 if (clid->cl_boot == nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001160 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001161 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 1;
1163}
1164
1165/*
1166 * XXX Should we use a slab cache ?
1167 * This type of memory management is somewhat inefficient, but we use it
1168 * anyway since SETCLIENTID is not a common operation.
1169 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001170static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 struct nfs4_client *clp;
1173
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001174 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1175 if (clp == NULL)
1176 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001177 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001178 if (clp->cl_name.data == NULL) {
1179 kfree(clp);
1180 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001182 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001183 INIT_LIST_HEAD(&clp->cl_sessions);
1184 idr_init(&clp->cl_stateids);
1185 atomic_set(&clp->cl_refcount, 0);
1186 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1187 INIT_LIST_HEAD(&clp->cl_idhash);
1188 INIT_LIST_HEAD(&clp->cl_openowners);
1189 INIT_LIST_HEAD(&clp->cl_delegations);
1190 INIT_LIST_HEAD(&clp->cl_lru);
1191 INIT_LIST_HEAD(&clp->cl_callbacks);
1192 INIT_LIST_HEAD(&clp->cl_revoked);
1193 spin_lock_init(&clp->cl_lock);
1194 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return clp;
1196}
1197
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001198static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199free_client(struct nfs4_client *clp)
1200{
Stanislav Kinsburskybca0ec62013-01-09 12:38:34 +03001201 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001202
1203 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001204 while (!list_empty(&clp->cl_sessions)) {
1205 struct nfsd4_session *ses;
1206 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1207 se_perclnt);
1208 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001209 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1210 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001211 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001212 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001213 free_svc_cred(&clp->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001215 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 kfree(clp);
1217}
1218
Benny Halevy84d38ac2010-05-12 00:13:16 +03001219/* must be called under the client_lock */
1220static inline void
1221unhash_client_locked(struct nfs4_client *clp)
1222{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001223 struct nfsd4_session *ses;
1224
Benny Halevy84d38ac2010-05-12 00:13:16 +03001225 list_del(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001226 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001227 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1228 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001229 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static void
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001233destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001235 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct list_head reaplist;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001238 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001241 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001242 while (!list_empty(&clp->cl_delegations)) {
1243 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
NeilBrownea1da632005-06-23 22:04:17 -07001244 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 list_move(&dp->dl_recall_lru, &reaplist);
1246 }
Benny Halevycdc97502014-05-30 09:09:30 -04001247 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 while (!list_empty(&reaplist)) {
1249 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001250 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
Benny Halevy956c4fe2013-10-29 11:39:12 +02001252 list_splice_init(&clp->cl_revoked, &reaplist);
1253 while (!list_empty(&reaplist)) {
1254 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1255 destroy_revoked_delegation(dp);
1256 }
NeilBrownea1da632005-06-23 22:04:17 -07001257 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001258 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1259 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001261 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001262 if (clp->cl_cb_conn.cb_xprt)
1263 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001264 list_del(&clp->cl_idhash);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001265 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001266 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001267 else
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001268 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001269 spin_lock(&nn->client_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001270 unhash_client_locked(clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001271 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1272 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001273 spin_unlock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001276static void expire_client(struct nfs4_client *clp)
1277{
1278 nfsd4_client_record_remove(clp);
1279 destroy_client(clp);
1280}
1281
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001282static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1283{
1284 memcpy(target->cl_verifier.data, source->data,
1285 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001288static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1289{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1291 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1292}
1293
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001294static int copy_cred(struct svc_cred *target, struct svc_cred *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001295{
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001296 if (source->cr_principal) {
1297 target->cr_principal =
1298 kstrdup(source->cr_principal, GFP_KERNEL);
1299 if (target->cr_principal == NULL)
1300 return -ENOMEM;
1301 } else
1302 target->cr_principal = NULL;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001303 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 target->cr_uid = source->cr_uid;
1305 target->cr_gid = source->cr_gid;
1306 target->cr_group_info = source->cr_group_info;
1307 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001308 target->cr_gss_mech = source->cr_gss_mech;
1309 if (source->cr_gss_mech)
1310 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001314static long long
1315compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1316{
1317 long long res;
1318
1319 res = o1->len - o2->len;
1320 if (res)
1321 return res;
1322 return (long long)memcmp(o1->data, o2->data, o1->len);
1323}
1324
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001325static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001326{
NeilBrowna55370a2005-06-23 22:03:52 -07001327 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001331same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1332{
1333 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001337same_clid(clientid_t *cl1, clientid_t *cl2)
1338{
1339 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001342static bool groups_equal(struct group_info *g1, struct group_info *g2)
1343{
1344 int i;
1345
1346 if (g1->ngroups != g2->ngroups)
1347 return false;
1348 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001349 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001350 return false;
1351 return true;
1352}
1353
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001354/*
1355 * RFC 3530 language requires clid_inuse be returned when the
1356 * "principal" associated with a requests differs from that previously
1357 * used. We use uid, gid's, and gss principal string as our best
1358 * approximation. We also don't want to allow non-gss use of a client
1359 * established using gss: in theory cr_principal should catch that
1360 * change, but in practice cr_principal can be null even in the gss case
1361 * since gssd doesn't always pass down a principal string.
1362 */
1363static bool is_gss_cred(struct svc_cred *cr)
1364{
1365 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1366 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1367}
1368
1369
Vivek Trivedi5559b502012-07-24 21:18:20 +05301370static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001371same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1372{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001373 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001374 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1375 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001376 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1377 return false;
1378 if (cr1->cr_principal == cr2->cr_principal)
1379 return true;
1380 if (!cr1->cr_principal || !cr2->cr_principal)
1381 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05301382 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
J. Bruce Fields57266a62013-04-13 14:27:29 -04001385static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1386{
1387 struct svc_cred *cr = &rqstp->rq_cred;
1388 u32 service;
1389
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04001390 if (!cr->cr_gss_mech)
1391 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001392 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1393 return service == RPC_GSS_SVC_INTEGRITY ||
1394 service == RPC_GSS_SVC_PRIVACY;
1395}
1396
1397static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1398{
1399 struct svc_cred *cr = &rqstp->rq_cred;
1400
1401 if (!cl->cl_mach_cred)
1402 return true;
1403 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1404 return false;
1405 if (!svc_rqst_integrity_protected(rqstp))
1406 return false;
1407 if (!cr->cr_principal)
1408 return false;
1409 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1410}
1411
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001412static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fields5ec7b462007-11-21 21:58:56 -05001413{
1414 static u32 current_clientid = 1;
1415
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001416 clp->cl_clientid.cl_boot = nn->boot_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 clp->cl_clientid.cl_id = current_clientid++;
1418}
1419
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001420static void gen_confirm(struct nfs4_client *clp)
1421{
Chuck Leverab4684d2012-03-02 17:13:50 -05001422 __be32 verf[2];
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05001423 static u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Jeff Laytonf4199922014-06-17 07:44:11 -04001425 /*
1426 * This is opaque to client, so no need to byte-swap. Use
1427 * __force to keep sparse happy
1428 */
1429 verf[0] = (__force __be32)get_seconds();
1430 verf[1] = (__force __be32)i++;
Chuck Leverab4684d2012-03-02 17:13:50 -05001431 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001434static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04001435{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001436 struct nfs4_stid *ret;
1437
1438 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1439 if (!ret || !ret->sc_type)
1440 return NULL;
1441 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001442}
1443
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001444static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001445{
1446 struct nfs4_stid *s;
1447
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04001448 s = find_stateid(cl, t);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04001449 if (!s)
1450 return NULL;
1451 if (typemask & s->sc_type)
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04001452 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04001453 return NULL;
1454}
1455
Jeff Layton2216d442012-11-12 15:00:57 -05001456static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001457 struct svc_rqst *rqstp, nfs4_verifier *verf)
1458{
1459 struct nfs4_client *clp;
1460 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001461 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001462 struct net *net = SVC_NET(rqstp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001463 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001464
1465 clp = alloc_client(name);
1466 if (clp == NULL)
1467 return NULL;
1468
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001469 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1470 if (ret) {
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001471 spin_lock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001472 free_client(clp);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03001473 spin_unlock(&nn->client_lock);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001474 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001475 }
J. Bruce Fields57725152012-11-05 15:10:26 -05001476 nfsd4_init_callback(&clp->cl_cb_null);
Benny Halevy07cd4902010-05-12 00:13:41 +03001477 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001478 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001479 copy_verf(clp, verf);
1480 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001481 gen_confirm(clp);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001482 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001483 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03001484 return clp;
1485}
1486
NeilBrownfd39ca92005-06-23 22:04:03 -07001487static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001488add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1489{
1490 struct rb_node **new = &(root->rb_node), *parent = NULL;
1491 struct nfs4_client *clp;
1492
1493 while (*new) {
1494 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1495 parent = *new;
1496
1497 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1498 new = &((*new)->rb_left);
1499 else
1500 new = &((*new)->rb_right);
1501 }
1502
1503 rb_link_node(&new_clp->cl_namenode, parent, new);
1504 rb_insert_color(&new_clp->cl_namenode, root);
1505}
1506
1507static struct nfs4_client *
1508find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1509{
1510 long long cmp;
1511 struct rb_node *node = root->rb_node;
1512 struct nfs4_client *clp;
1513
1514 while (node) {
1515 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1516 cmp = compare_blob(&clp->cl_name, name);
1517 if (cmp > 0)
1518 node = node->rb_left;
1519 else if (cmp < 0)
1520 node = node->rb_right;
1521 else
1522 return clp;
1523 }
1524 return NULL;
1525}
1526
1527static void
1528add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001531 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001533 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001534 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001536 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Benny Halevy36acb662010-05-12 00:13:04 +03001537 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
NeilBrownfd39ca92005-06-23 22:04:03 -07001540static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541move_to_confirmed(struct nfs4_client *clp)
1542{
1543 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001544 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03001547 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001548 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001549 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001550 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 renew_client(clp);
1552}
1553
1554static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001555find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 struct nfs4_client *clp;
1558 unsigned int idhashval = clientid_hashval(clid->cl_id);
1559
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001560 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001561 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04001562 if ((bool)clp->cl_minorversion != sessions)
1563 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001564 renew_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04001566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 return NULL;
1569}
1570
1571static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001572find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1573{
1574 struct list_head *tbl = nn->conf_id_hashtbl;
1575
1576 return find_client_in_id_table(tbl, clid, sessions);
1577}
1578
1579static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03001580find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001582 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04001584 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001587static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001588{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05001589 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05001590}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03001591
NeilBrown28ce6052005-06-23 22:03:56 -07001592static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001593find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001594{
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001595 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001596}
1597
1598static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001599find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07001600{
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001601 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07001602}
1603
NeilBrownfd39ca92005-06-23 22:04:03 -07001604static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001605gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
J. Bruce Fields07263f12010-05-31 19:09:40 -04001607 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09001608 struct sockaddr *sa = svc_addr(rqstp);
1609 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04001610 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Jeff Layton7077ecb2009-08-14 12:57:58 -04001612 /* Currently, we only support tcp and tcp6 for the callback channel */
1613 if (se->se_callback_netid_len == 3 &&
1614 !memcmp(se->se_callback_netid_val, "tcp", 3))
1615 expected_family = AF_INET;
1616 else if (se->se_callback_netid_len == 4 &&
1617 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1618 expected_family = AF_INET6;
1619 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto out_err;
1621
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001622 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001623 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04001624 (struct sockaddr *)&conn->cb_addr,
1625 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001626
J. Bruce Fields07263f12010-05-31 19:09:40 -04001627 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04001629
J. Bruce Fields07263f12010-05-31 19:09:40 -04001630 if (conn->cb_addr.ss_family == AF_INET6)
1631 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04001632
J. Bruce Fields07263f12010-05-31 19:09:40 -04001633 conn->cb_prog = se->se_callback_prog;
1634 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08001635 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return;
1637out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04001638 conn->cb_addr.ss_family = AF_UNSPEC;
1639 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07001640 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 "will not receive delegations\n",
1642 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return;
1645}
1646
Andy Adamson074fe892009-04-03 08:28:15 +03001647/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04001648 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03001649 */
Trond Myklebustb6076642014-06-30 11:48:35 -04001650static void
Andy Adamson074fe892009-04-03 08:28:15 +03001651nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1652{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001653 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04001654 struct nfsd4_slot *slot = resp->cstate.slot;
1655 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03001656
Andy Adamson557ce262009-08-28 08:45:04 -04001657 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001658
Andy Adamson557ce262009-08-28 08:45:04 -04001659 slot->sl_opcnt = resp->opcnt;
1660 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001661
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05001662 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001663 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04001664 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03001665 return;
1666 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001667 base = resp->cstate.data_offset;
1668 slot->sl_datalen = buf->len - base;
1669 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Andy Adamson557ce262009-08-28 08:45:04 -04001670 WARN("%s: sessions DRC could not cache compound\n", __func__);
1671 return;
Andy Adamson074fe892009-04-03 08:28:15 +03001672}
1673
1674/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04001675 * Encode the replay sequence operation from the slot values.
1676 * If cachethis is FALSE encode the uncached rep error on the next
1677 * operation which sets resp->p and increments resp->opcnt for
1678 * nfs4svc_encode_compoundres.
1679 *
Andy Adamson074fe892009-04-03 08:28:15 +03001680 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04001681static __be32
1682nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1683 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03001684{
Andy Adamsonabfabf82009-07-23 19:02:18 -04001685 struct nfsd4_op *op;
1686 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03001687
Andy Adamsonabfabf82009-07-23 19:02:18 -04001688 /* Encode the replayed sequence operation */
1689 op = &args->ops[resp->opcnt - 1];
1690 nfsd4_encode_operation(resp, op);
1691
1692 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05001693 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04001694 op = &args->ops[resp->opcnt++];
1695 op->status = nfserr_retry_uncached_rep;
1696 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03001697 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04001698 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03001699}
1700
1701/*
Andy Adamson557ce262009-08-28 08:45:04 -04001702 * The sequence operation is not cached because we can use the slot and
1703 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03001704 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04001705static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03001706nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1707 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03001708{
Andy Adamson557ce262009-08-28 08:45:04 -04001709 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001710 struct xdr_stream *xdr = &resp->xdr;
1711 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03001712 __be32 status;
1713
Andy Adamson557ce262009-08-28 08:45:04 -04001714 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03001715
Andy Adamsonabfabf82009-07-23 19:02:18 -04001716 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04001717 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04001718 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03001719
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001720 p = xdr_reserve_space(xdr, slot->sl_datalen);
1721 if (!p) {
1722 WARN_ON_ONCE(1);
1723 return nfserr_serverfault;
1724 }
1725 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1726 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03001727
Andy Adamson557ce262009-08-28 08:45:04 -04001728 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04001729 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03001730}
1731
Andy Adamson0733d212009-04-03 08:28:01 +03001732/*
1733 * Set the exchange_id flags returned by the server.
1734 */
1735static void
1736nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1737{
1738 /* pNFS is not supported */
1739 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1740
1741 /* Referrals are supported, Migration is not. */
1742 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1743
1744 /* set the wire flags to return to client. */
1745 clid->flags = new->cl_exchange_flags;
1746}
1747
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001748static bool client_has_state(struct nfs4_client *clp)
1749{
1750 /*
1751 * Note clp->cl_openowners check isn't quite right: there's no
1752 * need to count owners without stateid's.
1753 *
1754 * Also note we should probably be using this in 4.0 case too.
1755 */
J. Bruce Fields6eccece2012-05-29 16:37:44 -04001756 return !list_empty(&clp->cl_openowners)
1757 || !list_empty(&clp->cl_delegations)
1758 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001759}
1760
Al Virob37ad282006-10-19 23:28:59 -07001761__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03001762nfsd4_exchange_id(struct svc_rqst *rqstp,
1763 struct nfsd4_compound_state *cstate,
1764 struct nfsd4_exchange_id *exid)
1765{
Andy Adamson0733d212009-04-03 08:28:01 +03001766 struct nfs4_client *unconf, *conf, *new;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001767 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04001768 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03001769 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04001770 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001771 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001772 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03001773
Jeff Layton363168b2009-08-14 12:57:56 -04001774 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03001775 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04001776 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03001777 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04001778 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03001779
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001780 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03001781 return nfserr_inval;
1782
Andy Adamson0733d212009-04-03 08:28:01 +03001783 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04001784 case SP4_MACH_CRED:
1785 if (!svc_rqst_integrity_protected(rqstp))
1786 return nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001787 case SP4_NONE:
1788 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001789 default: /* checked by xdr code */
1790 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03001791 case SP4_SSV:
J. Bruce Fieldsdd303332013-04-12 18:10:56 -04001792 return nfserr_encr_alg_unsupp;
Andy Adamson0733d212009-04-03 08:28:01 +03001793 }
1794
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001795 /* Cases below refer to rfc 5661 section 18.35.4: */
Andy Adamson0733d212009-04-03 08:28:01 +03001796 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03001797 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03001798 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04001799 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1800 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1801
J. Bruce Fields136e6582012-05-12 20:37:23 -04001802 if (update) {
1803 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001804 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03001805 goto out;
1806 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04001807 if (!mach_creds_match(conf, rqstp)) {
1808 status = nfserr_wrong_cred;
1809 goto out;
1810 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001811 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03001812 status = nfserr_perm;
1813 goto out;
1814 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001815 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03001816 status = nfserr_not_same;
1817 goto out;
1818 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001819 /* case 6 */
1820 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1821 new = conf;
1822 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03001823 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001824 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04001825 if (client_has_state(conf)) {
1826 status = nfserr_clid_inuse;
1827 goto out;
1828 }
Andy Adamson0733d212009-04-03 08:28:01 +03001829 expire_client(conf);
1830 goto out_new;
1831 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04001832 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04001833 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04001834 new = conf;
1835 goto out_copy;
1836 }
1837 /* case 5, client reboot */
J. Bruce Fields136e6582012-05-12 20:37:23 -04001838 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001839 }
1840
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001841 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03001842 status = nfserr_noent;
1843 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001844 }
1845
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03001846 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001847 if (unconf) /* case 4, possible retry or client restart */
Andy Adamson0733d212009-04-03 08:28:01 +03001848 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03001849
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04001850 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03001851out_new:
Jeff Layton2216d442012-11-12 15:00:57 -05001852 new = create_client(exid->clname, rqstp, &verf);
Andy Adamson0733d212009-04-03 08:28:01 +03001853 if (new == NULL) {
J. Bruce Fields47310302010-06-22 16:17:12 -04001854 status = nfserr_jukebox;
Andy Adamson0733d212009-04-03 08:28:01 +03001855 goto out;
1856 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04001857 new->cl_minorversion = cstate->minorversion;
J. Bruce Fields57266a62013-04-13 14:27:29 -04001858 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
Andy Adamson0733d212009-04-03 08:28:01 +03001859
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03001860 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001861 add_to_unconfirmed(new);
Andy Adamson0733d212009-04-03 08:28:01 +03001862out_copy:
1863 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1864 exid->clientid.cl_id = new->cl_clientid.cl_id;
1865
J. Bruce Fields778df3f2012-05-25 21:40:23 -04001866 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
Andy Adamson0733d212009-04-03 08:28:01 +03001867 nfsd4_set_ex_flags(new, exid);
1868
1869 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Andy Adamson49557cc2009-07-23 19:02:16 -04001870 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03001871 status = nfs_ok;
1872
1873out:
1874 nfs4_unlock_state();
Andy Adamson0733d212009-04-03 08:28:01 +03001875 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001876}
1877
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001878static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04001879check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001880{
Andy Adamson88e588d2009-07-23 19:02:15 -04001881 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1882 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001883
1884 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04001885 if (slot_inuse) {
1886 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001887 return nfserr_jukebox;
1888 else
1889 return nfserr_seq_misordered;
1890 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05001891 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04001892 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03001893 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04001894 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03001895 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03001896 return nfserr_seq_misordered;
1897}
1898
Andy Adamson49557cc2009-07-23 19:02:16 -04001899/*
1900 * Cache the create session result into the create session single DRC
1901 * slot cache by saving the xdr structure. sl_seqid has been set.
1902 * Do this for solo or embedded create session operations.
1903 */
1904static void
1905nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04001906 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04001907{
1908 slot->sl_status = nfserr;
1909 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1910}
1911
1912static __be32
1913nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1914 struct nfsd4_clid_slot *slot)
1915{
1916 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1917 return slot->sl_status;
1918}
1919
Mi Jinlong1b74c252011-07-14 14:50:17 +08001920#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1921 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1922 1 + /* MIN tag is length with zero, only length */ \
1923 3 + /* version, opcount, opcode */ \
1924 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1925 /* seqid, slotID, slotID, cache */ \
1926 4 ) * sizeof(__be32))
1927
1928#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1929 2 + /* verifier: AUTH_NULL, length 0 */\
1930 1 + /* status */ \
1931 1 + /* MIN tag is length with zero, only length */ \
1932 3 + /* opcount, opcode, opstatus*/ \
1933 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1934 /* seqid, slotID, slotID, slotID, status */ \
1935 5 ) * sizeof(__be32))
1936
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001937static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08001938{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001939 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
1940
J. Bruce Fields373cd402013-04-08 15:42:12 -04001941 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
1942 return nfserr_toosmall;
1943 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
1944 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001945 ca->headerpadsz = 0;
1946 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
1947 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
1948 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
1949 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
1950 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
1951 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
1952 /*
1953 * Note decreasing slot size below client's request may make it
1954 * difficult for client to function correctly, whereas
1955 * decreasing the number of slots will (just?) affect
1956 * performance. When short on memory we therefore prefer to
1957 * decrease number of slots instead of their size. Clients that
1958 * request larger slots than they need will get poor results:
1959 */
1960 ca->maxreqs = nfsd4_get_drc_mem(ca);
1961 if (!ca->maxreqs)
1962 return nfserr_jukebox;
1963
J. Bruce Fields373cd402013-04-08 15:42:12 -04001964 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08001965}
1966
Kinglong Mee8a891632013-12-23 18:11:02 +08001967#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
1968 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
1969#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
1970 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
1971
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001972static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
1973{
1974 ca->headerpadsz = 0;
1975
1976 /*
1977 * These RPC_MAX_HEADER macros are overkill, especially since we
1978 * don't even do gss on the backchannel yet. But this is still
1979 * less than 1k. Tighten up this estimate in the unlikely event
1980 * it turns out to be a problem for some client:
1981 */
Kinglong Mee8a891632013-12-23 18:11:02 +08001982 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001983 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08001984 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04001985 return nfserr_toosmall;
1986 ca->maxresp_cached = 0;
1987 if (ca->maxops < 2)
1988 return nfserr_toosmall;
1989
1990 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03001991}
1992
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04001993static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
1994{
1995 switch (cbs->flavor) {
1996 case RPC_AUTH_NULL:
1997 case RPC_AUTH_UNIX:
1998 return nfs_ok;
1999 default:
2000 /*
2001 * GSS case: the spec doesn't allow us to return this
2002 * error. But it also doesn't allow us not to support
2003 * GSS.
2004 * I'd rather this fail hard than return some error the
2005 * client might think it can already handle:
2006 */
2007 return nfserr_encr_alg_unsupp;
2008 }
2009}
2010
Andy Adamson069b6ad2009-04-03 08:27:58 +03002011__be32
2012nfsd4_create_session(struct svc_rqst *rqstp,
2013 struct nfsd4_compound_state *cstate,
2014 struct nfsd4_create_session *cr_ses)
2015{
Jeff Layton363168b2009-08-14 12:57:56 -04002016 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002017 struct nfs4_client *conf, *unconf;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002018 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002019 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002020 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002021 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002022 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002023
Mi Jinlonga62573d2011-03-23 17:57:07 +08002024 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2025 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002026 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2027 if (status)
2028 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002029 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002030 if (status)
2031 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002032 status = check_backchannel_attrs(&cr_ses->back_channel);
2033 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002034 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002035 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002036 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002037 if (!new)
2038 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002039 conn = alloc_conn_from_crses(rqstp, cr_ses);
2040 if (!conn)
2041 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002042
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002043 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002044 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002045 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002046 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002047
2048 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002049 status = nfserr_wrong_cred;
2050 if (!mach_creds_match(conf, rqstp))
2051 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002052 cs_slot = &conf->cl_cs_slot;
2053 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002054 if (status == nfserr_replay_cache) {
Andy Adamson49557cc2009-07-23 19:02:16 -04002055 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002056 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002057 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002058 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002059 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002060 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002061 } else if (unconf) {
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002062 struct nfs4_client *old;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002063 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002064 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002065 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002066 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002067 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002068 status = nfserr_wrong_cred;
2069 if (!mach_creds_match(unconf, rqstp))
2070 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002071 cs_slot = &unconf->cl_cs_slot;
2072 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002073 if (status) {
2074 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002075 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002076 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002077 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002078 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002079 if (old) {
2080 status = mark_client_expired(old);
2081 if (status)
2082 goto out_free_conn;
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002083 expire_client(old);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002084 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002085 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002086 conf = unconf;
2087 } else {
2088 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002089 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002090 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002091 status = nfs_ok;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002092 /*
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002093 * We do not support RDMA or persistent sessions
2094 */
2095 cr_ses->flags &= ~SESSION4_PERSIST;
2096 cr_ses->flags &= ~SESSION4_RDMA;
2097
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002098 init_session(rqstp, new, conf, cr_ses);
2099 nfsd4_init_conn(rqstp, conn, new);
2100
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002101 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002102 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002103 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002104 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002105
Andy Adamson49557cc2009-07-23 19:02:16 -04002106 /* cache solo and embedded create sessions under the state lock */
2107 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002108 nfs4_unlock_state();
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002109 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002110out_free_conn:
Yanchuan Nian266533c2012-12-24 18:11:45 +08002111 nfs4_unlock_state();
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002112 free_conn(conn);
2113out_free_session:
2114 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002115out_release_drc_mem:
2116 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002117 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002118}
2119
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002120static __be32 nfsd4_map_bcts_dir(u32 *dir)
2121{
2122 switch (*dir) {
2123 case NFS4_CDFC4_FORE:
2124 case NFS4_CDFC4_BACK:
2125 return nfs_ok;
2126 case NFS4_CDFC4_FORE_OR_BOTH:
2127 case NFS4_CDFC4_BACK_OR_BOTH:
2128 *dir = NFS4_CDFC4_BOTH;
2129 return nfs_ok;
2130 };
2131 return nfserr_inval;
2132}
2133
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002134__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2135{
2136 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002137 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002138 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002139
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002140 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2141 if (status)
2142 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002143 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002144 session->se_cb_prog = bc->bc_cb_program;
2145 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002146 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002147
2148 nfsd4_probe_callback(session->se_client);
2149
2150 return nfs_ok;
2151}
2152
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002153__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2154 struct nfsd4_compound_state *cstate,
2155 struct nfsd4_bind_conn_to_session *bcts)
2156{
2157 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002158 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002159 struct nfsd4_session *session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002160 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002161
2162 if (!nfsd4_last_compound_op(rqstp))
2163 return nfserr_not_only_op;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002164 nfs4_lock_state();
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002165 spin_lock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002166 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002167 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002168 status = nfserr_badsession;
2169 if (!session)
2170 goto out;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002171 status = nfserr_wrong_cred;
2172 if (!mach_creds_match(session->se_client, rqstp))
2173 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002174 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002175 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002176 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002177 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002178 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002179 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002180 goto out;
2181 nfsd4_init_conn(rqstp, conn, session);
2182 status = nfs_ok;
2183out:
2184 nfs4_unlock_state();
2185 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002186}
2187
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002188static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2189{
2190 if (!session)
2191 return 0;
2192 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2193}
2194
Andy Adamson069b6ad2009-04-03 08:27:58 +03002195__be32
2196nfsd4_destroy_session(struct svc_rqst *r,
2197 struct nfsd4_compound_state *cstate,
2198 struct nfsd4_destroy_session *sessionid)
2199{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002200 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002201 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002202 int ref_held_by_me = 0;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002203 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002204
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002205 nfs4_lock_state();
2206 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002207 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002208 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002209 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002210 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002211 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002212 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002213 spin_lock(&nn->client_lock);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002214 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002215 status = nfserr_badsession;
2216 if (!ses)
2217 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002218 status = nfserr_wrong_cred;
2219 if (!mach_creds_match(ses->se_client, r))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002220 goto out_client_lock;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002221 nfsd4_get_session_locked(ses);
2222 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002223 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002224 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002225 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002226 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002227
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002228 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002229
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002230 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002231 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002232out_put_session:
2233 nfsd4_put_session(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002234out_client_lock:
2235 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002236out:
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002237 nfs4_unlock_state();
Benny Halevye10e0cf2009-04-03 08:28:38 +03002238 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002239}
2240
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002241static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002242{
2243 struct nfsd4_conn *c;
2244
2245 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002246 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002247 return c;
2248 }
2249 }
2250 return NULL;
2251}
2252
J. Bruce Fields57266a62013-04-13 14:27:29 -04002253static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002254{
2255 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002256 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002257 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002258 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002259
2260 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002261 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002262 if (c)
2263 goto out_free;
2264 status = nfserr_conn_not_bound_to_session;
2265 if (clp->cl_mach_cred)
2266 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002267 __nfsd4_hash_conn(new, ses);
2268 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002269 ret = nfsd4_register_conn(new);
2270 if (ret)
2271 /* oops; xprt is already down: */
2272 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002273 return nfs_ok;
2274out_free:
2275 spin_unlock(&clp->cl_lock);
2276 free_conn(new);
2277 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002278}
2279
Mi Jinlong868b89c2011-04-27 09:09:58 +08002280static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2281{
2282 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2283
2284 return args->opcnt > session->se_fchannel.maxops;
2285}
2286
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002287static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2288 struct nfsd4_session *session)
2289{
2290 struct xdr_buf *xb = &rqstp->rq_arg;
2291
2292 return xb->len > session->se_fchannel.maxreq_sz;
2293}
2294
Andy Adamson069b6ad2009-04-03 08:27:58 +03002295__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03002296nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03002297 struct nfsd4_compound_state *cstate,
2298 struct nfsd4_sequence *seq)
2299{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002300 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002301 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002302 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002303 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002304 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002305 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002306 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002307 int buflen;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002308 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002309
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03002310 if (resp->opcnt != 1)
2311 return nfserr_sequence_pos;
2312
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002313 /*
2314 * Will be either used or freed by nfsd4_sequence_check_conn
2315 * below.
2316 */
2317 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2318 if (!conn)
2319 return nfserr_jukebox;
2320
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002321 spin_lock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002322 status = nfserr_badsession;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002323 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
Benny Halevyb85d4c02009-04-03 08:28:08 +03002324 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04002325 goto out_no_session;
2326 clp = session->se_client;
2327 status = get_client_locked(clp);
2328 if (status)
2329 goto out_no_session;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002330 status = nfsd4_get_session_locked(session);
2331 if (status)
2332 goto out_put_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002333
Mi Jinlong868b89c2011-04-27 09:09:58 +08002334 status = nfserr_too_many_ops;
2335 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002336 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08002337
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002338 status = nfserr_req_too_big;
2339 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002340 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002341
Benny Halevyb85d4c02009-04-03 08:28:08 +03002342 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03002343 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002344 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002345
Andy Adamson557ce262009-08-28 08:45:04 -04002346 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03002347 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2348
Andy Adamsona8dfdae2009-08-28 08:45:02 -04002349 /* We do not negotiate the number of slots yet, so set the
2350 * maxslots to the session maxreqs which is used to encode
2351 * sr_highest_slotid and the sr_target_slot id to maxslots */
2352 seq->maxslots = session->se_fchannel.maxreqs;
2353
J. Bruce Fields73e79482012-02-13 16:39:00 -05002354 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2355 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002356 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002357 status = nfserr_seq_misordered;
2358 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002359 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002360 cstate->slot = slot;
2361 cstate->session = session;
Andy Adamsonda3846a2009-04-03 08:28:22 +03002362 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04002363 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03002364 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03002365 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002366 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002367 }
2368 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002369 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002370
J. Bruce Fields57266a62013-04-13 14:27:29 -04002371 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002372 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002373 if (status)
2374 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002375
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002376 buflen = (seq->cachethis) ?
2377 session->se_fchannel.maxresp_cached :
2378 session->se_fchannel.maxresp_sz;
2379 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2380 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002381 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002382 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04002383 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04002384
2385 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002386 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03002387 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002388 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05002389 if (seq->cachethis)
2390 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002391 else
2392 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002393
2394 cstate->slot = slot;
2395 cstate->session = session;
2396
Benny Halevyb85d4c02009-04-03 08:28:08 +03002397out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04002398 switch (clp->cl_cb_state) {
2399 case NFSD4_CB_DOWN:
2400 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2401 break;
2402 case NFSD4_CB_FAULT:
2403 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2404 break;
2405 default:
2406 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03002407 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04002408 if (!list_empty(&clp->cl_revoked))
2409 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002410out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08002411 if (conn)
2412 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002413 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002414 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002415out_put_session:
2416 nfsd4_put_session(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002417out_put_client:
2418 put_client_renew_locked(clp);
2419 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002420}
2421
Trond Myklebustb6076642014-06-30 11:48:35 -04002422void
2423nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2424{
2425 struct nfsd4_compound_state *cs = &resp->cstate;
2426
2427 if (nfsd4_has_session(cs)) {
2428 struct nfs4_client *clp = cs->session->se_client;
2429 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2430
2431 if (cs->status != nfserr_replay_cache) {
2432 nfsd4_store_cache_entry(resp);
2433 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2434 }
2435 /* Renew the clientid on success and on replay */
2436 spin_lock(&nn->client_lock);
2437 nfsd4_put_session(cs->session);
2438 put_client_renew_locked(clp);
2439 spin_unlock(&nn->client_lock);
2440 }
2441}
2442
Mi Jinlong345c2842011-10-20 17:51:39 +08002443__be32
2444nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2445{
2446 struct nfs4_client *conf, *unconf, *clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002447 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002448 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08002449
2450 nfs4_lock_state();
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002451 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002452 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002453 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08002454
2455 if (conf) {
2456 clp = conf;
2457
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04002458 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08002459 status = nfserr_clientid_busy;
2460 goto out;
2461 }
2462 } else if (unconf)
2463 clp = unconf;
2464 else {
2465 status = nfserr_stale_clientid;
2466 goto out;
2467 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002468 if (!mach_creds_match(clp, rqstp)) {
2469 status = nfserr_wrong_cred;
2470 goto out;
2471 }
Mi Jinlong345c2842011-10-20 17:51:39 +08002472 expire_client(clp);
2473out:
2474 nfs4_unlock_state();
Mi Jinlong345c2842011-10-20 17:51:39 +08002475 return status;
2476}
2477
Andy Adamson069b6ad2009-04-03 08:27:58 +03002478__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002479nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2480{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002481 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002482
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002483 if (rc->rca_one_fs) {
2484 if (!cstate->current_fh.fh_dentry)
2485 return nfserr_nofilehandle;
2486 /*
2487 * We don't take advantage of the rca_one_fs case.
2488 * That's OK, it's optional, we can safely ignore it.
2489 */
2490 return nfs_ok;
2491 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002492
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002493 nfs4_lock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002494 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04002495 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2496 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002497 goto out;
2498
2499 status = nfserr_stale_clientid;
2500 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002501 /*
2502 * The following error isn't really legal.
2503 * But we only get here if the client just explicitly
2504 * destroyed the client. Surely it no longer cares what
2505 * error it gets back on an operation for the dead
2506 * client.
2507 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002508 goto out;
2509
2510 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04002511 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002512out:
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002513 nfs4_unlock_state();
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08002514 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002515}
2516
2517__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002518nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2519 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002521 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 nfs4_verifier clverifier = setclid->se_verf;
NeilBrown28ce6052005-06-23 22:03:56 -07002523 struct nfs4_client *conf, *unconf, *new;
Al Virob37ad282006-10-19 23:28:59 -07002524 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002525 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2526
J. Bruce Fields63db4632012-05-18 21:54:19 -04002527 /* Cases below refer to rfc 3530 section 14.2.33: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 nfs4_lock_state();
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002529 conf = find_confirmed_client_by_name(&clname, nn);
NeilBrown28ce6052005-06-23 22:03:56 -07002530 if (conf) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04002531 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002533 if (clp_used_exchangeid(conf))
2534 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04002535 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04002536 char addr_str[INET6_ADDRSTRLEN];
2537 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2538 sizeof(addr_str));
2539 dprintk("NFSD: setclientid: string in use by client "
2540 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 goto out;
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002544 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002545 if (unconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 status = nfserr_jukebox;
Jeff Layton2216d442012-11-12 15:00:57 -05002548 new = create_client(clname, rqstp, &clverifier);
J. Bruce Fields8f930712012-05-18 22:06:41 -04002549 if (new == NULL)
2550 goto out;
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002551 if (conf && same_verf(&conf->cl_verifier, &clverifier))
J. Bruce Fields63db4632012-05-18 21:54:19 -04002552 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 copy_clid(new, conf);
J. Bruce Fields34b232b2012-05-18 22:23:42 -04002554 else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002555 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04002556 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002557 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002558 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2560 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2561 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2562 status = nfs_ok;
2563out:
2564 nfs4_unlock_state();
2565 return status;
2566}
2567
2568
Al Virob37ad282006-10-19 23:28:59 -07002569__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08002570nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2571 struct nfsd4_compound_state *cstate,
2572 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
NeilBrown21ab45a2005-06-23 22:04:14 -07002574 struct nfs4_client *conf, *unconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2576 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07002577 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03002578 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002580 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return nfserr_stale_clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 nfs4_lock_state();
NeilBrown21ab45a2005-06-23 22:04:14 -07002583
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002584 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002585 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002586 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04002587 * We try hard to give out unique clientid's, so if we get an
2588 * attempt to confirm the same clientid with a different cred,
2589 * there's a bug somewhere. Let's charitably assume it's our
2590 * bug.
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05002591 */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002592 status = nfserr_serverfault;
2593 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2594 goto out;
2595 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2596 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04002597 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002598 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2599 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002601 else /* case 4: client hasn't noticed we rebooted yet? */
2602 status = nfserr_stale_clientid;
2603 goto out;
2604 }
2605 status = nfs_ok;
2606 if (conf) { /* case 1: callback update */
J. Bruce Fields8695b902012-05-19 10:05:58 -04002607 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2608 nfsd4_probe_callback(conf);
2609 expire_client(unconf);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04002610 } else { /* case 3: normal case; new or rebooted client */
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002611 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002612 if (conf) {
2613 status = mark_client_expired(conf);
2614 if (status)
2615 goto out;
J. Bruce Fields8695b902012-05-19 10:05:58 -04002616 expire_client(conf);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002617 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04002618 move_to_confirmed(unconf);
J. Bruce Fieldsf3d03b92012-05-23 11:38:38 -04002619 nfsd4_probe_callback(unconf);
NeilBrown08e89872005-06-23 22:04:11 -07002620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 nfs4_unlock_state();
2623 return status;
2624}
2625
J. Bruce Fields32513b42011-10-13 16:00:16 -04002626static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
J. Bruce Fields32513b42011-10-13 16:00:16 -04002628 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2629}
2630
2631/* OPEN Share state helper functions */
2632static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2633{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 unsigned int hashval = file_hashval(ino);
2635
Trond Myklebust950e0112014-06-30 11:48:31 -04002636 lockdep_assert_held(&state_lock);
2637
J. Bruce Fields32513b42011-10-13 16:00:16 -04002638 atomic_set(&fp->fi_ref, 1);
J. Bruce Fields32513b42011-10-13 16:00:16 -04002639 INIT_LIST_HEAD(&fp->fi_stateids);
2640 INIT_LIST_HEAD(&fp->fi_delegations);
Trond Myklebust950e0112014-06-30 11:48:31 -04002641 ihold(ino);
2642 fp->fi_inode = ino;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002643 fp->fi_had_conflict = false;
2644 fp->fi_lease = NULL;
2645 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2646 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Jeff Layton89876f82013-04-02 09:01:59 -04002647 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04002650void
NeilBrowne60d4392005-06-23 22:03:01 -07002651nfsd4_free_slabs(void)
2652{
Christoph Hellwigabf11352014-05-21 07:43:03 -07002653 kmem_cache_destroy(openowner_slab);
2654 kmem_cache_destroy(lockowner_slab);
2655 kmem_cache_destroy(file_slab);
2656 kmem_cache_destroy(stateid_slab);
2657 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07002658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Bryan Schumaker72083392011-11-01 15:24:59 -04002660int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661nfsd4_init_slabs(void)
2662{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002663 openowner_slab = kmem_cache_create("nfsd4_openowners",
2664 sizeof(struct nfs4_openowner), 0, 0, NULL);
2665 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002666 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002667 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08002668 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002669 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002670 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07002671 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09002672 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07002673 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002674 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07002675 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002676 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07002677 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002678 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07002679 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09002680 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07002681 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07002682 goto out_free_stateid_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07002684
2685out_free_stateid_slab:
2686 kmem_cache_destroy(stateid_slab);
2687out_free_file_slab:
2688 kmem_cache_destroy(file_slab);
2689out_free_lockowner_slab:
2690 kmem_cache_destroy(lockowner_slab);
2691out_free_openowner_slab:
2692 kmem_cache_destroy(openowner_slab);
2693out:
NeilBrowne60d4392005-06-23 22:03:01 -07002694 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2695 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002698static void init_nfs4_replay(struct nfs4_replay *rp)
2699{
2700 rp->rp_status = nfserr_serverfault;
2701 rp->rp_buflen = 0;
2702 rp->rp_buf = rp->rp_ibuf;
2703}
2704
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002705static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
2707 struct nfs4_stateowner *sop;
2708
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002709 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002710 if (!sop)
2711 return NULL;
2712
2713 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2714 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002715 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002716 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002718 sop->so_owner.len = owner->len;
2719
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002720 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002721 sop->so_client = clp;
2722 init_nfs4_replay(&sop->so_replay);
2723 return sop;
2724}
2725
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002726static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04002727{
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002728 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2729
2730 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002731 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002734static struct nfs4_openowner *
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002735alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp,
2736 struct nfsd4_open *open,
2737 struct nfsd4_compound_state *cstate)
2738{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002739 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002741 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2742 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002744 oo->oo_owner.so_is_open_owner = 1;
2745 oo->oo_owner.so_seqid = open->op_seqid;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04002746 oo->oo_flags = NFS4_OO_NEW;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04002747 if (nfsd4_has_session(cstate))
2748 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002749 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04002750 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002751 INIT_LIST_HEAD(&oo->oo_close_lru);
2752 hash_openowner(oo, clp, strhashval);
2753 return oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
J. Bruce Fields996e0932011-10-17 11:14:48 -04002756static 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 -04002757 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002759 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04002760 INIT_LIST_HEAD(&stp->st_locks);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002761 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
NeilBrown8beefa22005-06-23 22:03:08 -07002762 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002763 stp->st_stateowner = &oo->oo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07002764 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 stp->st_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 stp->st_access_bmap = 0;
2767 stp->st_deny_bmap = 0;
Jeff Layton82c5ff12012-05-11 09:45:13 -04002768 set_access(open->op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04002769 set_deny(open->op_share_deny, stp);
NeilBrown4c4cd222005-07-07 17:59:27 -07002770 stp->st_openstp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
2773static void
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002774move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002776 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2777
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002778 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03002780 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002781 oo->oo_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002785same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2786 clientid_t *clid)
2787{
2788 return (sop->so_owner.len == owner->len) &&
2789 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2790 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002793static struct nfs4_openowner *
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002794find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2795 bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002797 struct nfs4_stateowner *so;
2798 struct nfs4_openowner *oo;
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002799 struct nfs4_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002801 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002802 if (!so->so_is_open_owner)
2803 continue;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002804 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2805 oo = openowner(so);
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002806 clp = oo->oo_owner.so_client;
2807 if ((bool)clp->cl_minorversion != sessions)
2808 return NULL;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002809 renew_client(oo->oo_owner.so_client);
2810 return oo;
2811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
2813 return NULL;
2814}
2815
2816/* search file_hashtbl[] for file */
2817static struct nfs4_file *
Trond Myklebust950e0112014-06-30 11:48:31 -04002818find_file_locked(struct inode *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
2820 unsigned int hashval = file_hashval(ino);
2821 struct nfs4_file *fp;
2822
Trond Myklebust950e0112014-06-30 11:48:31 -04002823 lockdep_assert_held(&state_lock);
2824
Jeff Layton89876f82013-04-02 09:01:59 -04002825 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
NeilBrown13cd2182005-06-23 22:03:10 -07002826 if (fp->fi_inode == ino) {
2827 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07002829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 }
2831 return NULL;
2832}
2833
Trond Myklebust950e0112014-06-30 11:48:31 -04002834static struct nfs4_file *
2835find_file(struct inode *ino)
2836{
2837 struct nfs4_file *fp;
2838
2839 spin_lock(&state_lock);
2840 fp = find_file_locked(ino);
2841 spin_unlock(&state_lock);
2842 return fp;
2843}
2844
2845static struct nfs4_file *
2846find_or_add_file(struct inode *ino, struct nfs4_file *new)
2847{
2848 struct nfs4_file *fp;
2849
2850 spin_lock(&state_lock);
2851 fp = find_file_locked(ino);
2852 if (fp == NULL) {
2853 nfsd4_init_file(new, ino);
2854 fp = new;
2855 }
2856 spin_unlock(&state_lock);
2857
2858 return fp;
2859}
2860
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04002861/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 * Called to check deny when READ with all zero stateid or
2863 * WRITE with all zero or all one stateid
2864 */
Al Virob37ad282006-10-19 23:28:59 -07002865static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2867{
2868 struct inode *ino = current_fh->fh_dentry->d_inode;
2869 struct nfs4_file *fp;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04002870 struct nfs4_ol_stateid *stp;
Al Virob37ad282006-10-19 23:28:59 -07002871 __be32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 fp = find_file(ino);
NeilBrown13cd2182005-06-23 22:03:10 -07002874 if (!fp)
2875 return nfs_ok;
NeilBrownb7009492005-07-07 17:59:23 -07002876 ret = nfserr_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 /* Search for conflicting share reservations */
NeilBrown13cd2182005-06-23 22:03:10 -07002878 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
Jeff Laytonce0fc432012-05-11 09:45:14 -04002879 if (test_deny(deny_type, stp) ||
2880 test_deny(NFS4_SHARE_DENY_BOTH, stp))
NeilBrown13cd2182005-06-23 22:03:10 -07002881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
NeilBrown13cd2182005-06-23 22:03:10 -07002883 ret = nfs_ok;
2884out:
2885 put_nfs4_file(fp);
2886 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002889static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002891 struct nfs4_client *clp = dp->dl_stid.sc_client;
2892 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2893
Benny Halevycdc97502014-05-30 09:09:30 -04002894 lockdep_assert_held(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 /* We're assuming the state code never drops its reference
2896 * without first removing the lease. Since we're in this lease
2897 * callback (and since the lease code is serialized by the kernel
2898 * lock) we know the server hasn't removed the lease yet, we know
2899 * it's safe to take a reference: */
2900 atomic_inc(&dp->dl_count);
2901
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04002902 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Jeff Layton1c8c6012013-06-21 08:58:15 -04002904 /* Only place dl_time is set; protected by i_lock: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 dp->dl_time = get_seconds();
2906
NeilBrown6282cd52014-06-04 17:39:26 +10002907 block_delegations(&dp->dl_fh);
2908
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002909 nfsd4_cb_recall(dp);
2910}
2911
Jeff Layton1c8c6012013-06-21 08:58:15 -04002912/* Called from break_lease() with i_lock held. */
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002913static void nfsd_break_deleg_cb(struct file_lock *fl)
2914{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002915 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2916 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002917
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04002918 if (!fp) {
2919 WARN(1, "(%p)->fl_owner NULL\n", fl);
2920 return;
2921 }
2922 if (fp->fi_had_conflict) {
2923 WARN(1, "duplicate break on %p\n", fp);
2924 return;
2925 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002926 /*
2927 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002928 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05002929 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04002930 */
2931 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Benny Halevycdc97502014-05-30 09:09:30 -04002933 spin_lock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05002934 fp->fi_had_conflict = true;
2935 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2936 nfsd_break_one_deleg(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04002937 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940static
2941int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2942{
2943 if (arg & F_UNLCK)
2944 return lease_modify(onlist, arg);
2945 else
2946 return -EAGAIN;
2947}
2948
Alexey Dobriyan7b021962009-09-21 17:01:12 -07002949static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002950 .lm_break = nfsd_break_deleg_cb,
2951 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952};
2953
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04002954static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2955{
2956 if (nfsd4_has_session(cstate))
2957 return nfs_ok;
2958 if (seqid == so->so_seqid - 1)
2959 return nfserr_replay_me;
2960 if (seqid == so->so_seqid)
2961 return nfs_ok;
2962 return nfserr_bad_seqid;
2963}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Al Virob37ad282006-10-19 23:28:59 -07002965__be32
Andy Adamson66689582009-04-03 08:28:45 +03002966nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03002967 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 clientid_t *clientid = &open->op_clientid;
2970 struct nfs4_client *clp = NULL;
2971 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002972 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04002973 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002975 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04002977 /*
2978 * In case we need it later, after we've already created the
2979 * file and don't want to risk a further failure:
2980 */
2981 open->op_file = nfsd4_alloc_file();
2982 if (open->op_file == NULL)
2983 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05002985 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03002986 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002987 open->op_openowner = oo;
2988 if (!oo) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002989 clp = find_confirmed_client(clientid, cstate->minorversion,
2990 nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (clp == NULL)
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002992 return nfserr_expired;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04002993 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04002995 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08002996 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002997 clp = oo->oo_owner.so_client;
2998 release_openowner(oo);
2999 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003000 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003001 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003002 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3003 if (status)
3004 return status;
3005 clp = oo->oo_owner.so_client;
3006 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003007new_owner:
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003008 oo = alloc_init_open_stateowner(strhashval, clp, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003009 if (oo == NULL)
3010 return nfserr_jukebox;
3011 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003012alloc_stateid:
3013 open->op_stp = nfs4_alloc_stateid(clp);
3014 if (!open->op_stp)
3015 return nfserr_jukebox;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003016 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
Al Virob37ad282006-10-19 23:28:59 -07003019static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003020nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3021{
3022 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3023 return nfserr_openmode;
3024 else
3025 return nfs_ok;
3026}
3027
Daniel Mackc47d8322011-05-16 16:38:14 +02003028static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003029{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003030 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3031}
3032
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003033static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003034{
3035 struct nfs4_stid *ret;
3036
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003037 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003038 if (!ret)
3039 return NULL;
3040 return delegstateid(ret);
3041}
3042
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003043static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3044{
3045 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3046 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3047}
3048
Al Virob37ad282006-10-19 23:28:59 -07003049static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003050nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003051 struct nfs4_delegation **dp)
3052{
3053 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003054 __be32 status = nfserr_bad_stateid;
NeilBrown567d9822005-06-23 22:02:53 -07003055
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003056 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
NeilBrown567d9822005-06-23 22:02:53 -07003057 if (*dp == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003058 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003059 flags = share_access_to_flags(open->op_share_access);
NeilBrown567d9822005-06-23 22:02:53 -07003060 status = nfs4_check_delegmode(*dp, flags);
3061 if (status)
3062 *dp = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003063out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003064 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003065 return nfs_ok;
3066 if (status)
3067 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003068 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003069 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07003070}
3071
Al Virob37ad282006-10-19 23:28:59 -07003072static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003073nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003075 struct nfs4_ol_stateid *local;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003076 struct nfs4_openowner *oo = open->op_openowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
NeilBrown8beefa22005-06-23 22:03:08 -07003078 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /* ignore lock owners */
3080 if (local->st_stateowner->so_is_open_owner == 0)
3081 continue;
3082 /* remember if we have seen this open owner */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003083 if (local->st_stateowner == &oo->oo_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 *stpp = local;
3085 /* check for conflicting share reservations */
3086 if (!test_share(local, open))
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003087 return nfserr_share_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
J. Bruce Fields77eaae82011-09-02 12:08:20 -04003089 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
J. Bruce Fields21fb4012010-07-28 12:21:23 -04003092static inline int nfs4_access_to_access(u32 nfs4_access)
3093{
3094 int flags = 0;
3095
3096 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3097 flags |= NFSD_MAY_READ;
3098 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3099 flags |= NFSD_MAY_WRITE;
3100 return flags;
3101}
3102
Al Virob37ad282006-10-19 23:28:59 -07003103static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3105 struct nfsd4_open *open)
3106{
3107 struct iattr iattr = {
3108 .ia_valid = ATTR_SIZE,
3109 .ia_size = 0,
3110 };
3111 if (!open->op_truncate)
3112 return 0;
3113 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08003114 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3116}
3117
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003118static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3119 struct svc_fh *cur_fh, struct nfsd4_open *open)
3120{
3121 __be32 status;
3122 int oflag = nfs4_access_to_omode(open->op_share_access);
3123 int access = nfs4_access_to_access(open->op_share_access);
3124
3125 if (!fp->fi_fds[oflag]) {
3126 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
3127 &fp->fi_fds[oflag]);
3128 if (status)
3129 goto out;
3130 }
3131 nfs4_file_get_access(fp, oflag);
3132
3133 status = nfsd4_truncate(rqstp, cur_fh, open);
3134 if (status)
3135 goto out_put_access;
3136
3137 return nfs_ok;
3138
3139out_put_access:
3140 nfs4_file_put_access(fp, oflag);
3141out:
3142 return status;
3143}
3144
Al Virob37ad282006-10-19 23:28:59 -07003145static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003146nfs4_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 -07003147{
J. Bruce Fieldsb6d2f1c2011-10-10 17:44:19 -04003148 u32 op_share_access = open->op_share_access;
Al Virob37ad282006-10-19 23:28:59 -07003149 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003151 if (!test_access(op_share_access, stp))
Casey Bodley0c12eaf2011-07-23 14:58:10 -04003152 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003153 else
3154 status = nfsd4_truncate(rqstp, cur_fh, open);
3155
3156 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return status;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04003158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /* remember the open */
Jeff Layton82c5ff12012-05-11 09:45:13 -04003160 set_access(op_share_access, stp);
Jeff Laytonce0fc432012-05-11 09:45:14 -04003161 set_deny(open->op_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 return nfs_ok;
3163}
3164
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166static void
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003167nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003169 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003172/* Should we give out recallable state?: */
3173static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3174{
3175 if (clp->cl_cb_state == NFSD4_CB_UP)
3176 return true;
3177 /*
3178 * In the sessions case, since we don't have to establish a
3179 * separate connection for callbacks, we assume it's OK
3180 * until we hear otherwise:
3181 */
3182 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3183}
3184
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003185static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
3186{
3187 struct file_lock *fl;
3188
3189 fl = locks_alloc_lock();
3190 if (!fl)
3191 return NULL;
3192 locks_init_lock(fl);
3193 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04003194 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003195 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3196 fl->fl_end = OFFSET_MAX;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003197 fl->fl_owner = (fl_owner_t)(dp->dl_file);
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003198 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05003199 return fl;
3200}
3201
J. Bruce Fields99c41512013-05-21 16:21:25 -04003202static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003203{
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003204 struct nfs4_file *fp = dp->dl_file;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003205 struct file_lock *fl;
3206 int status;
3207
J. Bruce Fields99c41512013-05-21 16:21:25 -04003208 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003209 if (!fl)
3210 return -ENOMEM;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003211 fl->fl_file = find_readable_file(fp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003212 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003213 if (status)
3214 goto out_free;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003215 fp->fi_lease = fl;
Al Virocb0942b2012-08-27 14:48:26 -04003216 fp->fi_deleg_file = get_file(fl->fl_file);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003217 atomic_set(&fp->fi_delegees, 1);
Benny Halevycdc97502014-05-30 09:09:30 -04003218 spin_lock(&state_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04003219 hash_delegation_locked(dp, fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003220 spin_unlock(&state_lock);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003221 return 0;
J. Bruce Fieldse8730882012-01-23 13:52:01 -05003222out_free:
3223 locks_free_lock(fl);
3224 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003225}
3226
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003227static int nfs4_set_delegation(struct nfs4_delegation *dp, struct nfs4_file *fp)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003228{
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003229 if (fp->fi_had_conflict)
3230 return -EAGAIN;
3231 get_nfs4_file(fp);
3232 dp->dl_file = fp;
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003233 if (!fp->fi_lease)
3234 return nfs4_setlease(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04003235 spin_lock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003236 atomic_inc(&fp->fi_delegees);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003237 if (fp->fi_had_conflict) {
Benny Halevycdc97502014-05-30 09:09:30 -04003238 spin_unlock(&state_lock);
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003239 return -EAGAIN;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003240 }
Benny Halevy931ee562014-05-30 09:09:27 -04003241 hash_delegation_locked(dp, fp);
Benny Halevycdc97502014-05-30 09:09:30 -04003242 spin_unlock(&state_lock);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003243 return 0;
3244}
3245
Benny Halevy4aa89132012-02-21 14:16:44 -08003246static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3247{
3248 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3249 if (status == -EAGAIN)
3250 open->op_why_no_deleg = WND4_CONTENTION;
3251 else {
3252 open->op_why_no_deleg = WND4_RESOURCE;
3253 switch (open->op_deleg_want) {
3254 case NFS4_SHARE_WANT_READ_DELEG:
3255 case NFS4_SHARE_WANT_WRITE_DELEG:
3256 case NFS4_SHARE_WANT_ANY_DELEG:
3257 break;
3258 case NFS4_SHARE_WANT_CANCEL:
3259 open->op_why_no_deleg = WND4_CANCELLED;
3260 break;
3261 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003262 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08003263 }
3264 }
3265}
3266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267/*
3268 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04003269 *
3270 * Note we don't support write delegations, and won't until the vfs has
3271 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 */
3273static void
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003274nfs4_open_delegation(struct net *net, struct svc_fh *fh,
3275 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
3277 struct nfs4_delegation *dp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003278 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
J. Bruce Fields14a24e92010-12-10 19:02:49 -05003279 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003280 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003282 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07003283 open->op_recall = 0;
3284 switch (open->op_claim_type) {
3285 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05003286 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07003287 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04003288 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3289 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003290 break;
3291 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00003292 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003293 /*
3294 * Let's not give out any delegations till everyone's
3295 * had the chance to reclaim theirs....
3296 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003297 if (locks_in_grace(net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003298 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003299 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04003300 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04003301 /*
3302 * Also, if the file was opened for write or
3303 * create, there's a good chance the client's
3304 * about to write to it, resulting in an
3305 * immediate recall (since we don't support
3306 * write delegations):
3307 */
NeilBrown7b190fe2005-06-23 22:03:23 -07003308 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04003309 goto out_no_deleg;
3310 if (open->op_create == NFS4_OPEN_CREATE)
3311 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003312 break;
3313 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003314 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07003315 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003316 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003317 if (dp == NULL)
3318 goto out_no_deleg;
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04003319 status = nfs4_set_delegation(dp, stp->st_file);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05003320 if (status)
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003321 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003323 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003325 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04003326 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04003327 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003328 return;
3329out_free:
J. Bruce Fieldscbf7a752014-03-03 12:19:18 -05003330 destroy_delegation(dp);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05003331out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04003332 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3333 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003334 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04003335 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04003336 open->op_recall = 1;
3337 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04003338
3339 /* 4.1 client asking for a delegation? */
3340 if (open->op_deleg_want)
3341 nfsd4_open_deleg_none_ext(open, status);
3342 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
Benny Halevye27f49c2012-02-21 14:16:54 -08003345static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3346 struct nfs4_delegation *dp)
3347{
3348 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3349 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3350 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3351 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3352 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3353 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3354 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3355 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3356 }
3357 /* Otherwise the client must be confused wanting a delegation
3358 * it already has, therefore we don't return
3359 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3360 */
3361}
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363/*
3364 * called with nfs4_lock_state() held.
3365 */
Al Virob37ad282006-10-19 23:28:59 -07003366__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3368{
Andy Adamson66689582009-04-03 08:28:45 +03003369 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003370 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 struct nfs4_file *fp = NULL;
3372 struct inode *ino = current_fh->fh_dentry->d_inode;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003373 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07003374 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003375 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 /*
3378 * Lookup file; if found, lookup stateid and check open request,
3379 * and check for delegations in the process of being recalled.
3380 * If not found, create the nfs4_file struct
3381 */
Trond Myklebust950e0112014-06-30 11:48:31 -04003382 fp = find_or_add_file(ino, open->op_file);
3383 if (fp != open->op_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 if ((status = nfs4_check_open(fp, open, &stp)))
3385 goto out;
J. Bruce Fields41d22662013-03-21 15:49:47 -04003386 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07003387 if (status)
3388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04003390 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07003391 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003392 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07003393 goto out;
J. Bruce Fields3e772462011-08-10 19:07:33 -04003394 status = nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396
3397 /*
3398 * OPEN the file, or upgrade an existing OPEN.
3399 * If truncate fails, the OPEN fails.
3400 */
3401 if (stp) {
3402 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003403 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if (status)
3405 goto out;
3406 } else {
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003407 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
NeilBrown567d9822005-06-23 22:02:53 -07003408 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 goto out;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003410 stp = open->op_stp;
3411 open->op_stp = NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -04003412 init_open_stateid(stp, fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003414 update_stateid(&stp->st_stid.sc_stateid);
3415 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Benny Halevyd24433c2012-02-16 20:57:17 +02003417 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02003418 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3419 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3420 open->op_why_no_deleg = WND4_NOT_WANTED;
3421 goto nodeleg;
3422 }
3423 }
3424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 /*
3426 * Attempt to hand out a delegation. No error return, because the
3427 * OPEN succeeds even if we fail.
3428 */
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003429 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003430nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 status = nfs_ok;
3432
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003433 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003434 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435out:
Benny Halevyd24433c2012-02-16 20:57:17 +02003436 /* 4.1 client trying to upgrade/downgrade delegation? */
3437 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08003438 open->op_deleg_want)
3439 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02003440
NeilBrown13cd2182005-06-23 22:03:10 -07003441 if (fp)
3442 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07003443 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
J. Bruce Fields1255a8f2012-03-06 14:35:16 -05003444 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 /*
3446 * To finish the open response, we just need to set the rflags.
3447 */
3448 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003449 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03003450 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3452
3453 return status;
3454}
3455
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003456void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3457{
3458 if (open->op_openowner) {
3459 struct nfs4_openowner *oo = open->op_openowner;
3460
3461 if (!list_empty(&oo->oo_owner.so_stateids))
3462 list_del_init(&oo->oo_close_lru);
3463 if (oo->oo_flags & NFS4_OO_NEW) {
3464 if (status) {
3465 release_openowner(oo);
3466 open->op_openowner = NULL;
3467 } else
3468 oo->oo_flags &= ~NFS4_OO_NEW;
3469 }
3470 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04003471 if (open->op_file)
3472 nfsd4_free_file(open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003473 if (open->op_stp)
J. Bruce Fieldsef798592012-08-29 10:32:54 -07003474 free_generic_stateid(open->op_stp);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04003475}
3476
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003477static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3478{
3479 struct nfs4_client *found;
3480
3481 if (STALE_CLIENTID(clid, nn))
3482 return nfserr_stale_clientid;
3483 found = find_confirmed_client(clid, session, nn);
3484 if (clp)
3485 *clp = found;
3486 return found ? nfs_ok : nfserr_expired;
3487}
3488
Al Virob37ad282006-10-19 23:28:59 -07003489__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003490nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3491 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07003494 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003495 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497 nfs4_lock_state();
3498 dprintk("process_renew(%08x/%08x): starting\n",
3499 clid->cl_boot, clid->cl_id);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05003500 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3501 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07003504 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04003505 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 goto out;
3507 status = nfs_ok;
3508out:
3509 nfs4_unlock_state();
3510 return status;
3511}
3512
NeilBrowna76b4312005-06-23 22:04:01 -07003513static void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003514nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07003515{
Jeff Layton33dcc482012-04-10 11:08:48 -04003516 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003517 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04003518 return;
3519
NeilBrowna76b4312005-06-23 22:04:01 -07003520 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04003521 nn->grace_ended = true;
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003522 nfsd4_record_grace_done(nn, nn->boot_time);
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04003523 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fieldse46b4982010-03-01 19:21:21 -05003524 /*
3525 * Now that every NFSv4 client has had the chance to recover and
3526 * to see the (possibly new, possibly shorter) lease time, we
3527 * can safely set the next grace time to the current lease time:
3528 */
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03003529 nn->nfsd4_grace = nn->nfsd4_lease;
NeilBrowna76b4312005-06-23 22:04:01 -07003530}
3531
NeilBrownfd39ca92005-06-23 22:04:03 -07003532static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003533nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
3535 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003536 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 struct nfs4_delegation *dp;
3538 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03003539 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003540 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 nfs4_lock_state();
3543
3544 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03003545 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03003546 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003547 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03003548 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 clp = list_entry(pos, struct nfs4_client, cl_lru);
3550 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3551 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04003552 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 break;
3554 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003555 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03003556 dprintk("NFSD: client in use (clientid %08x)\n",
3557 clp->cl_clientid.cl_id);
3558 continue;
3559 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003560 list_move(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03003561 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003562 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03003563 list_for_each_safe(pos, next, &reaplist) {
3564 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 dprintk("NFSD: purging unused client (clientid %08x)\n",
3566 clp->cl_clientid.cl_id);
3567 expire_client(clp);
3568 }
Benny Halevycdc97502014-05-30 09:09:30 -04003569 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003570 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Stanislav Kinsbursky4e37a7c22012-11-26 15:22:03 +03003572 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3573 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003575 t = dp->dl_time - cutoff;
3576 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 break;
3578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 list_move(&dp->dl_recall_lru, &reaplist);
3580 }
Benny Halevycdc97502014-05-30 09:09:30 -04003581 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 list_for_each_safe(pos, next, &reaplist) {
3583 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003584 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003586 list_for_each_safe(pos, next, &nn->close_lru) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003587 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3588 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04003589 t = oo->oo_time - cutoff;
3590 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 break;
3592 }
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003593 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 }
Jeff Laytona832e7a2014-05-30 09:09:26 -04003595 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 nfs4_unlock_state();
Jeff Laytona832e7a2014-05-30 09:09:26 -04003597 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Harvey Harrisona254b242008-02-20 12:49:00 -08003600static struct workqueue_struct *laundry_wq;
3601static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08003602
3603static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003604laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
3606 time_t t;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003607 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3608 work);
3609 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3610 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003612 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03003614 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003617static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07003618{
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003619 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3620 return nfserr_bad_stateid;
3621 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003625access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003627 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3628 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3629 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
3632static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04003633access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Jeff Layton82c5ff12012-05-11 09:45:13 -04003635 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3636 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
3639static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003640__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641{
Al Virob37ad282006-10-19 23:28:59 -07003642 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
J. Bruce Fields02921912010-07-29 15:16:59 -04003644 /* For lock stateid's, we test the parent open, not the lock: */
3645 if (stp->st_openstp)
3646 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003647 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04003649 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 goto out;
3651 status = nfs_ok;
3652out:
3653 return status;
3654}
3655
Al Virob37ad282006-10-19 23:28:59 -07003656static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003657check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08003659 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return nfs_ok;
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003661 else if (locks_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003662 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 * conflicting state; so we must wait out the grace period. */
3664 return nfserr_grace;
3665 } else if (flags & WR_STATE)
3666 return nfs4_share_conflict(current_fh,
3667 NFS4_SHARE_DENY_WRITE);
3668 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3669 return nfs4_share_conflict(current_fh,
3670 NFS4_SHARE_DENY_READ);
3671}
3672
3673/*
3674 * Allow READ/WRITE during grace period on recovered state only for files
3675 * that are not able to provide mandatory locking.
3676 */
3677static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003678grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003680 return locks_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
J. Bruce Fields81b82962011-08-23 11:03:29 -04003683/* Returns true iff a is later than b: */
3684static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3685{
Jim Rees1a9357f2013-05-17 17:33:00 -04003686 return (s32)(a->si_generation - b->si_generation) > 0;
J. Bruce Fields81b82962011-08-23 11:03:29 -04003687}
3688
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003689static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05003690{
Andy Adamson66689582009-04-03 08:28:45 +03003691 /*
3692 * When sessions are used the stateid generation number is ignored
3693 * when it is zero.
3694 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04003695 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04003696 return nfs_ok;
3697
3698 if (in->si_generation == ref->si_generation)
3699 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03003700
J. Bruce Fields0836f582008-01-26 19:08:12 -05003701 /* If the client sends us a stateid from the future, it's buggy: */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003702 if (stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05003703 return nfserr_bad_stateid;
3704 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04003705 * However, we could see a stateid from the past, even from a
3706 * non-buggy client. For example, if the client sends a lock
3707 * while some IO is outstanding, the lock may bump si_generation
3708 * while the IO is still in flight. The client could avoid that
3709 * situation by waiting for responses on all the IO requests,
3710 * but better performance may result in retrying IO that
3711 * receives an old_stateid error if requests are rarely
3712 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05003713 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04003714 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05003715}
3716
Chuck Lever7df302f2012-05-29 13:56:37 -04003717static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04003718{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003719 struct nfs4_stid *s;
3720 struct nfs4_ol_stateid *ols;
3721 __be32 status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003722
Chuck Lever7df302f2012-05-29 13:56:37 -04003723 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3724 return nfserr_bad_stateid;
3725 /* Client debugging aid. */
3726 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3727 char addr_str[INET6_ADDRSTRLEN];
3728 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3729 sizeof(addr_str));
3730 pr_warn_ratelimited("NFSD: client %s testing state ID "
3731 "with incorrect client ID\n", addr_str);
3732 return nfserr_bad_stateid;
3733 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003734 s = find_stateid(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003735 if (!s)
Chuck Lever7df302f2012-05-29 13:56:37 -04003736 return nfserr_bad_stateid;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04003737 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04003738 if (status)
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003739 return status;
J. Bruce Fields23340032013-04-09 17:42:28 -04003740 switch (s->sc_type) {
3741 case NFS4_DELEG_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003742 return nfs_ok;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003743 case NFS4_REVOKED_DELEG_STID:
3744 return nfserr_deleg_revoked;
J. Bruce Fields23340032013-04-09 17:42:28 -04003745 case NFS4_OPEN_STID:
3746 case NFS4_LOCK_STID:
3747 ols = openlockstateid(s);
3748 if (ols->st_stateowner->so_is_open_owner
3749 && !(openowner(ols->st_stateowner)->oo_flags
3750 & NFS4_OO_CONFIRMED))
3751 return nfserr_bad_stateid;
3752 return nfs_ok;
3753 default:
3754 printk("unknown stateid type %x\n", s->sc_type);
3755 case NFS4_CLOSED_STID:
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04003756 return nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04003757 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003758}
3759
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003760static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3761 struct nfs4_stid **s, bool sessions,
3762 struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003763{
3764 struct nfs4_client *cl;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003765 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003766
3767 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3768 return nfserr_bad_stateid;
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003769 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3770 nn, &cl);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003771 if (status == nfserr_stale_clientid) {
3772 if (sessions)
3773 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003774 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04003775 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04003776 if (status)
3777 return status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003778 *s = find_stateid_by_type(cl, stateid, typemask);
3779 if (!*s)
3780 return nfserr_bad_stateid;
3781 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003782}
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784/*
3785* Checks for stateid operations
3786*/
Al Virob37ad282006-10-19 23:28:59 -07003787__be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003788nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
Benny Halevydd453df2009-04-03 08:28:41 +03003789 stateid_t *stateid, int flags, struct file **filpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
J. Bruce Fields69064a22011-09-09 11:54:57 -04003791 struct nfs4_stid *s;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003792 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 struct nfs4_delegation *dp = NULL;
Benny Halevydd453df2009-04-03 08:28:41 +03003794 struct svc_fh *current_fh = &cstate->current_fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 struct inode *ino = current_fh->fh_dentry->d_inode;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003796 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Trond Myklebust14bcab12014-04-18 14:44:07 -04003797 struct file *file = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003798 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 if (filpp)
3801 *filpp = NULL;
3802
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003803 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return nfserr_grace;
3805
3806 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04003807 return check_special_stateids(net, current_fh, stateid, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Trond Myklebust14bcab12014-04-18 14:44:07 -04003809 nfs4_lock_state();
3810
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003811 status = nfsd4_lookup_stateid(stateid,
3812 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3813 &s, cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003814 if (status)
Trond Myklebust14bcab12014-04-18 14:44:07 -04003815 goto out;
J. Bruce Fields69064a22011-09-09 11:54:57 -04003816 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3817 if (status)
3818 goto out;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003819 switch (s->sc_type) {
3820 case NFS4_DELEG_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003821 dp = delegstateid(s);
J. Bruce Fieldsdc9bf702009-02-21 11:14:43 -08003822 status = nfs4_check_delegmode(dp, flags);
3823 if (status)
3824 goto out;
Dan Carpenter43b01782010-10-27 23:19:04 +02003825 if (filpp) {
Trond Myklebust14bcab12014-04-18 14:44:07 -04003826 file = dp->dl_file->fi_deleg_file;
3827 if (!file) {
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003828 WARN_ON_ONCE(1);
3829 status = nfserr_serverfault;
3830 goto out;
3831 }
Dan Carpenter43b01782010-10-27 23:19:04 +02003832 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003833 break;
3834 case NFS4_OPEN_STID:
3835 case NFS4_LOCK_STID:
J. Bruce Fields69064a22011-09-09 11:54:57 -04003836 stp = openlockstateid(s);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003837 status = nfs4_check_fh(current_fh, stp);
3838 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003840 if (stp->st_stateowner->so_is_open_owner
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003841 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 goto out;
J. Bruce Fieldsa4455be2009-02-21 10:40:22 -08003843 status = nfs4_check_openmode(stp, flags);
3844 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003846 if (filpp) {
3847 if (flags & RD_STATE)
Trond Myklebust14bcab12014-04-18 14:44:07 -04003848 file = find_readable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003849 else
Trond Myklebust14bcab12014-04-18 14:44:07 -04003850 file = find_writeable_file(stp->st_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04003851 }
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003852 break;
3853 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04003854 status = nfserr_bad_stateid;
3855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
3857 status = nfs_ok;
Trond Myklebust14bcab12014-04-18 14:44:07 -04003858 if (file)
3859 *filpp = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860out:
Trond Myklebust14bcab12014-04-18 14:44:07 -04003861 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return status;
3863}
3864
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003865static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003866nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003867{
J. Bruce Fieldsa1b8ff42014-05-20 15:55:21 -04003868 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
3869
3870 if (check_for_locks(stp->st_file, lo))
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003871 return nfserr_locks_held;
Jeff Laytonc53530d2014-06-30 11:48:39 -04003872 release_lockowner_if_empty(lo);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003873 return nfs_ok;
3874}
3875
3876/*
Bryan Schumaker17456802011-07-13 10:50:48 -04003877 * Test if the stateid is valid
3878 */
3879__be32
3880nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3881 struct nfsd4_test_stateid *test_stateid)
3882{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003883 struct nfsd4_test_stateid_id *stateid;
3884 struct nfs4_client *cl = cstate->session->se_client;
3885
3886 nfs4_lock_state();
3887 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04003888 stateid->ts_id_status =
3889 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003890 nfs4_unlock_state();
3891
Bryan Schumaker17456802011-07-13 10:50:48 -04003892 return nfs_ok;
3893}
3894
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003895__be32
3896nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3897 struct nfsd4_free_stateid *free_stateid)
3898{
3899 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003900 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003901 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003902 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003903 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003904
3905 nfs4_lock_state();
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003906 s = find_stateid(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003907 if (!s)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003908 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003909 switch (s->sc_type) {
3910 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003911 ret = nfserr_locks_held;
3912 goto out;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04003913 case NFS4_OPEN_STID:
3914 case NFS4_LOCK_STID:
3915 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3916 if (ret)
3917 goto out;
3918 if (s->sc_type == NFS4_LOCK_STID)
3919 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3920 else
3921 ret = nfserr_locks_held;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003922 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003923 case NFS4_REVOKED_DELEG_STID:
3924 dp = delegstateid(s);
3925 destroy_revoked_delegation(dp);
3926 ret = nfs_ok;
3927 break;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003928 default:
3929 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003930 }
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003931out:
3932 nfs4_unlock_state();
3933 return ret;
3934}
3935
NeilBrown4c4cd222005-07-07 17:59:27 -07003936static inline int
3937setlkflg (int type)
3938{
3939 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3940 RD_STATE : WR_STATE;
3941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003943static __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 -04003944{
3945 struct svc_fh *current_fh = &cstate->current_fh;
3946 struct nfs4_stateowner *sop = stp->st_stateowner;
3947 __be32 status;
3948
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003949 status = nfsd4_check_seqid(cstate, sop, seqid);
3950 if (status)
3951 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003952 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
3953 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003954 /*
3955 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003956 * nfserr_replay_me from the previous step, and
3957 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04003958 */
3959 return nfserr_bad_stateid;
3960 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3961 if (status)
3962 return status;
3963 return nfs4_check_fh(current_fh, stp);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003964}
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966/*
3967 * Checks for sequence id mutating operations.
3968 */
Al Virob37ad282006-10-19 23:28:59 -07003969static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03003970nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04003971 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003972 struct nfs4_ol_stateid **stpp,
3973 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974{
J. Bruce Fields0836f582008-01-26 19:08:12 -05003975 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003976 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04003977 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Benny Halevy8c10cbd2009-10-19 12:04:53 +02003979 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3980 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07003981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 *stpp = NULL;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003983 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3984 cstate->minorversion, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003985 if (status)
3986 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04003987 stp = openlockstateid(s);
J. Bruce Fields3d74e6a2013-03-22 17:44:19 -04003988 if (!nfsd4_has_session(cstate))
Trond Myklebuste17f99b2014-06-30 11:48:34 -04003989 cstate->replay_owner = stp->st_stateowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Trond Myklebuste17f99b2014-06-30 11:48:34 -04003991 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
3992 if (!status)
3993 *stpp = stp;
3994 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003995}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05003996
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003997static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3998 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04003999{
4000 __be32 status;
4001 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004003 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004004 NFS4_OPEN_STID, stpp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004005 if (status)
4006 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004007 oo = openowner((*stpp)->st_stateowner);
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004008 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
NeilBrown3a4f98b2005-07-07 17:59:26 -07004009 return nfserr_bad_stateid;
NeilBrown3a4f98b2005-07-07 17:59:26 -07004010 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
Al Virob37ad282006-10-19 23:28:59 -07004013__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004014nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004015 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Al Virob37ad282006-10-19 23:28:59 -07004017 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004018 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004019 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004020 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Al Viroa6a9f182013-09-16 10:57:01 -04004022 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4023 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004025 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07004026 if (status)
4027 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029 nfs4_lock_state();
4030
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004031 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004032 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004033 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004034 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004035 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004036 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004037 status = nfserr_bad_stateid;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004038 if (oo->oo_flags & NFS4_OO_CONFIRMED)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004039 goto out;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004040 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004041 update_stateid(&stp->st_stid.sc_stateid);
4042 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004043 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004044 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07004045
Jeff Layton2a4317c2012-03-21 16:42:43 -04004046 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04004047 status = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004049 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004050 if (!cstate->replay_owner)
4051 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 return status;
4053}
4054
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004055static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004057 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004058 return;
4059 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
Jeff Layton82c5ff12012-05-11 09:45:13 -04004060 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004061}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04004062
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004063static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4064{
4065 switch (to_access) {
4066 case NFS4_SHARE_ACCESS_READ:
4067 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4068 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4069 break;
4070 case NFS4_SHARE_ACCESS_WRITE:
4071 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4072 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4073 break;
4074 case NFS4_SHARE_ACCESS_BOTH:
4075 break;
4076 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004077 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 }
4079}
4080
4081static void
Jeff Laytonce0fc432012-05-11 09:45:14 -04004082reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
4084 int i;
4085 for (i = 0; i < 4; i++) {
4086 if ((i & deny) != i)
Jeff Laytonce0fc432012-05-11 09:45:14 -04004087 clear_deny(i, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
4089}
4090
Al Virob37ad282006-10-19 23:28:59 -07004091__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004092nfsd4_open_downgrade(struct svc_rqst *rqstp,
4093 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004094 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095{
Al Virob37ad282006-10-19 23:28:59 -07004096 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004097 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004098 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Al Viroa6a9f182013-09-16 10:57:01 -04004100 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4101 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04004103 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02004104 if (od->od_deleg_want)
4105 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4106 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 nfs4_lock_state();
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004109 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004110 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004111 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004114 if (!test_access(od->od_share_access, stp)) {
4115 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 stp->st_access_bmap, od->od_share_access);
4117 goto out;
4118 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04004119 if (!test_deny(od->od_share_deny, stp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
4121 stp->st_deny_bmap, od->od_share_deny);
4122 goto out;
4123 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04004124 nfs4_stateid_downgrade(stp, od->od_share_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Jeff Laytonce0fc432012-05-11 09:45:14 -04004126 reset_union_bmap_deny(od->od_share_deny, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004128 update_stateid(&stp->st_stid.sc_stateid);
4129 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 status = nfs_ok;
4131out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004132 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004133 if (!cstate->replay_owner)
4134 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 return status;
4136}
4137
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004138static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4139{
Trond Myklebustacf92952014-06-30 11:48:37 -04004140 struct nfs4_client *clp = s->st_stid.sc_client;
4141 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4142
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004143 s->st_stid.sc_type = NFS4_CLOSED_STID;
Trond Myklebustacf92952014-06-30 11:48:37 -04004144 unhash_open_stateid(s);
4145
4146 if (clp->cl_minorversion) {
4147 free_generic_stateid(s);
4148 if (list_empty(&oo->oo_owner.so_stateids))
4149 release_openowner(oo);
4150 } else {
4151 oo->oo_last_closed_stid = s;
4152 /*
4153 * In the 4.0 case we need to keep the owners around a
4154 * little while to handle CLOSE replay.
4155 */
4156 if (list_empty(&oo->oo_owner.so_stateids))
4157 move_to_close_lru(oo, clp->net);
4158 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004159}
4160
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161/*
4162 * nfs4_unlock_state() called after encode
4163 */
Al Virob37ad282006-10-19 23:28:59 -07004164__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004165nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004166 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
Al Virob37ad282006-10-19 23:28:59 -07004168 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004169 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004170 struct net *net = SVC_NET(rqstp);
4171 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Al Viroa6a9f182013-09-16 10:57:01 -04004173 dprintk("NFSD: nfsd4_close on file %pd\n",
4174 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 nfs4_lock_state();
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004177 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4178 &close->cl_stateid,
4179 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004180 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004181 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004182 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 goto out;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004184 update_stateid(&stp->st_stid.sc_stateid);
4185 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004187 nfsd4_close_open_stateid(stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188out:
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004189 if (!cstate->replay_owner)
4190 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 return status;
4192}
4193
Al Virob37ad282006-10-19 23:28:59 -07004194__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004195nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4196 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004198 struct nfs4_delegation *dp;
4199 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004200 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07004201 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004202 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004204 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004205 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 nfs4_lock_state();
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004208 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
4209 cstate->minorversion, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004210 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004211 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004212 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004213 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004214 if (status)
4215 goto out;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004216
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004217 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218out:
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004219 nfs4_unlock_state();
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 return status;
4222}
4223
4224
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225#define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Benny Halevy87df4de2008-12-15 19:42:03 +02004227static inline u64
4228end_offset(u64 start, u64 len)
4229{
4230 u64 end;
4231
4232 end = start + len;
4233 return end >= start ? end: NFS4_MAX_UINT64;
4234}
4235
4236/* last octet in a range */
4237static inline u64
4238last_byte_offset(u64 start, u64 len)
4239{
4240 u64 end;
4241
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004242 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02004243 end = start + len;
4244 return end > start ? end - 1: NFS4_MAX_UINT64;
4245}
4246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247/*
4248 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4249 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4250 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4251 * locking, this prevents us from being completely protocol-compliant. The
4252 * real solution to this problem is to start using unsigned file offsets in
4253 * the VFS, but this is a very deep change!
4254 */
4255static inline void
4256nfs4_transform_lock_offset(struct file_lock *lock)
4257{
4258 if (lock->fl_start < 0)
4259 lock->fl_start = OFFSET_MAX;
4260 if (lock->fl_end < 0)
4261 lock->fl_end = OFFSET_MAX;
4262}
4263
NeilBrownd5b90262006-04-10 22:55:22 -07004264/* Hack!: For now, we're defining this just so we can use a pointer to it
4265 * as a unique cookie to identify our (NFSv4's) posix locks. */
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004266static const struct lock_manager_operations nfsd_posix_mng_ops = {
NeilBrownd5b90262006-04-10 22:55:22 -07004267};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269static inline void
4270nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4271{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004272 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
NeilBrownd5b90262006-04-10 22:55:22 -07004274 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004275 lo = (struct nfs4_lockowner *) fl->fl_owner;
4276 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4277 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004278 if (!deny->ld_owner.data)
4279 /* We just don't care that much */
4280 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004281 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4282 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07004283 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04004284nevermind:
4285 deny->ld_owner.len = 0;
4286 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07004287 deny->ld_clientid.cl_boot = 0;
4288 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 }
4290 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02004291 deny->ld_length = NFS4_MAX_UINT64;
4292 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4294 deny->ld_type = NFS4_READ_LT;
4295 if (fl->fl_type != F_RDLCK)
4296 deny->ld_type = NFS4_WRITE_LT;
4297}
4298
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004299static struct nfs4_lockowner *
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004300find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4301 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004303 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4304 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004306 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4307 if (so->so_is_open_owner)
4308 continue;
4309 if (!same_owner_str(so, owner, clid))
4310 continue;
4311 return lockowner(so);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 }
4313 return NULL;
4314}
4315
4316/*
4317 * Alloc a lock owner structure.
4318 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004319 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004321 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004323static struct nfs4_lockowner *
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004324alloc_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 -04004325 struct nfs4_lockowner *lo;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004326 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004328 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4329 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004331 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4332 lo->lo_owner.so_is_open_owner = 0;
Neil Brownb59e3c02005-09-13 01:25:38 -07004333 /* It is the openowner seqid that will be incremented in encode in the
4334 * case of new lockowners; so increment the lock seqid manually: */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004335 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004336 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004337 return lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
4339
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004340static struct nfs4_ol_stateid *
4341alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004343 struct nfs4_ol_stateid *stp;
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04004344 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
J. Bruce Fields996e0932011-10-17 11:14:48 -04004346 stp = nfs4_alloc_stateid(clp);
NeilBrown5ac049a2005-06-23 22:03:03 -07004347 if (stp == NULL)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04004348 return NULL;
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004349 stp->st_stid.sc_type = NFS4_LOCK_STID;
NeilBrown8beefa22005-06-23 22:03:08 -07004350 list_add(&stp->st_perfile, &fp->fi_stateids);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004351 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4352 stp->st_stateowner = &lo->lo_owner;
NeilBrown13cd2182005-06-23 22:03:10 -07004353 get_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 stp->st_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05004355 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07004357 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004358 list_add(&stp->st_locks, &open_stp->st_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 return stp;
4360}
4361
Jeff Laytonc53530d2014-06-30 11:48:39 -04004362static struct nfs4_ol_stateid *
4363find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4364{
4365 struct nfs4_ol_stateid *lst;
4366
4367 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4368 if (lst->st_file == fp)
4369 return lst;
4370 }
4371 return NULL;
4372}
4373
4374
NeilBrownfd39ca92005-06-23 22:04:03 -07004375static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376check_lock_length(u64 offset, u64 length)
4377{
Benny Halevy87df4de2008-12-15 19:42:03 +02004378 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 LOFF_OVERFLOW(offset, length)));
4380}
4381
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004382static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05004383{
4384 struct nfs4_file *fp = lock_stp->st_file;
4385 int oflag = nfs4_access_to_omode(access);
4386
Jeff Layton82c5ff12012-05-11 09:45:13 -04004387 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05004388 return;
4389 nfs4_file_get_access(fp, oflag);
Jeff Layton82c5ff12012-05-11 09:45:13 -04004390 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05004391}
4392
J. Bruce Fields2355c592012-04-25 16:56:22 -04004393static __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 -04004394{
4395 struct nfs4_file *fi = ost->st_file;
4396 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4397 struct nfs4_client *cl = oo->oo_owner.so_client;
4398 struct nfs4_lockowner *lo;
4399 unsigned int strhashval;
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03004400 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004401
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004402 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
Jeff Laytonc53530d2014-06-30 11:48:39 -04004403 if (!lo) {
4404 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4405 &lock->v.new.owner);
4406 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4407 if (lo == NULL)
4408 return nfserr_jukebox;
4409 } else {
4410 /* with an existing lockowner, seqids must be the same */
4411 if (!cstate->minorversion &&
4412 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004413 return nfserr_bad_seqid;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004414 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04004415
4416 *lst = find_lock_stateid(lo, fi);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004417 if (*lst == NULL) {
Jeff Laytonc53530d2014-06-30 11:48:39 -04004418 *lst = alloc_init_lock_stateid(lo, fi, ost);
4419 if (*lst == NULL) {
4420 release_lockowner_if_empty(lo);
4421 return nfserr_jukebox;
4422 }
4423 *new = true;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004424 }
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004425 return nfs_ok;
4426}
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428/*
4429 * LOCK operation
4430 */
Al Virob37ad282006-10-19 23:28:59 -07004431__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004432nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004433 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004435 struct nfs4_openowner *open_sop = NULL;
4436 struct nfs4_lockowner *lock_sop = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004437 struct nfs4_ol_stateid *lock_stp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04004438 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004439 struct file_lock *file_lock = NULL;
4440 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004441 __be32 status = 0;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004442 bool new_state = false;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004443 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07004444 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004445 struct net *net = SVC_NET(rqstp);
4446 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4449 (long long) lock->lk_offset,
4450 (long long) lock->lk_length);
4451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 if (check_lock_length(lock->lk_offset, lock->lk_length))
4453 return nfserr_inval;
4454
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004455 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02004456 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08004457 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4458 return status;
4459 }
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 nfs4_lock_state();
4462
4463 if (lock->lk_is_new) {
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004464 struct nfs4_ol_stateid *open_stp = NULL;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004465
4466 if (nfsd4_has_session(cstate))
4467 /* See rfc 5661 18.10.3: given clientid is ignored: */
4468 memcpy(&lock->v.new.clientid,
4469 &cstate->session->se_client->cl_clientid,
4470 sizeof(clientid_t));
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004473 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04004477 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 lock->lk_new_open_seqid,
4479 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004480 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07004481 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004483 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004484 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04004485 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004486 &lock->v.new.clientid))
4487 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004488 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4489 &lock_stp, &new_state);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004490 } else
Benny Halevydd453df2009-04-03 08:28:41 +03004491 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004492 lock->lk_old_lock_seqid,
4493 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004494 NFS4_LOCK_STID, &lock_stp, nn);
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04004495 if (status)
4496 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004497 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04004499 lkflg = setlkflg(lock->lk_type);
4500 status = nfs4_check_openmode(lock_stp, lkflg);
4501 if (status)
4502 goto out;
4503
NeilBrown0dd395d2005-07-07 17:59:15 -07004504 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004505 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004506 goto out;
4507 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004508 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07004509 goto out;
4510
Jeff Layton21179d82012-08-21 08:03:32 -04004511 file_lock = locks_alloc_lock();
4512 if (!file_lock) {
4513 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4514 status = nfserr_jukebox;
4515 goto out;
4516 }
4517
4518 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 switch (lock->lk_type) {
4520 case NFS4_READ_LT:
4521 case NFS4_READW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004522 filp = find_readable_file(lock_stp->st_file);
4523 if (filp)
4524 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton21179d82012-08-21 08:03:32 -04004525 file_lock->fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 case NFS4_WRITE_LT:
4528 case NFS4_WRITEW_LT:
J. Bruce Fields0997b172011-03-02 18:01:35 -05004529 filp = find_writeable_file(lock_stp->st_file);
4530 if (filp)
4531 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton21179d82012-08-21 08:03:32 -04004532 file_lock->fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05004533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 default:
4535 status = nfserr_inval;
4536 goto out;
4537 }
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004538 if (!filp) {
4539 status = nfserr_openmode;
4540 goto out;
4541 }
Jeff Layton21179d82012-08-21 08:03:32 -04004542 file_lock->fl_owner = (fl_owner_t)lock_sop;
4543 file_lock->fl_pid = current->tgid;
4544 file_lock->fl_file = filp;
4545 file_lock->fl_flags = FL_POSIX;
4546 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4547 file_lock->fl_start = lock->lk_offset;
4548 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4549 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
Jeff Layton21179d82012-08-21 08:03:32 -04004551 conflock = locks_alloc_lock();
4552 if (!conflock) {
4553 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4554 status = nfserr_jukebox;
4555 goto out;
4556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Jeff Layton21179d82012-08-21 08:03:32 -04004558 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Al Virob8dd7b92006-10-19 23:29:01 -07004559 switch (-err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 case 0: /* success! */
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004561 update_stateid(&lock_stp->st_stid.sc_stateid);
4562 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 sizeof(stateid_t));
Al Virob8dd7b92006-10-19 23:29:01 -07004564 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004565 break;
4566 case (EAGAIN): /* conflock holds conflicting lock */
4567 status = nfserr_denied;
4568 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04004569 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 case (EDEADLK):
4572 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004573 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04004574 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05004575 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04004576 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08004577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579out:
J. Bruce Fields64a284d2011-10-20 06:57:46 -04004580 if (status && new_state)
Jeff Laytonc53530d2014-06-30 11:48:39 -04004581 release_lock_stateid(lock_stp);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004582 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields5ec094c2011-08-30 17:02:48 -04004583 if (!cstate->replay_owner)
4584 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004585 if (file_lock)
4586 locks_free_lock(file_lock);
4587 if (conflock)
4588 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 return status;
4590}
4591
4592/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004593 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4594 * so we do a temporary open here just to get an open file to pass to
4595 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4596 * inode operation.)
4597 */
Al Viro04da6e92012-04-13 00:00:04 -04004598static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004599{
4600 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04004601 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4602 if (!err) {
4603 err = nfserrno(vfs_test_lock(file, lock));
4604 nfsd_close(file);
4605 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08004606 return err;
4607}
4608
4609/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 * LOCKT operation
4611 */
Al Virob37ad282006-10-19 23:28:59 -07004612__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004613nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4614 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615{
Jeff Layton21179d82012-08-21 08:03:32 -04004616 struct file_lock *file_lock = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004617 struct nfs4_lockowner *lo;
Al Virob37ad282006-10-19 23:28:59 -07004618 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004619 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004621 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 return nfserr_grace;
4623
4624 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4625 return nfserr_inval;
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 nfs4_lock_state();
4628
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004629 if (!nfsd4_has_session(cstate)) {
4630 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4631 if (status)
4632 goto out;
4633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
J. Bruce Fields75c096f2011-08-15 18:39:32 -04004635 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Jeff Layton21179d82012-08-21 08:03:32 -04004638 file_lock = locks_alloc_lock();
4639 if (!file_lock) {
4640 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4641 status = nfserr_jukebox;
4642 goto out;
4643 }
4644 locks_init_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 switch (lockt->lt_type) {
4646 case NFS4_READ_LT:
4647 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004648 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 break;
4650 case NFS4_WRITE_LT:
4651 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04004652 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 break;
4654 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04004655 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 status = nfserr_inval;
4657 goto out;
4658 }
4659
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04004660 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004661 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04004662 file_lock->fl_owner = (fl_owner_t)lo;
4663 file_lock->fl_pid = current->tgid;
4664 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Jeff Layton21179d82012-08-21 08:03:32 -04004666 file_lock->fl_start = lockt->lt_offset;
4667 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Jeff Layton21179d82012-08-21 08:03:32 -04004669 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Jeff Layton21179d82012-08-21 08:03:32 -04004671 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04004672 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05004673 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04004674
Jeff Layton21179d82012-08-21 08:03:32 -04004675 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04004677 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
4679out:
4680 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004681 if (file_lock)
4682 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 return status;
4684}
4685
Al Virob37ad282006-10-19 23:28:59 -07004686__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08004687nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08004688 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004690 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04004692 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004693 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07004694 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004695 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4698 (long long) locku->lu_offset,
4699 (long long) locku->lu_length);
4700
4701 if (check_lock_length(locku->lu_offset, locku->lu_length))
4702 return nfserr_inval;
4703
4704 nfs4_lock_state();
4705
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004706 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004707 &locku->lu_stateid, NFS4_LOCK_STID,
4708 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04004709 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 goto out;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004711 filp = find_any_file(stp->st_file);
4712 if (!filp) {
4713 status = nfserr_lock_range;
4714 goto out;
4715 }
Jeff Layton21179d82012-08-21 08:03:32 -04004716 file_lock = locks_alloc_lock();
4717 if (!file_lock) {
4718 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4719 status = nfserr_jukebox;
4720 goto out;
4721 }
4722 locks_init_lock(file_lock);
4723 file_lock->fl_type = F_UNLCK;
J. Bruce Fields0a262ff2013-06-17 17:29:40 -04004724 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
Jeff Layton21179d82012-08-21 08:03:32 -04004725 file_lock->fl_pid = current->tgid;
4726 file_lock->fl_file = filp;
4727 file_lock->fl_flags = FL_POSIX;
4728 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4729 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
Jeff Layton21179d82012-08-21 08:03:32 -04004731 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4732 locku->lu_length);
4733 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
Jeff Layton21179d82012-08-21 08:03:32 -04004735 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07004736 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05004737 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 goto out_nfserr;
4739 }
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004740 update_stateid(&stp->st_stid.sc_stateid);
4741 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
4743out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04004744 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields71c3bcd2011-09-27 21:42:29 -04004745 if (!cstate->replay_owner)
4746 nfs4_unlock_state();
Jeff Layton21179d82012-08-21 08:03:32 -04004747 if (file_lock)
4748 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 return status;
4750
4751out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07004752 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 goto out;
4754}
4755
4756/*
4757 * returns
4758 * 1: locks held by lockowner
4759 * 0: no locks held by lockowner
4760 */
4761static int
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004762check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
4764 struct file_lock **flpp;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004765 struct inode *inode = filp->fi_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 int status = 0;
4767
Jeff Layton1c8c6012013-06-21 08:58:15 -04004768 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004770 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 status = 1;
4772 goto out;
J. Bruce Fields796dadf2006-01-18 17:43:22 -08004773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 }
4775out:
Jeff Layton1c8c6012013-06-21 08:58:15 -04004776 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 return status;
4778}
4779
Al Virob37ad282006-10-19 23:28:59 -07004780__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004781nfsd4_release_lockowner(struct svc_rqst *rqstp,
4782 struct nfsd4_compound_state *cstate,
4783 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
4785 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Laytonfd449072014-06-30 11:48:41 -04004786 struct nfs4_stateowner *sop = NULL, *tmp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004787 struct nfs4_lockowner *lo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004788 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 struct xdr_netobj *owner = &rlockowner->rl_owner;
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004790 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
Al Virob37ad282006-10-19 23:28:59 -07004791 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004792 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
4794 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4795 clid->cl_boot, clid->cl_id);
4796
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 nfs4_lock_state();
4798
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004799 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4800 if (status)
4801 goto out;
4802
NeilBrown3e9e3db2005-06-23 22:04:20 -07004803 status = nfserr_locks_held;
J. Bruce Fields06f1f862011-11-07 16:58:18 -05004804
Jeff Laytonfd449072014-06-30 11:48:41 -04004805 /* Find the matching lock stateowner */
4806 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
4807 if (tmp->so_is_open_owner)
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05004808 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04004809 if (same_owner_str(tmp, owner, clid)) {
4810 sop = tmp;
4811 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 }
NeilBrown3e9e3db2005-06-23 22:04:20 -07004813 }
Jeff Laytonfd449072014-06-30 11:48:41 -04004814
4815 /* No matching owner found, maybe a replay? Just declare victory... */
4816 if (!sop) {
4817 status = nfs_ok;
4818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
Jeff Laytonfd449072014-06-30 11:48:41 -04004820
4821 lo = lockowner(sop);
4822 /* see if there are still any locks associated with it */
4823 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
4824 if (check_for_locks(stp->st_file, lo))
4825 goto out;
4826 }
4827
4828 status = nfs_ok;
4829 release_lockowner(lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830out:
4831 nfs4_unlock_state();
4832 return status;
4833}
4834
4835static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07004836alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
NeilBrowna55370a2005-06-23 22:03:52 -07004838 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839}
4840
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004841bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004842nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07004843{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004844 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07004845
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004846 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004847 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07004848}
4849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850/*
4851 * failure => all reset bets are off, nfserr_no_grace...
4852 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05004853struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004854nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855{
4856 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004857 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
NeilBrowna55370a2005-06-23 22:03:52 -07004859 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4860 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05004861 if (crp) {
4862 strhashval = clientstr_hashval(name);
4863 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004864 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05004865 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05004866 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004867 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05004868 }
4869 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870}
4871
Jeff Layton2a4317c2012-03-21 16:42:43 -04004872void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004873nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05004874{
4875 list_del(&crp->cr_strhash);
4876 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004877 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05004878}
4879
4880void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004881nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
4883 struct nfs4_client_reclaim *crp = NULL;
4884 int i;
4885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004887 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4888 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004890 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
4892 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004893 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895
4896/*
4897 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04004898struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004899nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
4901 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 struct nfs4_client_reclaim *crp = NULL;
4903
Jeff Layton278c9312012-11-12 15:00:52 -05004904 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Jeff Layton278c9312012-11-12 15:00:52 -05004906 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03004907 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05004908 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 return crp;
4910 }
4911 }
4912 return NULL;
4913}
4914
4915/*
4916* Called from OPEN. Look for clientid in reclaim list.
4917*/
Al Virob37ad282006-10-19 23:28:59 -07004918__be32
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004919nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920{
Jeff Laytona52d7262012-03-21 09:52:02 -04004921 struct nfs4_client *clp;
4922
4923 /* find clientid in conf_id_hashtbl */
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004924 clp = find_confirmed_client(clid, sessions, nn);
Jeff Laytona52d7262012-03-21 09:52:02 -04004925 if (clp == NULL)
4926 return nfserr_reclaim_bad;
4927
4928 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930
Bryan Schumaker65178db2011-11-01 13:35:21 -04004931#ifdef CONFIG_NFSD_FAULT_INJECTION
4932
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004933u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4934{
J. Bruce Fields221a6872013-04-01 22:23:49 -04004935 if (mark_client_expired(clp))
4936 return 0;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05004937 expire_client(clp);
4938 return 1;
4939}
4940
Bryan Schumaker184c1842012-11-29 11:40:44 -05004941u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4942{
4943 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004944 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004945 printk(KERN_INFO "NFS Client: %s\n", buf);
4946 return 1;
4947}
4948
4949static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4950 const char *type)
4951{
4952 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05004953 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05004954 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4955}
4956
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004957static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
4958 void (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05004959{
4960 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05004961 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004962 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05004963 u64 count = 0;
4964
4965 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004966 list_for_each_entry_safe(stp, st_next,
4967 &oop->oo_owner.so_stateids, st_perstateowner) {
4968 list_for_each_entry_safe(lst, lst_next,
4969 &stp->st_locks, st_locks) {
Bryan Schumakerfc291712012-11-29 11:40:40 -05004970 if (func)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004971 func(lst);
Bryan Schumakerfc291712012-11-29 11:40:40 -05004972 if (++count == max)
4973 return count;
4974 }
4975 }
4976 }
4977
4978 return count;
4979}
4980
4981u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4982{
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004983 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05004984}
4985
Bryan Schumaker184c1842012-11-29 11:40:44 -05004986u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4987{
4988 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4989 nfsd_print_count(clp, count, "locked files");
4990 return count;
4991}
4992
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05004993static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4994{
4995 struct nfs4_openowner *oop, *next;
4996 u64 count = 0;
4997
4998 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4999 if (func)
5000 func(oop);
5001 if (++count == max)
5002 break;
5003 }
5004
5005 return count;
5006}
5007
5008u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5009{
5010 return nfsd_foreach_client_open(clp, max, release_openowner);
5011}
5012
Bryan Schumaker184c1842012-11-29 11:40:44 -05005013u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5014{
5015 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5016 nfsd_print_count(clp, count, "open files");
5017 return count;
5018}
5019
Bryan Schumaker269de302012-11-29 11:40:42 -05005020static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5021 struct list_head *victims)
5022{
5023 struct nfs4_delegation *dp, *next;
5024 u64 count = 0;
5025
Benny Halevycdc97502014-05-30 09:09:30 -04005026 lockdep_assert_held(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005027 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5028 if (victims)
5029 list_move(&dp->dl_recall_lru, victims);
5030 if (++count == max)
5031 break;
5032 }
5033 return count;
5034}
5035
5036u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5037{
5038 struct nfs4_delegation *dp, *next;
5039 LIST_HEAD(victims);
5040 u64 count;
5041
Benny Halevycdc97502014-05-30 09:09:30 -04005042 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005043 count = nfsd_find_all_delegations(clp, max, &victims);
Benny Halevycdc97502014-05-30 09:09:30 -04005044 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005045
5046 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005047 revoke_delegation(dp);
Bryan Schumaker269de302012-11-29 11:40:42 -05005048
5049 return count;
5050}
5051
5052u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5053{
5054 struct nfs4_delegation *dp, *next;
5055 LIST_HEAD(victims);
5056 u64 count;
5057
Benny Halevycdc97502014-05-30 09:09:30 -04005058 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005059 count = nfsd_find_all_delegations(clp, max, &victims);
5060 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
5061 nfsd_break_one_deleg(dp);
Benny Halevycdc97502014-05-30 09:09:30 -04005062 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05005063
5064 return count;
5065}
5066
Bryan Schumaker184c1842012-11-29 11:40:44 -05005067u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5068{
5069 u64 count = 0;
5070
Benny Halevycdc97502014-05-30 09:09:30 -04005071 spin_lock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005072 count = nfsd_find_all_delegations(clp, max, NULL);
Benny Halevycdc97502014-05-30 09:09:30 -04005073 spin_unlock(&state_lock);
Bryan Schumaker184c1842012-11-29 11:40:44 -05005074
5075 nfsd_print_count(clp, count, "delegations");
5076 return count;
5077}
5078
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005079u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005080{
5081 struct nfs4_client *clp, *next;
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005082 u64 count = 0;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005083 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
Bryan Schumaker65178db2011-11-01 13:35:21 -04005084
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005085 if (!nfsd_netns_ready(nn))
5086 return 0;
5087
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005088 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005089 count += func(clp, max - count);
5090 if ((max != 0) && (count >= max))
Bryan Schumaker65178db2011-11-01 13:35:21 -04005091 break;
5092 }
Bryan Schumaker65178db2011-11-01 13:35:21 -04005093
Bryan Schumaker44e34da602012-11-29 11:40:39 -05005094 return count;
5095}
5096
Bryan Schumaker6c1e82a2012-11-29 11:40:46 -05005097struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5098{
5099 struct nfs4_client *clp;
5100 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5101
5102 if (!nfsd_netns_ready(nn))
5103 return NULL;
5104
5105 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5106 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5107 return clp;
5108 }
5109 return NULL;
5110}
5111
Bryan Schumaker65178db2011-11-01 13:35:21 -04005112#endif /* CONFIG_NFSD_FAULT_INJECTION */
5113
Meelap Shahc2f1a552007-07-17 04:04:39 -07005114/*
5115 * Since the lifetime of a delegation isn't limited to that of an open, a
5116 * client may quite reasonably hang on to a delegation as long as it has
5117 * the inode cached. This becomes an obvious problem the first time a
5118 * client's inode cache approaches the size of the server's total memory.
5119 *
5120 * For now we avoid this problem by imposing a hard limit on the number
5121 * of delegations, which varies according to the server's memory size.
5122 */
5123static void
5124set_max_delegations(void)
5125{
5126 /*
5127 * Allow at most 4 delegations per megabyte of RAM. Quick
5128 * estimates suggest that in the worst case (where every delegation
5129 * is for a different inode), a delegation could take about 1.5K,
5130 * giving a worst case usage of about 6% of memory.
5131 */
5132 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5133}
5134
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005135static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005136{
5137 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5138 int i;
5139
5140 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5141 CLIENT_HASH_SIZE, GFP_KERNEL);
5142 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005143 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005144 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5145 CLIENT_HASH_SIZE, GFP_KERNEL);
5146 if (!nn->unconf_id_hashtbl)
5147 goto err_unconf_id;
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005148 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5149 OWNER_HASH_SIZE, GFP_KERNEL);
5150 if (!nn->ownerstr_hashtbl)
5151 goto err_ownerstr;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005152 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5153 SESSION_HASH_SIZE, GFP_KERNEL);
5154 if (!nn->sessionid_hashtbl)
5155 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005156
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005157 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005158 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005159 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005160 }
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005161 for (i = 0; i < OWNER_HASH_SIZE; i++)
5162 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005163 for (i = 0; i < SESSION_HASH_SIZE; i++)
5164 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005165 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005166 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005167 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03005168 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005169 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005170 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005171
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005172 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005173 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005174
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005175 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005176
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005177err_sessionid:
Stanislav Kinsbursky20e9e2b2012-11-14 18:21:46 +03005178 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005179err_ownerstr:
5180 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005181err_unconf_id:
5182 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03005183err:
5184 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005185}
5186
5187static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005188nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005189{
5190 int i;
5191 struct nfs4_client *clp = NULL;
5192 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5193
5194 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5195 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5196 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5197 destroy_client(clp);
5198 }
5199 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005200
Kinglong Mee2b905632014-03-26 22:09:30 +08005201 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5202 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5203 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5204 destroy_client(clp);
5205 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03005206 }
5207
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03005208 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03005209 kfree(nn->ownerstr_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03005210 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005211 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005212 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005213}
5214
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005215int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005216nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07005217{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005218 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005219 int ret;
5220
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005221 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03005222 if (ret)
5223 return ret;
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005224 nfsd4_client_tracking_init(net);
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005225 nn->boot_time = get_seconds();
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005226 locks_start_grace(net, &nn->nfsd4_manager);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005227 nn->grace_ended = false;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005228 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03005229 nn->nfsd4_grace, net);
5230 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005231 return 0;
5232}
5233
5234/* initialization to perform when the nfsd service is started: */
5235
5236int
5237nfs4_state_start(void)
5238{
5239 int ret;
5240
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005241 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005242 if (ret)
5243 return -ENOMEM;
NeilBrown58da2822005-06-23 22:03:19 -07005244 laundry_wq = create_singlethread_workqueue("nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05005245 if (laundry_wq == NULL) {
5246 ret = -ENOMEM;
5247 goto out_recovery;
5248 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005249 ret = nfsd4_create_callback_queue();
5250 if (ret)
5251 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005252
Meelap Shahc2f1a552007-07-17 04:04:39 -07005253 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005254
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005255 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03005256
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005257out_free_laundry:
5258 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05005259out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05005260 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261}
5262
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03005263void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005264nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005268 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005270 cancel_delayed_work_sync(&nn->laundromat_work);
5271 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05005272
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005273 nfs4_lock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04005275 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005276 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5278 list_move(&dp->dl_recall_lru, &reaplist);
5279 }
Benny Halevycdc97502014-05-30 09:09:30 -04005280 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 list_for_each_safe(pos, next, &reaplist) {
5282 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005283 destroy_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 }
5285
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005286 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03005287 nfs4_state_destroy_net(net);
J. Bruce Fieldse50a26d2013-10-30 10:33:09 -04005288 nfs4_unlock_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289}
5290
5291void
5292nfs4_state_shutdown(void)
5293{
NeilBrown5e8d5c22006-04-10 22:55:37 -07005294 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04005295 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005297
5298static void
5299get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5300{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005301 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5302 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005303}
5304
5305static void
5306put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5307{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01005308 if (cstate->minorversion) {
5309 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5310 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5311 }
5312}
5313
5314void
5315clear_current_stateid(struct nfsd4_compound_state *cstate)
5316{
5317 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005318}
5319
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005320/*
5321 * functions to set current state id
5322 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005323void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005324nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5325{
5326 put_stateid(cstate, &odp->od_stateid);
5327}
5328
5329void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005330nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5331{
5332 put_stateid(cstate, &open->op_stateid);
5333}
5334
5335void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005336nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5337{
5338 put_stateid(cstate, &close->cl_stateid);
5339}
5340
5341void
5342nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5343{
5344 put_stateid(cstate, &lock->lk_resp_stateid);
5345}
5346
5347/*
5348 * functions to consume current state id
5349 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005350
5351void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01005352nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5353{
5354 get_stateid(cstate, &odp->od_stateid);
5355}
5356
5357void
5358nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5359{
5360 get_stateid(cstate, &drp->dr_stateid);
5361}
5362
5363void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01005364nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5365{
5366 get_stateid(cstate, &fsp->fr_stateid);
5367}
5368
5369void
5370nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5371{
5372 get_stateid(cstate, &setattr->sa_stateid);
5373}
5374
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005375void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005376nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5377{
5378 get_stateid(cstate, &close->cl_stateid);
5379}
5380
5381void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005382nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005383{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01005384 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01005385}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01005386
5387void
5388nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5389{
5390 get_stateid(cstate, &read->rd_stateid);
5391}
5392
5393void
5394nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5395{
5396 get_stateid(cstate, &write->wr_stateid);
5397}