blob: ca0db4974e5b194aa9454f19128788c56345212f [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>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.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"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050066
Andy Adamsonec6b5d72009-04-03 08:28:28 +030067static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070068
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
70#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010071#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040074static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040075static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
J. Bruce Fields8b671b82009-02-22 14:51:34 -080077/* Locking: */
78
J. Bruce Fields8b671b82009-02-22 14:51:34 -080079/*
80 * Currently used for the del_recall_lru and file hash table. In an
81 * effort to decrease the scope of the client_mutex, this spinlock may
82 * eventually cover more:
83 */
Benny Halevycdc97502014-05-30 09:09:30 -040084static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080085
Jeff Laytonb401be222014-07-29 21:34:33 -040086/*
87 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
88 * the refcount on the open stateid to drop.
89 */
90static DECLARE_WAIT_QUEUE_HEAD(close_wq);
91
Christoph Hellwigabf11352014-05-21 07:43:03 -070092static struct kmem_cache *openowner_slab;
93static struct kmem_cache *lockowner_slab;
94static struct kmem_cache *file_slab;
95static struct kmem_cache *stateid_slab;
96static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +020097static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -070098
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -040099static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800100
Julia Lawallc4cb8972015-11-21 22:57:39 +0100101static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400102static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200103
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400104static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800105{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400106 return ses->se_flags & NFS4_SESSION_DEAD;
107}
108
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400109static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
110{
111 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400112 return nfserr_jukebox;
113 ses->se_flags |= NFS4_SESSION_DEAD;
114 return nfs_ok;
115}
116
J. Bruce Fields221a6872013-04-01 22:23:49 -0400117static bool is_client_expired(struct nfs4_client *clp)
118{
119 return clp->cl_time == 0;
120}
121
J. Bruce Fields221a6872013-04-01 22:23:49 -0400122static __be32 get_client_locked(struct nfs4_client *clp)
123{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400124 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
125
126 lockdep_assert_held(&nn->client_lock);
127
J. Bruce Fields221a6872013-04-01 22:23:49 -0400128 if (is_client_expired(clp))
129 return nfserr_expired;
130 atomic_inc(&clp->cl_refcount);
131 return nfs_ok;
132}
133
134/* must be called under the client_lock */
135static inline void
136renew_client_locked(struct nfs4_client *clp)
137{
138 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
139
140 if (is_client_expired(clp)) {
141 WARN_ON(1);
142 printk("%s: client (clientid %08x/%08x) already expired\n",
143 __func__,
144 clp->cl_clientid.cl_boot,
145 clp->cl_clientid.cl_id);
146 return;
147 }
148
149 dprintk("renewing client (clientid %08x/%08x)\n",
150 clp->cl_clientid.cl_boot,
151 clp->cl_clientid.cl_id);
152 list_move_tail(&clp->cl_lru, &nn->client_lru);
153 clp->cl_time = get_seconds();
154}
155
Fengguang Wuba138432013-04-16 22:14:15 -0400156static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400157{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400158 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
159
160 lockdep_assert_held(&nn->client_lock);
161
J. Bruce Fields221a6872013-04-01 22:23:49 -0400162 if (!atomic_dec_and_test(&clp->cl_refcount))
163 return;
164 if (!is_client_expired(clp))
165 renew_client_locked(clp);
166}
167
Jeff Layton4b24ca72014-06-30 11:48:44 -0400168static void put_client_renew(struct nfs4_client *clp)
169{
170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
171
Jeff Laytond6c249b2014-07-08 14:02:50 -0400172 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
173 return;
174 if (!is_client_expired(clp))
175 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400176 spin_unlock(&nn->client_lock);
177}
178
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400179static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
180{
181 __be32 status;
182
183 if (is_session_dead(ses))
184 return nfserr_badsession;
185 status = get_client_locked(ses->se_client);
186 if (status)
187 return status;
188 atomic_inc(&ses->se_ref);
189 return nfs_ok;
190}
191
192static void nfsd4_put_session_locked(struct nfsd4_session *ses)
193{
194 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400195 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
196
197 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400198
199 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
200 free_session(ses);
201 put_client_renew_locked(clp);
202}
203
204static void nfsd4_put_session(struct nfsd4_session *ses)
205{
206 struct nfs4_client *clp = ses->se_client;
207 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
208
209 spin_lock(&nn->client_lock);
210 nfsd4_put_session_locked(ses);
211 spin_unlock(&nn->client_lock);
212}
213
Jeff Layton76d348f2016-09-16 16:28:24 -0400214static struct nfsd4_blocked_lock *
215find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
216 struct nfsd_net *nn)
217{
218 struct nfsd4_blocked_lock *cur, *found = NULL;
219
220 spin_lock(&nn->client_lock);
221 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
222 if (fh_match(fh, &cur->nbl_fh)) {
223 list_del_init(&cur->nbl_list);
224 found = cur;
225 break;
226 }
227 }
228 spin_unlock(&nn->client_lock);
229 if (found)
230 posix_unblock_lock(&found->nbl_lock);
231 return found;
232}
233
234static struct nfsd4_blocked_lock *
235find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
236 struct nfsd_net *nn)
237{
238 struct nfsd4_blocked_lock *nbl;
239
240 nbl = find_blocked_lock(lo, fh, nn);
241 if (!nbl) {
242 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
243 if (nbl) {
244 fh_copy_shallow(&nbl->nbl_fh, fh);
245 locks_init_lock(&nbl->nbl_lock);
246 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
247 &nfsd4_cb_notify_lock_ops,
248 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
249 }
250 }
251 return nbl;
252}
253
254static void
255free_blocked_lock(struct nfsd4_blocked_lock *nbl)
256{
257 locks_release_private(&nbl->nbl_lock);
258 kfree(nbl);
259}
260
261static int
262nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
263{
264 /*
265 * Since this is just an optimization, we don't try very hard if it
266 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
267 * just quit trying on anything else.
268 */
269 switch (task->tk_status) {
270 case -NFS4ERR_DELAY:
271 rpc_delay(task, 1 * HZ);
272 return 0;
273 default:
274 return 1;
275 }
276}
277
278static void
279nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
280{
281 struct nfsd4_blocked_lock *nbl = container_of(cb,
282 struct nfsd4_blocked_lock, nbl_cb);
283
284 free_blocked_lock(nbl);
285}
286
287static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
288 .done = nfsd4_cb_notify_lock_done,
289 .release = nfsd4_cb_notify_lock_release,
290};
291
Kinglong Meeb5971af2014-08-22 10:18:43 -0400292static inline struct nfs4_stateowner *
293nfs4_get_stateowner(struct nfs4_stateowner *sop)
294{
295 atomic_inc(&sop->so_count);
296 return sop;
297}
298
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400299static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400300same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400301{
302 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400303 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400304}
305
306static struct nfs4_openowner *
307find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400308 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400309{
310 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400311
Trond Myklebustd4f04892014-07-29 21:34:36 -0400312 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400313
Trond Myklebustd4f04892014-07-29 21:34:36 -0400314 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
315 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400316 if (!so->so_is_open_owner)
317 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400318 if (same_owner_str(so, &open->op_owner))
319 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400320 }
321 return NULL;
322}
323
324static struct nfs4_openowner *
325find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400326 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400327{
328 struct nfs4_openowner *oo;
329
Trond Myklebustd4f04892014-07-29 21:34:36 -0400330 spin_lock(&clp->cl_lock);
331 oo = find_openstateowner_str_locked(hashval, open, clp);
332 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400333 return oo;
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static inline u32
337opaque_hashval(const void *ptr, int nbytes)
338{
339 unsigned char *cptr = (unsigned char *) ptr;
340
341 u32 x = 0;
342 while (nbytes--) {
343 x *= 37;
344 x += *cptr++;
345 }
346 return x;
347}
348
Jeff Layton5b095e92014-10-23 08:01:02 -0400349static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400350{
Jeff Layton5b095e92014-10-23 08:01:02 -0400351 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
352
353 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400354}
355
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200356void
NeilBrown13cd2182005-06-23 22:03:10 -0700357put_nfs4_file(struct nfs4_file *fi)
358{
Jeff Layton02e12152014-07-16 10:31:57 -0400359 might_lock(&state_lock);
360
Benny Halevycdc97502014-05-30 09:09:30 -0400361 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400362 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400363 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200364 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400365 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
366 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800367 }
NeilBrown13cd2182005-06-23 22:03:10 -0700368}
369
Trond Myklebustde186432014-07-10 14:07:26 -0400370static struct file *
371__nfs4_get_fd(struct nfs4_file *f, int oflag)
372{
373 if (f->fi_fds[oflag])
374 return get_file(f->fi_fds[oflag]);
375 return NULL;
376}
377
378static struct file *
379find_writeable_file_locked(struct nfs4_file *f)
380{
381 struct file *ret;
382
383 lockdep_assert_held(&f->fi_lock);
384
385 ret = __nfs4_get_fd(f, O_WRONLY);
386 if (!ret)
387 ret = __nfs4_get_fd(f, O_RDWR);
388 return ret;
389}
390
391static struct file *
392find_writeable_file(struct nfs4_file *f)
393{
394 struct file *ret;
395
396 spin_lock(&f->fi_lock);
397 ret = find_writeable_file_locked(f);
398 spin_unlock(&f->fi_lock);
399
400 return ret;
401}
402
403static struct file *find_readable_file_locked(struct nfs4_file *f)
404{
405 struct file *ret;
406
407 lockdep_assert_held(&f->fi_lock);
408
409 ret = __nfs4_get_fd(f, O_RDONLY);
410 if (!ret)
411 ret = __nfs4_get_fd(f, O_RDWR);
412 return ret;
413}
414
415static struct file *
416find_readable_file(struct nfs4_file *f)
417{
418 struct file *ret;
419
420 spin_lock(&f->fi_lock);
421 ret = find_readable_file_locked(f);
422 spin_unlock(&f->fi_lock);
423
424 return ret;
425}
426
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500427struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400428find_any_file(struct nfs4_file *f)
429{
430 struct file *ret;
431
432 spin_lock(&f->fi_lock);
433 ret = __nfs4_get_fd(f, O_RDWR);
434 if (!ret) {
435 ret = __nfs4_get_fd(f, O_WRONLY);
436 if (!ret)
437 ret = __nfs4_get_fd(f, O_RDONLY);
438 }
439 spin_unlock(&f->fi_lock);
440 return ret;
441}
442
Trond Myklebust02a35082014-07-25 07:34:22 -0400443static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800444unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700445
446/*
447 * Open owner state (share locks)
448 */
449
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500450/* hash tables for lock and open owners */
451#define OWNER_HASH_BITS 8
452#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
453#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700454
Trond Myklebustd4f04892014-07-29 21:34:36 -0400455static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400456{
457 unsigned int ret;
458
459 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500460 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400461}
NeilBrownef0f3392006-04-10 22:55:41 -0700462
NeilBrownef0f3392006-04-10 22:55:41 -0700463/* hash table for nfs4_file */
464#define FILE_HASH_BITS 8
465#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800466
Trond Myklebustca943212014-07-23 16:17:39 -0400467static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400468{
Trond Myklebustca943212014-07-23 16:17:39 -0400469 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
470}
471
472static unsigned int file_hashval(struct knfsd_fh *fh)
473{
474 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
475}
476
Jeff Layton89876f82013-04-02 09:01:59 -0400477static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700478
Jeff Layton12659652014-07-10 14:07:28 -0400479static void
480__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400481{
Jeff Layton7214e862014-07-10 14:07:33 -0400482 lockdep_assert_held(&fp->fi_lock);
483
Jeff Layton12659652014-07-10 14:07:28 -0400484 if (access & NFS4_SHARE_ACCESS_WRITE)
485 atomic_inc(&fp->fi_access[O_WRONLY]);
486 if (access & NFS4_SHARE_ACCESS_READ)
487 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400488}
489
Jeff Layton12659652014-07-10 14:07:28 -0400490static __be32
491nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400492{
Jeff Layton7214e862014-07-10 14:07:33 -0400493 lockdep_assert_held(&fp->fi_lock);
494
Jeff Layton12659652014-07-10 14:07:28 -0400495 /* Does this access mode make sense? */
496 if (access & ~NFS4_SHARE_ACCESS_BOTH)
497 return nfserr_inval;
498
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400499 /* Does it conflict with a deny mode already set? */
500 if ((access & fp->fi_share_deny) != 0)
501 return nfserr_share_denied;
502
Jeff Layton12659652014-07-10 14:07:28 -0400503 __nfs4_file_get_access(fp, access);
504 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400505}
506
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400507static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
508{
509 /* Common case is that there is no deny mode. */
510 if (deny) {
511 /* Does this deny mode make sense? */
512 if (deny & ~NFS4_SHARE_DENY_BOTH)
513 return nfserr_inval;
514
515 if ((deny & NFS4_SHARE_DENY_READ) &&
516 atomic_read(&fp->fi_access[O_RDONLY]))
517 return nfserr_share_denied;
518
519 if ((deny & NFS4_SHARE_DENY_WRITE) &&
520 atomic_read(&fp->fi_access[O_WRONLY]))
521 return nfserr_share_denied;
522 }
523 return nfs_ok;
524}
525
J. Bruce Fields998db522010-08-07 09:21:41 -0400526static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400527{
Trond Myklebustde186432014-07-10 14:07:26 -0400528 might_lock(&fp->fi_lock);
529
530 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
531 struct file *f1 = NULL;
532 struct file *f2 = NULL;
533
Jeff Layton6d338b52014-07-10 14:07:29 -0400534 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400535 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400536 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400537 spin_unlock(&fp->fi_lock);
538 if (f1)
539 fput(f1);
540 if (f2)
541 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400542 }
543}
544
Jeff Layton12659652014-07-10 14:07:28 -0400545static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400546{
Jeff Layton12659652014-07-10 14:07:28 -0400547 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
548
549 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400550 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400551 if (access & NFS4_SHARE_ACCESS_READ)
552 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400553}
554
Sachin Bhamare8287f002015-04-27 14:50:14 +0200555/*
556 * Allocate a new open/delegation state counter. This is needed for
557 * pNFS for proper return on close semantics.
558 *
559 * Note that we only allocate it for pNFS-enabled exports, otherwise
560 * all pointers to struct nfs4_clnt_odstate are always NULL.
561 */
562static struct nfs4_clnt_odstate *
563alloc_clnt_odstate(struct nfs4_client *clp)
564{
565 struct nfs4_clnt_odstate *co;
566
567 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
568 if (co) {
569 co->co_client = clp;
570 atomic_set(&co->co_odcount, 1);
571 }
572 return co;
573}
574
575static void
576hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
577{
578 struct nfs4_file *fp = co->co_file;
579
580 lockdep_assert_held(&fp->fi_lock);
581 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
582}
583
584static inline void
585get_clnt_odstate(struct nfs4_clnt_odstate *co)
586{
587 if (co)
588 atomic_inc(&co->co_odcount);
589}
590
591static void
592put_clnt_odstate(struct nfs4_clnt_odstate *co)
593{
594 struct nfs4_file *fp;
595
596 if (!co)
597 return;
598
599 fp = co->co_file;
600 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
601 list_del(&co->co_perfile);
602 spin_unlock(&fp->fi_lock);
603
604 nfsd4_return_all_file_layouts(co->co_client, fp);
605 kmem_cache_free(odstate_slab, co);
606 }
607}
608
609static struct nfs4_clnt_odstate *
610find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
611{
612 struct nfs4_clnt_odstate *co;
613 struct nfs4_client *cl;
614
615 if (!new)
616 return NULL;
617
618 cl = new->co_client;
619
620 spin_lock(&fp->fi_lock);
621 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
622 if (co->co_client == cl) {
623 get_clnt_odstate(co);
624 goto out;
625 }
626 }
627 co = new;
628 co->co_file = fp;
629 hash_clnt_odstate_locked(new);
630out:
631 spin_unlock(&fp->fi_lock);
632 return co;
633}
634
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200635struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
Trond Myklebust60116952014-07-29 21:34:06 -0400636 struct kmem_cache *slab)
J. Bruce Fields996e0932011-10-17 11:14:48 -0400637{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500638 struct nfs4_stid *stid;
639 int new_id;
640
Trond Myklebustf8338832014-07-25 07:34:19 -0400641 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500642 if (!stid)
643 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400644
Jeff Layton4770d722014-07-29 21:34:10 -0400645 idr_preload(GFP_KERNEL);
646 spin_lock(&cl->cl_lock);
647 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
648 spin_unlock(&cl->cl_lock);
649 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700650 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500651 goto out_free;
652 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500653 stid->sc_stateid.si_opaque.so_id = new_id;
654 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
655 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400656 atomic_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400657 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500658
J. Bruce Fields996e0932011-10-17 11:14:48 -0400659 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500660 * It shouldn't be a problem to reuse an opaque stateid value.
661 * I don't think it is for 4.1. But with 4.0 I worry that, for
662 * example, a stray write retransmission could be accepted by
663 * the server when it should have been rejected. Therefore,
664 * adopt a trick from the sctp code to attempt to maximize the
665 * amount of time until an id is reused, by ensuring they always
666 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400667 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500668 return stid;
669out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800670 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500671 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400672}
673
Jeff Laytonb49e0842014-07-29 21:34:11 -0400674static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400675{
Trond Myklebust60116952014-07-29 21:34:06 -0400676 struct nfs4_stid *stid;
677 struct nfs4_ol_stateid *stp;
678
679 stid = nfs4_alloc_stid(clp, stateid_slab);
680 if (!stid)
681 return NULL;
682
683 stp = openlockstateid(stid);
684 stp->st_stid.sc_free = nfs4_free_ol_stateid;
685 return stp;
686}
687
688static void nfs4_free_deleg(struct nfs4_stid *stid)
689{
Trond Myklebust60116952014-07-29 21:34:06 -0400690 kmem_cache_free(deleg_slab, stid);
691 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400692}
693
NeilBrown6282cd52014-06-04 17:39:26 +1000694/*
695 * When we recall a delegation, we should be careful not to hand it
696 * out again straight away.
697 * To ensure this we keep a pair of bloom filters ('new' and 'old')
698 * in which the filehandles of recalled delegations are "stored".
699 * If a filehandle appear in either filter, a delegation is blocked.
700 * When a delegation is recalled, the filehandle is stored in the "new"
701 * filter.
702 * Every 30 seconds we swap the filters and clear the "new" one,
703 * unless both are empty of course.
704 *
705 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
706 * low 3 bytes as hash-table indices.
707 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400708 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000709 * is used to manage concurrent access. Testing does not need the lock
710 * except when swapping the two filters.
711 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400712static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000713static struct bloom_pair {
714 int entries, old_entries;
715 time_t swap_time;
716 int new; /* index into 'set' */
717 DECLARE_BITMAP(set[2], 256);
718} blocked_delegations;
719
720static int delegation_blocked(struct knfsd_fh *fh)
721{
722 u32 hash;
723 struct bloom_pair *bd = &blocked_delegations;
724
725 if (bd->entries == 0)
726 return 0;
727 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400728 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000729 if (seconds_since_boot() - bd->swap_time > 30) {
730 bd->entries -= bd->old_entries;
731 bd->old_entries = bd->entries;
732 memset(bd->set[bd->new], 0,
733 sizeof(bd->set[0]));
734 bd->new = 1-bd->new;
735 bd->swap_time = seconds_since_boot();
736 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400737 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000738 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100739 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000740 if (test_bit(hash&255, bd->set[0]) &&
741 test_bit((hash>>8)&255, bd->set[0]) &&
742 test_bit((hash>>16)&255, bd->set[0]))
743 return 1;
744
745 if (test_bit(hash&255, bd->set[1]) &&
746 test_bit((hash>>8)&255, bd->set[1]) &&
747 test_bit((hash>>16)&255, bd->set[1]))
748 return 1;
749
750 return 0;
751}
752
753static void block_delegations(struct knfsd_fh *fh)
754{
755 u32 hash;
756 struct bloom_pair *bd = &blocked_delegations;
757
Daniel Borkmann87545892014-12-10 16:33:11 +0100758 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000759
Jeff Laytonf54fe962014-07-25 07:34:26 -0400760 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000761 __set_bit(hash&255, bd->set[bd->new]);
762 __set_bit((hash>>8)&255, bd->set[bd->new]);
763 __set_bit((hash>>16)&255, bd->set[bd->new]);
764 if (bd->entries == 0)
765 bd->swap_time = seconds_since_boot();
766 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400767 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static struct nfs4_delegation *
Sachin Bhamare8287f002015-04-27 14:50:14 +0200771alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
772 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400775 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400778 n = atomic_long_inc_return(&num_delegations);
779 if (n < 0 || n > max_delegations)
780 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000781 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400782 goto out_dec;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400783 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700784 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400785 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400786
787 dp->dl_stid.sc_free = nfs4_free_deleg;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400788 /*
789 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400790 * meaning of seqid 0 isn't meaningful, really, but let's avoid
791 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400792 */
793 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700794 INIT_LIST_HEAD(&dp->dl_perfile);
795 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200797 dp->dl_clnt_odstate = odstate;
798 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400799 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200800 dp->dl_retries = 1;
801 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200802 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400804out_dec:
805 atomic_long_dec(&num_delegations);
806 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809void
Trond Myklebust60116952014-07-29 21:34:06 -0400810nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Trond Myklebust11b91642014-07-29 21:34:08 -0400812 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400813 struct nfs4_client *clp = s->sc_client;
814
Jeff Layton4770d722014-07-29 21:34:10 -0400815 might_lock(&clp->cl_lock);
816
Jeff Laytonb401be222014-07-29 21:34:33 -0400817 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
818 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400819 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400820 }
Trond Myklebust60116952014-07-29 21:34:06 -0400821 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400822 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400823 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400824 if (fp)
825 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Jeff Layton9767feb2015-10-01 09:05:50 -0400828void
829nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
830{
831 stateid_t *src = &stid->sc_stateid;
832
833 spin_lock(&stid->sc_lock);
834 if (unlikely(++src->si_generation == 0))
835 src->si_generation = 1;
836 memcpy(dst, src, sizeof(*dst));
837 spin_unlock(&stid->sc_lock);
838}
839
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500840static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400842 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400843
Jeff Layton6bcc0342014-08-09 10:22:40 -0400844 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500845 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400846 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400847 spin_unlock(&fp->fi_lock);
848
849 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100850 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400851 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200855void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400856{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500857 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400858}
859
Andrew Elble34ed9872015-10-15 12:07:28 -0400860/**
861 * nfs4_get_existing_delegation - Discover if this delegation already exists
862 * @clp: a pointer to the nfs4_client we're granting a delegation to
863 * @fp: a pointer to the nfs4_file we're granting a delegation on
864 *
865 * Return:
866 * On success: NULL if an existing delegation was not found.
867 *
868 * On error: -EAGAIN if one was previously granted to this nfs4_client
869 * for this nfs4_file.
870 *
871 */
872
873static int
874nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400875{
Andrew Elble34ed9872015-10-15 12:07:28 -0400876 struct nfs4_delegation *searchdp = NULL;
877 struct nfs4_client *searchclp = NULL;
878
Benny Halevycdc97502014-05-30 09:09:30 -0400879 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400880 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400881
Andrew Elble34ed9872015-10-15 12:07:28 -0400882 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
883 searchclp = searchdp->dl_stid.sc_client;
884 if (clp == searchclp) {
885 return -EAGAIN;
886 }
887 }
888 return 0;
889}
890
891/**
892 * hash_delegation_locked - Add a delegation to the appropriate lists
893 * @dp: a pointer to the nfs4_delegation we are adding.
894 * @fp: a pointer to the nfs4_file we're granting a delegation on
895 *
896 * Return:
897 * On success: NULL if the delegation was successfully hashed.
898 *
899 * On error: -EAGAIN if one was previously granted to this
900 * nfs4_client for this nfs4_file. Delegation is not hashed.
901 *
902 */
903
904static int
905hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
906{
907 int status;
908 struct nfs4_client *clp = dp->dl_stid.sc_client;
909
910 lockdep_assert_held(&state_lock);
911 lockdep_assert_held(&fp->fi_lock);
912
913 status = nfs4_get_existing_delegation(clp, fp);
914 if (status)
915 return status;
916 ++fp->fi_delegees;
Trond Myklebust67cb1272014-07-29 21:34:17 -0400917 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400918 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400919 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -0400920 list_add(&dp->dl_perclnt, &clp->cl_delegations);
921 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -0400922}
923
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400924static bool
Jeff Layton42690672014-07-25 07:34:20 -0400925unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Trond Myklebust11b91642014-07-29 21:34:08 -0400927 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400928
Jeff Layton42690672014-07-25 07:34:20 -0400929 lockdep_assert_held(&state_lock);
930
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400931 if (list_empty(&dp->dl_perfile))
932 return false;
933
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400934 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400935 /* Ensure that deleg break won't try to requeue it */
936 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400937 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400938 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400940 list_del_init(&dp->dl_perfile);
941 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400942 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400943}
944
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400945static void destroy_delegation(struct nfs4_delegation *dp)
946{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400947 bool unhashed;
948
Jeff Layton42690672014-07-25 07:34:20 -0400949 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400950 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400951 spin_unlock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400952 if (unhashed) {
953 put_clnt_odstate(dp->dl_clnt_odstate);
954 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
955 nfs4_put_stid(&dp->dl_stid);
956 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400957}
958
959static void revoke_delegation(struct nfs4_delegation *dp)
960{
961 struct nfs4_client *clp = dp->dl_stid.sc_client;
962
Jeff Layton2d4a5322014-07-25 07:34:21 -0400963 WARN_ON(!list_empty(&dp->dl_recall_lru));
964
Sachin Bhamare8287f002015-04-27 14:50:14 +0200965 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -0400966 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
967
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400968 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400969 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400970 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400971 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400972 spin_lock(&clp->cl_lock);
973 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
974 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400975 }
976}
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/*
979 * SETCLIENTID state
980 */
981
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400982static unsigned int clientid_hashval(u32 id)
983{
984 return id & CLIENT_HASH_MASK;
985}
986
987static unsigned int clientstr_hashval(const char *name)
988{
989 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400993 * We store the NONE, READ, WRITE, and BOTH bits separately in the
994 * st_{access,deny}_bmap field of the stateid, in order to track not
995 * only what share bits are currently in force, but also what
996 * combinations of share bits previous opens have used. This allows us
997 * to enforce the recommendation of rfc 3530 14.2.19 that the server
998 * return an error if the client attempt to downgrade to a combination
999 * of share bits not explicable by closing some of its previous opens.
1000 *
1001 * XXX: This enforcement is actually incomplete, since we don't keep
1002 * track of access/deny bit combinations; so, e.g., we allow:
1003 *
1004 * OPEN allow read, deny write
1005 * OPEN allow both, deny none
1006 * DOWNGRADE allow read, deny none
1007 *
1008 * which we should reject.
1009 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001010static unsigned int
1011bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001012 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001013 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001014
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001015 for (i = 1; i < 4; i++) {
1016 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001017 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001018 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001019 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001020}
1021
Jeff Layton82c5ff12012-05-11 09:45:13 -04001022/* set share access for a given stateid */
1023static inline void
1024set_access(u32 access, struct nfs4_ol_stateid *stp)
1025{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001026 unsigned char mask = 1 << access;
1027
1028 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1029 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001030}
1031
1032/* clear share access for a given stateid */
1033static inline void
1034clear_access(u32 access, struct nfs4_ol_stateid *stp)
1035{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001036 unsigned char mask = 1 << access;
1037
1038 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1039 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001040}
1041
1042/* test whether a given stateid has access */
1043static inline bool
1044test_access(u32 access, struct nfs4_ol_stateid *stp)
1045{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001046 unsigned char mask = 1 << access;
1047
1048 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001049}
1050
Jeff Laytonce0fc432012-05-11 09:45:14 -04001051/* set share deny for a given stateid */
1052static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001053set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001054{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001055 unsigned char mask = 1 << deny;
1056
1057 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1058 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001059}
1060
1061/* clear share deny for a given stateid */
1062static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001063clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001064{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001065 unsigned char mask = 1 << deny;
1066
1067 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1068 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001069}
1070
1071/* test whether a given stateid is denying specific access */
1072static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001073test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001074{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001075 unsigned char mask = 1 << deny;
1076
1077 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001078}
1079
1080static int nfs4_access_to_omode(u32 access)
1081{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001082 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001083 case NFS4_SHARE_ACCESS_READ:
1084 return O_RDONLY;
1085 case NFS4_SHARE_ACCESS_WRITE:
1086 return O_WRONLY;
1087 case NFS4_SHARE_ACCESS_BOTH:
1088 return O_RDWR;
1089 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001090 WARN_ON_ONCE(1);
1091 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001092}
1093
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001094/*
1095 * A stateid that had a deny mode associated with it is being released
1096 * or downgraded. Recalculate the deny mode on the file.
1097 */
1098static void
1099recalculate_deny_mode(struct nfs4_file *fp)
1100{
1101 struct nfs4_ol_stateid *stp;
1102
1103 spin_lock(&fp->fi_lock);
1104 fp->fi_share_deny = 0;
1105 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1106 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1107 spin_unlock(&fp->fi_lock);
1108}
1109
1110static void
1111reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1112{
1113 int i;
1114 bool change = false;
1115
1116 for (i = 1; i < 4; i++) {
1117 if ((i & deny) != i) {
1118 change = true;
1119 clear_deny(i, stp);
1120 }
1121 }
1122
1123 /* Recalculate per-file deny mode if there was a change */
1124 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001125 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001126}
1127
Jeff Layton82c5ff12012-05-11 09:45:13 -04001128/* release all access and file references for a given stateid */
1129static void
1130release_all_access(struct nfs4_ol_stateid *stp)
1131{
1132 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001133 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001134
1135 if (fp && stp->st_deny_bmap != 0)
1136 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001137
1138 for (i = 1; i < 4; i++) {
1139 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001140 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001141 clear_access(i, stp);
1142 }
1143}
1144
Kinglong Meed50ffde2015-07-16 12:05:07 +08001145static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1146{
1147 kfree(sop->so_owner.data);
1148 sop->so_ops->so_free(sop);
1149}
1150
Jeff Layton6b180f02014-07-29 21:34:26 -04001151static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1152{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001153 struct nfs4_client *clp = sop->so_client;
1154
1155 might_lock(&clp->cl_lock);
1156
1157 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001158 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001159 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001160 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001161 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001162}
1163
Jeff Laytone8568732015-08-24 12:41:47 -04001164static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001165{
Trond Myklebust11b91642014-07-29 21:34:08 -04001166 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001167
Jeff Layton1c755dc2014-07-29 21:34:12 -04001168 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1169
Jeff Laytone8568732015-08-24 12:41:47 -04001170 if (list_empty(&stp->st_perfile))
1171 return false;
1172
Trond Myklebust1d31a252014-07-10 14:07:25 -04001173 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001174 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001175 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001176 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001177 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001178}
1179
Trond Myklebust60116952014-07-29 21:34:06 -04001180static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001181{
Trond Myklebust60116952014-07-29 21:34:06 -04001182 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001183
Sachin Bhamare8287f002015-04-27 14:50:14 +02001184 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001185 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001186 if (stp->st_stateowner)
1187 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001188 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001189}
1190
Jeff Laytonb49e0842014-07-29 21:34:11 -04001191static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001192{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001193 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1194 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001195 struct file *file;
1196
Jeff Laytonb49e0842014-07-29 21:34:11 -04001197 file = find_any_file(stp->st_stid.sc_file);
1198 if (file)
1199 filp_close(file, (fl_owner_t)lo);
1200 nfs4_free_ol_stateid(stid);
1201}
1202
Jeff Layton2c41beb2014-07-29 21:34:41 -04001203/*
1204 * Put the persistent reference to an already unhashed generic stateid, while
1205 * holding the cl_lock. If it's the last reference, then put it onto the
1206 * reaplist for later destruction.
1207 */
1208static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1209 struct list_head *reaplist)
1210{
1211 struct nfs4_stid *s = &stp->st_stid;
1212 struct nfs4_client *clp = s->sc_client;
1213
1214 lockdep_assert_held(&clp->cl_lock);
1215
1216 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1217
1218 if (!atomic_dec_and_test(&s->sc_count)) {
1219 wake_up_all(&close_wq);
1220 return;
1221 }
1222
1223 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1224 list_add(&stp->st_locks, reaplist);
1225}
1226
Jeff Laytone8568732015-08-24 12:41:47 -04001227static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001228{
1229 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1230
1231 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1232
1233 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001234 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001235 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001236}
1237
Jeff Layton5adfd882014-07-29 21:34:31 -04001238static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001239{
Jeff Layton1c755dc2014-07-29 21:34:12 -04001240 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001241 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001242
1243 spin_lock(&oo->oo_owner.so_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001244 unhashed = unhash_lock_stateid(stp);
Jeff Layton1c755dc2014-07-29 21:34:12 -04001245 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001246 if (unhashed)
1247 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001248}
1249
Trond Myklebustc58c6612014-07-29 21:34:35 -04001250static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001251{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001252 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001253
Trond Myklebustd4f04892014-07-29 21:34:36 -04001254 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001255
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001256 list_del_init(&lo->lo_owner.so_strhash);
1257}
1258
Jeff Layton2c41beb2014-07-29 21:34:41 -04001259/*
1260 * Free a list of generic stateids that were collected earlier after being
1261 * fully unhashed.
1262 */
1263static void
1264free_ol_stateid_reaplist(struct list_head *reaplist)
1265{
1266 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001267 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001268
1269 might_sleep();
1270
1271 while (!list_empty(reaplist)) {
1272 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1273 st_locks);
1274 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001275 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001276 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001277 if (fp)
1278 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001279 }
1280}
1281
Jeff Laytond83017f2014-07-29 21:34:42 -04001282static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1283 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001284{
1285 struct nfs4_ol_stateid *stp;
1286
Jeff Laytone8568732015-08-24 12:41:47 -04001287 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1288
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001289 while (!list_empty(&open_stp->st_locks)) {
1290 stp = list_entry(open_stp->st_locks.next,
1291 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001292 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001293 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001294 }
1295}
1296
Jeff Laytone8568732015-08-24 12:41:47 -04001297static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001298 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001299{
Jeff Laytone8568732015-08-24 12:41:47 -04001300 bool unhashed;
1301
Jeff Layton2c41beb2014-07-29 21:34:41 -04001302 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1303
Jeff Laytone8568732015-08-24 12:41:47 -04001304 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001305 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001306 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001307}
1308
1309static void release_open_stateid(struct nfs4_ol_stateid *stp)
1310{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001311 LIST_HEAD(reaplist);
1312
1313 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001314 if (unhash_open_stateid(stp, &reaplist))
1315 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001316 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1317 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001318}
1319
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001320static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001321{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001322 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001323
Trond Myklebustd4f04892014-07-29 21:34:36 -04001324 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001325
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001326 list_del_init(&oo->oo_owner.so_strhash);
1327 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001328}
1329
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001330static void release_last_closed_stateid(struct nfs4_openowner *oo)
1331{
Jeff Layton217526e2014-07-30 08:27:11 -04001332 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1333 nfsd_net_id);
1334 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001335
Jeff Layton217526e2014-07-30 08:27:11 -04001336 spin_lock(&nn->client_lock);
1337 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001338 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001339 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001340 oo->oo_last_closed_stid = NULL;
1341 }
Jeff Layton217526e2014-07-30 08:27:11 -04001342 spin_unlock(&nn->client_lock);
1343 if (s)
1344 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001345}
1346
Jeff Layton2c41beb2014-07-29 21:34:41 -04001347static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001348{
1349 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001350 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001351 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001352
Jeff Layton2c41beb2014-07-29 21:34:41 -04001353 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001354
Trond Myklebustd4f04892014-07-29 21:34:36 -04001355 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001356 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001357 while (!list_empty(&oo->oo_owner.so_stateids)) {
1358 stp = list_first_entry(&oo->oo_owner.so_stateids,
1359 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001360 if (unhash_open_stateid(stp, &reaplist))
1361 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001362 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001363 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001364 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001365 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001366 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001367}
1368
Marc Eshel5282fd72009-04-03 08:27:52 +03001369static inline int
1370hash_sessionid(struct nfs4_sessionid *sessionid)
1371{
1372 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1373
1374 return sid->sequence % SESSION_HASH_SIZE;
1375}
1376
Mark Salter135dd002015-04-06 09:46:00 -04001377#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001378static inline void
1379dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1380{
1381 u32 *ptr = (u32 *)(&sessionid->data[0]);
1382 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1383}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001384#else
1385static inline void
1386dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1387{
1388}
1389#endif
1390
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001391/*
1392 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1393 * won't be used for replay.
1394 */
1395void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1396{
1397 struct nfs4_stateowner *so = cstate->replay_owner;
1398
1399 if (nfserr == nfserr_replay_me)
1400 return;
1401
1402 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001403 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001404 return;
1405 }
1406 if (!so)
1407 return;
1408 if (so->so_is_open_owner)
1409 release_last_closed_stateid(openowner(so));
1410 so->so_seqid++;
1411 return;
1412}
Marc Eshel5282fd72009-04-03 08:27:52 +03001413
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001414static void
1415gen_sessionid(struct nfsd4_session *ses)
1416{
1417 struct nfs4_client *clp = ses->se_client;
1418 struct nfsd4_sessionid *sid;
1419
1420 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1421 sid->clientid = clp->cl_clientid;
1422 sid->sequence = current_sessionid++;
1423 sid->reserved = 0;
1424}
1425
1426/*
Andy Adamsona6496372009-08-28 08:45:01 -04001427 * The protocol defines ca_maxresponssize_cached to include the size of
1428 * the rpc header, but all we need to cache is the data starting after
1429 * the end of the initial SEQUENCE operation--the rest we regenerate
1430 * each time. Therefore we can advertise a ca_maxresponssize_cached
1431 * value that is the number of bytes in our cache plus a few additional
1432 * bytes. In order to stay on the safe side, and not promise more than
1433 * we can cache, those additional bytes must be the minimum possible: 24
1434 * bytes of rpc header (xid through accept state, with AUTH_NULL
1435 * verifier), 12 for the compound header (with zero-length tag), and 44
1436 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001437 */
Andy Adamsona6496372009-08-28 08:45:01 -04001438#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1439
Andy Adamson557ce262009-08-28 08:45:04 -04001440static void
1441free_session_slots(struct nfsd4_session *ses)
1442{
1443 int i;
1444
1445 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1446 kfree(ses->se_slots[i]);
1447}
1448
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001449/*
1450 * We don't actually need to cache the rpc and session headers, so we
1451 * can allocate a little less for each slot:
1452 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001453static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001454{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001455 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001456
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001457 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1458 size = 0;
1459 else
1460 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1461 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001462}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001463
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001464/*
1465 * XXX: If we run out of reserved DRC memory we could (up to a point)
1466 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001467 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001468 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001469static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001470{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001471 u32 slotsize = slot_bytes(ca);
1472 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001473 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001474
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001475 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001476 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1477 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001478 num = min_t(int, num, avail / slotsize);
1479 nfsd_drc_mem_used += num * slotsize;
1480 spin_unlock(&nfsd_drc_lock);
1481
1482 return num;
1483}
1484
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001485static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001486{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001487 int slotsize = slot_bytes(ca);
1488
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001489 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001490 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001491 spin_unlock(&nfsd_drc_lock);
1492}
1493
Kinglong Mee60810e52014-01-01 00:35:47 +08001494static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1495 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001496{
Kinglong Mee60810e52014-01-01 00:35:47 +08001497 int numslots = fattrs->maxreqs;
1498 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001499 struct nfsd4_session *new;
1500 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001501
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001502 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001503 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1504 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001505
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001506 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001507 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001508 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001509 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001510 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001511 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001512 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001513 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001514 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001515
1516 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1517 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1518
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001519 return new;
1520out_free:
1521 while (i--)
1522 kfree(new->se_slots[i]);
1523 kfree(new);
1524 return NULL;
1525}
1526
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001527static void free_conn(struct nfsd4_conn *c)
1528{
1529 svc_xprt_put(c->cn_xprt);
1530 kfree(c);
1531}
1532
1533static void nfsd4_conn_lost(struct svc_xpt_user *u)
1534{
1535 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1536 struct nfs4_client *clp = c->cn_session->se_client;
1537
1538 spin_lock(&clp->cl_lock);
1539 if (!list_empty(&c->cn_persession)) {
1540 list_del(&c->cn_persession);
1541 free_conn(c);
1542 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001543 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001544 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001545}
1546
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001547static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001548{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001549 struct nfsd4_conn *conn;
1550
1551 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1552 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001553 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001554 svc_xprt_get(rqstp->rq_xprt);
1555 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001556 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001557 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1558 return conn;
1559}
1560
J. Bruce Fields328ead22010-09-29 16:11:06 -04001561static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1562{
1563 conn->cn_session = ses;
1564 list_add(&conn->cn_persession, &ses->se_conns);
1565}
1566
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001567static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1568{
1569 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001570
1571 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001572 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001573 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001574}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001575
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001576static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001577{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001578 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001579 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001580}
1581
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001582static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001583{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001584 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001585
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001586 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001587 ret = nfsd4_register_conn(conn);
1588 if (ret)
1589 /* oops; xprt is already down: */
1590 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001591 /* We may have gained or lost a callback channel: */
1592 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001593}
1594
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001595static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001596{
1597 u32 dir = NFS4_CDFC4_FORE;
1598
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001599 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001600 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001601 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001602}
1603
1604/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001605static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001606{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001607 struct nfs4_client *clp = s->se_client;
1608 struct nfsd4_conn *c;
1609
1610 spin_lock(&clp->cl_lock);
1611 while (!list_empty(&s->se_conns)) {
1612 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1613 list_del_init(&c->cn_persession);
1614 spin_unlock(&clp->cl_lock);
1615
1616 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1617 free_conn(c);
1618
1619 spin_lock(&clp->cl_lock);
1620 }
1621 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001622}
1623
J. Bruce Fields1377b692012-09-11 21:42:40 -04001624static void __free_session(struct nfsd4_session *ses)
1625{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001626 free_session_slots(ses);
1627 kfree(ses);
1628}
1629
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001630static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001631{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001632 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001633 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001634 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001635}
Andy Adamson557ce262009-08-28 08:45:04 -04001636
Fengguang Wu135ae822012-11-10 07:20:25 -05001637static 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 -04001638{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001639 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001640 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001641
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001642 new->se_client = clp;
1643 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001644
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001645 INIT_LIST_HEAD(&new->se_conns);
1646
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001647 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001648 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001649 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001650 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001651 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001652 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001653 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001654 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001655 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001656 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001657
Chuck Leverb0d2e422014-08-22 15:10:59 -04001658 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001659 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001660 /*
1661 * This is a little silly; with sessions there's no real
1662 * use for the callback address. Use the peer address
1663 * as a reasonable default for now, but consider fixing
1664 * the rpc client not to require an address in the
1665 * future:
1666 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001667 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1668 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001669 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001670}
1671
Benny Halevy9089f1b2010-05-12 00:12:26 +03001672/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001673static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001674__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001675{
1676 struct nfsd4_session *elem;
1677 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001678 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001679
Trond Myklebust0a880a22014-07-30 08:27:10 -04001680 lockdep_assert_held(&nn->client_lock);
1681
Marc Eshel5282fd72009-04-03 08:27:52 +03001682 dump_sessionid(__func__, sessionid);
1683 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001684 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001685 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001686 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1687 NFS4_MAX_SESSIONID_LEN)) {
1688 return elem;
1689 }
1690 }
1691
1692 dprintk("%s: session not found\n", __func__);
1693 return NULL;
1694}
1695
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001696static struct nfsd4_session *
1697find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1698 __be32 *ret)
1699{
1700 struct nfsd4_session *session;
1701 __be32 status = nfserr_badsession;
1702
1703 session = __find_in_sessionid_hashtbl(sessionid, net);
1704 if (!session)
1705 goto out;
1706 status = nfsd4_get_session_locked(session);
1707 if (status)
1708 session = NULL;
1709out:
1710 *ret = status;
1711 return session;
1712}
1713
Benny Halevy9089f1b2010-05-12 00:12:26 +03001714/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001715static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001716unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001717{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001718 struct nfs4_client *clp = ses->se_client;
1719 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1720
1721 lockdep_assert_held(&nn->client_lock);
1722
Andy Adamson7116ed62009-04-03 08:27:43 +03001723 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001724 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001725 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001726 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1730static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001731STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001733 /*
1734 * We're assuming the clid was not given out from a boot
1735 * precisely 2^32 (about 136 years) before this one. That seems
1736 * a safe assumption:
1737 */
1738 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001740 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001741 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 1;
1743}
1744
1745/*
1746 * XXX Should we use a slab cache ?
1747 * This type of memory management is somewhat inefficient, but we use it
1748 * anyway since SETCLIENTID is not a common operation.
1749 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001750static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001753 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001755 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1756 if (clp == NULL)
1757 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001758 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001759 if (clp->cl_name.data == NULL)
1760 goto err_no_name;
1761 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1762 OWNER_HASH_SIZE, GFP_KERNEL);
1763 if (!clp->cl_ownerstr_hashtbl)
1764 goto err_no_hashtbl;
1765 for (i = 0; i < OWNER_HASH_SIZE; i++)
1766 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001767 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001768 INIT_LIST_HEAD(&clp->cl_sessions);
1769 idr_init(&clp->cl_stateids);
1770 atomic_set(&clp->cl_refcount, 0);
1771 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1772 INIT_LIST_HEAD(&clp->cl_idhash);
1773 INIT_LIST_HEAD(&clp->cl_openowners);
1774 INIT_LIST_HEAD(&clp->cl_delegations);
1775 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001776 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001777#ifdef CONFIG_NFSD_PNFS
1778 INIT_LIST_HEAD(&clp->cl_lo_states);
1779#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001780 spin_lock_init(&clp->cl_lock);
1781 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001783err_no_hashtbl:
1784 kfree(clp->cl_name.data);
1785err_no_name:
1786 kfree(clp);
1787 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001790static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791free_client(struct nfs4_client *clp)
1792{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001793 while (!list_empty(&clp->cl_sessions)) {
1794 struct nfsd4_session *ses;
1795 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1796 se_perclnt);
1797 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001798 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1799 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001800 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001801 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001802 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001803 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001805 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 kfree(clp);
1807}
1808
Benny Halevy84d38ac2010-05-12 00:13:16 +03001809/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001810static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001811unhash_client_locked(struct nfs4_client *clp)
1812{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001813 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001814 struct nfsd4_session *ses;
1815
Trond Myklebust0a880a22014-07-30 08:27:10 -04001816 lockdep_assert_held(&nn->client_lock);
1817
Trond Myklebust4beb3452014-07-30 08:27:02 -04001818 /* Mark the client as expired! */
1819 clp->cl_time = 0;
1820 /* Make it invisible */
1821 if (!list_empty(&clp->cl_idhash)) {
1822 list_del_init(&clp->cl_idhash);
1823 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1824 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1825 else
1826 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1827 }
1828 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001829 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001830 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1831 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001832 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001833}
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001836unhash_client(struct nfs4_client *clp)
1837{
1838 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1839
1840 spin_lock(&nn->client_lock);
1841 unhash_client_locked(clp);
1842 spin_unlock(&nn->client_lock);
1843}
1844
Jeff Layton97403d92014-07-30 08:27:12 -04001845static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1846{
1847 if (atomic_read(&clp->cl_refcount))
1848 return nfserr_jukebox;
1849 unhash_client_locked(clp);
1850 return nfs_ok;
1851}
1852
Trond Myklebust4beb3452014-07-30 08:27:02 -04001853static void
1854__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001856 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct list_head reaplist;
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001861 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001862 while (!list_empty(&clp->cl_delegations)) {
1863 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001864 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001865 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Benny Halevycdc97502014-05-30 09:09:30 -04001867 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 while (!list_empty(&reaplist)) {
1869 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001870 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001871 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001872 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001873 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001875 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001876 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001877 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001878 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001879 }
NeilBrownea1da632005-06-23 22:04:17 -07001880 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001881 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001882 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001883 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001885 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001886 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001887 if (clp->cl_cb_conn.cb_xprt)
1888 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001889 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Trond Myklebust4beb3452014-07-30 08:27:02 -04001892static void
1893destroy_client(struct nfs4_client *clp)
1894{
1895 unhash_client(clp);
1896 __destroy_client(clp);
1897}
1898
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001899static void expire_client(struct nfs4_client *clp)
1900{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001901 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001902 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001903 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001904}
1905
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001906static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1907{
1908 memcpy(target->cl_verifier.data, source->data,
1909 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001912static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1913{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1915 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1916}
1917
J. Bruce Fields50043852015-11-20 15:58:37 -05001918int strdup_if_nonnull(char **target, char *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001919{
J. Bruce Fields50043852015-11-20 15:58:37 -05001920 if (source) {
1921 *target = kstrdup(source, GFP_KERNEL);
1922 if (!*target)
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001923 return -ENOMEM;
1924 } else
J. Bruce Fields50043852015-11-20 15:58:37 -05001925 *target = NULL;
1926 return 0;
1927}
1928
1929static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1930{
1931 int ret;
1932
1933 ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
1934 if (ret)
1935 return ret;
J. Bruce Fields414ca012015-11-20 10:48:02 -05001936 ret = strdup_if_nonnull(&target->cr_raw_principal,
1937 source->cr_raw_principal);
1938 if (ret)
1939 return ret;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001940 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 target->cr_uid = source->cr_uid;
1942 target->cr_gid = source->cr_gid;
1943 target->cr_group_info = source->cr_group_info;
1944 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001945 target->cr_gss_mech = source->cr_gss_mech;
1946 if (source->cr_gss_mech)
1947 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001951static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001952compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1953{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001954 if (o1->len < o2->len)
1955 return -1;
1956 if (o1->len > o2->len)
1957 return 1;
1958 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001959}
1960
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001961static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001962{
NeilBrowna55370a2005-06-23 22:03:52 -07001963 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
1966static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001967same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1968{
1969 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001973same_clid(clientid_t *cl1, clientid_t *cl2)
1974{
1975 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001978static bool groups_equal(struct group_info *g1, struct group_info *g2)
1979{
1980 int i;
1981
1982 if (g1->ngroups != g2->ngroups)
1983 return false;
1984 for (i=0; i<g1->ngroups; i++)
Eric W. Biederman6fab8772013-02-02 06:53:11 -08001985 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001986 return false;
1987 return true;
1988}
1989
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001990/*
1991 * RFC 3530 language requires clid_inuse be returned when the
1992 * "principal" associated with a requests differs from that previously
1993 * used. We use uid, gid's, and gss principal string as our best
1994 * approximation. We also don't want to allow non-gss use of a client
1995 * established using gss: in theory cr_principal should catch that
1996 * change, but in practice cr_principal can be null even in the gss case
1997 * since gssd doesn't always pass down a principal string.
1998 */
1999static bool is_gss_cred(struct svc_cred *cr)
2000{
2001 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2002 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2003}
2004
2005
Vivek Trivedi5559b502012-07-24 21:18:20 +05302006static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002007same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2008{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002009 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002010 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2011 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002012 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2013 return false;
2014 if (cr1->cr_principal == cr2->cr_principal)
2015 return true;
2016 if (!cr1->cr_principal || !cr2->cr_principal)
2017 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302018 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
J. Bruce Fields57266a62013-04-13 14:27:29 -04002021static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2022{
2023 struct svc_cred *cr = &rqstp->rq_cred;
2024 u32 service;
2025
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002026 if (!cr->cr_gss_mech)
2027 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002028 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2029 return service == RPC_GSS_SVC_INTEGRITY ||
2030 service == RPC_GSS_SVC_PRIVACY;
2031}
2032
Andrew Elblededeb132016-06-15 12:52:08 -04002033bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002034{
2035 struct svc_cred *cr = &rqstp->rq_cred;
2036
2037 if (!cl->cl_mach_cred)
2038 return true;
2039 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2040 return false;
2041 if (!svc_rqst_integrity_protected(rqstp))
2042 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002043 if (cl->cl_cred.cr_raw_principal)
2044 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2045 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002046 if (!cr->cr_principal)
2047 return false;
2048 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2049}
2050
Jeff Layton294ac322014-07-30 08:27:15 -04002051static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002052{
Chuck Leverab4684d2012-03-02 17:13:50 -05002053 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Jeff Laytonf4199922014-06-17 07:44:11 -04002055 /*
2056 * This is opaque to client, so no need to byte-swap. Use
2057 * __force to keep sparse happy
2058 */
2059 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002060 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002061 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Jeff Layton294ac322014-07-30 08:27:15 -04002064static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2065{
2066 clp->cl_clientid.cl_boot = nn->boot_time;
2067 clp->cl_clientid.cl_id = nn->clientid_counter++;
2068 gen_confirm(clp, nn);
2069}
2070
Jeff Layton4770d722014-07-29 21:34:10 -04002071static struct nfs4_stid *
2072find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002073{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002074 struct nfs4_stid *ret;
2075
2076 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2077 if (!ret || !ret->sc_type)
2078 return NULL;
2079 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002080}
2081
Jeff Layton4770d722014-07-29 21:34:10 -04002082static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002083find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002084{
2085 struct nfs4_stid *s;
2086
Jeff Layton4770d722014-07-29 21:34:10 -04002087 spin_lock(&cl->cl_lock);
2088 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002089 if (s != NULL) {
2090 if (typemask & s->sc_type)
2091 atomic_inc(&s->sc_count);
2092 else
2093 s = NULL;
2094 }
Jeff Layton4770d722014-07-29 21:34:10 -04002095 spin_unlock(&cl->cl_lock);
2096 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002097}
2098
Jeff Layton2216d442012-11-12 15:00:57 -05002099static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002100 struct svc_rqst *rqstp, nfs4_verifier *verf)
2101{
2102 struct nfs4_client *clp;
2103 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002104 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002105 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002106
2107 clp = alloc_client(name);
2108 if (clp == NULL)
2109 return NULL;
2110
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002111 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2112 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002113 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002114 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002115 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002116 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002117 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002118 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002119 copy_verf(clp, verf);
2120 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002121 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002122 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002123 return clp;
2124}
2125
NeilBrownfd39ca92005-06-23 22:04:03 -07002126static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002127add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2128{
2129 struct rb_node **new = &(root->rb_node), *parent = NULL;
2130 struct nfs4_client *clp;
2131
2132 while (*new) {
2133 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2134 parent = *new;
2135
2136 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2137 new = &((*new)->rb_left);
2138 else
2139 new = &((*new)->rb_right);
2140 }
2141
2142 rb_link_node(&new_clp->cl_namenode, parent, new);
2143 rb_insert_color(&new_clp->cl_namenode, root);
2144}
2145
2146static struct nfs4_client *
2147find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2148{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002149 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002150 struct rb_node *node = root->rb_node;
2151 struct nfs4_client *clp;
2152
2153 while (node) {
2154 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2155 cmp = compare_blob(&clp->cl_name, name);
2156 if (cmp > 0)
2157 node = node->rb_left;
2158 else if (cmp < 0)
2159 node = node->rb_right;
2160 else
2161 return clp;
2162 }
2163 return NULL;
2164}
2165
2166static void
2167add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002170 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Trond Myklebust0a880a22014-07-30 08:27:10 -04002172 lockdep_assert_held(&nn->client_lock);
2173
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002174 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002175 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002177 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002178 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
NeilBrownfd39ca92005-06-23 22:04:03 -07002181static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182move_to_confirmed(struct nfs4_client *clp)
2183{
2184 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002185 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Trond Myklebust0a880a22014-07-30 08:27:10 -04002187 lockdep_assert_held(&nn->client_lock);
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002190 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002191 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002192 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002193 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002194 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002198find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 struct nfs4_client *clp;
2201 unsigned int idhashval = clientid_hashval(clid->cl_id);
2202
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002203 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002204 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002205 if ((bool)clp->cl_minorversion != sessions)
2206 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002207 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211 return NULL;
2212}
2213
2214static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002215find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2216{
2217 struct list_head *tbl = nn->conf_id_hashtbl;
2218
Trond Myklebust0a880a22014-07-30 08:27:10 -04002219 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002220 return find_client_in_id_table(tbl, clid, sessions);
2221}
2222
2223static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002224find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002226 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Trond Myklebust0a880a22014-07-30 08:27:10 -04002228 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002229 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002232static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002233{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002234 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002235}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002236
NeilBrown28ce6052005-06-23 22:03:56 -07002237static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002238find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002239{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002240 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002241 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002242}
2243
2244static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002245find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002246{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002247 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002248 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002249}
2250
NeilBrownfd39ca92005-06-23 22:04:03 -07002251static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002252gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002254 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002255 struct sockaddr *sa = svc_addr(rqstp);
2256 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002257 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Jeff Layton7077ecb2009-08-14 12:57:58 -04002259 /* Currently, we only support tcp and tcp6 for the callback channel */
2260 if (se->se_callback_netid_len == 3 &&
2261 !memcmp(se->se_callback_netid_val, "tcp", 3))
2262 expected_family = AF_INET;
2263 else if (se->se_callback_netid_len == 4 &&
2264 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2265 expected_family = AF_INET6;
2266 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto out_err;
2268
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002269 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002270 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002271 (struct sockaddr *)&conn->cb_addr,
2272 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002273
J. Bruce Fields07263f12010-05-31 19:09:40 -04002274 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002276
J. Bruce Fields07263f12010-05-31 19:09:40 -04002277 if (conn->cb_addr.ss_family == AF_INET6)
2278 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002279
J. Bruce Fields07263f12010-05-31 19:09:40 -04002280 conn->cb_prog = se->se_callback_prog;
2281 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002282 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return;
2284out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002285 conn->cb_addr.ss_family = AF_UNSPEC;
2286 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002287 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 "will not receive delegations\n",
2289 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return;
2292}
2293
Andy Adamson074fe892009-04-03 08:28:15 +03002294/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002295 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002296 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002297static void
Andy Adamson074fe892009-04-03 08:28:15 +03002298nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2299{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002300 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002301 struct nfsd4_slot *slot = resp->cstate.slot;
2302 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002303
Andy Adamson557ce262009-08-28 08:45:04 -04002304 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002305
Andy Adamson557ce262009-08-28 08:45:04 -04002306 slot->sl_opcnt = resp->opcnt;
2307 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002308
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002309 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002310 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002311 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002312 return;
2313 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002314 base = resp->cstate.data_offset;
2315 slot->sl_datalen = buf->len - base;
2316 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002317 WARN(1, "%s: sessions DRC could not cache compound\n",
2318 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002319 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002320}
2321
2322/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002323 * Encode the replay sequence operation from the slot values.
2324 * If cachethis is FALSE encode the uncached rep error on the next
2325 * operation which sets resp->p and increments resp->opcnt for
2326 * nfs4svc_encode_compoundres.
2327 *
Andy Adamson074fe892009-04-03 08:28:15 +03002328 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002329static __be32
2330nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2331 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002332{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002333 struct nfsd4_op *op;
2334 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002335
Andy Adamsonabfabf82009-07-23 19:02:18 -04002336 /* Encode the replayed sequence operation */
2337 op = &args->ops[resp->opcnt - 1];
2338 nfsd4_encode_operation(resp, op);
2339
2340 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002341 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002342 op = &args->ops[resp->opcnt++];
2343 op->status = nfserr_retry_uncached_rep;
2344 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002345 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002346 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002347}
2348
2349/*
Andy Adamson557ce262009-08-28 08:45:04 -04002350 * The sequence operation is not cached because we can use the slot and
2351 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002352 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002353static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002354nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2355 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002356{
Andy Adamson557ce262009-08-28 08:45:04 -04002357 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002358 struct xdr_stream *xdr = &resp->xdr;
2359 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002360 __be32 status;
2361
Andy Adamson557ce262009-08-28 08:45:04 -04002362 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002363
Andy Adamsonabfabf82009-07-23 19:02:18 -04002364 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002365 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002366 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002367
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002368 p = xdr_reserve_space(xdr, slot->sl_datalen);
2369 if (!p) {
2370 WARN_ON_ONCE(1);
2371 return nfserr_serverfault;
2372 }
2373 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2374 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002375
Andy Adamson557ce262009-08-28 08:45:04 -04002376 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002377 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002378}
2379
Andy Adamson0733d212009-04-03 08:28:01 +03002380/*
2381 * Set the exchange_id flags returned by the server.
2382 */
2383static void
2384nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2385{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002386#ifdef CONFIG_NFSD_PNFS
2387 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2388#else
Andy Adamson0733d212009-04-03 08:28:01 +03002389 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002390#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002391
2392 /* Referrals are supported, Migration is not. */
2393 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2394
2395 /* set the wire flags to return to client. */
2396 clid->flags = new->cl_exchange_flags;
2397}
2398
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002399static bool client_has_openowners(struct nfs4_client *clp)
2400{
2401 struct nfs4_openowner *oo;
2402
2403 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2404 if (!list_empty(&oo->oo_owner.so_stateids))
2405 return true;
2406 }
2407 return false;
2408}
2409
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002410static bool client_has_state(struct nfs4_client *clp)
2411{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002412 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002413#ifdef CONFIG_NFSD_PNFS
2414 || !list_empty(&clp->cl_lo_states)
2415#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002416 || !list_empty(&clp->cl_delegations)
2417 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002418}
2419
Al Virob37ad282006-10-19 23:28:59 -07002420__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002421nfsd4_exchange_id(struct svc_rqst *rqstp,
2422 struct nfsd4_compound_state *cstate,
2423 struct nfsd4_exchange_id *exid)
2424{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002425 struct nfs4_client *conf, *new;
2426 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002427 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002428 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002429 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002430 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002431 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002432 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002433
Jeff Layton363168b2009-08-14 12:57:56 -04002434 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002435 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002436 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002437 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002438 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002439
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002440 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002441 return nfserr_inval;
2442
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002443 new = create_client(exid->clname, rqstp, &verf);
2444 if (new == NULL)
2445 return nfserr_jukebox;
2446
Andy Adamson0733d212009-04-03 08:28:01 +03002447 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002448 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002449 exid->spo_must_enforce[0] = 0;
2450 exid->spo_must_enforce[1] = (
2451 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2452 1 << (OP_EXCHANGE_ID - 32) |
2453 1 << (OP_CREATE_SESSION - 32) |
2454 1 << (OP_DESTROY_SESSION - 32) |
2455 1 << (OP_DESTROY_CLIENTID - 32));
2456
2457 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2458 1 << (OP_OPEN_DOWNGRADE) |
2459 1 << (OP_LOCKU) |
2460 1 << (OP_DELEGRETURN));
2461
2462 exid->spo_must_allow[1] &= (
2463 1 << (OP_TEST_STATEID - 32) |
2464 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002465 if (!svc_rqst_integrity_protected(rqstp)) {
2466 status = nfserr_inval;
2467 goto out_nolock;
2468 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002469 /*
2470 * Sometimes userspace doesn't give us a principal.
2471 * Which is a bug, really. Anyway, we can't enforce
2472 * MACH_CRED in that case, better to give up now:
2473 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002474 if (!new->cl_cred.cr_principal &&
2475 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002476 status = nfserr_serverfault;
2477 goto out_nolock;
2478 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002479 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002480 case SP4_NONE:
2481 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002482 default: /* checked by xdr code */
2483 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002484 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002485 status = nfserr_encr_alg_unsupp;
2486 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002487 }
2488
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002489 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002490 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002491 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002492 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002493 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2494 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2495
J. Bruce Fields136e6582012-05-12 20:37:23 -04002496 if (update) {
2497 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002498 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002499 goto out;
2500 }
Andrew Elblededeb132016-06-15 12:52:08 -04002501 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002502 status = nfserr_wrong_cred;
2503 goto out;
2504 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002505 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002506 status = nfserr_perm;
2507 goto out;
2508 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002509 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002510 status = nfserr_not_same;
2511 goto out;
2512 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002513 /* case 6 */
2514 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002515 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002516 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002517 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002518 if (client_has_state(conf)) {
2519 status = nfserr_clid_inuse;
2520 goto out;
2521 }
Andy Adamson0733d212009-04-03 08:28:01 +03002522 goto out_new;
2523 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002524 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002525 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002526 goto out_copy;
2527 }
2528 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002529 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002530 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002531 }
2532
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002533 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002534 status = nfserr_noent;
2535 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002536 }
2537
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002538 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002539 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002540 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002541
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002542 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002543out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002544 if (conf) {
2545 status = mark_client_expired_locked(conf);
2546 if (status)
2547 goto out;
2548 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002549 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002550 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2551 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002552
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002553 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002554 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002555 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002556out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002557 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2558 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002559
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002560 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2561 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002562
2563 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002564 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002565 status = nfs_ok;
2566
2567out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002568 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002569out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002570 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002571 expire_client(new);
2572 if (unconf)
2573 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002574 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002575}
2576
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002577static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002578check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002579{
Andy Adamson88e588d2009-07-23 19:02:15 -04002580 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2581 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002582
2583 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002584 if (slot_inuse) {
2585 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002586 return nfserr_jukebox;
2587 else
2588 return nfserr_seq_misordered;
2589 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002590 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002591 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002592 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002593 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002594 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002595 return nfserr_seq_misordered;
2596}
2597
Andy Adamson49557cc2009-07-23 19:02:16 -04002598/*
2599 * Cache the create session result into the create session single DRC
2600 * slot cache by saving the xdr structure. sl_seqid has been set.
2601 * Do this for solo or embedded create session operations.
2602 */
2603static void
2604nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002605 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002606{
2607 slot->sl_status = nfserr;
2608 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2609}
2610
2611static __be32
2612nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2613 struct nfsd4_clid_slot *slot)
2614{
2615 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2616 return slot->sl_status;
2617}
2618
Mi Jinlong1b74c252011-07-14 14:50:17 +08002619#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2620 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2621 1 + /* MIN tag is length with zero, only length */ \
2622 3 + /* version, opcount, opcode */ \
2623 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2624 /* seqid, slotID, slotID, cache */ \
2625 4 ) * sizeof(__be32))
2626
2627#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2628 2 + /* verifier: AUTH_NULL, length 0 */\
2629 1 + /* status */ \
2630 1 + /* MIN tag is length with zero, only length */ \
2631 3 + /* opcount, opcode, opstatus*/ \
2632 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2633 /* seqid, slotID, slotID, slotID, status */ \
2634 5 ) * sizeof(__be32))
2635
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002636static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002637{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002638 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2639
J. Bruce Fields373cd402013-04-08 15:42:12 -04002640 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2641 return nfserr_toosmall;
2642 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2643 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002644 ca->headerpadsz = 0;
2645 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2646 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2647 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2648 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2649 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2650 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2651 /*
2652 * Note decreasing slot size below client's request may make it
2653 * difficult for client to function correctly, whereas
2654 * decreasing the number of slots will (just?) affect
2655 * performance. When short on memory we therefore prefer to
2656 * decrease number of slots instead of their size. Clients that
2657 * request larger slots than they need will get poor results:
2658 */
2659 ca->maxreqs = nfsd4_get_drc_mem(ca);
2660 if (!ca->maxreqs)
2661 return nfserr_jukebox;
2662
J. Bruce Fields373cd402013-04-08 15:42:12 -04002663 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002664}
2665
Chuck Lever45006322016-03-01 13:06:02 -05002666/*
2667 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2668 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2669 */
2670#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2671 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2672
2673#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2674 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2675
Kinglong Mee8a891632013-12-23 18:11:02 +08002676#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002677 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002678#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002679 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2680 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002681
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002682static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2683{
2684 ca->headerpadsz = 0;
2685
Kinglong Mee8a891632013-12-23 18:11:02 +08002686 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002687 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002688 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002689 return nfserr_toosmall;
2690 ca->maxresp_cached = 0;
2691 if (ca->maxops < 2)
2692 return nfserr_toosmall;
2693
2694 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002695}
2696
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002697static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2698{
2699 switch (cbs->flavor) {
2700 case RPC_AUTH_NULL:
2701 case RPC_AUTH_UNIX:
2702 return nfs_ok;
2703 default:
2704 /*
2705 * GSS case: the spec doesn't allow us to return this
2706 * error. But it also doesn't allow us not to support
2707 * GSS.
2708 * I'd rather this fail hard than return some error the
2709 * client might think it can already handle:
2710 */
2711 return nfserr_encr_alg_unsupp;
2712 }
2713}
2714
Andy Adamson069b6ad2009-04-03 08:27:58 +03002715__be32
2716nfsd4_create_session(struct svc_rqst *rqstp,
2717 struct nfsd4_compound_state *cstate,
2718 struct nfsd4_create_session *cr_ses)
2719{
Jeff Layton363168b2009-08-14 12:57:56 -04002720 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002721 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002722 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002723 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002724 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002725 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002726 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002727 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002728
Mi Jinlonga62573d2011-03-23 17:57:07 +08002729 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2730 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002731 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2732 if (status)
2733 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002734 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002735 if (status)
2736 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002737 status = check_backchannel_attrs(&cr_ses->back_channel);
2738 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002739 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002740 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002741 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002742 if (!new)
2743 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002744 conn = alloc_conn_from_crses(rqstp, cr_ses);
2745 if (!conn)
2746 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002747
Jeff Laytond20c11d2014-07-30 08:27:07 -04002748 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002749 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002750 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002751 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002752
2753 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002754 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002755 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002756 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002757 cs_slot = &conf->cl_cs_slot;
2758 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002759 if (status) {
2760 if (status == nfserr_replay_cache)
2761 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002762 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002763 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002764 } else if (unconf) {
2765 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002766 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002767 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002768 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002769 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002770 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002771 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002772 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002773 cs_slot = &unconf->cl_cs_slot;
2774 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002775 if (status) {
2776 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002777 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002778 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002779 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002780 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002781 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002782 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002783 if (status) {
2784 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002785 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002786 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002787 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002788 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002789 conf = unconf;
2790 } else {
2791 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002792 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002793 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002794 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002795 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002796 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002797 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002798 cr_ses->flags &= ~SESSION4_RDMA;
2799
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002800 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002801 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002802
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002803 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002804 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002805 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002806 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002807
Jeff Laytond20c11d2014-07-30 08:27:07 -04002808 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002809 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002810 spin_unlock(&nn->client_lock);
2811 /* init connection and backchannel */
2812 nfsd4_init_conn(rqstp, conn, new);
2813 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002814 if (old)
2815 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002816 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002817out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002818 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002819 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002820 if (old)
2821 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002822out_free_session:
2823 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002824out_release_drc_mem:
2825 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002826 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002827}
2828
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002829static __be32 nfsd4_map_bcts_dir(u32 *dir)
2830{
2831 switch (*dir) {
2832 case NFS4_CDFC4_FORE:
2833 case NFS4_CDFC4_BACK:
2834 return nfs_ok;
2835 case NFS4_CDFC4_FORE_OR_BOTH:
2836 case NFS4_CDFC4_BACK_OR_BOTH:
2837 *dir = NFS4_CDFC4_BOTH;
2838 return nfs_ok;
2839 };
2840 return nfserr_inval;
2841}
2842
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002843__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2844{
2845 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002846 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002847 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002848
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002849 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2850 if (status)
2851 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002852 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002853 session->se_cb_prog = bc->bc_cb_program;
2854 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002855 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002856
2857 nfsd4_probe_callback(session->se_client);
2858
2859 return nfs_ok;
2860}
2861
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002862__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2863 struct nfsd4_compound_state *cstate,
2864 struct nfsd4_bind_conn_to_session *bcts)
2865{
2866 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002867 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002868 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002869 struct net *net = SVC_NET(rqstp);
2870 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002871
2872 if (!nfsd4_last_compound_op(rqstp))
2873 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002874 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002875 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002876 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002877 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002878 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002879 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002880 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002881 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002882 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002883 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002884 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002885 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002886 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002887 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002888 goto out;
2889 nfsd4_init_conn(rqstp, conn, session);
2890 status = nfs_ok;
2891out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002892 nfsd4_put_session(session);
2893out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002894 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002895}
2896
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002897static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2898{
2899 if (!session)
2900 return 0;
2901 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2902}
2903
Andy Adamson069b6ad2009-04-03 08:27:58 +03002904__be32
2905nfsd4_destroy_session(struct svc_rqst *r,
2906 struct nfsd4_compound_state *cstate,
2907 struct nfsd4_destroy_session *sessionid)
2908{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002909 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002910 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002911 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002912 struct net *net = SVC_NET(r);
2913 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002914
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002915 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002916 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002917 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002918 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002919 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002920 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002921 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002922 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002923 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002924 if (!ses)
2925 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002926 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002927 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002928 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002929 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002930 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002931 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002932 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002933 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002934
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002935 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002936
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002937 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002938 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002939out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002940 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002941out_client_lock:
2942 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002943out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002944 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002945}
2946
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002947static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002948{
2949 struct nfsd4_conn *c;
2950
2951 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002952 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002953 return c;
2954 }
2955 }
2956 return NULL;
2957}
2958
J. Bruce Fields57266a62013-04-13 14:27:29 -04002959static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002960{
2961 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002962 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002963 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002964 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002965
2966 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002967 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002968 if (c)
2969 goto out_free;
2970 status = nfserr_conn_not_bound_to_session;
2971 if (clp->cl_mach_cred)
2972 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002973 __nfsd4_hash_conn(new, ses);
2974 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002975 ret = nfsd4_register_conn(new);
2976 if (ret)
2977 /* oops; xprt is already down: */
2978 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002979 return nfs_ok;
2980out_free:
2981 spin_unlock(&clp->cl_lock);
2982 free_conn(new);
2983 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002984}
2985
Mi Jinlong868b89c2011-04-27 09:09:58 +08002986static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2987{
2988 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2989
2990 return args->opcnt > session->se_fchannel.maxops;
2991}
2992
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002993static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2994 struct nfsd4_session *session)
2995{
2996 struct xdr_buf *xb = &rqstp->rq_arg;
2997
2998 return xb->len > session->se_fchannel.maxreq_sz;
2999}
3000
Andy Adamson069b6ad2009-04-03 08:27:58 +03003001__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03003002nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003003 struct nfsd4_compound_state *cstate,
3004 struct nfsd4_sequence *seq)
3005{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003006 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003007 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003008 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003009 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003010 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003011 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003012 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003013 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003014 struct net *net = SVC_NET(rqstp);
3015 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003016
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003017 if (resp->opcnt != 1)
3018 return nfserr_sequence_pos;
3019
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003020 /*
3021 * Will be either used or freed by nfsd4_sequence_check_conn
3022 * below.
3023 */
3024 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3025 if (!conn)
3026 return nfserr_jukebox;
3027
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003028 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003029 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003030 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003031 goto out_no_session;
3032 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003033
Mi Jinlong868b89c2011-04-27 09:09:58 +08003034 status = nfserr_too_many_ops;
3035 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003036 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003037
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003038 status = nfserr_req_too_big;
3039 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003040 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003041
Benny Halevyb85d4c02009-04-03 08:28:08 +03003042 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003043 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003044 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003045
Andy Adamson557ce262009-08-28 08:45:04 -04003046 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003047 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3048
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003049 /* We do not negotiate the number of slots yet, so set the
3050 * maxslots to the session maxreqs which is used to encode
3051 * sr_highest_slotid and the sr_target_slot id to maxslots */
3052 seq->maxslots = session->se_fchannel.maxreqs;
3053
J. Bruce Fields73e79482012-02-13 16:39:00 -05003054 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3055 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003056 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003057 status = nfserr_seq_misordered;
3058 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003059 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003060 cstate->slot = slot;
3061 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003062 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003063 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003064 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003065 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003066 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003067 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003068 }
3069 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003070 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003071
J. Bruce Fields57266a62013-04-13 14:27:29 -04003072 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003073 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003074 if (status)
3075 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003076
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003077 buflen = (seq->cachethis) ?
3078 session->se_fchannel.maxresp_cached :
3079 session->se_fchannel.maxresp_sz;
3080 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3081 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003082 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003083 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003084 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003085
3086 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003087 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003088 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003089 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003090 if (seq->cachethis)
3091 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003092 else
3093 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003094
3095 cstate->slot = slot;
3096 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003097 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003098
Benny Halevyb85d4c02009-04-03 08:28:08 +03003099out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003100 switch (clp->cl_cb_state) {
3101 case NFSD4_CB_DOWN:
3102 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3103 break;
3104 case NFSD4_CB_FAULT:
3105 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3106 break;
3107 default:
3108 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003109 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003110 if (!list_empty(&clp->cl_revoked))
3111 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003112out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003113 if (conn)
3114 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003115 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003116 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003117out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003118 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003119 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003120}
3121
Trond Myklebustb6076642014-06-30 11:48:35 -04003122void
3123nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3124{
3125 struct nfsd4_compound_state *cs = &resp->cstate;
3126
3127 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003128 if (cs->status != nfserr_replay_cache) {
3129 nfsd4_store_cache_entry(resp);
3130 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3131 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003132 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003133 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003134 } else if (cs->clp)
3135 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003136}
3137
Mi Jinlong345c2842011-10-20 17:51:39 +08003138__be32
3139nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3140{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003141 struct nfs4_client *conf, *unconf;
3142 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003143 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003144 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003145
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003146 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003147 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003148 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003149 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003150
3151 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003152 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003153 status = nfserr_clientid_busy;
3154 goto out;
3155 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003156 status = mark_client_expired_locked(conf);
3157 if (status)
3158 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003159 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003160 } else if (unconf)
3161 clp = unconf;
3162 else {
3163 status = nfserr_stale_clientid;
3164 goto out;
3165 }
Andrew Elblededeb132016-06-15 12:52:08 -04003166 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003167 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003168 status = nfserr_wrong_cred;
3169 goto out;
3170 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003171 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003172out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003173 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003174 if (clp)
3175 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003176 return status;
3177}
3178
Andy Adamson069b6ad2009-04-03 08:27:58 +03003179__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003180nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3181{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003182 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003183
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003184 if (rc->rca_one_fs) {
3185 if (!cstate->current_fh.fh_dentry)
3186 return nfserr_nofilehandle;
3187 /*
3188 * We don't take advantage of the rca_one_fs case.
3189 * That's OK, it's optional, we can safely ignore it.
3190 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003191 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003192 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003193
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003194 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003195 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3196 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003197 goto out;
3198
3199 status = nfserr_stale_clientid;
3200 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003201 /*
3202 * The following error isn't really legal.
3203 * But we only get here if the client just explicitly
3204 * destroyed the client. Surely it no longer cares what
3205 * error it gets back on an operation for the dead
3206 * client.
3207 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003208 goto out;
3209
3210 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003211 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003212out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003213 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003214}
3215
3216__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003217nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3218 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003220 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003222 struct nfs4_client *conf, *new;
3223 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003224 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003225 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3226
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003227 new = create_client(clname, rqstp, &clverifier);
3228 if (new == NULL)
3229 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003230 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003231 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003232 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003233 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003234 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003236 if (clp_used_exchangeid(conf))
3237 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003238 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003239 char addr_str[INET6_ADDRSTRLEN];
3240 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3241 sizeof(addr_str));
3242 dprintk("NFSD: setclientid: string in use by client "
3243 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 goto out;
3245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003247 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003248 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003249 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003250 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003251 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003253 gen_confirm(new, nn);
3254 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003255 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003256 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003257 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003258 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3260 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3261 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003262 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 status = nfs_ok;
3264out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003265 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003266 if (new)
3267 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003268 if (unconf)
3269 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return status;
3271}
3272
3273
Al Virob37ad282006-10-19 23:28:59 -07003274__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003275nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3276 struct nfsd4_compound_state *cstate,
3277 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
NeilBrown21ab45a2005-06-23 22:04:14 -07003279 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003280 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3282 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003283 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003284 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003286 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003288
Jeff Laytond20c11d2014-07-30 08:27:07 -04003289 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003290 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003291 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003292 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003293 * We try hard to give out unique clientid's, so if we get an
3294 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003295 * the client may be buggy; this should never happen.
3296 *
3297 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003298 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003299 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003300 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3301 goto out;
3302 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3303 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003304 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003305 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3306 if (conf && !unconf) /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 status = nfs_ok;
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003308 else /* case 4: client hasn't noticed we rebooted yet? */
3309 status = nfserr_stale_clientid;
3310 goto out;
3311 }
3312 status = nfs_ok;
3313 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003314 old = unconf;
3315 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003316 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003317 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003318 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3319 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003320 status = nfserr_clid_inuse;
3321 if (client_has_state(old)
3322 && !same_creds(&unconf->cl_cred,
3323 &old->cl_cred))
3324 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003325 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003326 if (status) {
3327 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003328 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003329 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003330 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003331 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003332 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003333 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003334 get_client_locked(conf);
3335 spin_unlock(&nn->client_lock);
3336 nfsd4_probe_callback(conf);
3337 spin_lock(&nn->client_lock);
3338 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003340 spin_unlock(&nn->client_lock);
3341 if (old)
3342 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return status;
3344}
3345
J. Bruce Fields32513b42011-10-13 16:00:16 -04003346static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003348 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3349}
3350
3351/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003352static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3353 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003354{
Trond Myklebust950e0112014-06-30 11:48:31 -04003355 lockdep_assert_held(&state_lock);
3356
J. Bruce Fields32513b42011-10-13 16:00:16 -04003357 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003358 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003359 INIT_LIST_HEAD(&fp->fi_stateids);
3360 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003361 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003362 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003363 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003364 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003365 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003366 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3367 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003368#ifdef CONFIG_NFSD_PNFS
3369 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003370 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003371#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003372 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003375void
NeilBrowne60d4392005-06-23 22:03:01 -07003376nfsd4_free_slabs(void)
3377{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003378 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003379 kmem_cache_destroy(openowner_slab);
3380 kmem_cache_destroy(lockowner_slab);
3381 kmem_cache_destroy(file_slab);
3382 kmem_cache_destroy(stateid_slab);
3383 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Bryan Schumaker72083392011-11-01 15:24:59 -04003386int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387nfsd4_init_slabs(void)
3388{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003389 openowner_slab = kmem_cache_create("nfsd4_openowners",
3390 sizeof(struct nfs4_openowner), 0, 0, NULL);
3391 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003392 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003393 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003394 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003395 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003396 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003397 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003398 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003399 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003400 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003401 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003402 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003403 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003404 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003405 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003406 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003407 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003408 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003409 odstate_slab = kmem_cache_create("nfsd4_odstate",
3410 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3411 if (odstate_slab == NULL)
3412 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003414
Sachin Bhamare8287f002015-04-27 14:50:14 +02003415out_free_deleg_slab:
3416 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003417out_free_stateid_slab:
3418 kmem_cache_destroy(stateid_slab);
3419out_free_file_slab:
3420 kmem_cache_destroy(file_slab);
3421out_free_lockowner_slab:
3422 kmem_cache_destroy(lockowner_slab);
3423out_free_openowner_slab:
3424 kmem_cache_destroy(openowner_slab);
3425out:
NeilBrowne60d4392005-06-23 22:03:01 -07003426 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3427 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428}
3429
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003430static void init_nfs4_replay(struct nfs4_replay *rp)
3431{
3432 rp->rp_status = nfserr_serverfault;
3433 rp->rp_buflen = 0;
3434 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003435 mutex_init(&rp->rp_mutex);
3436}
3437
3438static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3439 struct nfs4_stateowner *so)
3440{
3441 if (!nfsd4_has_session(cstate)) {
3442 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003443 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003444 }
3445}
3446
3447void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3448{
3449 struct nfs4_stateowner *so = cstate->replay_owner;
3450
3451 if (so != NULL) {
3452 cstate->replay_owner = NULL;
3453 mutex_unlock(&so->so_replay.rp_mutex);
3454 nfs4_put_stateowner(so);
3455 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003456}
3457
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003458static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
3460 struct nfs4_stateowner *sop;
3461
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003462 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003463 if (!sop)
3464 return NULL;
3465
3466 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3467 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003468 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003469 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003471 sop->so_owner.len = owner->len;
3472
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003473 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003474 sop->so_client = clp;
3475 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003476 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003477 return sop;
3478}
3479
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003480static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003481{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003482 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003483
Trond Myklebustd4f04892014-07-29 21:34:36 -04003484 list_add(&oo->oo_owner.so_strhash,
3485 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003486 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003489static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3490{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003491 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003492}
3493
Jeff Layton6b180f02014-07-29 21:34:26 -04003494static void nfs4_free_openowner(struct nfs4_stateowner *so)
3495{
3496 struct nfs4_openowner *oo = openowner(so);
3497
3498 kmem_cache_free(openowner_slab, oo);
3499}
3500
3501static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003502 .so_unhash = nfs4_unhash_openowner,
3503 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003504};
3505
Andrew Elble7fc05642015-11-05 20:42:43 -05003506static struct nfs4_ol_stateid *
3507nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3508{
3509 struct nfs4_ol_stateid *local, *ret = NULL;
3510 struct nfs4_openowner *oo = open->op_openowner;
3511
3512 lockdep_assert_held(&fp->fi_lock);
3513
3514 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3515 /* ignore lock owners */
3516 if (local->st_stateowner->so_is_open_owner == 0)
3517 continue;
3518 if (local->st_stateowner == &oo->oo_owner) {
3519 ret = local;
3520 atomic_inc(&ret->st_stid.sc_count);
3521 break;
3522 }
3523 }
3524 return ret;
3525}
3526
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003527static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003528alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003529 struct nfsd4_compound_state *cstate)
3530{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003531 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003532 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003534 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3535 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003537 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003538 oo->oo_owner.so_is_open_owner = 1;
3539 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003540 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003541 if (nfsd4_has_session(cstate))
3542 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003543 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003544 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003545 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003546 spin_lock(&clp->cl_lock);
3547 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003548 if (ret == NULL) {
3549 hash_openowner(oo, clp, strhashval);
3550 ret = oo;
3551 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003552 nfs4_free_stateowner(&oo->oo_owner);
3553
Trond Myklebustd4f04892014-07-29 21:34:36 -04003554 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
3557
Andrew Elble7fc05642015-11-05 20:42:43 -05003558static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003559init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003560{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Andrew Elble7fc05642015-11-05 20:42:43 -05003562 struct nfs4_openowner *oo = open->op_openowner;
3563 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003564 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003565
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003566 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003567 /* We are moving these outside of the spinlocks to avoid the warnings */
3568 mutex_init(&stp->st_mutex);
3569 mutex_lock(&stp->st_mutex);
3570
Andrew Elble7fc05642015-11-05 20:42:43 -05003571 spin_lock(&oo->oo_owner.so_client->cl_lock);
3572 spin_lock(&fp->fi_lock);
3573
3574 retstp = nfsd4_find_existing_open(fp, open);
3575 if (retstp)
3576 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003577
3578 open->op_stp = NULL;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003579 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003580 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003581 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003582 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003583 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003584 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 stp->st_access_bmap = 0;
3586 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003587 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003588 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003589 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003590
3591out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003592 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003593 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003594 if (retstp) {
3595 mutex_lock(&retstp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003596 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003597 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003598 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003599 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003600 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
Jeff Laytond3134b12014-07-29 21:34:32 -04003603/*
3604 * In the 4.0 case we need to keep the owners around a little while to handle
3605 * CLOSE replay. We still do need to release any file access that is held by
3606 * them before returning however.
3607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003609move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
Jeff Layton217526e2014-07-30 08:27:11 -04003611 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003612 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3613 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3614 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003615
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003616 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Jeff Laytonb401be222014-07-29 21:34:33 -04003618 /*
3619 * We know that we hold one reference via nfsd4_close, and another
3620 * "persistent" reference for the client. If the refcount is higher
3621 * than 2, then there are still calls in progress that are using this
3622 * stateid. We can't put the sc_file reference until they are finished.
3623 * Wait for the refcount to drop to 2. Since it has been unhashed,
3624 * there should be no danger of the refcount going back up again at
3625 * this point.
3626 */
3627 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3628
Jeff Laytond3134b12014-07-29 21:34:32 -04003629 release_all_access(s);
3630 if (s->st_stid.sc_file) {
3631 put_nfs4_file(s->st_stid.sc_file);
3632 s->st_stid.sc_file = NULL;
3633 }
Jeff Layton217526e2014-07-30 08:27:11 -04003634
3635 spin_lock(&nn->client_lock);
3636 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003637 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003638 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003639 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003640 spin_unlock(&nn->client_lock);
3641 if (last)
3642 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645/* search file_hashtbl[] for file */
3646static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003647find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 struct nfs4_file *fp;
3650
Jeff Layton5b095e92014-10-23 08:01:02 -04003651 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003652 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003653 if (atomic_inc_not_zero(&fp->fi_ref))
3654 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
3657 return NULL;
3658}
3659
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003660struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003661find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003662{
3663 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003664 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003665
Jeff Layton5b095e92014-10-23 08:01:02 -04003666 rcu_read_lock();
3667 fp = find_file_locked(fh, hashval);
3668 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003669 return fp;
3670}
3671
3672static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003673find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003674{
3675 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003676 unsigned int hashval = file_hashval(fh);
3677
3678 rcu_read_lock();
3679 fp = find_file_locked(fh, hashval);
3680 rcu_read_unlock();
3681 if (fp)
3682 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003683
3684 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003685 fp = find_file_locked(fh, hashval);
3686 if (likely(fp == NULL)) {
3687 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003688 fp = new;
3689 }
3690 spin_unlock(&state_lock);
3691
3692 return fp;
3693}
3694
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003695/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 * Called to check deny when READ with all zero stateid or
3697 * WRITE with all zero or all one stateid
3698 */
Al Virob37ad282006-10-19 23:28:59 -07003699static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003703 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Trond Myklebustca943212014-07-23 16:17:39 -04003705 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003706 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003707 return ret;
3708 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003709 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003710 if (fp->fi_share_deny & deny_type)
3711 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003712 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003713 put_nfs4_file(fp);
3714 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
3716
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003717static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003719 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003720 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3721 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003722
Trond Myklebust11b91642014-07-29 21:34:08 -04003723 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003724
Jeff Layton02e12152014-07-16 10:31:57 -04003725 /*
3726 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003727 * already holding inode->i_lock.
3728 *
Jeff Laytondff13992014-07-08 14:02:49 -04003729 * If the dl_time != 0, then we know that it has already been
3730 * queued for a lease break. Don't queue it again.
3731 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003732 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003733 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003734 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003735 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003736 }
Jeff Layton02e12152014-07-16 10:31:57 -04003737 spin_unlock(&state_lock);
3738}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003740static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3741 struct rpc_task *task)
3742{
3743 struct nfs4_delegation *dp = cb_to_delegation(cb);
3744
Andrew Elblea4579742015-08-31 12:06:41 -04003745 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3746 return 1;
3747
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003748 switch (task->tk_status) {
3749 case 0:
3750 return 1;
3751 case -EBADHANDLE:
3752 case -NFS4ERR_BAD_STATEID:
3753 /*
3754 * Race: client probably got cb_recall before open reply
3755 * granting delegation.
3756 */
3757 if (dp->dl_retries--) {
3758 rpc_delay(task, 2 * HZ);
3759 return 0;
3760 }
3761 /*FALLTHRU*/
3762 default:
3763 return -1;
3764 }
3765}
3766
3767static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3768{
3769 struct nfs4_delegation *dp = cb_to_delegation(cb);
3770
3771 nfs4_put_stid(&dp->dl_stid);
3772}
3773
Julia Lawallc4cb8972015-11-21 22:57:39 +01003774static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003775 .prepare = nfsd4_cb_recall_prepare,
3776 .done = nfsd4_cb_recall_done,
3777 .release = nfsd4_cb_recall_release,
3778};
3779
Jeff Layton02e12152014-07-16 10:31:57 -04003780static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3781{
3782 /*
3783 * We're assuming the state code never drops its reference
3784 * without first removing the lease. Since we're in this lease
3785 * callback (and since the lease code is serialized by the kernel
3786 * lock) we know the server hasn't removed the lease yet, we know
3787 * it's safe to take a reference.
3788 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003789 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003790 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003791}
3792
Jeff Layton1c8c6012013-06-21 08:58:15 -04003793/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003794static bool
3795nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003796{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003797 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003798 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3799 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003800
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003801 if (!fp) {
3802 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003803 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003804 }
3805 if (fp->fi_had_conflict) {
3806 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003807 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003808 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003809 /*
3810 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003811 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003812 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003813 */
3814 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Jeff Layton02e12152014-07-16 10:31:57 -04003816 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003817 fp->fi_had_conflict = true;
3818 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003819 * If there are no delegations on the list, then return true
3820 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003821 */
3822 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003823 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003824 else
3825 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3826 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003827 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003828 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
Jeff Laytonc45198e2014-09-01 07:12:07 -04003831static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003832nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3833 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
3835 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003836 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 else
3838 return -EAGAIN;
3839}
3840
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003841static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003842 .lm_break = nfsd_break_deleg_cb,
3843 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844};
3845
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003846static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3847{
3848 if (nfsd4_has_session(cstate))
3849 return nfs_ok;
3850 if (seqid == so->so_seqid - 1)
3851 return nfserr_replay_me;
3852 if (seqid == so->so_seqid)
3853 return nfs_ok;
3854 return nfserr_bad_seqid;
3855}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Jeff Layton4b24ca72014-06-30 11:48:44 -04003857static __be32 lookup_clientid(clientid_t *clid,
3858 struct nfsd4_compound_state *cstate,
3859 struct nfsd_net *nn)
3860{
3861 struct nfs4_client *found;
3862
3863 if (cstate->clp) {
3864 found = cstate->clp;
3865 if (!same_clid(&found->cl_clientid, clid))
3866 return nfserr_stale_clientid;
3867 return nfs_ok;
3868 }
3869
3870 if (STALE_CLIENTID(clid, nn))
3871 return nfserr_stale_clientid;
3872
3873 /*
3874 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3875 * cached already then we know this is for is for v4.0 and "sessions"
3876 * will be false.
3877 */
3878 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003879 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003880 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003881 if (!found) {
3882 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003883 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003884 }
3885 atomic_inc(&found->cl_refcount);
3886 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003887
3888 /* Cache the nfs4_client in cstate! */
3889 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003890 return nfs_ok;
3891}
3892
Al Virob37ad282006-10-19 23:28:59 -07003893__be32
Andy Adamson66689582009-04-03 08:28:45 +03003894nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003895 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 clientid_t *clientid = &open->op_clientid;
3898 struct nfs4_client *clp = NULL;
3899 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003900 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003901 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003903 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003905 /*
3906 * In case we need it later, after we've already created the
3907 * file and don't want to risk a further failure:
3908 */
3909 open->op_file = nfsd4_alloc_file();
3910 if (open->op_file == NULL)
3911 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Trond Myklebust2d91e892014-06-30 11:48:46 -04003913 status = lookup_clientid(clientid, cstate, nn);
3914 if (status)
3915 return status;
3916 clp = cstate->clp;
3917
Trond Myklebustd4f04892014-07-29 21:34:36 -04003918 strhashval = ownerstr_hashval(&open->op_owner);
3919 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003920 open->op_openowner = oo;
3921 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003922 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003924 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003925 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003926 release_openowner(oo);
3927 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003928 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003929 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003930 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3931 if (status)
3932 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003933 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003934new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003935 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003936 if (oo == NULL)
3937 return nfserr_jukebox;
3938 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003939alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003940 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003941 if (!open->op_stp)
3942 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003943
3944 if (nfsd4_has_session(cstate) &&
3945 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3946 open->op_odstate = alloc_clnt_odstate(clp);
3947 if (!open->op_odstate)
3948 return nfserr_jukebox;
3949 }
3950
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003951 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
3953
Al Virob37ad282006-10-19 23:28:59 -07003954static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07003955nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3956{
3957 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3958 return nfserr_openmode;
3959 else
3960 return nfs_ok;
3961}
3962
Daniel Mackc47d8322011-05-16 16:38:14 +02003963static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04003964{
J. Bruce Fields24a01112010-05-18 20:01:35 -04003965 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3966}
3967
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003968static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003969{
3970 struct nfs4_stid *ret;
3971
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003972 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04003973 if (!ret)
3974 return NULL;
3975 return delegstateid(ret);
3976}
3977
J. Bruce Fields8b289b22011-10-19 11:52:12 -04003978static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3979{
3980 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3981 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3982}
3983
Al Virob37ad282006-10-19 23:28:59 -07003984static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04003985nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07003986 struct nfs4_delegation **dp)
3987{
3988 int flags;
Al Virob37ad282006-10-19 23:28:59 -07003989 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003990 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07003991
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003992 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3993 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07003994 goto out;
J. Bruce Fields24a01112010-05-18 20:01:35 -04003995 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04003996 status = nfs4_check_delegmode(deleg, flags);
3997 if (status) {
3998 nfs4_put_stid(&deleg->dl_stid);
3999 goto out;
4000 }
4001 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004002out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004003 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004004 return nfs_ok;
4005 if (status)
4006 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004007 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004008 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004009}
4010
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004011static inline int nfs4_access_to_access(u32 nfs4_access)
4012{
4013 int flags = 0;
4014
4015 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4016 flags |= NFSD_MAY_READ;
4017 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4018 flags |= NFSD_MAY_WRITE;
4019 return flags;
4020}
4021
Al Virob37ad282006-10-19 23:28:59 -07004022static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4024 struct nfsd4_open *open)
4025{
4026 struct iattr iattr = {
4027 .ia_valid = ATTR_SIZE,
4028 .ia_size = 0,
4029 };
4030 if (!open->op_truncate)
4031 return 0;
4032 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004033 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4035}
4036
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004037static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004038 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4039 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004040{
Trond Myklebustde186432014-07-10 14:07:26 -04004041 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004042 __be32 status;
4043 int oflag = nfs4_access_to_omode(open->op_share_access);
4044 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004045 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004046
Trond Myklebustde186432014-07-10 14:07:26 -04004047 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004048
4049 /*
4050 * Are we trying to set a deny mode that would conflict with
4051 * current access?
4052 */
4053 status = nfs4_file_check_deny(fp, open->op_share_deny);
4054 if (status != nfs_ok) {
4055 spin_unlock(&fp->fi_lock);
4056 goto out;
4057 }
4058
4059 /* set access to the file */
4060 status = nfs4_file_get_access(fp, open->op_share_access);
4061 if (status != nfs_ok) {
4062 spin_unlock(&fp->fi_lock);
4063 goto out;
4064 }
4065
4066 /* Set access bits in stateid */
4067 old_access_bmap = stp->st_access_bmap;
4068 set_access(open->op_share_access, stp);
4069
4070 /* Set new deny mask */
4071 old_deny_bmap = stp->st_deny_bmap;
4072 set_deny(open->op_share_deny, stp);
4073 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4074
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004075 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004076 spin_unlock(&fp->fi_lock);
4077 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004078 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004079 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004080 spin_lock(&fp->fi_lock);
4081 if (!fp->fi_fds[oflag]) {
4082 fp->fi_fds[oflag] = filp;
4083 filp = NULL;
4084 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004085 }
Trond Myklebustde186432014-07-10 14:07:26 -04004086 spin_unlock(&fp->fi_lock);
4087 if (filp)
4088 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004089
4090 status = nfsd4_truncate(rqstp, cur_fh, open);
4091 if (status)
4092 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004093out:
4094 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004095out_put_access:
4096 stp->st_access_bmap = old_access_bmap;
4097 nfs4_file_put_access(fp, open->op_share_access);
4098 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4099 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004100}
4101
Al Virob37ad282006-10-19 23:28:59 -07004102static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004103nfs4_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 -07004104{
Al Virob37ad282006-10-19 23:28:59 -07004105 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004106 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004108 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004109 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004110
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004111 /* test and set deny mode */
4112 spin_lock(&fp->fi_lock);
4113 status = nfs4_file_check_deny(fp, open->op_share_deny);
4114 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004115 set_deny(open->op_share_deny, stp);
4116 fp->fi_share_deny |=
4117 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4118 }
4119 spin_unlock(&fp->fi_lock);
4120
4121 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004124 status = nfsd4_truncate(rqstp, cur_fh, open);
4125 if (status != nfs_ok)
4126 reset_union_bmap_deny(old_deny_bmap, stp);
4127 return status;
4128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004130/* Should we give out recallable state?: */
4131static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4132{
4133 if (clp->cl_cb_state == NFSD4_CB_UP)
4134 return true;
4135 /*
4136 * In the sessions case, since we don't have to establish a
4137 * separate connection for callbacks, we assume it's OK
4138 * until we hear otherwise:
4139 */
4140 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4141}
4142
Jeff Laytond564fbe2014-07-16 10:31:58 -04004143static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004144{
4145 struct file_lock *fl;
4146
4147 fl = locks_alloc_lock();
4148 if (!fl)
4149 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004150 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004151 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004152 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4153 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04004154 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004155 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004156 return fl;
4157}
4158
Andrew Elble34ed9872015-10-15 12:07:28 -04004159/**
4160 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4161 * @dp: a pointer to the nfs4_delegation we're adding.
4162 *
4163 * Return:
4164 * On success: Return code will be 0 on success.
4165 *
4166 * On error: -EAGAIN if there was an existing delegation.
4167 * nonzero if there is an error in other cases.
4168 *
4169 */
4170
J. Bruce Fields99c41512013-05-21 16:21:25 -04004171static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004172{
Trond Myklebust11b91642014-07-29 21:34:08 -04004173 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08004174 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004175 struct file *filp;
4176 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004177
Jeff Laytond564fbe2014-07-16 10:31:58 -04004178 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004179 if (!fl)
4180 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04004181 filp = find_readable_file(fp);
4182 if (!filp) {
4183 /* We should always have a readable file here */
4184 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08004185 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04004186 return -EBADF;
4187 }
4188 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04004189 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004190 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04004191 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004192 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04004193 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04004194 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004195 spin_lock(&fp->fi_lock);
4196 /* Did the lease get broken before we took the lock? */
4197 status = -EAGAIN;
4198 if (fp->fi_had_conflict)
4199 goto out_unlock;
4200 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04004201 if (fp->fi_deleg_file) {
Andrew Elble34ed9872015-10-15 12:07:28 -04004202 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004203 goto out_unlock;
4204 }
Jeff Layton417c6622014-07-21 09:34:57 -04004205 fp->fi_deleg_file = filp;
Andrew Elble34ed9872015-10-15 12:07:28 -04004206 fp->fi_delegees = 0;
4207 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004208 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004209 spin_unlock(&state_lock);
Andrew Elble34ed9872015-10-15 12:07:28 -04004210 if (status) {
4211 /* Should never happen, this is a new fi_deleg_file */
4212 WARN_ON_ONCE(1);
4213 goto out_fput;
4214 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004215 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004216out_unlock:
4217 spin_unlock(&fp->fi_lock);
4218 spin_unlock(&state_lock);
4219out_fput:
4220 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004221 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004222}
4223
Jeff Layton0b266932014-07-25 07:34:25 -04004224static struct nfs4_delegation *
4225nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004226 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004227{
Jeff Layton0b266932014-07-25 07:34:25 -04004228 int status;
4229 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004230
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004231 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004232 return ERR_PTR(-EAGAIN);
4233
Andrew Elble34ed9872015-10-15 12:07:28 -04004234 spin_lock(&state_lock);
4235 spin_lock(&fp->fi_lock);
4236 status = nfs4_get_existing_delegation(clp, fp);
4237 spin_unlock(&fp->fi_lock);
4238 spin_unlock(&state_lock);
4239
4240 if (status)
4241 return ERR_PTR(status);
4242
Sachin Bhamare8287f002015-04-27 14:50:14 +02004243 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004244 if (!dp)
4245 return ERR_PTR(-ENOMEM);
4246
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004247 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004248 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004249 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004250 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004251 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004252 spin_unlock(&fp->fi_lock);
4253 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004254 status = nfs4_setlease(dp);
4255 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004256 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004257 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004258 status = -EAGAIN;
4259 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004260 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004261 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004262out_unlock:
4263 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004264 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004265out:
4266 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004267 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004268 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004269 return ERR_PTR(status);
4270 }
4271 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004272}
4273
Benny Halevy4aa89132012-02-21 14:16:44 -08004274static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4275{
4276 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4277 if (status == -EAGAIN)
4278 open->op_why_no_deleg = WND4_CONTENTION;
4279 else {
4280 open->op_why_no_deleg = WND4_RESOURCE;
4281 switch (open->op_deleg_want) {
4282 case NFS4_SHARE_WANT_READ_DELEG:
4283 case NFS4_SHARE_WANT_WRITE_DELEG:
4284 case NFS4_SHARE_WANT_ANY_DELEG:
4285 break;
4286 case NFS4_SHARE_WANT_CANCEL:
4287 open->op_why_no_deleg = WND4_CANCELLED;
4288 break;
4289 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004290 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004291 }
4292 }
4293}
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295/*
4296 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004297 *
4298 * Note we don't support write delegations, and won't until the vfs has
4299 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 */
4301static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004302nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4303 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
4305 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004306 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4307 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004308 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004309 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004311 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004312 open->op_recall = 0;
4313 switch (open->op_claim_type) {
4314 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004315 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004316 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004317 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4318 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004319 break;
4320 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004321 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004322 /*
4323 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004324 * had the chance to reclaim theirs, *and* until
4325 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004326 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004327 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004328 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004329 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004330 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004331 /*
4332 * Also, if the file was opened for write or
4333 * create, there's a good chance the client's
4334 * about to write to it, resulting in an
4335 * immediate recall (since we don't support
4336 * write delegations):
4337 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004338 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004339 goto out_no_deleg;
4340 if (open->op_create == NFS4_OPEN_CREATE)
4341 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004342 break;
4343 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004344 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004345 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004346 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004347 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004348 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004350 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004352 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004353 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004354 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004355 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004356 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004357out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004358 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4359 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004360 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004361 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004362 open->op_recall = 1;
4363 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004364
4365 /* 4.1 client asking for a delegation? */
4366 if (open->op_deleg_want)
4367 nfsd4_open_deleg_none_ext(open, status);
4368 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369}
4370
Benny Halevye27f49c2012-02-21 14:16:54 -08004371static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4372 struct nfs4_delegation *dp)
4373{
4374 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4375 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4376 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4377 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4378 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4379 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4380 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4381 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4382 }
4383 /* Otherwise the client must be confused wanting a delegation
4384 * it already has, therefore we don't return
4385 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4386 */
4387}
4388
Al Virob37ad282006-10-19 23:28:59 -07004389__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4391{
Andy Adamson66689582009-04-03 08:28:45 +03004392 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004393 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004395 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004396 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004397 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 /*
4400 * Lookup file; if found, lookup stateid and check open request,
4401 * and check for delegations in the process of being recalled.
4402 * If not found, create the nfs4_file struct
4403 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004404 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004405 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004406 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004407 if (status)
4408 goto out;
Andrew Elble7fc05642015-11-05 20:42:43 -05004409 spin_lock(&fp->fi_lock);
Jeff Laytona46cb7f2014-07-10 14:07:35 -04004410 stp = nfsd4_find_existing_open(fp, open);
Andrew Elble7fc05642015-11-05 20:42:43 -05004411 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004413 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004414 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004415 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004416 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
4418
4419 /*
4420 * OPEN the file, or upgrade an existing OPEN.
4421 * If truncate fails, the OPEN fails.
4422 */
4423 if (stp) {
4424 /* Stateid was found, this is an OPEN upgrade */
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004425 mutex_lock(&stp->st_mutex);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004426 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004427 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004428 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 } else {
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004432 /* stp is returned locked. */
4433 stp = init_open_stateid(fp, open);
4434 /* See if we lost the race to some other thread */
4435 if (stp->st_access_bmap != 0) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004436 status = nfs4_upgrade_open(rqstp, fp, current_fh,
4437 stp, open);
4438 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004439 mutex_unlock(&stp->st_mutex);
Andrew Elble7fc05642015-11-05 20:42:43 -05004440 goto out;
4441 }
4442 goto upgrade_out;
4443 }
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004444 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4445 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004446 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004447 release_open_stateid(stp);
4448 goto out;
4449 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004450
4451 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4452 open->op_odstate);
4453 if (stp->st_clnt_odstate == open->op_odstate)
4454 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
Andrew Elble7fc05642015-11-05 20:42:43 -05004456upgrade_out:
Jeff Layton9767feb2015-10-01 09:05:50 -04004457 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004458 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Benny Halevyd24433c2012-02-16 20:57:17 +02004460 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004461 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4462 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4463 open->op_why_no_deleg = WND4_NOT_WANTED;
4464 goto nodeleg;
4465 }
4466 }
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 /*
4469 * Attempt to hand out a delegation. No error return, because the
4470 * OPEN succeeds even if we fail.
4471 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004472 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004473nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 status = nfs_ok;
4475
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004476 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004477 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004479 /* 4.1 client trying to upgrade/downgrade delegation? */
4480 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004481 open->op_deleg_want)
4482 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004483
NeilBrown13cd2182005-06-23 22:03:10 -07004484 if (fp)
4485 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004486 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004487 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 /*
4489 * To finish the open response, we just need to set the rflags.
4490 */
4491 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004492 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
Andy Adamson66689582009-04-03 08:28:45 +03004493 !nfsd4_has_session(&resp->cstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004495 if (dp)
4496 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004497 if (stp)
4498 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
4500 return status;
4501}
4502
Jeff Layton58fb12e2014-07-29 21:34:27 -04004503void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004504 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004505{
4506 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004507 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004508
Jeff Laytond3134b12014-07-29 21:34:32 -04004509 nfsd4_cstate_assign_replay(cstate, so);
4510 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004511 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004512 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004513 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004514 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004515 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004516 if (open->op_odstate)
4517 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004518}
4519
Al Virob37ad282006-10-19 23:28:59 -07004520__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004521nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4522 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523{
4524 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004525 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004526 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 dprintk("process_renew(%08x/%08x): starting\n",
4529 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004530 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004531 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004533 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004535 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004536 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 goto out;
4538 status = nfs_ok;
4539out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 return status;
4541}
4542
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004543void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004544nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004545{
Jeff Layton33dcc482012-04-10 11:08:48 -04004546 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004547 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004548 return;
4549
NeilBrowna76b4312005-06-23 22:04:01 -07004550 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004551 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004552 /*
4553 * If the server goes down again right now, an NFSv4
4554 * client will still be allowed to reclaim after it comes back up,
4555 * even if it hasn't yet had a chance to reclaim state this time.
4556 *
4557 */
Jeff Layton919b8042014-09-12 16:40:20 -04004558 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004559 /*
4560 * At this point, NFSv4 clients can still reclaim. But if the
4561 * server crashes, any that have not yet reclaimed will be out
4562 * of luck on the next boot.
4563 *
4564 * (NFSv4.1+ clients are considered to have reclaimed once they
4565 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4566 * have reclaimed after their first OPEN.)
4567 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004568 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004569 /*
4570 * At this point, and once lockd and/or any other containers
4571 * exit their grace period, further reclaims will fail and
4572 * regular locking can resume.
4573 */
NeilBrowna76b4312005-06-23 22:04:01 -07004574}
4575
NeilBrownfd39ca92005-06-23 22:04:03 -07004576static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004577nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
4579 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004580 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004582 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004584 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004585 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004588 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004589 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004590 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004591 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 clp = list_entry(pos, struct nfs4_client, cl_lru);
4593 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4594 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004595 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 break;
4597 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004598 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004599 dprintk("NFSD: client in use (clientid %08x)\n",
4600 clp->cl_clientid.cl_id);
4601 continue;
4602 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004603 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004604 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004605 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004606 list_for_each_safe(pos, next, &reaplist) {
4607 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 dprintk("NFSD: purging unused client (clientid %08x)\n",
4609 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004610 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 expire_client(clp);
4612 }
Benny Halevycdc97502014-05-30 09:09:30 -04004613 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004614 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4616 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004617 t = dp->dl_time - cutoff;
4618 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 break;
4620 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004621 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004622 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 }
Benny Halevycdc97502014-05-30 09:09:30 -04004624 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004625 while (!list_empty(&reaplist)) {
4626 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4627 dl_recall_lru);
4628 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004629 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 }
Jeff Layton217526e2014-07-30 08:27:11 -04004631
4632 spin_lock(&nn->client_lock);
4633 while (!list_empty(&nn->close_lru)) {
4634 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4635 oo_close_lru);
4636 if (time_after((unsigned long)oo->oo_time,
4637 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004638 t = oo->oo_time - cutoff;
4639 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 break;
4641 }
Jeff Layton217526e2014-07-30 08:27:11 -04004642 list_del_init(&oo->oo_close_lru);
4643 stp = oo->oo_last_closed_stid;
4644 oo->oo_last_closed_stid = NULL;
4645 spin_unlock(&nn->client_lock);
4646 nfs4_put_stid(&stp->st_stid);
4647 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
Jeff Layton217526e2014-07-30 08:27:11 -04004649 spin_unlock(&nn->client_lock);
4650
Jeff Laytona832e7a2014-05-30 09:09:26 -04004651 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004652 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
Harvey Harrisona254b242008-02-20 12:49:00 -08004655static struct workqueue_struct *laundry_wq;
4656static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004657
4658static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004659laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
4661 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004662 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004663 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4664 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004666 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004668 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
Jeff Layton8fcd4612015-07-30 06:57:46 -04004671static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004672{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004673 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004674 return nfserr_bad_stateid;
4675 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004679access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004681 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4682 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4683 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
4686static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004687access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004689 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4690 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
4693static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004694__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
Al Virob37ad282006-10-19 23:28:59 -07004696 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
J. Bruce Fields02921912010-07-29 15:16:59 -04004698 /* For lock stateid's, we test the parent open, not the lock: */
4699 if (stp->st_openstp)
4700 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004701 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004703 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 goto out;
4705 status = nfs_ok;
4706out:
4707 return status;
4708}
4709
Al Virob37ad282006-10-19 23:28:59 -07004710static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004711check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004713 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004715 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004716 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 * conflicting state; so we must wait out the grace period. */
4718 return nfserr_grace;
4719 } else if (flags & WR_STATE)
4720 return nfs4_share_conflict(current_fh,
4721 NFS4_SHARE_DENY_WRITE);
4722 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4723 return nfs4_share_conflict(current_fh,
4724 NFS4_SHARE_DENY_READ);
4725}
4726
4727/*
4728 * Allow READ/WRITE during grace period on recovered state only for files
4729 * that are not able to provide mandatory locking.
4730 */
4731static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004732grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004734 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004737static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004738{
Andy Adamson66689582009-04-03 08:28:45 +03004739 /*
4740 * When sessions are used the stateid generation number is ignored
4741 * when it is zero.
4742 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004743 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004744 return nfs_ok;
4745
4746 if (in->si_generation == ref->si_generation)
4747 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004748
J. Bruce Fields0836f582008-01-26 19:08:12 -05004749 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004750 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004751 return nfserr_bad_stateid;
4752 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004753 * However, we could see a stateid from the past, even from a
4754 * non-buggy client. For example, if the client sends a lock
4755 * while some IO is outstanding, the lock may bump si_generation
4756 * while the IO is still in flight. The client could avoid that
4757 * situation by waiting for responses on all the IO requests,
4758 * but better performance may result in retrying IO that
4759 * receives an old_stateid error if requests are rarely
4760 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004761 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004762 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004763}
4764
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004765static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4766{
4767 if (ols->st_stateowner->so_is_open_owner &&
4768 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4769 return nfserr_bad_stateid;
4770 return nfs_ok;
4771}
4772
Chuck Lever7df302f2012-05-29 13:56:37 -04004773static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004774{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004775 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004776 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004777
Chuck Lever7df302f2012-05-29 13:56:37 -04004778 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004779 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004780 /* Client debugging aid. */
4781 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4782 char addr_str[INET6_ADDRSTRLEN];
4783 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4784 sizeof(addr_str));
4785 pr_warn_ratelimited("NFSD: client %s testing state ID "
4786 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004787 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004788 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004789 spin_lock(&cl->cl_lock);
4790 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004791 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004792 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004793 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004794 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004795 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004796 switch (s->sc_type) {
4797 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004798 status = nfs_ok;
4799 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004800 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004801 status = nfserr_deleg_revoked;
4802 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004803 case NFS4_OPEN_STID:
4804 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004805 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004806 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004807 default:
4808 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004809 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004810 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004811 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004812 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004813 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004814out_unlock:
4815 spin_unlock(&cl->cl_lock);
4816 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004817}
4818
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004819__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004820nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4821 stateid_t *stateid, unsigned char typemask,
4822 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004823{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004824 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004825
4826 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4827 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004828 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004829 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004830 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004831 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004832 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004833 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004834 if (status)
4835 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004836 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004837 if (!*s)
4838 return nfserr_bad_stateid;
4839 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004840}
4841
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004842static struct file *
4843nfs4_find_file(struct nfs4_stid *s, int flags)
4844{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004845 if (!s)
4846 return NULL;
4847
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004848 switch (s->sc_type) {
4849 case NFS4_DELEG_STID:
4850 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4851 return NULL;
4852 return get_file(s->sc_file->fi_deleg_file);
4853 case NFS4_OPEN_STID:
4854 case NFS4_LOCK_STID:
4855 if (flags & RD_STATE)
4856 return find_readable_file(s->sc_file);
4857 else
4858 return find_writeable_file(s->sc_file);
4859 break;
4860 }
4861
4862 return NULL;
4863}
4864
4865static __be32
4866nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4867{
4868 __be32 status;
4869
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004870 status = nfsd4_check_openowner_confirmed(ols);
4871 if (status)
4872 return status;
4873 return nfs4_check_openmode(ols, flags);
4874}
4875
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004876static __be32
4877nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4878 struct file **filpp, bool *tmp_file, int flags)
4879{
4880 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4881 struct file *file;
4882 __be32 status;
4883
4884 file = nfs4_find_file(s, flags);
4885 if (file) {
4886 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4887 acc | NFSD_MAY_OWNER_OVERRIDE);
4888 if (status) {
4889 fput(file);
4890 return status;
4891 }
4892
4893 *filpp = file;
4894 } else {
4895 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4896 if (status)
4897 return status;
4898
4899 if (tmp_file)
4900 *tmp_file = true;
4901 }
4902
4903 return 0;
4904}
4905
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004907 * Checks for stateid operations
4908 */
Al Virob37ad282006-10-19 23:28:59 -07004909__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004910nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01004911 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
4912 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004914 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004915 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004916 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004917 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004918 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 if (filpp)
4921 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004922 if (tmp_file)
4923 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004925 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 return nfserr_grace;
4927
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004928 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4929 status = check_special_stateids(net, fhp, stateid, flags);
4930 goto done;
4931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004933 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004934 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004935 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004936 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04004937 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004938 status = check_stateid_generation(stateid, &s->sc_stateid,
4939 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04004940 if (status)
4941 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004942
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004943 switch (s->sc_type) {
4944 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004945 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004946 break;
4947 case NFS4_OPEN_STID:
4948 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004949 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004950 break;
4951 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04004952 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004953 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04004955 if (status)
4956 goto out;
4957 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004958
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004959done:
4960 if (!status && filpp)
4961 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004963 if (s)
4964 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 return status;
4966}
4967
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04004968/*
Bryan Schumaker17456802011-07-13 10:50:48 -04004969 * Test if the stateid is valid
4970 */
4971__be32
4972nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4973 struct nfsd4_test_stateid *test_stateid)
4974{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004975 struct nfsd4_test_stateid_id *stateid;
4976 struct nfs4_client *cl = cstate->session->se_client;
4977
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004978 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04004979 stateid->ts_id_status =
4980 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004981
Bryan Schumaker17456802011-07-13 10:50:48 -04004982 return nfs_ok;
4983}
4984
Chuck Lever42691392016-08-11 10:37:30 -04004985static __be32
4986nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
4987{
4988 struct nfs4_ol_stateid *stp = openlockstateid(s);
4989 __be32 ret;
4990
4991 mutex_lock(&stp->st_mutex);
4992
4993 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4994 if (ret)
4995 goto out;
4996
4997 ret = nfserr_locks_held;
4998 if (check_for_locks(stp->st_stid.sc_file,
4999 lockowner(stp->st_stateowner)))
5000 goto out;
5001
5002 release_lock_stateid(stp);
5003 ret = nfs_ok;
5004
5005out:
5006 mutex_unlock(&stp->st_mutex);
5007 nfs4_put_stid(s);
5008 return ret;
5009}
5010
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005011__be32
5012nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5013 struct nfsd4_free_stateid *free_stateid)
5014{
5015 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005016 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005017 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005018 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005019 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005020
Jeff Layton1af71cc2014-07-29 21:34:14 -04005021 spin_lock(&cl->cl_lock);
5022 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005023 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005024 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005025 switch (s->sc_type) {
5026 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005027 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005028 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005029 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005030 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5031 if (ret)
5032 break;
5033 ret = nfserr_locks_held;
5034 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005035 case NFS4_LOCK_STID:
Chuck Lever42691392016-08-11 10:37:30 -04005036 atomic_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005037 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005038 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005039 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005040 case NFS4_REVOKED_DELEG_STID:
5041 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005042 list_del_init(&dp->dl_recall_lru);
5043 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005044 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005045 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005046 goto out;
5047 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005048 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005049out_unlock:
5050 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005051out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005052 return ret;
5053}
5054
NeilBrown4c4cd222005-07-07 17:59:27 -07005055static inline int
5056setlkflg (int type)
5057{
5058 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5059 RD_STATE : WR_STATE;
5060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005062static __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 -04005063{
5064 struct svc_fh *current_fh = &cstate->current_fh;
5065 struct nfs4_stateowner *sop = stp->st_stateowner;
5066 __be32 status;
5067
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005068 status = nfsd4_check_seqid(cstate, sop, seqid);
5069 if (status)
5070 return status;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005071 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
5072 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005073 /*
5074 * "Closed" stateid's exist *only* to return
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005075 * nfserr_replay_me from the previous step, and
5076 * revoked delegations are kept only for free_stateid.
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005077 */
5078 return nfserr_bad_stateid;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005079 mutex_lock(&stp->st_mutex);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005080 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005081 if (status == nfs_ok)
5082 status = nfs4_check_fh(current_fh, &stp->st_stid);
5083 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005084 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005085 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005086}
5087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088/*
5089 * Checks for sequence id mutating operations.
5090 */
Al Virob37ad282006-10-19 23:28:59 -07005091static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005092nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005093 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005094 struct nfs4_ol_stateid **stpp,
5095 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005097 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005098 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005099 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005101 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5102 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005105 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005106 if (status)
5107 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005108 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005109 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005111 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005112 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005113 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005114 else
5115 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005116 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005117}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005118
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005119static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5120 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005121{
5122 __be32 status;
5123 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005124 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005126 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005127 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005128 if (status)
5129 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005130 oo = openowner(stp->st_stateowner);
5131 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005132 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005133 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005134 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005135 }
5136 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005137 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138}
5139
Al Virob37ad282006-10-19 23:28:59 -07005140__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005141nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005142 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143{
Al Virob37ad282006-10-19 23:28:59 -07005144 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005145 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005146 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005147 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
Al Viroa6a9f182013-09-16 10:57:01 -04005149 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5150 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005152 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005153 if (status)
5154 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005156 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005157 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005158 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005159 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005160 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005161 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005162 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005163 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005164 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005165 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005166 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005167 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005168 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005169 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005170 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005171 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005172
Jeff Layton2a4317c2012-03-21 16:42:43 -04005173 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005174 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005175put_stateid:
5176 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005178 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 return status;
5180}
5181
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005182static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005184 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005185 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005186 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005187 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005188}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005189
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005190static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5191{
5192 switch (to_access) {
5193 case NFS4_SHARE_ACCESS_READ:
5194 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5195 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5196 break;
5197 case NFS4_SHARE_ACCESS_WRITE:
5198 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5199 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5200 break;
5201 case NFS4_SHARE_ACCESS_BOTH:
5202 break;
5203 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005204 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 }
5206}
5207
Al Virob37ad282006-10-19 23:28:59 -07005208__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005209nfsd4_open_downgrade(struct svc_rqst *rqstp,
5210 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005211 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212{
Al Virob37ad282006-10-19 23:28:59 -07005213 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005214 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005215 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
Al Viroa6a9f182013-09-16 10:57:01 -04005217 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5218 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005220 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005221 if (od->od_deleg_want)
5222 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5223 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005225 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005226 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005227 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005230 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005231 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005233 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005235 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005236 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005238 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005240 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005241 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005242 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005244put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005245 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005246 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005248 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 return status;
5250}
5251
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005252static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5253{
Trond Myklebustacf92952014-06-30 11:48:37 -04005254 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005255 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005256 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005257
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005258 s->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton2c41beb2014-07-29 21:34:41 -04005259 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005260 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005261
Jeff Laytond83017f2014-07-29 21:34:42 -04005262 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005263 if (unhashed)
5264 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005265 spin_unlock(&clp->cl_lock);
5266 free_ol_stateid_reaplist(&reaplist);
5267 } else {
5268 spin_unlock(&clp->cl_lock);
5269 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005270 if (unhashed)
5271 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005272 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005273}
5274
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275/*
5276 * nfs4_unlock_state() called after encode
5277 */
Al Virob37ad282006-10-19 23:28:59 -07005278__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005279nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005280 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281{
Al Virob37ad282006-10-19 23:28:59 -07005282 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005283 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005284 struct net *net = SVC_NET(rqstp);
5285 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286
Al Viroa6a9f182013-09-16 10:57:01 -04005287 dprintk("NFSD: nfsd4_close on file %pd\n",
5288 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005290 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5291 &close->cl_stateid,
5292 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005293 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005294 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005295 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 goto out;
Jeff Layton9767feb2015-10-01 09:05:50 -04005297 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005298 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005300 nfsd4_close_open_stateid(stp);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005301
5302 /* put reference from nfs4_preprocess_seqid_op */
5303 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 return status;
5306}
5307
Al Virob37ad282006-10-19 23:28:59 -07005308__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005309nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5310 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005312 struct nfs4_delegation *dp;
5313 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005314 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005315 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005316 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005318 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005319 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005321 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005322 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005323 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005324 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005325 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005326 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005327 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005328
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005329 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005330put_stateid:
5331 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332out:
5333 return status;
5334}
5335
Benny Halevy87df4de2008-12-15 19:42:03 +02005336static inline u64
5337end_offset(u64 start, u64 len)
5338{
5339 u64 end;
5340
5341 end = start + len;
5342 return end >= start ? end: NFS4_MAX_UINT64;
5343}
5344
5345/* last octet in a range */
5346static inline u64
5347last_byte_offset(u64 start, u64 len)
5348{
5349 u64 end;
5350
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005351 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005352 end = start + len;
5353 return end > start ? end - 1: NFS4_MAX_UINT64;
5354}
5355
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356/*
5357 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5358 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5359 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5360 * locking, this prevents us from being completely protocol-compliant. The
5361 * real solution to this problem is to start using unsigned file offsets in
5362 * the VFS, but this is a very deep change!
5363 */
5364static inline void
5365nfs4_transform_lock_offset(struct file_lock *lock)
5366{
5367 if (lock->fl_start < 0)
5368 lock->fl_start = OFFSET_MAX;
5369 if (lock->fl_end < 0)
5370 lock->fl_end = OFFSET_MAX;
5371}
5372
Jeff Laytoncae80b32015-04-03 09:04:04 -04005373static fl_owner_t
5374nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005375{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005376 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5377
5378 nfs4_get_stateowner(&lo->lo_owner);
5379 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005380}
5381
Jeff Laytoncae80b32015-04-03 09:04:04 -04005382static void
5383nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005384{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005385 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005386
Jeff Laytoncae80b32015-04-03 09:04:04 -04005387 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005388 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005389}
5390
Jeff Layton76d348f2016-09-16 16:28:24 -04005391static void
5392nfsd4_lm_notify(struct file_lock *fl)
5393{
5394 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5395 struct net *net = lo->lo_owner.so_client->net;
5396 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5397 struct nfsd4_blocked_lock *nbl = container_of(fl,
5398 struct nfsd4_blocked_lock, nbl_lock);
5399 bool queue = false;
5400
5401 spin_lock(&nn->client_lock);
5402 if (!list_empty(&nbl->nbl_list)) {
5403 list_del_init(&nbl->nbl_list);
5404 queue = true;
5405 }
5406 spin_unlock(&nn->client_lock);
5407
5408 if (queue)
5409 nfsd4_run_cb(&nbl->nbl_cb);
5410}
5411
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005412static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005413 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005414 .lm_get_owner = nfsd4_fl_get_owner,
5415 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005416};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
5418static inline void
5419nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5420{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005421 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
NeilBrownd5b90262006-04-10 22:55:22 -07005423 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005424 lo = (struct nfs4_lockowner *) fl->fl_owner;
5425 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5426 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005427 if (!deny->ld_owner.data)
5428 /* We just don't care that much */
5429 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005430 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5431 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005432 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005433nevermind:
5434 deny->ld_owner.len = 0;
5435 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005436 deny->ld_clientid.cl_boot = 0;
5437 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 }
5439 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005440 deny->ld_length = NFS4_MAX_UINT64;
5441 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5443 deny->ld_type = NFS4_READ_LT;
5444 if (fl->fl_type != F_RDLCK)
5445 deny->ld_type = NFS4_WRITE_LT;
5446}
5447
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005448static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005449find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005451 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005452 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Trond Myklebust0a880a22014-07-30 08:27:10 -04005454 lockdep_assert_held(&clp->cl_lock);
5455
Trond Myklebustd4f04892014-07-29 21:34:36 -04005456 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5457 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005458 if (so->so_is_open_owner)
5459 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005460 if (same_owner_str(so, owner))
5461 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 }
5463 return NULL;
5464}
5465
Trond Myklebustc58c6612014-07-29 21:34:35 -04005466static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005467find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005468{
5469 struct nfs4_lockowner *lo;
5470
Trond Myklebustd4f04892014-07-29 21:34:36 -04005471 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005472 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005473 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005474 return lo;
5475}
5476
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005477static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5478{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005479 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005480}
5481
Jeff Layton6b180f02014-07-29 21:34:26 -04005482static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5483{
5484 struct nfs4_lockowner *lo = lockowner(sop);
5485
5486 kmem_cache_free(lockowner_slab, lo);
5487}
5488
5489static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005490 .so_unhash = nfs4_unhash_lockowner,
5491 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005492};
5493
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494/*
5495 * Alloc a lock owner structure.
5496 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005497 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005499 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005501static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005502alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5503 struct nfs4_ol_stateid *open_stp,
5504 struct nfsd4_lock *lock)
5505{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005506 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005508 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5509 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005511 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005512 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5513 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005514 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005515 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005516 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005517 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005518 if (ret == NULL) {
5519 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005520 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005521 ret = lo;
5522 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005523 nfs4_free_stateowner(&lo->lo_owner);
5524
Trond Myklebustd4f04892014-07-29 21:34:36 -04005525 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005526 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527}
5528
Jeff Layton356a95e2014-07-29 21:34:13 -04005529static void
5530init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5531 struct nfs4_file *fp, struct inode *inode,
5532 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005534 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Jeff Layton356a95e2014-07-29 21:34:13 -04005536 lockdep_assert_held(&clp->cl_lock);
5537
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005538 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005539 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005540 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005541 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005542 stp->st_stid.sc_file = fp;
Jeff Laytonb49e0842014-07-29 21:34:11 -04005543 stp->st_stid.sc_free = nfs4_free_lock_stateid;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005544 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005546 stp->st_openstp = open_stp;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005547 mutex_init(&stp->st_mutex);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005548 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005549 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005550 spin_lock(&fp->fi_lock);
5551 list_add(&stp->st_perfile, &fp->fi_stateids);
5552 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553}
5554
Jeff Laytonc53530d2014-06-30 11:48:39 -04005555static struct nfs4_ol_stateid *
5556find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5557{
5558 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005559 struct nfs4_client *clp = lo->lo_owner.so_client;
5560
5561 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005562
5563 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005564 if (lst->st_stid.sc_file == fp) {
5565 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005566 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005567 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005568 }
5569 return NULL;
5570}
5571
Jeff Layton356a95e2014-07-29 21:34:13 -04005572static struct nfs4_ol_stateid *
5573find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5574 struct inode *inode, struct nfs4_ol_stateid *ost,
5575 bool *new)
5576{
5577 struct nfs4_stid *ns = NULL;
5578 struct nfs4_ol_stateid *lst;
5579 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5580 struct nfs4_client *clp = oo->oo_owner.so_client;
5581
5582 spin_lock(&clp->cl_lock);
5583 lst = find_lock_stateid(lo, fi);
5584 if (lst == NULL) {
5585 spin_unlock(&clp->cl_lock);
5586 ns = nfs4_alloc_stid(clp, stateid_slab);
5587 if (ns == NULL)
5588 return NULL;
5589
5590 spin_lock(&clp->cl_lock);
5591 lst = find_lock_stateid(lo, fi);
5592 if (likely(!lst)) {
5593 lst = openlockstateid(ns);
5594 init_lock_stateid(lst, lo, fi, inode, ost);
5595 ns = NULL;
5596 *new = true;
5597 }
5598 }
5599 spin_unlock(&clp->cl_lock);
5600 if (ns)
5601 nfs4_put_stid(ns);
5602 return lst;
5603}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005604
NeilBrownfd39ca92005-06-23 22:04:03 -07005605static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606check_lock_length(u64 offset, u64 length)
5607{
Kinglong Meee7969312015-07-13 17:34:19 +08005608 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5609 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610}
5611
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005612static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005613{
Trond Myklebust11b91642014-07-29 21:34:08 -04005614 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005615
Jeff Layton7214e862014-07-10 14:07:33 -04005616 lockdep_assert_held(&fp->fi_lock);
5617
Jeff Layton82c5ff12012-05-11 09:45:13 -04005618 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005619 return;
Jeff Layton12659652014-07-10 14:07:28 -04005620 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005621 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005622}
5623
Jeff Layton356a95e2014-07-29 21:34:13 -04005624static __be32
5625lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5626 struct nfs4_ol_stateid *ost,
5627 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005628 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005629{
Jeff Layton5db1c032014-07-29 21:34:28 -04005630 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005631 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005632 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5633 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005634 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005635 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005636 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005637 unsigned int strhashval;
Jeff Laytondd257932016-08-11 10:37:39 -04005638 bool hashed;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005639
Kinglong Meec8623992015-07-13 17:35:37 +08005640 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005641 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005642 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005643 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5644 if (lo == NULL)
5645 return nfserr_jukebox;
5646 } else {
5647 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005648 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005649 if (!cstate->minorversion &&
5650 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005651 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005652 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005653
Jeff Laytondd257932016-08-11 10:37:39 -04005654retry:
5655 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5656 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005657 status = nfserr_jukebox;
5658 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005659 }
Jeff Laytondd257932016-08-11 10:37:39 -04005660
5661 mutex_lock(&lst->st_mutex);
5662
5663 /* See if it's still hashed to avoid race with FREE_STATEID */
5664 spin_lock(&cl->cl_lock);
5665 hashed = !list_empty(&lst->st_perfile);
5666 spin_unlock(&cl->cl_lock);
5667
5668 if (!hashed) {
5669 mutex_unlock(&lst->st_mutex);
5670 nfs4_put_stid(&lst->st_stid);
5671 goto retry;
5672 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005673 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005674 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005675out:
5676 nfs4_put_stateowner(&lo->lo_owner);
5677 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005678}
5679
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680/*
5681 * LOCK operation
5682 */
Al Virob37ad282006-10-19 23:28:59 -07005683__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005684nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005685 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005687 struct nfs4_openowner *open_sop = NULL;
5688 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005689 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005690 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005691 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005692 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005693 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005694 struct file_lock *file_lock = NULL;
5695 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005696 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005697 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005698 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005699 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005700 unsigned char fl_type;
5701 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005702 struct net *net = SVC_NET(rqstp);
5703 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
5705 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5706 (long long) lock->lk_offset,
5707 (long long) lock->lk_length);
5708
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 if (check_lock_length(lock->lk_offset, lock->lk_length))
5710 return nfserr_inval;
5711
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005712 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005713 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005714 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5715 return status;
5716 }
5717
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005719 if (nfsd4_has_session(cstate))
5720 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005721 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005722 &cstate->session->se_client->cl_clientid,
5723 sizeof(clientid_t));
5724
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005726 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005730 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 lock->lk_new_open_seqid,
5732 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005733 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005734 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005736 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005737 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005738 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005739 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005740 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005741 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005742 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005743 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005744 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005745 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005746 lock->lk_old_lock_seqid,
5747 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005748 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005749 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005750 if (status)
5751 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005752 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005754 lkflg = setlkflg(lock->lk_type);
5755 status = nfs4_check_openmode(lock_stp, lkflg);
5756 if (status)
5757 goto out;
5758
NeilBrown0dd395d2005-07-07 17:59:15 -07005759 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005760 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005761 goto out;
5762 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005763 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005764 goto out;
5765
Trond Myklebust11b91642014-07-29 21:34:08 -04005766 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005769 if (nfsd4_has_session(cstate))
5770 fl_flags |= FL_SLEEP;
5771 /* Fallthrough */
5772 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005773 spin_lock(&fp->fi_lock);
5774 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005775 if (filp)
5776 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005777 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005778 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005779 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005781 if (nfsd4_has_session(cstate))
5782 fl_flags |= FL_SLEEP;
5783 /* Fallthrough */
5784 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005785 spin_lock(&fp->fi_lock);
5786 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005787 if (filp)
5788 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005789 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005790 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 default:
5793 status = nfserr_inval;
5794 goto out;
5795 }
Jeff Layton76d348f2016-09-16 16:28:24 -04005796
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005797 if (!filp) {
5798 status = nfserr_openmode;
5799 goto out;
5800 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005801
Jeff Layton76d348f2016-09-16 16:28:24 -04005802 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
5803 if (!nbl) {
5804 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
5805 status = nfserr_jukebox;
5806 goto out;
5807 }
5808
5809 file_lock = &nbl->nbl_lock;
5810 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04005811 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005812 file_lock->fl_pid = current->tgid;
5813 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04005814 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04005815 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5816 file_lock->fl_start = lock->lk_offset;
5817 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5818 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
Jeff Layton21179d82012-08-21 08:03:32 -04005820 conflock = locks_alloc_lock();
5821 if (!conflock) {
5822 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5823 status = nfserr_jukebox;
5824 goto out;
5825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Jeff Layton76d348f2016-09-16 16:28:24 -04005827 if (fl_flags & FL_SLEEP) {
5828 spin_lock(&nn->client_lock);
5829 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
5830 spin_unlock(&nn->client_lock);
5831 }
5832
Jeff Layton21179d82012-08-21 08:03:32 -04005833 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005834 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04005836 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07005837 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005838 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005839 case FILE_LOCK_DEFERRED:
5840 nbl = NULL;
5841 /* Fallthrough */
5842 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005843 status = nfserr_denied;
5844 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005845 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005846 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005847 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005849 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005850 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005851 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005852 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005853 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855out:
Jeff Layton76d348f2016-09-16 16:28:24 -04005856 if (nbl) {
5857 /* dequeue it if we queued it before */
5858 if (fl_flags & FL_SLEEP) {
5859 spin_lock(&nn->client_lock);
5860 list_del_init(&nbl->nbl_list);
5861 spin_unlock(&nn->client_lock);
5862 }
5863 free_blocked_lock(nbl);
5864 }
Trond Myklebustde186432014-07-10 14:07:26 -04005865 if (filp)
5866 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005867 if (lock_stp) {
5868 /* Bump seqid manually if the 4.0 replay owner is openowner */
5869 if (cstate->replay_owner &&
5870 cstate->replay_owner != &lock_sop->lo_owner &&
5871 seqid_mutating_err(ntohl(status)))
5872 lock_sop->lo_owner.so_seqid++;
5873
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005874 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005875
Jeff Layton5db1c032014-07-29 21:34:28 -04005876 /*
5877 * If this is a new, never-before-used stateid, and we are
5878 * returning an error, then just go ahead and release it.
5879 */
5880 if (status && new)
5881 release_lock_stateid(lock_stp);
5882
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005883 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04005884 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005885 if (open_stp)
5886 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005887 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04005888 if (conflock)
5889 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 return status;
5891}
5892
5893/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005894 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5895 * so we do a temporary open here just to get an open file to pass to
5896 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5897 * inode operation.)
5898 */
Al Viro04da6e92012-04-13 00:00:04 -04005899static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005900{
5901 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04005902 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5903 if (!err) {
5904 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02005905 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04005906 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08005907 return err;
5908}
5909
5910/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 * LOCKT operation
5912 */
Al Virob37ad282006-10-19 23:28:59 -07005913__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005914nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5915 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916{
Jeff Layton21179d82012-08-21 08:03:32 -04005917 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04005918 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005919 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005920 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005922 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 return nfserr_grace;
5924
5925 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5926 return nfserr_inval;
5927
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005928 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005929 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005930 if (status)
5931 goto out;
5932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
J. Bruce Fields75c096f2011-08-15 18:39:32 -04005934 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
Jeff Layton21179d82012-08-21 08:03:32 -04005937 file_lock = locks_alloc_lock();
5938 if (!file_lock) {
5939 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5940 status = nfserr_jukebox;
5941 goto out;
5942 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08005943
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 switch (lockt->lt_type) {
5945 case NFS4_READ_LT:
5946 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005947 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 break;
5949 case NFS4_WRITE_LT:
5950 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04005951 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 break;
5953 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04005954 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 status = nfserr_inval;
5956 goto out;
5957 }
5958
Kinglong Meec8623992015-07-13 17:35:37 +08005959 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005960 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04005961 file_lock->fl_owner = (fl_owner_t)lo;
5962 file_lock->fl_pid = current->tgid;
5963 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
Jeff Layton21179d82012-08-21 08:03:32 -04005965 file_lock->fl_start = lockt->lt_offset;
5966 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
Jeff Layton21179d82012-08-21 08:03:32 -04005968 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
Jeff Layton21179d82012-08-21 08:03:32 -04005970 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04005971 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05005972 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04005973
Jeff Layton21179d82012-08-21 08:03:32 -04005974 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04005976 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 }
5978out:
Jeff Layton5db1c032014-07-29 21:34:28 -04005979 if (lo)
5980 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04005981 if (file_lock)
5982 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 return status;
5984}
5985
Al Virob37ad282006-10-19 23:28:59 -07005986__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005987nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005988 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005990 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005992 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005993 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07005994 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005995 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5998 (long long) locku->lu_offset,
5999 (long long) locku->lu_length);
6000
6001 if (check_lock_length(locku->lu_offset, locku->lu_length))
6002 return nfserr_inval;
6003
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006004 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006005 &locku->lu_stateid, NFS4_LOCK_STID,
6006 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006007 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006009 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006010 if (!filp) {
6011 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006012 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006013 }
Jeff Layton21179d82012-08-21 08:03:32 -04006014 file_lock = locks_alloc_lock();
6015 if (!file_lock) {
6016 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6017 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006018 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006019 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006020
Jeff Layton21179d82012-08-21 08:03:32 -04006021 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006022 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006023 file_lock->fl_pid = current->tgid;
6024 file_lock->fl_file = filp;
6025 file_lock->fl_flags = FL_POSIX;
6026 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6027 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Jeff Layton21179d82012-08-21 08:03:32 -04006029 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6030 locku->lu_length);
6031 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
Jeff Layton21179d82012-08-21 08:03:32 -04006033 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006034 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006035 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 goto out_nfserr;
6037 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006038 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006039fput:
6040 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006041put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006042 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006043 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006045 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006046 if (file_lock)
6047 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 return status;
6049
6050out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006051 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006052 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053}
6054
6055/*
6056 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006057 * true: locks held by lockowner
6058 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006060static bool
6061check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006063 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006064 int status = false;
6065 struct file *filp = find_any_file(fp);
6066 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006067 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006068
6069 if (!filp) {
6070 /* Any valid lock stateid should have some sort of access */
6071 WARN_ON_ONCE(1);
6072 return status;
6073 }
6074
6075 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006076 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006078 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006079 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006080 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6081 if (fl->fl_owner == (fl_owner_t)lowner) {
6082 status = true;
6083 break;
6084 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006085 }
Jeff Layton6109c852015-01-16 15:05:57 -05006086 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006088 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 return status;
6090}
6091
Al Virob37ad282006-10-19 23:28:59 -07006092__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006093nfsd4_release_lockowner(struct svc_rqst *rqstp,
6094 struct nfsd4_compound_state *cstate,
6095 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096{
6097 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006098 struct nfs4_stateowner *sop;
6099 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006100 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006102 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006103 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006104 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006105 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006106 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
6108 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6109 clid->cl_boot, clid->cl_id);
6110
Jeff Layton4b24ca72014-06-30 11:48:44 -04006111 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006112 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006113 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006114
Trond Myklebustd4f04892014-07-29 21:34:36 -04006115 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006116 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006117 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006118 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006119 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006120
6121 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006122 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006123
Jeff Layton882e9d22014-07-29 21:34:37 -04006124 /* see if there are still any locks associated with it */
6125 lo = lockowner(sop);
6126 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6127 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6128 status = nfserr_locks_held;
6129 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006130 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006131 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006132 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006133
Kinglong Meeb5971af2014-08-22 10:18:43 -04006134 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006135 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006136 }
Chuck Lever88584812016-07-13 16:40:14 -04006137 if (!lo) {
6138 spin_unlock(&clp->cl_lock);
6139 return status;
6140 }
6141
6142 unhash_lockowner_locked(lo);
6143 while (!list_empty(&lo->lo_owner.so_stateids)) {
6144 stp = list_first_entry(&lo->lo_owner.so_stateids,
6145 struct nfs4_ol_stateid,
6146 st_perstateowner);
6147 WARN_ON(!unhash_lock_stateid(stp));
6148 put_ol_stateid_locked(stp, &reaplist);
6149 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006150 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006151 free_ol_stateid_reaplist(&reaplist);
6152 nfs4_put_stateowner(&lo->lo_owner);
6153
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 return status;
6155}
6156
6157static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006158alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159{
NeilBrowna55370a2005-06-23 22:03:52 -07006160 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161}
6162
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006163bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006164nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006165{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006166 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006167
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006168 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006169 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006170}
6171
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172/*
6173 * failure => all reset bets are off, nfserr_no_grace...
6174 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006175struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006176nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177{
6178 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006179 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
NeilBrowna55370a2005-06-23 22:03:52 -07006181 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6182 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006183 if (crp) {
6184 strhashval = clientstr_hashval(name);
6185 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006186 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006187 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006188 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006189 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006190 }
6191 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192}
6193
Jeff Layton2a4317c2012-03-21 16:42:43 -04006194void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006195nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006196{
6197 list_del(&crp->cr_strhash);
6198 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006199 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006200}
6201
6202void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006203nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
6205 struct nfs4_client_reclaim *crp = NULL;
6206 int i;
6207
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006209 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6210 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006212 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 }
6214 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006215 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216}
6217
6218/*
6219 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006220struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006221nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222{
6223 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 struct nfs4_client_reclaim *crp = NULL;
6225
Jeff Layton278c9312012-11-12 15:00:52 -05006226 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
Jeff Layton278c9312012-11-12 15:00:52 -05006228 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006229 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006230 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 return crp;
6232 }
6233 }
6234 return NULL;
6235}
6236
6237/*
6238* Called from OPEN. Look for clientid in reclaim list.
6239*/
Al Virob37ad282006-10-19 23:28:59 -07006240__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006241nfs4_check_open_reclaim(clientid_t *clid,
6242 struct nfsd4_compound_state *cstate,
6243 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006245 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006246
6247 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006248 status = lookup_clientid(clid, cstate, nn);
6249 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006250 return nfserr_reclaim_bad;
6251
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006252 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6253 return nfserr_no_grace;
6254
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006255 if (nfsd4_client_record_check(cstate->clp))
6256 return nfserr_reclaim_bad;
6257
6258 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259}
6260
Bryan Schumaker65178db2011-11-01 13:35:21 -04006261#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006262static inline void
6263put_client(struct nfs4_client *clp)
6264{
6265 atomic_dec(&clp->cl_refcount);
6266}
6267
Jeff Layton285abde2014-07-30 08:27:24 -04006268static struct nfs4_client *
6269nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6270{
6271 struct nfs4_client *clp;
6272 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6273 nfsd_net_id);
6274
6275 if (!nfsd_netns_ready(nn))
6276 return NULL;
6277
6278 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6279 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6280 return clp;
6281 }
6282 return NULL;
6283}
6284
Jeff Layton7ec0e362014-07-30 08:27:17 -04006285u64
Jeff Layton285abde2014-07-30 08:27:24 -04006286nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006287{
6288 struct nfs4_client *clp;
6289 u64 count = 0;
6290 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6291 nfsd_net_id);
6292 char buf[INET6_ADDRSTRLEN];
6293
6294 if (!nfsd_netns_ready(nn))
6295 return 0;
6296
6297 spin_lock(&nn->client_lock);
6298 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6299 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6300 pr_info("NFS Client: %s\n", buf);
6301 ++count;
6302 }
6303 spin_unlock(&nn->client_lock);
6304
6305 return count;
6306}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006307
Jeff Laytona0926d12014-07-30 08:27:18 -04006308u64
Jeff Layton285abde2014-07-30 08:27:24 -04006309nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006310{
6311 u64 count = 0;
6312 struct nfs4_client *clp;
6313 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6314 nfsd_net_id);
6315
6316 if (!nfsd_netns_ready(nn))
6317 return count;
6318
6319 spin_lock(&nn->client_lock);
6320 clp = nfsd_find_client(addr, addr_size);
6321 if (clp) {
6322 if (mark_client_expired_locked(clp) == nfs_ok)
6323 ++count;
6324 else
6325 clp = NULL;
6326 }
6327 spin_unlock(&nn->client_lock);
6328
6329 if (clp)
6330 expire_client(clp);
6331
6332 return count;
6333}
6334
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006335u64
Jeff Layton285abde2014-07-30 08:27:24 -04006336nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006337{
6338 u64 count = 0;
6339 struct nfs4_client *clp, *next;
6340 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6341 nfsd_net_id);
6342 LIST_HEAD(reaplist);
6343
6344 if (!nfsd_netns_ready(nn))
6345 return count;
6346
6347 spin_lock(&nn->client_lock);
6348 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6349 if (mark_client_expired_locked(clp) == nfs_ok) {
6350 list_add(&clp->cl_lru, &reaplist);
6351 if (max != 0 && ++count >= max)
6352 break;
6353 }
6354 }
6355 spin_unlock(&nn->client_lock);
6356
6357 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6358 expire_client(clp);
6359
6360 return count;
6361}
6362
Bryan Schumaker184c1842012-11-29 11:40:44 -05006363static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6364 const char *type)
6365{
6366 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006367 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006368 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6369}
6370
Jeff Layton016200c2014-07-30 08:27:21 -04006371static void
6372nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6373 struct list_head *collect)
6374{
6375 struct nfs4_client *clp = lst->st_stid.sc_client;
6376 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6377 nfsd_net_id);
6378
6379 if (!collect)
6380 return;
6381
6382 lockdep_assert_held(&nn->client_lock);
6383 atomic_inc(&clp->cl_refcount);
6384 list_add(&lst->st_locks, collect);
6385}
6386
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006387static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006388 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006389 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006390{
6391 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006392 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006393 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006394 u64 count = 0;
6395
Jeff Layton016200c2014-07-30 08:27:21 -04006396 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006397 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006398 list_for_each_entry_safe(stp, st_next,
6399 &oop->oo_owner.so_stateids, st_perstateowner) {
6400 list_for_each_entry_safe(lst, lst_next,
6401 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006402 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006403 if (func(lst))
6404 nfsd_inject_add_lock_to_list(lst,
6405 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006406 }
Jeff Layton016200c2014-07-30 08:27:21 -04006407 ++count;
6408 /*
6409 * Despite the fact that these functions deal
6410 * with 64-bit integers for "count", we must
6411 * ensure that it doesn't blow up the
6412 * clp->cl_refcount. Throw a warning if we
6413 * start to approach INT_MAX here.
6414 */
6415 WARN_ON_ONCE(count == (INT_MAX / 2));
6416 if (count == max)
6417 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006418 }
6419 }
6420 }
Jeff Layton016200c2014-07-30 08:27:21 -04006421out:
6422 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006423
6424 return count;
6425}
6426
Jeff Layton016200c2014-07-30 08:27:21 -04006427static u64
6428nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6429 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006430{
Jeff Layton016200c2014-07-30 08:27:21 -04006431 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006432}
6433
Jeff Layton016200c2014-07-30 08:27:21 -04006434static u64
6435nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006436{
Jeff Layton016200c2014-07-30 08:27:21 -04006437 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006438 nfsd_print_count(clp, count, "locked files");
6439 return count;
6440}
6441
Jeff Layton016200c2014-07-30 08:27:21 -04006442u64
Jeff Layton285abde2014-07-30 08:27:24 -04006443nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006444{
6445 struct nfs4_client *clp;
6446 u64 count = 0;
6447 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6448 nfsd_net_id);
6449
6450 if (!nfsd_netns_ready(nn))
6451 return 0;
6452
6453 spin_lock(&nn->client_lock);
6454 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6455 count += nfsd_print_client_locks(clp);
6456 spin_unlock(&nn->client_lock);
6457
6458 return count;
6459}
6460
6461static void
6462nfsd_reap_locks(struct list_head *reaplist)
6463{
6464 struct nfs4_client *clp;
6465 struct nfs4_ol_stateid *stp, *next;
6466
6467 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6468 list_del_init(&stp->st_locks);
6469 clp = stp->st_stid.sc_client;
6470 nfs4_put_stid(&stp->st_stid);
6471 put_client(clp);
6472 }
6473}
6474
6475u64
Jeff Layton285abde2014-07-30 08:27:24 -04006476nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006477{
6478 unsigned int count = 0;
6479 struct nfs4_client *clp;
6480 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6481 nfsd_net_id);
6482 LIST_HEAD(reaplist);
6483
6484 if (!nfsd_netns_ready(nn))
6485 return count;
6486
6487 spin_lock(&nn->client_lock);
6488 clp = nfsd_find_client(addr, addr_size);
6489 if (clp)
6490 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6491 spin_unlock(&nn->client_lock);
6492 nfsd_reap_locks(&reaplist);
6493 return count;
6494}
6495
6496u64
Jeff Layton285abde2014-07-30 08:27:24 -04006497nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006498{
6499 u64 count = 0;
6500 struct nfs4_client *clp;
6501 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6502 nfsd_net_id);
6503 LIST_HEAD(reaplist);
6504
6505 if (!nfsd_netns_ready(nn))
6506 return count;
6507
6508 spin_lock(&nn->client_lock);
6509 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6510 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6511 if (max != 0 && count >= max)
6512 break;
6513 }
6514 spin_unlock(&nn->client_lock);
6515 nfsd_reap_locks(&reaplist);
6516 return count;
6517}
6518
Jeff Layton82e05ef2014-07-30 08:27:22 -04006519static u64
6520nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6521 struct list_head *collect,
6522 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006523{
6524 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006525 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6526 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006527 u64 count = 0;
6528
Jeff Layton82e05ef2014-07-30 08:27:22 -04006529 lockdep_assert_held(&nn->client_lock);
6530
6531 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006532 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006533 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006534 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006535 if (collect) {
6536 atomic_inc(&clp->cl_refcount);
6537 list_add(&oop->oo_perclient, collect);
6538 }
6539 }
6540 ++count;
6541 /*
6542 * Despite the fact that these functions deal with
6543 * 64-bit integers for "count", we must ensure that
6544 * it doesn't blow up the clp->cl_refcount. Throw a
6545 * warning if we start to approach INT_MAX here.
6546 */
6547 WARN_ON_ONCE(count == (INT_MAX / 2));
6548 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006549 break;
6550 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006551 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006552
6553 return count;
6554}
6555
Jeff Layton82e05ef2014-07-30 08:27:22 -04006556static u64
6557nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006558{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006559 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6560
6561 nfsd_print_count(clp, count, "openowners");
6562 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006563}
6564
Jeff Layton82e05ef2014-07-30 08:27:22 -04006565static u64
6566nfsd_collect_client_openowners(struct nfs4_client *clp,
6567 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006568{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006569 return nfsd_foreach_client_openowner(clp, max, collect,
6570 unhash_openowner_locked);
6571}
6572
6573u64
Jeff Layton285abde2014-07-30 08:27:24 -04006574nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006575{
6576 struct nfs4_client *clp;
6577 u64 count = 0;
6578 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6579 nfsd_net_id);
6580
6581 if (!nfsd_netns_ready(nn))
6582 return 0;
6583
6584 spin_lock(&nn->client_lock);
6585 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6586 count += nfsd_print_client_openowners(clp);
6587 spin_unlock(&nn->client_lock);
6588
6589 return count;
6590}
6591
6592static void
6593nfsd_reap_openowners(struct list_head *reaplist)
6594{
6595 struct nfs4_client *clp;
6596 struct nfs4_openowner *oop, *next;
6597
6598 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6599 list_del_init(&oop->oo_perclient);
6600 clp = oop->oo_owner.so_client;
6601 release_openowner(oop);
6602 put_client(clp);
6603 }
6604}
6605
6606u64
Jeff Layton285abde2014-07-30 08:27:24 -04006607nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6608 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006609{
6610 unsigned int count = 0;
6611 struct nfs4_client *clp;
6612 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6613 nfsd_net_id);
6614 LIST_HEAD(reaplist);
6615
6616 if (!nfsd_netns_ready(nn))
6617 return count;
6618
6619 spin_lock(&nn->client_lock);
6620 clp = nfsd_find_client(addr, addr_size);
6621 if (clp)
6622 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6623 spin_unlock(&nn->client_lock);
6624 nfsd_reap_openowners(&reaplist);
6625 return count;
6626}
6627
6628u64
Jeff Layton285abde2014-07-30 08:27:24 -04006629nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006630{
6631 u64 count = 0;
6632 struct nfs4_client *clp;
6633 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6634 nfsd_net_id);
6635 LIST_HEAD(reaplist);
6636
6637 if (!nfsd_netns_ready(nn))
6638 return count;
6639
6640 spin_lock(&nn->client_lock);
6641 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6642 count += nfsd_collect_client_openowners(clp, &reaplist,
6643 max - count);
6644 if (max != 0 && count >= max)
6645 break;
6646 }
6647 spin_unlock(&nn->client_lock);
6648 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006649 return count;
6650}
6651
Bryan Schumaker269de302012-11-29 11:40:42 -05006652static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6653 struct list_head *victims)
6654{
6655 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006656 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6657 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006658 u64 count = 0;
6659
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006660 lockdep_assert_held(&nn->client_lock);
6661
6662 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006663 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006664 if (victims) {
6665 /*
6666 * It's not safe to mess with delegations that have a
6667 * non-zero dl_time. They might have already been broken
6668 * and could be processed by the laundromat outside of
6669 * the state_lock. Just leave them be.
6670 */
6671 if (dp->dl_time != 0)
6672 continue;
6673
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006674 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006675 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006676 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006677 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006678 ++count;
6679 /*
6680 * Despite the fact that these functions deal with
6681 * 64-bit integers for "count", we must ensure that
6682 * it doesn't blow up the clp->cl_refcount. Throw a
6683 * warning if we start to approach INT_MAX here.
6684 */
6685 WARN_ON_ONCE(count == (INT_MAX / 2));
6686 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006687 break;
6688 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006689 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006690 return count;
6691}
6692
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006693static u64
6694nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006695{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006696 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006697
6698 nfsd_print_count(clp, count, "delegations");
6699 return count;
6700}
6701
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006702u64
Jeff Layton285abde2014-07-30 08:27:24 -04006703nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006704{
6705 struct nfs4_client *clp;
6706 u64 count = 0;
6707 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6708 nfsd_net_id);
6709
6710 if (!nfsd_netns_ready(nn))
6711 return 0;
6712
6713 spin_lock(&nn->client_lock);
6714 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6715 count += nfsd_print_client_delegations(clp);
6716 spin_unlock(&nn->client_lock);
6717
6718 return count;
6719}
6720
6721static void
6722nfsd_forget_delegations(struct list_head *reaplist)
6723{
6724 struct nfs4_client *clp;
6725 struct nfs4_delegation *dp, *next;
6726
6727 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6728 list_del_init(&dp->dl_recall_lru);
6729 clp = dp->dl_stid.sc_client;
6730 revoke_delegation(dp);
6731 put_client(clp);
6732 }
6733}
6734
6735u64
Jeff Layton285abde2014-07-30 08:27:24 -04006736nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6737 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006738{
6739 u64 count = 0;
6740 struct nfs4_client *clp;
6741 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6742 nfsd_net_id);
6743 LIST_HEAD(reaplist);
6744
6745 if (!nfsd_netns_ready(nn))
6746 return count;
6747
6748 spin_lock(&nn->client_lock);
6749 clp = nfsd_find_client(addr, addr_size);
6750 if (clp)
6751 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6752 spin_unlock(&nn->client_lock);
6753
6754 nfsd_forget_delegations(&reaplist);
6755 return count;
6756}
6757
6758u64
Jeff Layton285abde2014-07-30 08:27:24 -04006759nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006760{
6761 u64 count = 0;
6762 struct nfs4_client *clp;
6763 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6764 nfsd_net_id);
6765 LIST_HEAD(reaplist);
6766
6767 if (!nfsd_netns_ready(nn))
6768 return count;
6769
6770 spin_lock(&nn->client_lock);
6771 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6772 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6773 if (max != 0 && count >= max)
6774 break;
6775 }
6776 spin_unlock(&nn->client_lock);
6777 nfsd_forget_delegations(&reaplist);
6778 return count;
6779}
6780
6781static void
6782nfsd_recall_delegations(struct list_head *reaplist)
6783{
6784 struct nfs4_client *clp;
6785 struct nfs4_delegation *dp, *next;
6786
6787 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6788 list_del_init(&dp->dl_recall_lru);
6789 clp = dp->dl_stid.sc_client;
6790 /*
6791 * We skipped all entries that had a zero dl_time before,
6792 * so we can now reset the dl_time back to 0. If a delegation
6793 * break comes in now, then it won't make any difference since
6794 * we're recalling it either way.
6795 */
6796 spin_lock(&state_lock);
6797 dp->dl_time = 0;
6798 spin_unlock(&state_lock);
6799 nfsd_break_one_deleg(dp);
6800 put_client(clp);
6801 }
6802}
6803
6804u64
Jeff Layton285abde2014-07-30 08:27:24 -04006805nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006806 size_t addr_size)
6807{
6808 u64 count = 0;
6809 struct nfs4_client *clp;
6810 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6811 nfsd_net_id);
6812 LIST_HEAD(reaplist);
6813
6814 if (!nfsd_netns_ready(nn))
6815 return count;
6816
6817 spin_lock(&nn->client_lock);
6818 clp = nfsd_find_client(addr, addr_size);
6819 if (clp)
6820 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6821 spin_unlock(&nn->client_lock);
6822
6823 nfsd_recall_delegations(&reaplist);
6824 return count;
6825}
6826
6827u64
Jeff Layton285abde2014-07-30 08:27:24 -04006828nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006829{
6830 u64 count = 0;
6831 struct nfs4_client *clp, *next;
6832 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6833 nfsd_net_id);
6834 LIST_HEAD(reaplist);
6835
6836 if (!nfsd_netns_ready(nn))
6837 return count;
6838
6839 spin_lock(&nn->client_lock);
6840 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6841 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6842 if (max != 0 && ++count >= max)
6843 break;
6844 }
6845 spin_unlock(&nn->client_lock);
6846 nfsd_recall_delegations(&reaplist);
6847 return count;
6848}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006849#endif /* CONFIG_NFSD_FAULT_INJECTION */
6850
Meelap Shahc2f1a552007-07-17 04:04:39 -07006851/*
6852 * Since the lifetime of a delegation isn't limited to that of an open, a
6853 * client may quite reasonably hang on to a delegation as long as it has
6854 * the inode cached. This becomes an obvious problem the first time a
6855 * client's inode cache approaches the size of the server's total memory.
6856 *
6857 * For now we avoid this problem by imposing a hard limit on the number
6858 * of delegations, which varies according to the server's memory size.
6859 */
6860static void
6861set_max_delegations(void)
6862{
6863 /*
6864 * Allow at most 4 delegations per megabyte of RAM. Quick
6865 * estimates suggest that in the worst case (where every delegation
6866 * is for a different inode), a delegation could take about 1.5K,
6867 * giving a worst case usage of about 6% of memory.
6868 */
6869 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6870}
6871
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006872static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006873{
6874 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6875 int i;
6876
6877 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6878 CLIENT_HASH_SIZE, GFP_KERNEL);
6879 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006880 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006881 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6882 CLIENT_HASH_SIZE, GFP_KERNEL);
6883 if (!nn->unconf_id_hashtbl)
6884 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006885 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6886 SESSION_HASH_SIZE, GFP_KERNEL);
6887 if (!nn->sessionid_hashtbl)
6888 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006889
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006890 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006891 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006892 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006893 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006894 for (i = 0; i < SESSION_HASH_SIZE; i++)
6895 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006896 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006897 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03006898 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03006899 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006900 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03006901 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006902
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006903 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006904 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006905
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006906 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006907
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006908err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03006909 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006910err_unconf_id:
6911 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006912err:
6913 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006914}
6915
6916static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006917nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006918{
6919 int i;
6920 struct nfs4_client *clp = NULL;
6921 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6922
6923 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6924 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6925 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6926 destroy_client(clp);
6927 }
6928 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006929
Kinglong Mee2b905632014-03-26 22:09:30 +08006930 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6931 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6932 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6933 destroy_client(clp);
6934 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03006935 }
6936
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03006937 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006938 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006939 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006940 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006941}
6942
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006943int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006944nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07006945{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04006946 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006947 int ret;
6948
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006949 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006950 if (ret)
6951 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006952 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04006953 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04006954 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04006955 locks_start_grace(net, &nn->nfsd4_manager);
6956 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006957 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03006958 nn->nfsd4_grace, net);
6959 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006960 return 0;
6961}
6962
6963/* initialization to perform when the nfsd service is started: */
6964
6965int
6966nfs4_state_start(void)
6967{
6968 int ret;
6969
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006970 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006971 if (ret)
6972 return -ENOMEM;
Jeff Layton51a54562015-08-20 07:17:01 -04006973 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05006974 if (laundry_wq == NULL) {
6975 ret = -ENOMEM;
6976 goto out_recovery;
6977 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006978 ret = nfsd4_create_callback_queue();
6979 if (ret)
6980 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006981
Meelap Shahc2f1a552007-07-17 04:04:39 -07006982 set_max_delegations();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006983
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006984 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006985
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006986out_free_laundry:
6987 destroy_workqueue(laundry_wq);
Jeff Laytona6d6b782012-03-05 11:42:36 -05006988out_recovery:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05006989 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990}
6991
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03006992void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006993nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006997 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03006999 cancel_delayed_work_sync(&nn->laundromat_work);
7000 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007001
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04007003 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007004 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007006 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007007 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 }
Benny Halevycdc97502014-05-30 09:09:30 -04007009 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 list_for_each_safe(pos, next, &reaplist) {
7011 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007012 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02007013 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04007014 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04007015 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 }
7017
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007018 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007019 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020}
7021
7022void
7023nfs4_state_shutdown(void)
7024{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007025 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007026 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007028
7029static void
7030get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7031{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007032 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7033 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007034}
7035
7036static void
7037put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7038{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007039 if (cstate->minorversion) {
7040 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7041 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7042 }
7043}
7044
7045void
7046clear_current_stateid(struct nfsd4_compound_state *cstate)
7047{
7048 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007049}
7050
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007051/*
7052 * functions to set current state id
7053 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007054void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007055nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7056{
7057 put_stateid(cstate, &odp->od_stateid);
7058}
7059
7060void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007061nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
7062{
7063 put_stateid(cstate, &open->op_stateid);
7064}
7065
7066void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007067nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7068{
7069 put_stateid(cstate, &close->cl_stateid);
7070}
7071
7072void
7073nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
7074{
7075 put_stateid(cstate, &lock->lk_resp_stateid);
7076}
7077
7078/*
7079 * functions to consume current state id
7080 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007081
7082void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007083nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7084{
7085 get_stateid(cstate, &odp->od_stateid);
7086}
7087
7088void
7089nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7090{
7091 get_stateid(cstate, &drp->dr_stateid);
7092}
7093
7094void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007095nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7096{
7097 get_stateid(cstate, &fsp->fr_stateid);
7098}
7099
7100void
7101nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7102{
7103 get_stateid(cstate, &setattr->sa_stateid);
7104}
7105
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007106void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007107nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7108{
7109 get_stateid(cstate, &close->cl_stateid);
7110}
7111
7112void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007113nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007114{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007115 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007116}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007117
7118void
7119nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7120{
7121 get_stateid(cstate, &read->rd_stateid);
7122}
7123
7124void
7125nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7126{
7127 get_stateid(cstate, &write->wr_stateid);
7128}