blob: 3656f87d11e39e09dc4cb48d420bc84da88867bb [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};
Trond Myklebust5cd35862017-11-03 08:00:12 -040066static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
68};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069
Andy Adamsonec6b5d72009-04-03 08:28:28 +030070static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070071
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050072#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010074#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elblef25e2222017-11-09 13:41:10 -050075#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040078static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040079static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/* Locking: */
82
J. Bruce Fields8b671b82009-02-22 14:51:34 -080083/*
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
87 */
Benny Halevycdc97502014-05-30 09:09:30 -040088static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080089
Jeff Laytonb401be222014-07-29 21:34:33 -040090/*
91 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
92 * the refcount on the open stateid to drop.
93 */
94static DECLARE_WAIT_QUEUE_HEAD(close_wq);
95
Christoph Hellwigabf11352014-05-21 07:43:03 -070096static struct kmem_cache *openowner_slab;
97static struct kmem_cache *lockowner_slab;
98static struct kmem_cache *file_slab;
99static struct kmem_cache *stateid_slab;
100static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200101static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700102
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800104
Julia Lawallc4cb8972015-11-21 22:57:39 +0100105static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400106static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200107
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400108static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800109{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return ses->se_flags & NFS4_SESSION_DEAD;
111}
112
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400113static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
114{
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
118 return nfs_ok;
119}
120
J. Bruce Fields221a6872013-04-01 22:23:49 -0400121static bool is_client_expired(struct nfs4_client *clp)
122{
123 return clp->cl_time == 0;
124}
125
J. Bruce Fields221a6872013-04-01 22:23:49 -0400126static __be32 get_client_locked(struct nfs4_client *clp)
127{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400128 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
129
130 lockdep_assert_held(&nn->client_lock);
131
J. Bruce Fields221a6872013-04-01 22:23:49 -0400132 if (is_client_expired(clp))
133 return nfserr_expired;
134 atomic_inc(&clp->cl_refcount);
135 return nfs_ok;
136}
137
138/* must be called under the client_lock */
139static inline void
140renew_client_locked(struct nfs4_client *clp)
141{
142 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
143
144 if (is_client_expired(clp)) {
145 WARN_ON(1);
146 printk("%s: client (clientid %08x/%08x) already expired\n",
147 __func__,
148 clp->cl_clientid.cl_boot,
149 clp->cl_clientid.cl_id);
150 return;
151 }
152
153 dprintk("renewing client (clientid %08x/%08x)\n",
154 clp->cl_clientid.cl_boot,
155 clp->cl_clientid.cl_id);
156 list_move_tail(&clp->cl_lru, &nn->client_lru);
157 clp->cl_time = get_seconds();
158}
159
Fengguang Wuba138432013-04-16 22:14:15 -0400160static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400161{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400162 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
163
164 lockdep_assert_held(&nn->client_lock);
165
J. Bruce Fields221a6872013-04-01 22:23:49 -0400166 if (!atomic_dec_and_test(&clp->cl_refcount))
167 return;
168 if (!is_client_expired(clp))
169 renew_client_locked(clp);
170}
171
Jeff Layton4b24ca72014-06-30 11:48:44 -0400172static void put_client_renew(struct nfs4_client *clp)
173{
174 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
175
Jeff Laytond6c249b2014-07-08 14:02:50 -0400176 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
177 return;
178 if (!is_client_expired(clp))
179 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400180 spin_unlock(&nn->client_lock);
181}
182
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400183static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
184{
185 __be32 status;
186
187 if (is_session_dead(ses))
188 return nfserr_badsession;
189 status = get_client_locked(ses->se_client);
190 if (status)
191 return status;
192 atomic_inc(&ses->se_ref);
193 return nfs_ok;
194}
195
196static void nfsd4_put_session_locked(struct nfsd4_session *ses)
197{
198 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400199 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
200
201 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400202
203 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
204 free_session(ses);
205 put_client_renew_locked(clp);
206}
207
208static void nfsd4_put_session(struct nfsd4_session *ses)
209{
210 struct nfs4_client *clp = ses->se_client;
211 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
212
213 spin_lock(&nn->client_lock);
214 nfsd4_put_session_locked(ses);
215 spin_unlock(&nn->client_lock);
216}
217
Jeff Layton76d348f2016-09-16 16:28:24 -0400218static struct nfsd4_blocked_lock *
219find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
220 struct nfsd_net *nn)
221{
222 struct nfsd4_blocked_lock *cur, *found = NULL;
223
Jeff Layton0cc11a62016-10-20 09:34:31 -0400224 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400225 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
226 if (fh_match(fh, &cur->nbl_fh)) {
227 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400228 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400229 found = cur;
230 break;
231 }
232 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400233 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400234 if (found)
235 posix_unblock_lock(&found->nbl_lock);
236 return found;
237}
238
239static struct nfsd4_blocked_lock *
240find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
241 struct nfsd_net *nn)
242{
243 struct nfsd4_blocked_lock *nbl;
244
245 nbl = find_blocked_lock(lo, fh, nn);
246 if (!nbl) {
247 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
248 if (nbl) {
249 fh_copy_shallow(&nbl->nbl_fh, fh);
250 locks_init_lock(&nbl->nbl_lock);
251 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
252 &nfsd4_cb_notify_lock_ops,
253 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
254 }
255 }
256 return nbl;
257}
258
259static void
260free_blocked_lock(struct nfsd4_blocked_lock *nbl)
261{
262 locks_release_private(&nbl->nbl_lock);
263 kfree(nbl);
264}
265
Jeff Laytone294c4c2018-03-16 11:32:02 -0400266static void
267remove_blocked_locks(struct nfs4_lockowner *lo)
268{
269 struct nfs4_client *clp = lo->lo_owner.so_client;
270 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
271 struct nfsd4_blocked_lock *nbl;
272 LIST_HEAD(reaplist);
273
274 /* Dequeue all blocked locks */
275 spin_lock(&nn->blocked_locks_lock);
276 while (!list_empty(&lo->lo_blocked)) {
277 nbl = list_first_entry(&lo->lo_blocked,
278 struct nfsd4_blocked_lock,
279 nbl_list);
280 list_del_init(&nbl->nbl_list);
281 list_move(&nbl->nbl_lru, &reaplist);
282 }
283 spin_unlock(&nn->blocked_locks_lock);
284
285 /* Now free them */
286 while (!list_empty(&reaplist)) {
287 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
288 nbl_lru);
289 list_del_init(&nbl->nbl_lru);
290 posix_unblock_lock(&nbl->nbl_lock);
291 free_blocked_lock(nbl);
292 }
293}
294
Jeff Layton76d348f2016-09-16 16:28:24 -0400295static int
296nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
297{
298 /*
299 * Since this is just an optimization, we don't try very hard if it
300 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
301 * just quit trying on anything else.
302 */
303 switch (task->tk_status) {
304 case -NFS4ERR_DELAY:
305 rpc_delay(task, 1 * HZ);
306 return 0;
307 default:
308 return 1;
309 }
310}
311
312static void
313nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
314{
315 struct nfsd4_blocked_lock *nbl = container_of(cb,
316 struct nfsd4_blocked_lock, nbl_cb);
317
318 free_blocked_lock(nbl);
319}
320
321static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
322 .done = nfsd4_cb_notify_lock_done,
323 .release = nfsd4_cb_notify_lock_release,
324};
325
Kinglong Meeb5971af2014-08-22 10:18:43 -0400326static inline struct nfs4_stateowner *
327nfs4_get_stateowner(struct nfs4_stateowner *sop)
328{
329 atomic_inc(&sop->so_count);
330 return sop;
331}
332
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400333static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400334same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400335{
336 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400337 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400338}
339
340static struct nfs4_openowner *
341find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400342 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400343{
344 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400345
Trond Myklebustd4f04892014-07-29 21:34:36 -0400346 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400347
Trond Myklebustd4f04892014-07-29 21:34:36 -0400348 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
349 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400350 if (!so->so_is_open_owner)
351 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400352 if (same_owner_str(so, &open->op_owner))
353 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400354 }
355 return NULL;
356}
357
358static struct nfs4_openowner *
359find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400360 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400361{
362 struct nfs4_openowner *oo;
363
Trond Myklebustd4f04892014-07-29 21:34:36 -0400364 spin_lock(&clp->cl_lock);
365 oo = find_openstateowner_str_locked(hashval, open, clp);
366 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400367 return oo;
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static inline u32
371opaque_hashval(const void *ptr, int nbytes)
372{
373 unsigned char *cptr = (unsigned char *) ptr;
374
375 u32 x = 0;
376 while (nbytes--) {
377 x *= 37;
378 x += *cptr++;
379 }
380 return x;
381}
382
Jeff Layton5b095e92014-10-23 08:01:02 -0400383static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400384{
Jeff Layton5b095e92014-10-23 08:01:02 -0400385 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
386
387 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400388}
389
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200390void
NeilBrown13cd2182005-06-23 22:03:10 -0700391put_nfs4_file(struct nfs4_file *fi)
392{
Jeff Layton02e12152014-07-16 10:31:57 -0400393 might_lock(&state_lock);
394
Benny Halevycdc97502014-05-30 09:09:30 -0400395 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400396 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400397 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200398 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400399 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
400 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800401 }
NeilBrown13cd2182005-06-23 22:03:10 -0700402}
403
Trond Myklebustde186432014-07-10 14:07:26 -0400404static struct file *
405__nfs4_get_fd(struct nfs4_file *f, int oflag)
406{
407 if (f->fi_fds[oflag])
408 return get_file(f->fi_fds[oflag]);
409 return NULL;
410}
411
412static struct file *
413find_writeable_file_locked(struct nfs4_file *f)
414{
415 struct file *ret;
416
417 lockdep_assert_held(&f->fi_lock);
418
419 ret = __nfs4_get_fd(f, O_WRONLY);
420 if (!ret)
421 ret = __nfs4_get_fd(f, O_RDWR);
422 return ret;
423}
424
425static struct file *
426find_writeable_file(struct nfs4_file *f)
427{
428 struct file *ret;
429
430 spin_lock(&f->fi_lock);
431 ret = find_writeable_file_locked(f);
432 spin_unlock(&f->fi_lock);
433
434 return ret;
435}
436
437static struct file *find_readable_file_locked(struct nfs4_file *f)
438{
439 struct file *ret;
440
441 lockdep_assert_held(&f->fi_lock);
442
443 ret = __nfs4_get_fd(f, O_RDONLY);
444 if (!ret)
445 ret = __nfs4_get_fd(f, O_RDWR);
446 return ret;
447}
448
449static struct file *
450find_readable_file(struct nfs4_file *f)
451{
452 struct file *ret;
453
454 spin_lock(&f->fi_lock);
455 ret = find_readable_file_locked(f);
456 spin_unlock(&f->fi_lock);
457
458 return ret;
459}
460
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500461struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400462find_any_file(struct nfs4_file *f)
463{
464 struct file *ret;
465
466 spin_lock(&f->fi_lock);
467 ret = __nfs4_get_fd(f, O_RDWR);
468 if (!ret) {
469 ret = __nfs4_get_fd(f, O_WRONLY);
470 if (!ret)
471 ret = __nfs4_get_fd(f, O_RDONLY);
472 }
473 spin_unlock(&f->fi_lock);
474 return ret;
475}
476
Trond Myklebust02a35082014-07-25 07:34:22 -0400477static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800478unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700479
480/*
481 * Open owner state (share locks)
482 */
483
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500484/* hash tables for lock and open owners */
485#define OWNER_HASH_BITS 8
486#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
487#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700488
Trond Myklebustd4f04892014-07-29 21:34:36 -0400489static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400490{
491 unsigned int ret;
492
493 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500494 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400495}
NeilBrownef0f3392006-04-10 22:55:41 -0700496
NeilBrownef0f3392006-04-10 22:55:41 -0700497/* hash table for nfs4_file */
498#define FILE_HASH_BITS 8
499#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800500
Trond Myklebustca943212014-07-23 16:17:39 -0400501static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400502{
Trond Myklebustca943212014-07-23 16:17:39 -0400503 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
504}
505
506static unsigned int file_hashval(struct knfsd_fh *fh)
507{
508 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
509}
510
Jeff Layton89876f82013-04-02 09:01:59 -0400511static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700512
Jeff Layton12659652014-07-10 14:07:28 -0400513static void
514__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400515{
Jeff Layton7214e862014-07-10 14:07:33 -0400516 lockdep_assert_held(&fp->fi_lock);
517
Jeff Layton12659652014-07-10 14:07:28 -0400518 if (access & NFS4_SHARE_ACCESS_WRITE)
519 atomic_inc(&fp->fi_access[O_WRONLY]);
520 if (access & NFS4_SHARE_ACCESS_READ)
521 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400522}
523
Jeff Layton12659652014-07-10 14:07:28 -0400524static __be32
525nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400526{
Jeff Layton7214e862014-07-10 14:07:33 -0400527 lockdep_assert_held(&fp->fi_lock);
528
Jeff Layton12659652014-07-10 14:07:28 -0400529 /* Does this access mode make sense? */
530 if (access & ~NFS4_SHARE_ACCESS_BOTH)
531 return nfserr_inval;
532
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400533 /* Does it conflict with a deny mode already set? */
534 if ((access & fp->fi_share_deny) != 0)
535 return nfserr_share_denied;
536
Jeff Layton12659652014-07-10 14:07:28 -0400537 __nfs4_file_get_access(fp, access);
538 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400539}
540
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400541static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
542{
543 /* Common case is that there is no deny mode. */
544 if (deny) {
545 /* Does this deny mode make sense? */
546 if (deny & ~NFS4_SHARE_DENY_BOTH)
547 return nfserr_inval;
548
549 if ((deny & NFS4_SHARE_DENY_READ) &&
550 atomic_read(&fp->fi_access[O_RDONLY]))
551 return nfserr_share_denied;
552
553 if ((deny & NFS4_SHARE_DENY_WRITE) &&
554 atomic_read(&fp->fi_access[O_WRONLY]))
555 return nfserr_share_denied;
556 }
557 return nfs_ok;
558}
559
J. Bruce Fields998db522010-08-07 09:21:41 -0400560static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400561{
Trond Myklebustde186432014-07-10 14:07:26 -0400562 might_lock(&fp->fi_lock);
563
564 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
565 struct file *f1 = NULL;
566 struct file *f2 = NULL;
567
Jeff Layton6d338b52014-07-10 14:07:29 -0400568 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400569 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400570 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400571 spin_unlock(&fp->fi_lock);
572 if (f1)
573 fput(f1);
574 if (f2)
575 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400576 }
577}
578
Jeff Layton12659652014-07-10 14:07:28 -0400579static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400580{
Jeff Layton12659652014-07-10 14:07:28 -0400581 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
582
583 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400584 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400585 if (access & NFS4_SHARE_ACCESS_READ)
586 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400587}
588
Sachin Bhamare8287f002015-04-27 14:50:14 +0200589/*
590 * Allocate a new open/delegation state counter. This is needed for
591 * pNFS for proper return on close semantics.
592 *
593 * Note that we only allocate it for pNFS-enabled exports, otherwise
594 * all pointers to struct nfs4_clnt_odstate are always NULL.
595 */
596static struct nfs4_clnt_odstate *
597alloc_clnt_odstate(struct nfs4_client *clp)
598{
599 struct nfs4_clnt_odstate *co;
600
601 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
602 if (co) {
603 co->co_client = clp;
604 atomic_set(&co->co_odcount, 1);
605 }
606 return co;
607}
608
609static void
610hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
611{
612 struct nfs4_file *fp = co->co_file;
613
614 lockdep_assert_held(&fp->fi_lock);
615 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
616}
617
618static inline void
619get_clnt_odstate(struct nfs4_clnt_odstate *co)
620{
621 if (co)
622 atomic_inc(&co->co_odcount);
623}
624
625static void
626put_clnt_odstate(struct nfs4_clnt_odstate *co)
627{
628 struct nfs4_file *fp;
629
630 if (!co)
631 return;
632
633 fp = co->co_file;
634 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
635 list_del(&co->co_perfile);
636 spin_unlock(&fp->fi_lock);
637
638 nfsd4_return_all_file_layouts(co->co_client, fp);
639 kmem_cache_free(odstate_slab, co);
640 }
641}
642
643static struct nfs4_clnt_odstate *
644find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
645{
646 struct nfs4_clnt_odstate *co;
647 struct nfs4_client *cl;
648
649 if (!new)
650 return NULL;
651
652 cl = new->co_client;
653
654 spin_lock(&fp->fi_lock);
655 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
656 if (co->co_client == cl) {
657 get_clnt_odstate(co);
658 goto out;
659 }
660 }
661 co = new;
662 co->co_file = fp;
663 hash_clnt_odstate_locked(new);
664out:
665 spin_unlock(&fp->fi_lock);
666 return co;
667}
668
Kinglong Mee743146d32017-01-18 19:04:42 +0800669struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
670 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400671{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500672 struct nfs4_stid *stid;
673 int new_id;
674
Trond Myklebustf8338832014-07-25 07:34:19 -0400675 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500676 if (!stid)
677 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400678
Jeff Layton4770d722014-07-29 21:34:10 -0400679 idr_preload(GFP_KERNEL);
680 spin_lock(&cl->cl_lock);
681 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
682 spin_unlock(&cl->cl_lock);
683 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700684 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500685 goto out_free;
Kinglong Mee743146d32017-01-18 19:04:42 +0800686
687 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500688 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500689 stid->sc_stateid.si_opaque.so_id = new_id;
690 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
691 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400692 atomic_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400693 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500694
J. Bruce Fields996e0932011-10-17 11:14:48 -0400695 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500696 * It shouldn't be a problem to reuse an opaque stateid value.
697 * I don't think it is for 4.1. But with 4.0 I worry that, for
698 * example, a stray write retransmission could be accepted by
699 * the server when it should have been rejected. Therefore,
700 * adopt a trick from the sctp code to attempt to maximize the
701 * amount of time until an id is reused, by ensuring they always
702 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400703 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500704 return stid;
705out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800706 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500707 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400708}
709
Jeff Laytonb49e0842014-07-29 21:34:11 -0400710static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400711{
Trond Myklebust60116952014-07-29 21:34:06 -0400712 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400713
Kinglong Mee743146d32017-01-18 19:04:42 +0800714 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400715 if (!stid)
716 return NULL;
717
Kinglong Mee743146d32017-01-18 19:04:42 +0800718 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400719}
720
721static void nfs4_free_deleg(struct nfs4_stid *stid)
722{
Trond Myklebust60116952014-07-29 21:34:06 -0400723 kmem_cache_free(deleg_slab, stid);
724 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400725}
726
NeilBrown6282cd52014-06-04 17:39:26 +1000727/*
728 * When we recall a delegation, we should be careful not to hand it
729 * out again straight away.
730 * To ensure this we keep a pair of bloom filters ('new' and 'old')
731 * in which the filehandles of recalled delegations are "stored".
732 * If a filehandle appear in either filter, a delegation is blocked.
733 * When a delegation is recalled, the filehandle is stored in the "new"
734 * filter.
735 * Every 30 seconds we swap the filters and clear the "new" one,
736 * unless both are empty of course.
737 *
738 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
739 * low 3 bytes as hash-table indices.
740 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400741 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000742 * is used to manage concurrent access. Testing does not need the lock
743 * except when swapping the two filters.
744 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400745static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000746static struct bloom_pair {
747 int entries, old_entries;
748 time_t swap_time;
749 int new; /* index into 'set' */
750 DECLARE_BITMAP(set[2], 256);
751} blocked_delegations;
752
753static int delegation_blocked(struct knfsd_fh *fh)
754{
755 u32 hash;
756 struct bloom_pair *bd = &blocked_delegations;
757
758 if (bd->entries == 0)
759 return 0;
760 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400761 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000762 if (seconds_since_boot() - bd->swap_time > 30) {
763 bd->entries -= bd->old_entries;
764 bd->old_entries = bd->entries;
765 memset(bd->set[bd->new], 0,
766 sizeof(bd->set[0]));
767 bd->new = 1-bd->new;
768 bd->swap_time = seconds_since_boot();
769 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400770 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000771 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100772 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000773 if (test_bit(hash&255, bd->set[0]) &&
774 test_bit((hash>>8)&255, bd->set[0]) &&
775 test_bit((hash>>16)&255, bd->set[0]))
776 return 1;
777
778 if (test_bit(hash&255, bd->set[1]) &&
779 test_bit((hash>>8)&255, bd->set[1]) &&
780 test_bit((hash>>16)&255, bd->set[1]))
781 return 1;
782
783 return 0;
784}
785
786static void block_delegations(struct knfsd_fh *fh)
787{
788 u32 hash;
789 struct bloom_pair *bd = &blocked_delegations;
790
Daniel Borkmann87545892014-12-10 16:33:11 +0100791 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000792
Jeff Laytonf54fe962014-07-25 07:34:26 -0400793 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000794 __set_bit(hash&255, bd->set[bd->new]);
795 __set_bit((hash>>8)&255, bd->set[bd->new]);
796 __set_bit((hash>>16)&255, bd->set[bd->new]);
797 if (bd->entries == 0)
798 bd->swap_time = seconds_since_boot();
799 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400800 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803static struct nfs4_delegation *
Sachin Bhamare8287f002015-04-27 14:50:14 +0200804alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
805 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400808 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400811 n = atomic_long_inc_return(&num_delegations);
812 if (n < 0 || n > max_delegations)
813 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000814 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400815 goto out_dec;
Kinglong Mee743146d32017-01-18 19:04:42 +0800816 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700817 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400818 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400819
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400820 /*
821 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400822 * meaning of seqid 0 isn't meaningful, really, but let's avoid
823 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400824 */
825 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700826 INIT_LIST_HEAD(&dp->dl_perfile);
827 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200829 dp->dl_clnt_odstate = odstate;
830 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400831 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200832 dp->dl_retries = 1;
833 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200834 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400836out_dec:
837 atomic_long_dec(&num_delegations);
838 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841void
Trond Myklebust60116952014-07-29 21:34:06 -0400842nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Trond Myklebust11b91642014-07-29 21:34:08 -0400844 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400845 struct nfs4_client *clp = s->sc_client;
846
Jeff Layton4770d722014-07-29 21:34:10 -0400847 might_lock(&clp->cl_lock);
848
Jeff Laytonb401be222014-07-29 21:34:33 -0400849 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
850 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400851 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400852 }
Trond Myklebust60116952014-07-29 21:34:06 -0400853 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400854 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400855 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400856 if (fp)
857 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Jeff Layton9767feb2015-10-01 09:05:50 -0400860void
861nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
862{
863 stateid_t *src = &stid->sc_stateid;
864
865 spin_lock(&stid->sc_lock);
866 if (unlikely(++src->si_generation == 0))
867 src->si_generation = 1;
868 memcpy(dst, src, sizeof(*dst));
869 spin_unlock(&stid->sc_lock);
870}
871
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500872static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400874 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400875
Jeff Layton6bcc0342014-08-09 10:22:40 -0400876 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500877 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400878 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400879 spin_unlock(&fp->fi_lock);
880
881 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100882 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400883 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200887void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400888{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500889 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400890}
891
Andrew Elble34ed9872015-10-15 12:07:28 -0400892/**
893 * nfs4_get_existing_delegation - Discover if this delegation already exists
894 * @clp: a pointer to the nfs4_client we're granting a delegation to
895 * @fp: a pointer to the nfs4_file we're granting a delegation on
896 *
897 * Return:
898 * On success: NULL if an existing delegation was not found.
899 *
900 * On error: -EAGAIN if one was previously granted to this nfs4_client
901 * for this nfs4_file.
902 *
903 */
904
905static int
906nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400907{
Andrew Elble34ed9872015-10-15 12:07:28 -0400908 struct nfs4_delegation *searchdp = NULL;
909 struct nfs4_client *searchclp = NULL;
910
Benny Halevycdc97502014-05-30 09:09:30 -0400911 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400912 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400913
Andrew Elble34ed9872015-10-15 12:07:28 -0400914 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
915 searchclp = searchdp->dl_stid.sc_client;
916 if (clp == searchclp) {
917 return -EAGAIN;
918 }
919 }
920 return 0;
921}
922
923/**
924 * hash_delegation_locked - Add a delegation to the appropriate lists
925 * @dp: a pointer to the nfs4_delegation we are adding.
926 * @fp: a pointer to the nfs4_file we're granting a delegation on
927 *
928 * Return:
929 * On success: NULL if the delegation was successfully hashed.
930 *
931 * On error: -EAGAIN if one was previously granted to this
932 * nfs4_client for this nfs4_file. Delegation is not hashed.
933 *
934 */
935
936static int
937hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
938{
939 int status;
940 struct nfs4_client *clp = dp->dl_stid.sc_client;
941
942 lockdep_assert_held(&state_lock);
943 lockdep_assert_held(&fp->fi_lock);
944
945 status = nfs4_get_existing_delegation(clp, fp);
946 if (status)
947 return status;
948 ++fp->fi_delegees;
Trond Myklebust67cb1272014-07-29 21:34:17 -0400949 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400950 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400951 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -0400952 list_add(&dp->dl_perclnt, &clp->cl_delegations);
953 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -0400954}
955
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400956static bool
Jeff Layton42690672014-07-25 07:34:20 -0400957unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Trond Myklebust11b91642014-07-29 21:34:08 -0400959 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400960
Jeff Layton42690672014-07-25 07:34:20 -0400961 lockdep_assert_held(&state_lock);
962
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400963 if (list_empty(&dp->dl_perfile))
964 return false;
965
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400966 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400967 /* Ensure that deleg break won't try to requeue it */
968 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400969 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400970 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400972 list_del_init(&dp->dl_perfile);
973 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400974 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400975}
976
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400977static void destroy_delegation(struct nfs4_delegation *dp)
978{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400979 bool unhashed;
980
Jeff Layton42690672014-07-25 07:34:20 -0400981 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400982 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400983 spin_unlock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400984 if (unhashed) {
985 put_clnt_odstate(dp->dl_clnt_odstate);
986 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
987 nfs4_put_stid(&dp->dl_stid);
988 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400989}
990
991static void revoke_delegation(struct nfs4_delegation *dp)
992{
993 struct nfs4_client *clp = dp->dl_stid.sc_client;
994
Jeff Layton2d4a5322014-07-25 07:34:21 -0400995 WARN_ON(!list_empty(&dp->dl_recall_lru));
996
Sachin Bhamare8287f002015-04-27 14:50:14 +0200997 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -0400998 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
999
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001000 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -04001001 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001002 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001003 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -04001004 spin_lock(&clp->cl_lock);
1005 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1006 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001007 }
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010/*
1011 * SETCLIENTID state
1012 */
1013
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001014static unsigned int clientid_hashval(u32 id)
1015{
1016 return id & CLIENT_HASH_MASK;
1017}
1018
1019static unsigned int clientstr_hashval(const char *name)
1020{
1021 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001025 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1026 * st_{access,deny}_bmap field of the stateid, in order to track not
1027 * only what share bits are currently in force, but also what
1028 * combinations of share bits previous opens have used. This allows us
1029 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1030 * return an error if the client attempt to downgrade to a combination
1031 * of share bits not explicable by closing some of its previous opens.
1032 *
1033 * XXX: This enforcement is actually incomplete, since we don't keep
1034 * track of access/deny bit combinations; so, e.g., we allow:
1035 *
1036 * OPEN allow read, deny write
1037 * OPEN allow both, deny none
1038 * DOWNGRADE allow read, deny none
1039 *
1040 * which we should reject.
1041 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001042static unsigned int
1043bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001044 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001045 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001046
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001047 for (i = 1; i < 4; i++) {
1048 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001049 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001050 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001051 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001052}
1053
Jeff Layton82c5ff12012-05-11 09:45:13 -04001054/* set share access for a given stateid */
1055static inline void
1056set_access(u32 access, struct nfs4_ol_stateid *stp)
1057{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001058 unsigned char mask = 1 << access;
1059
1060 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1061 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001062}
1063
1064/* clear share access for a given stateid */
1065static inline void
1066clear_access(u32 access, struct nfs4_ol_stateid *stp)
1067{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001068 unsigned char mask = 1 << access;
1069
1070 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1071 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001072}
1073
1074/* test whether a given stateid has access */
1075static inline bool
1076test_access(u32 access, struct nfs4_ol_stateid *stp)
1077{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001078 unsigned char mask = 1 << access;
1079
1080 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001081}
1082
Jeff Laytonce0fc432012-05-11 09:45:14 -04001083/* set share deny for a given stateid */
1084static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001085set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001086{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001087 unsigned char mask = 1 << deny;
1088
1089 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1090 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001091}
1092
1093/* clear share deny for a given stateid */
1094static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001095clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001096{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001097 unsigned char mask = 1 << deny;
1098
1099 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1100 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001101}
1102
1103/* test whether a given stateid is denying specific access */
1104static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001105test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001106{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001107 unsigned char mask = 1 << deny;
1108
1109 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001110}
1111
1112static int nfs4_access_to_omode(u32 access)
1113{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001114 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001115 case NFS4_SHARE_ACCESS_READ:
1116 return O_RDONLY;
1117 case NFS4_SHARE_ACCESS_WRITE:
1118 return O_WRONLY;
1119 case NFS4_SHARE_ACCESS_BOTH:
1120 return O_RDWR;
1121 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001122 WARN_ON_ONCE(1);
1123 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001124}
1125
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001126/*
1127 * A stateid that had a deny mode associated with it is being released
1128 * or downgraded. Recalculate the deny mode on the file.
1129 */
1130static void
1131recalculate_deny_mode(struct nfs4_file *fp)
1132{
1133 struct nfs4_ol_stateid *stp;
1134
1135 spin_lock(&fp->fi_lock);
1136 fp->fi_share_deny = 0;
1137 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1138 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1139 spin_unlock(&fp->fi_lock);
1140}
1141
1142static void
1143reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1144{
1145 int i;
1146 bool change = false;
1147
1148 for (i = 1; i < 4; i++) {
1149 if ((i & deny) != i) {
1150 change = true;
1151 clear_deny(i, stp);
1152 }
1153 }
1154
1155 /* Recalculate per-file deny mode if there was a change */
1156 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001157 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001158}
1159
Jeff Layton82c5ff12012-05-11 09:45:13 -04001160/* release all access and file references for a given stateid */
1161static void
1162release_all_access(struct nfs4_ol_stateid *stp)
1163{
1164 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001165 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001166
1167 if (fp && stp->st_deny_bmap != 0)
1168 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001169
1170 for (i = 1; i < 4; i++) {
1171 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001172 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001173 clear_access(i, stp);
1174 }
1175}
1176
Kinglong Meed50ffde2015-07-16 12:05:07 +08001177static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1178{
1179 kfree(sop->so_owner.data);
1180 sop->so_ops->so_free(sop);
1181}
1182
Jeff Layton6b180f02014-07-29 21:34:26 -04001183static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1184{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001185 struct nfs4_client *clp = sop->so_client;
1186
1187 might_lock(&clp->cl_lock);
1188
1189 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001190 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001191 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001192 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001193 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001194}
1195
Jeff Laytone8568732015-08-24 12:41:47 -04001196static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001197{
Trond Myklebust11b91642014-07-29 21:34:08 -04001198 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001199
Jeff Layton1c755dc2014-07-29 21:34:12 -04001200 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1201
Jeff Laytone8568732015-08-24 12:41:47 -04001202 if (list_empty(&stp->st_perfile))
1203 return false;
1204
Trond Myklebust1d31a252014-07-10 14:07:25 -04001205 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001206 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001207 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001208 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001209 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001210}
1211
Trond Myklebust60116952014-07-29 21:34:06 -04001212static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001213{
Trond Myklebust60116952014-07-29 21:34:06 -04001214 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001215
Sachin Bhamare8287f002015-04-27 14:50:14 +02001216 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001217 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001218 if (stp->st_stateowner)
1219 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001220 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001221}
1222
Jeff Laytonb49e0842014-07-29 21:34:11 -04001223static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001224{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001225 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1226 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001227 struct file *file;
1228
Jeff Laytonb49e0842014-07-29 21:34:11 -04001229 file = find_any_file(stp->st_stid.sc_file);
1230 if (file)
1231 filp_close(file, (fl_owner_t)lo);
1232 nfs4_free_ol_stateid(stid);
1233}
1234
Jeff Layton2c41beb2014-07-29 21:34:41 -04001235/*
1236 * Put the persistent reference to an already unhashed generic stateid, while
1237 * holding the cl_lock. If it's the last reference, then put it onto the
1238 * reaplist for later destruction.
1239 */
1240static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1241 struct list_head *reaplist)
1242{
1243 struct nfs4_stid *s = &stp->st_stid;
1244 struct nfs4_client *clp = s->sc_client;
1245
1246 lockdep_assert_held(&clp->cl_lock);
1247
1248 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1249
1250 if (!atomic_dec_and_test(&s->sc_count)) {
1251 wake_up_all(&close_wq);
1252 return;
1253 }
1254
1255 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1256 list_add(&stp->st_locks, reaplist);
1257}
1258
Jeff Laytone8568732015-08-24 12:41:47 -04001259static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001260{
Chuck Leverf46c4452016-10-29 18:19:03 -04001261 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001262
1263 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001264 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001265 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001266}
1267
Jeff Layton5adfd882014-07-29 21:34:31 -04001268static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001269{
Chuck Leverf46c4452016-10-29 18:19:03 -04001270 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001271 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001272
Chuck Leverf46c4452016-10-29 18:19:03 -04001273 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001274 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001275 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001276 if (unhashed)
1277 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001278}
1279
Trond Myklebustc58c6612014-07-29 21:34:35 -04001280static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001281{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001282 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001283
Trond Myklebustd4f04892014-07-29 21:34:36 -04001284 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001285
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001286 list_del_init(&lo->lo_owner.so_strhash);
1287}
1288
Jeff Layton2c41beb2014-07-29 21:34:41 -04001289/*
1290 * Free a list of generic stateids that were collected earlier after being
1291 * fully unhashed.
1292 */
1293static void
1294free_ol_stateid_reaplist(struct list_head *reaplist)
1295{
1296 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001297 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001298
1299 might_sleep();
1300
1301 while (!list_empty(reaplist)) {
1302 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1303 st_locks);
1304 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001305 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001306 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001307 if (fp)
1308 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001309 }
1310}
1311
Jeff Laytond83017f2014-07-29 21:34:42 -04001312static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1313 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001314{
1315 struct nfs4_ol_stateid *stp;
1316
Jeff Laytone8568732015-08-24 12:41:47 -04001317 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1318
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001319 while (!list_empty(&open_stp->st_locks)) {
1320 stp = list_entry(open_stp->st_locks.next,
1321 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001322 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001323 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001324 }
1325}
1326
Jeff Laytone8568732015-08-24 12:41:47 -04001327static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001328 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001329{
Jeff Laytone8568732015-08-24 12:41:47 -04001330 bool unhashed;
1331
Jeff Layton2c41beb2014-07-29 21:34:41 -04001332 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1333
Jeff Laytone8568732015-08-24 12:41:47 -04001334 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001335 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001336 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001337}
1338
1339static void release_open_stateid(struct nfs4_ol_stateid *stp)
1340{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001341 LIST_HEAD(reaplist);
1342
1343 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001344 if (unhash_open_stateid(stp, &reaplist))
1345 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001346 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1347 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001348}
1349
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001350static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001351{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001352 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001353
Trond Myklebustd4f04892014-07-29 21:34:36 -04001354 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001355
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001356 list_del_init(&oo->oo_owner.so_strhash);
1357 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001358}
1359
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001360static void release_last_closed_stateid(struct nfs4_openowner *oo)
1361{
Jeff Layton217526e2014-07-30 08:27:11 -04001362 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1363 nfsd_net_id);
1364 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001365
Jeff Layton217526e2014-07-30 08:27:11 -04001366 spin_lock(&nn->client_lock);
1367 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001368 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001369 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001370 oo->oo_last_closed_stid = NULL;
1371 }
Jeff Layton217526e2014-07-30 08:27:11 -04001372 spin_unlock(&nn->client_lock);
1373 if (s)
1374 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001375}
1376
Jeff Layton2c41beb2014-07-29 21:34:41 -04001377static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001378{
1379 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001380 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001381 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001382
Jeff Layton2c41beb2014-07-29 21:34:41 -04001383 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001384
Trond Myklebustd4f04892014-07-29 21:34:36 -04001385 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001386 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001387 while (!list_empty(&oo->oo_owner.so_stateids)) {
1388 stp = list_first_entry(&oo->oo_owner.so_stateids,
1389 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001390 if (unhash_open_stateid(stp, &reaplist))
1391 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001392 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001393 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001394 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001395 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001396 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001397}
1398
Marc Eshel5282fd72009-04-03 08:27:52 +03001399static inline int
1400hash_sessionid(struct nfs4_sessionid *sessionid)
1401{
1402 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1403
1404 return sid->sequence % SESSION_HASH_SIZE;
1405}
1406
Mark Salter135dd002015-04-06 09:46:00 -04001407#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001408static inline void
1409dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1410{
1411 u32 *ptr = (u32 *)(&sessionid->data[0]);
1412 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1413}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001414#else
1415static inline void
1416dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1417{
1418}
1419#endif
1420
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001421/*
1422 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1423 * won't be used for replay.
1424 */
1425void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1426{
1427 struct nfs4_stateowner *so = cstate->replay_owner;
1428
1429 if (nfserr == nfserr_replay_me)
1430 return;
1431
1432 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001433 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001434 return;
1435 }
1436 if (!so)
1437 return;
1438 if (so->so_is_open_owner)
1439 release_last_closed_stateid(openowner(so));
1440 so->so_seqid++;
1441 return;
1442}
Marc Eshel5282fd72009-04-03 08:27:52 +03001443
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001444static void
1445gen_sessionid(struct nfsd4_session *ses)
1446{
1447 struct nfs4_client *clp = ses->se_client;
1448 struct nfsd4_sessionid *sid;
1449
1450 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1451 sid->clientid = clp->cl_clientid;
1452 sid->sequence = current_sessionid++;
1453 sid->reserved = 0;
1454}
1455
1456/*
Andy Adamsona6496372009-08-28 08:45:01 -04001457 * The protocol defines ca_maxresponssize_cached to include the size of
1458 * the rpc header, but all we need to cache is the data starting after
1459 * the end of the initial SEQUENCE operation--the rest we regenerate
1460 * each time. Therefore we can advertise a ca_maxresponssize_cached
1461 * value that is the number of bytes in our cache plus a few additional
1462 * bytes. In order to stay on the safe side, and not promise more than
1463 * we can cache, those additional bytes must be the minimum possible: 24
1464 * bytes of rpc header (xid through accept state, with AUTH_NULL
1465 * verifier), 12 for the compound header (with zero-length tag), and 44
1466 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001467 */
Andy Adamsona6496372009-08-28 08:45:01 -04001468#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1469
Andy Adamson557ce262009-08-28 08:45:04 -04001470static void
1471free_session_slots(struct nfsd4_session *ses)
1472{
1473 int i;
1474
J. Bruce Fields877362f2017-10-17 20:38:49 -04001475 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1476 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001477 kfree(ses->se_slots[i]);
J. Bruce Fields877362f2017-10-17 20:38:49 -04001478 }
Andy Adamson557ce262009-08-28 08:45:04 -04001479}
1480
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001481/*
1482 * We don't actually need to cache the rpc and session headers, so we
1483 * can allocate a little less for each slot:
1484 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001485static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001486{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001487 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001488
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001489 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1490 size = 0;
1491 else
1492 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1493 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001494}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001495
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001496/*
1497 * XXX: If we run out of reserved DRC memory we could (up to a point)
1498 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001499 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001500 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001501static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001502{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001503 u32 slotsize = slot_bytes(ca);
1504 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001505 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001506
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001507 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001508 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1509 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001510 num = min_t(int, num, avail / slotsize);
1511 nfsd_drc_mem_used += num * slotsize;
1512 spin_unlock(&nfsd_drc_lock);
1513
1514 return num;
1515}
1516
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001517static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001518{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001519 int slotsize = slot_bytes(ca);
1520
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001521 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001522 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001523 spin_unlock(&nfsd_drc_lock);
1524}
1525
Kinglong Mee60810e52014-01-01 00:35:47 +08001526static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1527 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001528{
Kinglong Mee60810e52014-01-01 00:35:47 +08001529 int numslots = fattrs->maxreqs;
1530 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001531 struct nfsd4_session *new;
1532 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001533
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001534 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001535 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1536 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001537
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001538 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001539 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001540 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001541 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001542 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001543 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001544 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001545 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001546 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001547
1548 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1549 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1550
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001551 return new;
1552out_free:
1553 while (i--)
1554 kfree(new->se_slots[i]);
1555 kfree(new);
1556 return NULL;
1557}
1558
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001559static void free_conn(struct nfsd4_conn *c)
1560{
1561 svc_xprt_put(c->cn_xprt);
1562 kfree(c);
1563}
1564
1565static void nfsd4_conn_lost(struct svc_xpt_user *u)
1566{
1567 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1568 struct nfs4_client *clp = c->cn_session->se_client;
1569
1570 spin_lock(&clp->cl_lock);
1571 if (!list_empty(&c->cn_persession)) {
1572 list_del(&c->cn_persession);
1573 free_conn(c);
1574 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001575 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001576 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001577}
1578
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001579static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001580{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001581 struct nfsd4_conn *conn;
1582
1583 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1584 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001585 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001586 svc_xprt_get(rqstp->rq_xprt);
1587 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001588 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001589 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1590 return conn;
1591}
1592
J. Bruce Fields328ead22010-09-29 16:11:06 -04001593static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1594{
1595 conn->cn_session = ses;
1596 list_add(&conn->cn_persession, &ses->se_conns);
1597}
1598
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001599static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1600{
1601 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001602
1603 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001604 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001605 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001606}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001607
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001608static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001609{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001610 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001611 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001612}
1613
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001614static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001615{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001616 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001617
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001618 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001619 ret = nfsd4_register_conn(conn);
1620 if (ret)
1621 /* oops; xprt is already down: */
1622 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001623 /* We may have gained or lost a callback channel: */
1624 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001625}
1626
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001627static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001628{
1629 u32 dir = NFS4_CDFC4_FORE;
1630
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001631 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001632 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001633 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001634}
1635
1636/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001637static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001638{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001639 struct nfs4_client *clp = s->se_client;
1640 struct nfsd4_conn *c;
1641
1642 spin_lock(&clp->cl_lock);
1643 while (!list_empty(&s->se_conns)) {
1644 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1645 list_del_init(&c->cn_persession);
1646 spin_unlock(&clp->cl_lock);
1647
1648 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1649 free_conn(c);
1650
1651 spin_lock(&clp->cl_lock);
1652 }
1653 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001654}
1655
J. Bruce Fields1377b692012-09-11 21:42:40 -04001656static void __free_session(struct nfsd4_session *ses)
1657{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001658 free_session_slots(ses);
1659 kfree(ses);
1660}
1661
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001662static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001663{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001664 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001665 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001666 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001667}
Andy Adamson557ce262009-08-28 08:45:04 -04001668
Fengguang Wu135ae822012-11-10 07:20:25 -05001669static 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 -04001670{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001671 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001672 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001673
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001674 new->se_client = clp;
1675 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001676
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001677 INIT_LIST_HEAD(&new->se_conns);
1678
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001679 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001680 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001681 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001682 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001683 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001684 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001685 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001686 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001687 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001688 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001689
Chuck Leverb0d2e422014-08-22 15:10:59 -04001690 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001691 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001692 /*
1693 * This is a little silly; with sessions there's no real
1694 * use for the callback address. Use the peer address
1695 * as a reasonable default for now, but consider fixing
1696 * the rpc client not to require an address in the
1697 * future:
1698 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001699 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1700 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001701 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001702}
1703
Benny Halevy9089f1b2010-05-12 00:12:26 +03001704/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001705static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001706__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001707{
1708 struct nfsd4_session *elem;
1709 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001710 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001711
Trond Myklebust0a880a22014-07-30 08:27:10 -04001712 lockdep_assert_held(&nn->client_lock);
1713
Marc Eshel5282fd72009-04-03 08:27:52 +03001714 dump_sessionid(__func__, sessionid);
1715 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001716 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001717 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001718 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1719 NFS4_MAX_SESSIONID_LEN)) {
1720 return elem;
1721 }
1722 }
1723
1724 dprintk("%s: session not found\n", __func__);
1725 return NULL;
1726}
1727
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001728static struct nfsd4_session *
1729find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1730 __be32 *ret)
1731{
1732 struct nfsd4_session *session;
1733 __be32 status = nfserr_badsession;
1734
1735 session = __find_in_sessionid_hashtbl(sessionid, net);
1736 if (!session)
1737 goto out;
1738 status = nfsd4_get_session_locked(session);
1739 if (status)
1740 session = NULL;
1741out:
1742 *ret = status;
1743 return session;
1744}
1745
Benny Halevy9089f1b2010-05-12 00:12:26 +03001746/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001747static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001748unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001749{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001750 struct nfs4_client *clp = ses->se_client;
1751 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1752
1753 lockdep_assert_held(&nn->client_lock);
1754
Andy Adamson7116ed62009-04-03 08:27:43 +03001755 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001756 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001757 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001758 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001759}
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1762static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001763STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001765 /*
1766 * We're assuming the clid was not given out from a boot
1767 * precisely 2^32 (about 136 years) before this one. That seems
1768 * a safe assumption:
1769 */
1770 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001772 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001773 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 1;
1775}
1776
1777/*
1778 * XXX Should we use a slab cache ?
1779 * This type of memory management is somewhat inefficient, but we use it
1780 * anyway since SETCLIENTID is not a common operation.
1781 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001782static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001785 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001787 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1788 if (clp == NULL)
1789 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001790 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001791 if (clp->cl_name.data == NULL)
1792 goto err_no_name;
1793 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1794 OWNER_HASH_SIZE, GFP_KERNEL);
1795 if (!clp->cl_ownerstr_hashtbl)
1796 goto err_no_hashtbl;
1797 for (i = 0; i < OWNER_HASH_SIZE; i++)
1798 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001799 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001800 INIT_LIST_HEAD(&clp->cl_sessions);
1801 idr_init(&clp->cl_stateids);
1802 atomic_set(&clp->cl_refcount, 0);
1803 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1804 INIT_LIST_HEAD(&clp->cl_idhash);
1805 INIT_LIST_HEAD(&clp->cl_openowners);
1806 INIT_LIST_HEAD(&clp->cl_delegations);
1807 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001808 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001809#ifdef CONFIG_NFSD_PNFS
1810 INIT_LIST_HEAD(&clp->cl_lo_states);
1811#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001812 spin_lock_init(&clp->cl_lock);
1813 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001815err_no_hashtbl:
1816 kfree(clp->cl_name.data);
1817err_no_name:
1818 kfree(clp);
1819 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001822static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823free_client(struct nfs4_client *clp)
1824{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001825 while (!list_empty(&clp->cl_sessions)) {
1826 struct nfsd4_session *ses;
1827 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1828 se_perclnt);
1829 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001830 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1831 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001832 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001833 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001834 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001835 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001837 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 kfree(clp);
1839}
1840
Benny Halevy84d38ac2010-05-12 00:13:16 +03001841/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001842static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001843unhash_client_locked(struct nfs4_client *clp)
1844{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001845 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001846 struct nfsd4_session *ses;
1847
Trond Myklebust0a880a22014-07-30 08:27:10 -04001848 lockdep_assert_held(&nn->client_lock);
1849
Trond Myklebust4beb3452014-07-30 08:27:02 -04001850 /* Mark the client as expired! */
1851 clp->cl_time = 0;
1852 /* Make it invisible */
1853 if (!list_empty(&clp->cl_idhash)) {
1854 list_del_init(&clp->cl_idhash);
1855 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1856 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1857 else
1858 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1859 }
1860 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001861 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001862 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1863 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001864 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001865}
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001868unhash_client(struct nfs4_client *clp)
1869{
1870 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1871
1872 spin_lock(&nn->client_lock);
1873 unhash_client_locked(clp);
1874 spin_unlock(&nn->client_lock);
1875}
1876
Jeff Layton97403d92014-07-30 08:27:12 -04001877static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1878{
1879 if (atomic_read(&clp->cl_refcount))
1880 return nfserr_jukebox;
1881 unhash_client_locked(clp);
1882 return nfs_ok;
1883}
1884
Trond Myklebust4beb3452014-07-30 08:27:02 -04001885static void
1886__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Jeff Laytone294c4c2018-03-16 11:32:02 -04001888 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001889 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 struct list_head reaplist;
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001894 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001895 while (!list_empty(&clp->cl_delegations)) {
1896 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001897 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001898 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Benny Halevycdc97502014-05-30 09:09:30 -04001900 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 while (!list_empty(&reaplist)) {
1902 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001903 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001904 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001905 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001906 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001908 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001909 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001910 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001911 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001912 }
NeilBrownea1da632005-06-23 22:04:17 -07001913 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001914 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001915 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001916 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Jeff Laytone294c4c2018-03-16 11:32:02 -04001918 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1919 struct nfs4_stateowner *so, *tmp;
1920
1921 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1922 so_strhash) {
1923 /* Should be no openowners at this point */
1924 WARN_ON_ONCE(so->so_is_open_owner);
1925 remove_blocked_locks(lockowner(so));
1926 }
1927 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001928 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001929 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001930 if (clp->cl_cb_conn.cb_xprt)
1931 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001932 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Trond Myklebust4beb3452014-07-30 08:27:02 -04001935static void
1936destroy_client(struct nfs4_client *clp)
1937{
1938 unhash_client(clp);
1939 __destroy_client(clp);
1940}
1941
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001942static void expire_client(struct nfs4_client *clp)
1943{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001944 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001945 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001946 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001947}
1948
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001949static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1950{
1951 memcpy(target->cl_verifier.data, source->data,
1952 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001955static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1956{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1958 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1959}
1960
J. Bruce Fields50043852015-11-20 15:58:37 -05001961int strdup_if_nonnull(char **target, char *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001962{
J. Bruce Fields50043852015-11-20 15:58:37 -05001963 if (source) {
1964 *target = kstrdup(source, GFP_KERNEL);
1965 if (!*target)
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001966 return -ENOMEM;
1967 } else
J. Bruce Fields50043852015-11-20 15:58:37 -05001968 *target = NULL;
1969 return 0;
1970}
1971
1972static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1973{
1974 int ret;
1975
1976 ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
1977 if (ret)
1978 return ret;
J. Bruce Fields414ca012015-11-20 10:48:02 -05001979 ret = strdup_if_nonnull(&target->cr_raw_principal,
1980 source->cr_raw_principal);
1981 if (ret)
1982 return ret;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001983 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 target->cr_uid = source->cr_uid;
1985 target->cr_gid = source->cr_gid;
1986 target->cr_group_info = source->cr_group_info;
1987 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001988 target->cr_gss_mech = source->cr_gss_mech;
1989 if (source->cr_gss_mech)
1990 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001994static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001995compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1996{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001997 if (o1->len < o2->len)
1998 return -1;
1999 if (o1->len > o2->len)
2000 return 1;
2001 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002002}
2003
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002004static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002005{
NeilBrowna55370a2005-06-23 22:03:52 -07002006 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002010same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2011{
2012 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
2015static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002016same_clid(clientid_t *cl1, clientid_t *cl2)
2017{
2018 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002021static bool groups_equal(struct group_info *g1, struct group_info *g2)
2022{
2023 int i;
2024
2025 if (g1->ngroups != g2->ngroups)
2026 return false;
2027 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002028 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002029 return false;
2030 return true;
2031}
2032
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002033/*
2034 * RFC 3530 language requires clid_inuse be returned when the
2035 * "principal" associated with a requests differs from that previously
2036 * used. We use uid, gid's, and gss principal string as our best
2037 * approximation. We also don't want to allow non-gss use of a client
2038 * established using gss: in theory cr_principal should catch that
2039 * change, but in practice cr_principal can be null even in the gss case
2040 * since gssd doesn't always pass down a principal string.
2041 */
2042static bool is_gss_cred(struct svc_cred *cr)
2043{
2044 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2045 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2046}
2047
2048
Vivek Trivedi5559b502012-07-24 21:18:20 +05302049static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002050same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2051{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002052 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002053 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2054 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002055 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2056 return false;
2057 if (cr1->cr_principal == cr2->cr_principal)
2058 return true;
2059 if (!cr1->cr_principal || !cr2->cr_principal)
2060 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302061 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
J. Bruce Fields57266a62013-04-13 14:27:29 -04002064static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2065{
2066 struct svc_cred *cr = &rqstp->rq_cred;
2067 u32 service;
2068
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002069 if (!cr->cr_gss_mech)
2070 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002071 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2072 return service == RPC_GSS_SVC_INTEGRITY ||
2073 service == RPC_GSS_SVC_PRIVACY;
2074}
2075
Andrew Elblededeb132016-06-15 12:52:08 -04002076bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002077{
2078 struct svc_cred *cr = &rqstp->rq_cred;
2079
2080 if (!cl->cl_mach_cred)
2081 return true;
2082 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2083 return false;
2084 if (!svc_rqst_integrity_protected(rqstp))
2085 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002086 if (cl->cl_cred.cr_raw_principal)
2087 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2088 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002089 if (!cr->cr_principal)
2090 return false;
2091 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2092}
2093
Jeff Layton294ac322014-07-30 08:27:15 -04002094static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002095{
Chuck Leverab4684d2012-03-02 17:13:50 -05002096 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Jeff Laytonf4199922014-06-17 07:44:11 -04002098 /*
2099 * This is opaque to client, so no need to byte-swap. Use
2100 * __force to keep sparse happy
2101 */
2102 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002103 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002104 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Jeff Layton294ac322014-07-30 08:27:15 -04002107static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2108{
2109 clp->cl_clientid.cl_boot = nn->boot_time;
2110 clp->cl_clientid.cl_id = nn->clientid_counter++;
2111 gen_confirm(clp, nn);
2112}
2113
Jeff Layton4770d722014-07-29 21:34:10 -04002114static struct nfs4_stid *
2115find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002116{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002117 struct nfs4_stid *ret;
2118
2119 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2120 if (!ret || !ret->sc_type)
2121 return NULL;
2122 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002123}
2124
Jeff Layton4770d722014-07-29 21:34:10 -04002125static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002126find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002127{
2128 struct nfs4_stid *s;
2129
Jeff Layton4770d722014-07-29 21:34:10 -04002130 spin_lock(&cl->cl_lock);
2131 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002132 if (s != NULL) {
2133 if (typemask & s->sc_type)
2134 atomic_inc(&s->sc_count);
2135 else
2136 s = NULL;
2137 }
Jeff Layton4770d722014-07-29 21:34:10 -04002138 spin_unlock(&cl->cl_lock);
2139 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002140}
2141
Jeff Layton2216d442012-11-12 15:00:57 -05002142static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002143 struct svc_rqst *rqstp, nfs4_verifier *verf)
2144{
2145 struct nfs4_client *clp;
2146 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002147 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002148 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002149
2150 clp = alloc_client(name);
2151 if (clp == NULL)
2152 return NULL;
2153
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002154 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2155 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002156 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002157 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002158 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002159 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002160 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002161 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002162 copy_verf(clp, verf);
2163 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002164 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002165 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002166 return clp;
2167}
2168
NeilBrownfd39ca92005-06-23 22:04:03 -07002169static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002170add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2171{
2172 struct rb_node **new = &(root->rb_node), *parent = NULL;
2173 struct nfs4_client *clp;
2174
2175 while (*new) {
2176 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2177 parent = *new;
2178
2179 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2180 new = &((*new)->rb_left);
2181 else
2182 new = &((*new)->rb_right);
2183 }
2184
2185 rb_link_node(&new_clp->cl_namenode, parent, new);
2186 rb_insert_color(&new_clp->cl_namenode, root);
2187}
2188
2189static struct nfs4_client *
2190find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2191{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002192 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002193 struct rb_node *node = root->rb_node;
2194 struct nfs4_client *clp;
2195
2196 while (node) {
2197 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2198 cmp = compare_blob(&clp->cl_name, name);
2199 if (cmp > 0)
2200 node = node->rb_left;
2201 else if (cmp < 0)
2202 node = node->rb_right;
2203 else
2204 return clp;
2205 }
2206 return NULL;
2207}
2208
2209static void
2210add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002213 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Trond Myklebust0a880a22014-07-30 08:27:10 -04002215 lockdep_assert_held(&nn->client_lock);
2216
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002217 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002218 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002220 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002221 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
NeilBrownfd39ca92005-06-23 22:04:03 -07002224static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225move_to_confirmed(struct nfs4_client *clp)
2226{
2227 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002228 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Trond Myklebust0a880a22014-07-30 08:27:10 -04002230 lockdep_assert_held(&nn->client_lock);
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002233 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002234 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002235 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002236 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002237 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002241find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 struct nfs4_client *clp;
2244 unsigned int idhashval = clientid_hashval(clid->cl_id);
2245
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002246 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002247 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002248 if ((bool)clp->cl_minorversion != sessions)
2249 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002250 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254 return NULL;
2255}
2256
2257static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002258find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2259{
2260 struct list_head *tbl = nn->conf_id_hashtbl;
2261
Trond Myklebust0a880a22014-07-30 08:27:10 -04002262 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002263 return find_client_in_id_table(tbl, clid, sessions);
2264}
2265
2266static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002267find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002269 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Trond Myklebust0a880a22014-07-30 08:27:10 -04002271 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002272 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002275static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002276{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002277 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002278}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002279
NeilBrown28ce6052005-06-23 22:03:56 -07002280static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002281find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002282{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002283 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002284 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002285}
2286
2287static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002288find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002289{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002290 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002291 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002292}
2293
NeilBrownfd39ca92005-06-23 22:04:03 -07002294static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002295gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002297 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002298 struct sockaddr *sa = svc_addr(rqstp);
2299 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002300 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Jeff Layton7077ecb2009-08-14 12:57:58 -04002302 /* Currently, we only support tcp and tcp6 for the callback channel */
2303 if (se->se_callback_netid_len == 3 &&
2304 !memcmp(se->se_callback_netid_val, "tcp", 3))
2305 expected_family = AF_INET;
2306 else if (se->se_callback_netid_len == 4 &&
2307 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2308 expected_family = AF_INET6;
2309 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 goto out_err;
2311
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002312 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002313 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002314 (struct sockaddr *)&conn->cb_addr,
2315 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002316
J. Bruce Fields07263f12010-05-31 19:09:40 -04002317 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002319
J. Bruce Fields07263f12010-05-31 19:09:40 -04002320 if (conn->cb_addr.ss_family == AF_INET6)
2321 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002322
J. Bruce Fields07263f12010-05-31 19:09:40 -04002323 conn->cb_prog = se->se_callback_prog;
2324 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002325 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return;
2327out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002328 conn->cb_addr.ss_family = AF_UNSPEC;
2329 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002330 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 "will not receive delegations\n",
2332 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return;
2335}
2336
Andy Adamson074fe892009-04-03 08:28:15 +03002337/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002338 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002339 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002340static void
Andy Adamson074fe892009-04-03 08:28:15 +03002341nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2342{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002343 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002344 struct nfsd4_slot *slot = resp->cstate.slot;
2345 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002346
Andy Adamson557ce262009-08-28 08:45:04 -04002347 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002348
J. Bruce Fieldsf92c45b2017-10-18 16:17:18 -04002349 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002350 slot->sl_opcnt = resp->opcnt;
2351 slot->sl_status = resp->cstate.status;
J. Bruce Fields877362f2017-10-17 20:38:49 -04002352 free_svc_cred(&slot->sl_cred);
2353 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002354
J. Bruce Fieldsf92c45b2017-10-18 16:17:18 -04002355 if (!nfsd4_cache_this(resp)) {
2356 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002357 return;
2358 }
J. Bruce Fieldsf92c45b2017-10-18 16:17:18 -04002359 slot->sl_flags |= NFSD4_SLOT_CACHED;
2360
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002361 base = resp->cstate.data_offset;
2362 slot->sl_datalen = buf->len - base;
2363 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002364 WARN(1, "%s: sessions DRC could not cache compound\n",
2365 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002366 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002367}
2368
2369/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002370 * Encode the replay sequence operation from the slot values.
2371 * If cachethis is FALSE encode the uncached rep error on the next
2372 * operation which sets resp->p and increments resp->opcnt for
2373 * nfs4svc_encode_compoundres.
2374 *
Andy Adamson074fe892009-04-03 08:28:15 +03002375 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002376static __be32
2377nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2378 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002379{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002380 struct nfsd4_op *op;
2381 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002382
Andy Adamsonabfabf82009-07-23 19:02:18 -04002383 /* Encode the replayed sequence operation */
2384 op = &args->ops[resp->opcnt - 1];
2385 nfsd4_encode_operation(resp, op);
2386
J. Bruce Fieldsf92c45b2017-10-18 16:17:18 -04002387 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2388 return op->status;
2389 if (args->opcnt == 1) {
2390 /*
2391 * The original operation wasn't a solo sequence--we
2392 * always cache those--so this retry must not match the
2393 * original:
2394 */
2395 op->status = nfserr_seq_false_retry;
2396 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002397 op = &args->ops[resp->opcnt++];
2398 op->status = nfserr_retry_uncached_rep;
2399 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002400 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002401 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002402}
2403
2404/*
Andy Adamson557ce262009-08-28 08:45:04 -04002405 * The sequence operation is not cached because we can use the slot and
2406 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002407 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002408static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002409nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2410 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002411{
Andy Adamson557ce262009-08-28 08:45:04 -04002412 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002413 struct xdr_stream *xdr = &resp->xdr;
2414 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002415 __be32 status;
2416
Andy Adamson557ce262009-08-28 08:45:04 -04002417 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002418
Andy Adamsonabfabf82009-07-23 19:02:18 -04002419 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002420 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002421 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002422
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002423 p = xdr_reserve_space(xdr, slot->sl_datalen);
2424 if (!p) {
2425 WARN_ON_ONCE(1);
2426 return nfserr_serverfault;
2427 }
2428 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2429 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002430
Andy Adamson557ce262009-08-28 08:45:04 -04002431 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002432 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002433}
2434
Andy Adamson0733d212009-04-03 08:28:01 +03002435/*
2436 * Set the exchange_id flags returned by the server.
2437 */
2438static void
2439nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2440{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002441#ifdef CONFIG_NFSD_PNFS
2442 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2443#else
Andy Adamson0733d212009-04-03 08:28:01 +03002444 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002445#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002446
2447 /* Referrals are supported, Migration is not. */
2448 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2449
2450 /* set the wire flags to return to client. */
2451 clid->flags = new->cl_exchange_flags;
2452}
2453
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002454static bool client_has_openowners(struct nfs4_client *clp)
2455{
2456 struct nfs4_openowner *oo;
2457
2458 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2459 if (!list_empty(&oo->oo_owner.so_stateids))
2460 return true;
2461 }
2462 return false;
2463}
2464
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002465static bool client_has_state(struct nfs4_client *clp)
2466{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002467 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002468#ifdef CONFIG_NFSD_PNFS
2469 || !list_empty(&clp->cl_lo_states)
2470#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002471 || !list_empty(&clp->cl_delegations)
2472 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002473}
2474
Al Virob37ad282006-10-19 23:28:59 -07002475__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002476nfsd4_exchange_id(struct svc_rqst *rqstp,
2477 struct nfsd4_compound_state *cstate,
2478 struct nfsd4_exchange_id *exid)
2479{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002480 struct nfs4_client *conf, *new;
2481 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002482 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002483 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002484 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002485 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002486 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002487 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002488
Jeff Layton363168b2009-08-14 12:57:56 -04002489 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002490 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002491 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002492 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002493 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002494
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002495 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002496 return nfserr_inval;
2497
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002498 new = create_client(exid->clname, rqstp, &verf);
2499 if (new == NULL)
2500 return nfserr_jukebox;
2501
Andy Adamson0733d212009-04-03 08:28:01 +03002502 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002503 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002504 exid->spo_must_enforce[0] = 0;
2505 exid->spo_must_enforce[1] = (
2506 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2507 1 << (OP_EXCHANGE_ID - 32) |
2508 1 << (OP_CREATE_SESSION - 32) |
2509 1 << (OP_DESTROY_SESSION - 32) |
2510 1 << (OP_DESTROY_CLIENTID - 32));
2511
2512 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2513 1 << (OP_OPEN_DOWNGRADE) |
2514 1 << (OP_LOCKU) |
2515 1 << (OP_DELEGRETURN));
2516
2517 exid->spo_must_allow[1] &= (
2518 1 << (OP_TEST_STATEID - 32) |
2519 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002520 if (!svc_rqst_integrity_protected(rqstp)) {
2521 status = nfserr_inval;
2522 goto out_nolock;
2523 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002524 /*
2525 * Sometimes userspace doesn't give us a principal.
2526 * Which is a bug, really. Anyway, we can't enforce
2527 * MACH_CRED in that case, better to give up now:
2528 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002529 if (!new->cl_cred.cr_principal &&
2530 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002531 status = nfserr_serverfault;
2532 goto out_nolock;
2533 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002534 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002535 case SP4_NONE:
2536 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002537 default: /* checked by xdr code */
2538 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002539 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002540 status = nfserr_encr_alg_unsupp;
2541 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002542 }
2543
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002544 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002545 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002546 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002547 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002548 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2549 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2550
J. Bruce Fields136e6582012-05-12 20:37:23 -04002551 if (update) {
2552 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002553 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002554 goto out;
2555 }
Andrew Elblededeb132016-06-15 12:52:08 -04002556 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002557 status = nfserr_wrong_cred;
2558 goto out;
2559 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002560 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002561 status = nfserr_perm;
2562 goto out;
2563 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002564 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002565 status = nfserr_not_same;
2566 goto out;
2567 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002568 /* case 6 */
2569 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002570 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002571 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002572 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002573 if (client_has_state(conf)) {
2574 status = nfserr_clid_inuse;
2575 goto out;
2576 }
Andy Adamson0733d212009-04-03 08:28:01 +03002577 goto out_new;
2578 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002579 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002580 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002581 goto out_copy;
2582 }
2583 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002584 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002585 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002586 }
2587
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002588 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002589 status = nfserr_noent;
2590 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002591 }
2592
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002593 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002594 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002595 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002596
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002597 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002598out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002599 if (conf) {
2600 status = mark_client_expired_locked(conf);
2601 if (status)
2602 goto out;
2603 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002604 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002605 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2606 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002607
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002608 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002609 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002610 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002611out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002612 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2613 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002614
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002615 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2616 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002617
2618 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002619 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002620 status = nfs_ok;
2621
2622out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002623 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002624out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002625 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002626 expire_client(new);
2627 if (unconf)
2628 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002629 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002630}
2631
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002632static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002633check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002634{
Andy Adamson88e588d2009-07-23 19:02:15 -04002635 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2636 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002637
2638 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002639 if (slot_inuse) {
2640 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002641 return nfserr_jukebox;
2642 else
2643 return nfserr_seq_misordered;
2644 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002645 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002646 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002647 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002648 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002649 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002650 return nfserr_seq_misordered;
2651}
2652
Andy Adamson49557cc2009-07-23 19:02:16 -04002653/*
2654 * Cache the create session result into the create session single DRC
2655 * slot cache by saving the xdr structure. sl_seqid has been set.
2656 * Do this for solo or embedded create session operations.
2657 */
2658static void
2659nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002660 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002661{
2662 slot->sl_status = nfserr;
2663 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2664}
2665
2666static __be32
2667nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2668 struct nfsd4_clid_slot *slot)
2669{
2670 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2671 return slot->sl_status;
2672}
2673
Mi Jinlong1b74c252011-07-14 14:50:17 +08002674#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2675 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2676 1 + /* MIN tag is length with zero, only length */ \
2677 3 + /* version, opcount, opcode */ \
2678 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2679 /* seqid, slotID, slotID, cache */ \
2680 4 ) * sizeof(__be32))
2681
2682#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2683 2 + /* verifier: AUTH_NULL, length 0 */\
2684 1 + /* status */ \
2685 1 + /* MIN tag is length with zero, only length */ \
2686 3 + /* opcount, opcode, opstatus*/ \
2687 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2688 /* seqid, slotID, slotID, slotID, status */ \
2689 5 ) * sizeof(__be32))
2690
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002691static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002692{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002693 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2694
J. Bruce Fields373cd402013-04-08 15:42:12 -04002695 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2696 return nfserr_toosmall;
2697 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2698 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002699 ca->headerpadsz = 0;
2700 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2701 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2702 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2703 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2704 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2705 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2706 /*
2707 * Note decreasing slot size below client's request may make it
2708 * difficult for client to function correctly, whereas
2709 * decreasing the number of slots will (just?) affect
2710 * performance. When short on memory we therefore prefer to
2711 * decrease number of slots instead of their size. Clients that
2712 * request larger slots than they need will get poor results:
2713 */
2714 ca->maxreqs = nfsd4_get_drc_mem(ca);
2715 if (!ca->maxreqs)
2716 return nfserr_jukebox;
2717
J. Bruce Fields373cd402013-04-08 15:42:12 -04002718 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002719}
2720
Chuck Lever45006322016-03-01 13:06:02 -05002721/*
2722 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2723 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2724 */
2725#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2726 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2727
2728#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2729 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2730
Kinglong Mee8a891632013-12-23 18:11:02 +08002731#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002732 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002733#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002734 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2735 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002736
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002737static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2738{
2739 ca->headerpadsz = 0;
2740
Kinglong Mee8a891632013-12-23 18:11:02 +08002741 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002742 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002743 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002744 return nfserr_toosmall;
2745 ca->maxresp_cached = 0;
2746 if (ca->maxops < 2)
2747 return nfserr_toosmall;
2748
2749 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002750}
2751
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002752static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2753{
2754 switch (cbs->flavor) {
2755 case RPC_AUTH_NULL:
2756 case RPC_AUTH_UNIX:
2757 return nfs_ok;
2758 default:
2759 /*
2760 * GSS case: the spec doesn't allow us to return this
2761 * error. But it also doesn't allow us not to support
2762 * GSS.
2763 * I'd rather this fail hard than return some error the
2764 * client might think it can already handle:
2765 */
2766 return nfserr_encr_alg_unsupp;
2767 }
2768}
2769
Andy Adamson069b6ad2009-04-03 08:27:58 +03002770__be32
2771nfsd4_create_session(struct svc_rqst *rqstp,
2772 struct nfsd4_compound_state *cstate,
2773 struct nfsd4_create_session *cr_ses)
2774{
Jeff Layton363168b2009-08-14 12:57:56 -04002775 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002776 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002777 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002778 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002779 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002780 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002781 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002782 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002783
Mi Jinlonga62573d2011-03-23 17:57:07 +08002784 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2785 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002786 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2787 if (status)
2788 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002789 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002790 if (status)
2791 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002792 status = check_backchannel_attrs(&cr_ses->back_channel);
2793 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002794 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002795 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002796 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002797 if (!new)
2798 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002799 conn = alloc_conn_from_crses(rqstp, cr_ses);
2800 if (!conn)
2801 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002802
Jeff Laytond20c11d2014-07-30 08:27:07 -04002803 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002804 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002805 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002806 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002807
2808 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002809 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002810 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002811 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002812 cs_slot = &conf->cl_cs_slot;
2813 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002814 if (status) {
2815 if (status == nfserr_replay_cache)
2816 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002817 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002818 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002819 } else if (unconf) {
2820 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002821 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002822 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002823 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002824 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002825 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002826 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002827 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002828 cs_slot = &unconf->cl_cs_slot;
2829 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002830 if (status) {
2831 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002832 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002833 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002834 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002835 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002836 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002837 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002838 if (status) {
2839 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002840 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002841 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002842 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002843 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002844 conf = unconf;
2845 } else {
2846 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002847 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002848 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002849 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002850 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002851 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002852 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002853 cr_ses->flags &= ~SESSION4_RDMA;
2854
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002855 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002856 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002857
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002858 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002859 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002860 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002861 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002862
Jeff Laytond20c11d2014-07-30 08:27:07 -04002863 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002864 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002865 spin_unlock(&nn->client_lock);
2866 /* init connection and backchannel */
2867 nfsd4_init_conn(rqstp, conn, new);
2868 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002869 if (old)
2870 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002871 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002872out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002873 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002874 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002875 if (old)
2876 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002877out_free_session:
2878 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002879out_release_drc_mem:
2880 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002881 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002882}
2883
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002884static __be32 nfsd4_map_bcts_dir(u32 *dir)
2885{
2886 switch (*dir) {
2887 case NFS4_CDFC4_FORE:
2888 case NFS4_CDFC4_BACK:
2889 return nfs_ok;
2890 case NFS4_CDFC4_FORE_OR_BOTH:
2891 case NFS4_CDFC4_BACK_OR_BOTH:
2892 *dir = NFS4_CDFC4_BOTH;
2893 return nfs_ok;
2894 };
2895 return nfserr_inval;
2896}
2897
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002898__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2899{
2900 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002901 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002902 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002903
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002904 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2905 if (status)
2906 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002907 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002908 session->se_cb_prog = bc->bc_cb_program;
2909 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002910 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002911
2912 nfsd4_probe_callback(session->se_client);
2913
2914 return nfs_ok;
2915}
2916
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002917__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2918 struct nfsd4_compound_state *cstate,
2919 struct nfsd4_bind_conn_to_session *bcts)
2920{
2921 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002922 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002923 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002924 struct net *net = SVC_NET(rqstp);
2925 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002926
2927 if (!nfsd4_last_compound_op(rqstp))
2928 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002929 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002930 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002931 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002932 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002933 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002934 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002935 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002936 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002937 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002938 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002939 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002940 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002941 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002942 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002943 goto out;
2944 nfsd4_init_conn(rqstp, conn, session);
2945 status = nfs_ok;
2946out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002947 nfsd4_put_session(session);
2948out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002949 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002950}
2951
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002952static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2953{
2954 if (!session)
2955 return 0;
2956 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2957}
2958
Andy Adamson069b6ad2009-04-03 08:27:58 +03002959__be32
2960nfsd4_destroy_session(struct svc_rqst *r,
2961 struct nfsd4_compound_state *cstate,
2962 struct nfsd4_destroy_session *sessionid)
2963{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002964 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002965 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002966 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002967 struct net *net = SVC_NET(r);
2968 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002969
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002970 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002971 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002972 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002973 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002974 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002975 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002976 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002977 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002978 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002979 if (!ses)
2980 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002981 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002982 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002983 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002984 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002985 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002986 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002987 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002988 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002989
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002990 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002991
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002992 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002993 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002994out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002995 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002996out_client_lock:
2997 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002998out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002999 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003000}
3001
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003002static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003003{
3004 struct nfsd4_conn *c;
3005
3006 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003007 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003008 return c;
3009 }
3010 }
3011 return NULL;
3012}
3013
J. Bruce Fields57266a62013-04-13 14:27:29 -04003014static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003015{
3016 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003017 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003018 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003019 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003020
3021 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003022 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003023 if (c)
3024 goto out_free;
3025 status = nfserr_conn_not_bound_to_session;
3026 if (clp->cl_mach_cred)
3027 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003028 __nfsd4_hash_conn(new, ses);
3029 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003030 ret = nfsd4_register_conn(new);
3031 if (ret)
3032 /* oops; xprt is already down: */
3033 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003034 return nfs_ok;
3035out_free:
3036 spin_unlock(&clp->cl_lock);
3037 free_conn(new);
3038 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003039}
3040
Mi Jinlong868b89c2011-04-27 09:09:58 +08003041static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3042{
3043 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3044
3045 return args->opcnt > session->se_fchannel.maxops;
3046}
3047
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003048static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3049 struct nfsd4_session *session)
3050{
3051 struct xdr_buf *xb = &rqstp->rq_arg;
3052
3053 return xb->len > session->se_fchannel.maxreq_sz;
3054}
3055
J. Bruce Fields877362f2017-10-17 20:38:49 -04003056static bool replay_matches_cache(struct svc_rqst *rqstp,
3057 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3058{
3059 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3060
3061 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3062 (bool)seq->cachethis)
3063 return false;
3064 /*
3065 * If there's an error than the reply can have fewer ops than
3066 * the call. But if we cached a reply with *more* ops than the
3067 * call you're sending us now, then this new call is clearly not
3068 * really a replay of the old one:
3069 */
3070 if (slot->sl_opcnt < argp->opcnt)
3071 return false;
3072 /* This is the only check explicitly called by spec: */
3073 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3074 return false;
3075 /*
3076 * There may be more comparisons we could actually do, but the
3077 * spec doesn't require us to catch every case where the calls
3078 * don't match (that would require caching the call as well as
3079 * the reply), so we don't bother.
3080 */
3081 return true;
3082}
3083
Andy Adamson069b6ad2009-04-03 08:27:58 +03003084__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03003085nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003086 struct nfsd4_compound_state *cstate,
3087 struct nfsd4_sequence *seq)
3088{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003089 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003090 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003091 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003092 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003093 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003094 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003095 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003096 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003097 struct net *net = SVC_NET(rqstp);
3098 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003099
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003100 if (resp->opcnt != 1)
3101 return nfserr_sequence_pos;
3102
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003103 /*
3104 * Will be either used or freed by nfsd4_sequence_check_conn
3105 * below.
3106 */
3107 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3108 if (!conn)
3109 return nfserr_jukebox;
3110
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003111 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003112 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003113 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003114 goto out_no_session;
3115 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003116
Mi Jinlong868b89c2011-04-27 09:09:58 +08003117 status = nfserr_too_many_ops;
3118 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003119 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003120
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003121 status = nfserr_req_too_big;
3122 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003123 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003124
Benny Halevyb85d4c02009-04-03 08:28:08 +03003125 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003126 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003127 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003128
Andy Adamson557ce262009-08-28 08:45:04 -04003129 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003130 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3131
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003132 /* We do not negotiate the number of slots yet, so set the
3133 * maxslots to the session maxreqs which is used to encode
3134 * sr_highest_slotid and the sr_target_slot id to maxslots */
3135 seq->maxslots = session->se_fchannel.maxreqs;
3136
J. Bruce Fields73e79482012-02-13 16:39:00 -05003137 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3138 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003139 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003140 status = nfserr_seq_misordered;
3141 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003142 goto out_put_session;
J. Bruce Fields877362f2017-10-17 20:38:49 -04003143 status = nfserr_seq_false_retry;
3144 if (!replay_matches_cache(rqstp, seq, slot))
3145 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003146 cstate->slot = slot;
3147 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003148 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003149 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003150 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003151 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003152 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003153 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003154 }
3155 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003156 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003157
J. Bruce Fields57266a62013-04-13 14:27:29 -04003158 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003159 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003160 if (status)
3161 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003162
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003163 buflen = (seq->cachethis) ?
3164 session->se_fchannel.maxresp_cached :
3165 session->se_fchannel.maxresp_sz;
3166 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3167 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003168 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003169 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003170 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003171
3172 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003173 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003174 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003175 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003176 if (seq->cachethis)
3177 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003178 else
3179 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003180
3181 cstate->slot = slot;
3182 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003183 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003184
Benny Halevyb85d4c02009-04-03 08:28:08 +03003185out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003186 switch (clp->cl_cb_state) {
3187 case NFSD4_CB_DOWN:
3188 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3189 break;
3190 case NFSD4_CB_FAULT:
3191 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3192 break;
3193 default:
3194 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003195 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003196 if (!list_empty(&clp->cl_revoked))
3197 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003198out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003199 if (conn)
3200 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003201 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003202 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003203out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003204 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003205 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003206}
3207
Trond Myklebustb6076642014-06-30 11:48:35 -04003208void
3209nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3210{
3211 struct nfsd4_compound_state *cs = &resp->cstate;
3212
3213 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003214 if (cs->status != nfserr_replay_cache) {
3215 nfsd4_store_cache_entry(resp);
3216 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3217 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003218 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003219 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003220 } else if (cs->clp)
3221 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003222}
3223
Mi Jinlong345c2842011-10-20 17:51:39 +08003224__be32
3225nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3226{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003227 struct nfs4_client *conf, *unconf;
3228 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003229 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003230 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003231
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003232 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003233 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003234 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003235 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003236
3237 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003238 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003239 status = nfserr_clientid_busy;
3240 goto out;
3241 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003242 status = mark_client_expired_locked(conf);
3243 if (status)
3244 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003245 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003246 } else if (unconf)
3247 clp = unconf;
3248 else {
3249 status = nfserr_stale_clientid;
3250 goto out;
3251 }
Andrew Elblededeb132016-06-15 12:52:08 -04003252 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003253 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003254 status = nfserr_wrong_cred;
3255 goto out;
3256 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003257 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003258out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003259 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003260 if (clp)
3261 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003262 return status;
3263}
3264
Andy Adamson069b6ad2009-04-03 08:27:58 +03003265__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003266nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3267{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003268 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003269
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003270 if (rc->rca_one_fs) {
3271 if (!cstate->current_fh.fh_dentry)
3272 return nfserr_nofilehandle;
3273 /*
3274 * We don't take advantage of the rca_one_fs case.
3275 * That's OK, it's optional, we can safely ignore it.
3276 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003277 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003278 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003279
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003280 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003281 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3282 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003283 goto out;
3284
3285 status = nfserr_stale_clientid;
3286 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003287 /*
3288 * The following error isn't really legal.
3289 * But we only get here if the client just explicitly
3290 * destroyed the client. Surely it no longer cares what
3291 * error it gets back on an operation for the dead
3292 * client.
3293 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003294 goto out;
3295
3296 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003297 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003298out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003299 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003300}
3301
3302__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003303nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3304 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003306 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003308 struct nfs4_client *conf, *new;
3309 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003310 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003311 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3312
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003313 new = create_client(clname, rqstp, &clverifier);
3314 if (new == NULL)
3315 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003316 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003317 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003318 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003319 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003320 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003322 if (clp_used_exchangeid(conf))
3323 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003324 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003325 char addr_str[INET6_ADDRSTRLEN];
3326 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3327 sizeof(addr_str));
3328 dprintk("NFSD: setclientid: string in use by client "
3329 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 goto out;
3331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003333 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003334 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003335 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003336 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003337 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003339 gen_confirm(new, nn);
3340 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003341 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003342 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003343 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003344 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3346 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3347 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003348 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 status = nfs_ok;
3350out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003351 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003352 if (new)
3353 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003354 if (unconf)
3355 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return status;
3357}
3358
3359
Al Virob37ad282006-10-19 23:28:59 -07003360__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003361nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3362 struct nfsd4_compound_state *cstate,
3363 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
NeilBrown21ab45a2005-06-23 22:04:14 -07003365 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003366 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3368 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003369 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003370 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003372 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003374
Jeff Laytond20c11d2014-07-30 08:27:07 -04003375 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003376 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003377 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003378 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003379 * We try hard to give out unique clientid's, so if we get an
3380 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003381 * the client may be buggy; this should never happen.
3382 *
3383 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003384 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003385 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003386 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3387 goto out;
3388 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3389 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003390 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003391 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003392 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3393 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003395 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003396 status = nfserr_stale_clientid;
3397 goto out;
3398 }
3399 status = nfs_ok;
3400 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003401 old = unconf;
3402 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003403 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003404 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003405 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3406 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003407 status = nfserr_clid_inuse;
3408 if (client_has_state(old)
3409 && !same_creds(&unconf->cl_cred,
3410 &old->cl_cred))
3411 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003412 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003413 if (status) {
3414 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003415 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003416 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003417 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003418 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003419 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003420 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003421 get_client_locked(conf);
3422 spin_unlock(&nn->client_lock);
3423 nfsd4_probe_callback(conf);
3424 spin_lock(&nn->client_lock);
3425 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003427 spin_unlock(&nn->client_lock);
3428 if (old)
3429 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return status;
3431}
3432
J. Bruce Fields32513b42011-10-13 16:00:16 -04003433static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003435 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3436}
3437
3438/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003439static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3440 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003441{
Trond Myklebust950e0112014-06-30 11:48:31 -04003442 lockdep_assert_held(&state_lock);
3443
J. Bruce Fields32513b42011-10-13 16:00:16 -04003444 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003445 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003446 INIT_LIST_HEAD(&fp->fi_stateids);
3447 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003448 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003449 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003450 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003451 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003452 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003453 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3454 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003455#ifdef CONFIG_NFSD_PNFS
3456 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003457 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003458#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003459 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460}
3461
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003462void
NeilBrowne60d4392005-06-23 22:03:01 -07003463nfsd4_free_slabs(void)
3464{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003465 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003466 kmem_cache_destroy(openowner_slab);
3467 kmem_cache_destroy(lockowner_slab);
3468 kmem_cache_destroy(file_slab);
3469 kmem_cache_destroy(stateid_slab);
3470 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Bryan Schumaker72083392011-11-01 15:24:59 -04003473int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474nfsd4_init_slabs(void)
3475{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003476 openowner_slab = kmem_cache_create("nfsd4_openowners",
3477 sizeof(struct nfs4_openowner), 0, 0, NULL);
3478 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003479 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003480 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003481 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003482 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003483 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003484 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003485 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003486 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003487 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003488 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003489 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003490 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003491 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003492 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003493 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003494 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003495 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003496 odstate_slab = kmem_cache_create("nfsd4_odstate",
3497 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3498 if (odstate_slab == NULL)
3499 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003501
Sachin Bhamare8287f002015-04-27 14:50:14 +02003502out_free_deleg_slab:
3503 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003504out_free_stateid_slab:
3505 kmem_cache_destroy(stateid_slab);
3506out_free_file_slab:
3507 kmem_cache_destroy(file_slab);
3508out_free_lockowner_slab:
3509 kmem_cache_destroy(lockowner_slab);
3510out_free_openowner_slab:
3511 kmem_cache_destroy(openowner_slab);
3512out:
NeilBrowne60d4392005-06-23 22:03:01 -07003513 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3514 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003517static void init_nfs4_replay(struct nfs4_replay *rp)
3518{
3519 rp->rp_status = nfserr_serverfault;
3520 rp->rp_buflen = 0;
3521 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003522 mutex_init(&rp->rp_mutex);
3523}
3524
3525static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3526 struct nfs4_stateowner *so)
3527{
3528 if (!nfsd4_has_session(cstate)) {
3529 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003530 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003531 }
3532}
3533
3534void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3535{
3536 struct nfs4_stateowner *so = cstate->replay_owner;
3537
3538 if (so != NULL) {
3539 cstate->replay_owner = NULL;
3540 mutex_unlock(&so->so_replay.rp_mutex);
3541 nfs4_put_stateowner(so);
3542 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003543}
3544
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003545static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 struct nfs4_stateowner *sop;
3548
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003549 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003550 if (!sop)
3551 return NULL;
3552
3553 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3554 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003555 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003556 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003558 sop->so_owner.len = owner->len;
3559
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003560 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003561 sop->so_client = clp;
3562 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003563 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003564 return sop;
3565}
3566
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003567static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003568{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003569 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003570
Trond Myklebustd4f04892014-07-29 21:34:36 -04003571 list_add(&oo->oo_owner.so_strhash,
3572 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003573 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003576static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3577{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003578 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003579}
3580
Jeff Layton6b180f02014-07-29 21:34:26 -04003581static void nfs4_free_openowner(struct nfs4_stateowner *so)
3582{
3583 struct nfs4_openowner *oo = openowner(so);
3584
3585 kmem_cache_free(openowner_slab, oo);
3586}
3587
3588static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003589 .so_unhash = nfs4_unhash_openowner,
3590 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003591};
3592
Andrew Elble7fc05642015-11-05 20:42:43 -05003593static struct nfs4_ol_stateid *
3594nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3595{
3596 struct nfs4_ol_stateid *local, *ret = NULL;
3597 struct nfs4_openowner *oo = open->op_openowner;
3598
3599 lockdep_assert_held(&fp->fi_lock);
3600
3601 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3602 /* ignore lock owners */
3603 if (local->st_stateowner->so_is_open_owner == 0)
3604 continue;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003605 if (local->st_stateowner != &oo->oo_owner)
3606 continue;
3607 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003608 ret = local;
3609 atomic_inc(&ret->st_stid.sc_count);
3610 break;
3611 }
3612 }
3613 return ret;
3614}
3615
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003616static __be32
3617nfsd4_verify_open_stid(struct nfs4_stid *s)
3618{
3619 __be32 ret = nfs_ok;
3620
3621 switch (s->sc_type) {
3622 default:
3623 break;
3624 case NFS4_CLOSED_STID:
3625 case NFS4_CLOSED_DELEG_STID:
3626 ret = nfserr_bad_stateid;
3627 break;
3628 case NFS4_REVOKED_DELEG_STID:
3629 ret = nfserr_deleg_revoked;
3630 }
3631 return ret;
3632}
3633
3634/* Lock the stateid st_mutex, and deal with races with CLOSE */
3635static __be32
3636nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3637{
3638 __be32 ret;
3639
3640 mutex_lock(&stp->st_mutex);
3641 ret = nfsd4_verify_open_stid(&stp->st_stid);
3642 if (ret != nfs_ok)
3643 mutex_unlock(&stp->st_mutex);
3644 return ret;
3645}
3646
3647static struct nfs4_ol_stateid *
3648nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3649{
3650 struct nfs4_ol_stateid *stp;
3651 for (;;) {
3652 spin_lock(&fp->fi_lock);
3653 stp = nfsd4_find_existing_open(fp, open);
3654 spin_unlock(&fp->fi_lock);
3655 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3656 break;
3657 nfs4_put_stid(&stp->st_stid);
3658 }
3659 return stp;
3660}
3661
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003662static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003663alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003664 struct nfsd4_compound_state *cstate)
3665{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003666 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003667 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003669 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3670 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003672 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003673 oo->oo_owner.so_is_open_owner = 1;
3674 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003675 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003676 if (nfsd4_has_session(cstate))
3677 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003678 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003679 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003680 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003681 spin_lock(&clp->cl_lock);
3682 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003683 if (ret == NULL) {
3684 hash_openowner(oo, clp, strhashval);
3685 ret = oo;
3686 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003687 nfs4_free_stateowner(&oo->oo_owner);
3688
Trond Myklebustd4f04892014-07-29 21:34:36 -04003689 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003690 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
Andrew Elble7fc05642015-11-05 20:42:43 -05003693static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003694init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Andrew Elble7fc05642015-11-05 20:42:43 -05003697 struct nfs4_openowner *oo = open->op_openowner;
3698 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003699 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003700
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003701 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003702 /* We are moving these outside of the spinlocks to avoid the warnings */
3703 mutex_init(&stp->st_mutex);
3704 mutex_lock(&stp->st_mutex);
3705
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003706retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05003707 spin_lock(&oo->oo_owner.so_client->cl_lock);
3708 spin_lock(&fp->fi_lock);
3709
3710 retstp = nfsd4_find_existing_open(fp, open);
3711 if (retstp)
3712 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003713
3714 open->op_stp = NULL;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003715 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003716 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003717 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003718 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003719 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003720 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 stp->st_access_bmap = 0;
3722 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003723 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003724 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003725 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003726
3727out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003728 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003729 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003730 if (retstp) {
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003731 /* Handle races with CLOSE */
3732 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3733 nfs4_put_stid(&retstp->st_stid);
3734 goto retry;
3735 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003736 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003737 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003738 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003739 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003740 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
Jeff Laytond3134b12014-07-29 21:34:32 -04003743/*
3744 * In the 4.0 case we need to keep the owners around a little while to handle
3745 * CLOSE replay. We still do need to release any file access that is held by
3746 * them before returning however.
3747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003749move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
Jeff Layton217526e2014-07-30 08:27:11 -04003751 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003752 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3753 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3754 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003755
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003756 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Jeff Laytonb401be222014-07-29 21:34:33 -04003758 /*
3759 * We know that we hold one reference via nfsd4_close, and another
3760 * "persistent" reference for the client. If the refcount is higher
3761 * than 2, then there are still calls in progress that are using this
3762 * stateid. We can't put the sc_file reference until they are finished.
3763 * Wait for the refcount to drop to 2. Since it has been unhashed,
3764 * there should be no danger of the refcount going back up again at
3765 * this point.
3766 */
3767 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3768
Jeff Laytond3134b12014-07-29 21:34:32 -04003769 release_all_access(s);
3770 if (s->st_stid.sc_file) {
3771 put_nfs4_file(s->st_stid.sc_file);
3772 s->st_stid.sc_file = NULL;
3773 }
Jeff Layton217526e2014-07-30 08:27:11 -04003774
3775 spin_lock(&nn->client_lock);
3776 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003777 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003778 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003779 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003780 spin_unlock(&nn->client_lock);
3781 if (last)
3782 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783}
3784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785/* search file_hashtbl[] for file */
3786static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003787find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 struct nfs4_file *fp;
3790
Jeff Layton5b095e92014-10-23 08:01:02 -04003791 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003792 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003793 if (atomic_inc_not_zero(&fp->fi_ref))
3794 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
3797 return NULL;
3798}
3799
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003800struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003801find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003802{
3803 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003804 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003805
Jeff Layton5b095e92014-10-23 08:01:02 -04003806 rcu_read_lock();
3807 fp = find_file_locked(fh, hashval);
3808 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003809 return fp;
3810}
3811
3812static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003813find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003814{
3815 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003816 unsigned int hashval = file_hashval(fh);
3817
3818 rcu_read_lock();
3819 fp = find_file_locked(fh, hashval);
3820 rcu_read_unlock();
3821 if (fp)
3822 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003823
3824 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003825 fp = find_file_locked(fh, hashval);
3826 if (likely(fp == NULL)) {
3827 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003828 fp = new;
3829 }
3830 spin_unlock(&state_lock);
3831
3832 return fp;
3833}
3834
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003835/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 * Called to check deny when READ with all zero stateid or
3837 * WRITE with all zero or all one stateid
3838 */
Al Virob37ad282006-10-19 23:28:59 -07003839static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003843 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Trond Myklebustca943212014-07-23 16:17:39 -04003845 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003846 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003847 return ret;
3848 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003849 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003850 if (fp->fi_share_deny & deny_type)
3851 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003852 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003853 put_nfs4_file(fp);
3854 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855}
3856
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003857static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003859 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003860 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3861 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003862
Trond Myklebust11b91642014-07-29 21:34:08 -04003863 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003864
Jeff Layton02e12152014-07-16 10:31:57 -04003865 /*
3866 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003867 * already holding inode->i_lock.
3868 *
Jeff Laytondff13992014-07-08 14:02:49 -04003869 * If the dl_time != 0, then we know that it has already been
3870 * queued for a lease break. Don't queue it again.
3871 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003872 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003873 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003874 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003875 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003876 }
Jeff Layton02e12152014-07-16 10:31:57 -04003877 spin_unlock(&state_lock);
3878}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003880static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3881 struct rpc_task *task)
3882{
3883 struct nfs4_delegation *dp = cb_to_delegation(cb);
3884
Andrew Elblea4579742015-08-31 12:06:41 -04003885 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3886 return 1;
3887
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003888 switch (task->tk_status) {
3889 case 0:
3890 return 1;
3891 case -EBADHANDLE:
3892 case -NFS4ERR_BAD_STATEID:
3893 /*
3894 * Race: client probably got cb_recall before open reply
3895 * granting delegation.
3896 */
3897 if (dp->dl_retries--) {
3898 rpc_delay(task, 2 * HZ);
3899 return 0;
3900 }
3901 /*FALLTHRU*/
3902 default:
3903 return -1;
3904 }
3905}
3906
3907static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3908{
3909 struct nfs4_delegation *dp = cb_to_delegation(cb);
3910
3911 nfs4_put_stid(&dp->dl_stid);
3912}
3913
Julia Lawallc4cb8972015-11-21 22:57:39 +01003914static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003915 .prepare = nfsd4_cb_recall_prepare,
3916 .done = nfsd4_cb_recall_done,
3917 .release = nfsd4_cb_recall_release,
3918};
3919
Jeff Layton02e12152014-07-16 10:31:57 -04003920static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3921{
3922 /*
3923 * We're assuming the state code never drops its reference
3924 * without first removing the lease. Since we're in this lease
3925 * callback (and since the lease code is serialized by the kernel
3926 * lock) we know the server hasn't removed the lease yet, we know
3927 * it's safe to take a reference.
3928 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003929 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003930 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003931}
3932
Jeff Layton1c8c6012013-06-21 08:58:15 -04003933/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003934static bool
3935nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003936{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003937 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003938 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3939 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003940
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003941 if (!fp) {
3942 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003943 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003944 }
3945 if (fp->fi_had_conflict) {
3946 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003947 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003948 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003949 /*
3950 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003951 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003952 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003953 */
3954 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Jeff Layton02e12152014-07-16 10:31:57 -04003956 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003957 fp->fi_had_conflict = true;
3958 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003959 * If there are no delegations on the list, then return true
3960 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003961 */
3962 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003963 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003964 else
3965 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3966 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003967 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003968 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969}
3970
Jeff Laytonc45198e2014-09-01 07:12:07 -04003971static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003972nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3973 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974{
3975 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003976 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 else
3978 return -EAGAIN;
3979}
3980
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003981static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003982 .lm_break = nfsd_break_deleg_cb,
3983 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984};
3985
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003986static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3987{
3988 if (nfsd4_has_session(cstate))
3989 return nfs_ok;
3990 if (seqid == so->so_seqid - 1)
3991 return nfserr_replay_me;
3992 if (seqid == so->so_seqid)
3993 return nfs_ok;
3994 return nfserr_bad_seqid;
3995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Jeff Layton4b24ca72014-06-30 11:48:44 -04003997static __be32 lookup_clientid(clientid_t *clid,
3998 struct nfsd4_compound_state *cstate,
3999 struct nfsd_net *nn)
4000{
4001 struct nfs4_client *found;
4002
4003 if (cstate->clp) {
4004 found = cstate->clp;
4005 if (!same_clid(&found->cl_clientid, clid))
4006 return nfserr_stale_clientid;
4007 return nfs_ok;
4008 }
4009
4010 if (STALE_CLIENTID(clid, nn))
4011 return nfserr_stale_clientid;
4012
4013 /*
4014 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4015 * cached already then we know this is for is for v4.0 and "sessions"
4016 * will be false.
4017 */
4018 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004019 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004020 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004021 if (!found) {
4022 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004023 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004024 }
4025 atomic_inc(&found->cl_refcount);
4026 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004027
4028 /* Cache the nfs4_client in cstate! */
4029 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004030 return nfs_ok;
4031}
4032
Al Virob37ad282006-10-19 23:28:59 -07004033__be32
Andy Adamson66689582009-04-03 08:28:45 +03004034nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004035 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 clientid_t *clientid = &open->op_clientid;
4038 struct nfs4_client *clp = NULL;
4039 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004040 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004041 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004043 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004045 /*
4046 * In case we need it later, after we've already created the
4047 * file and don't want to risk a further failure:
4048 */
4049 open->op_file = nfsd4_alloc_file();
4050 if (open->op_file == NULL)
4051 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Trond Myklebust2d91e892014-06-30 11:48:46 -04004053 status = lookup_clientid(clientid, cstate, nn);
4054 if (status)
4055 return status;
4056 clp = cstate->clp;
4057
Trond Myklebustd4f04892014-07-29 21:34:36 -04004058 strhashval = ownerstr_hashval(&open->op_owner);
4059 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004060 open->op_openowner = oo;
4061 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004062 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004064 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004065 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004066 release_openowner(oo);
4067 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004068 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004069 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004070 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4071 if (status)
4072 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004073 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004074new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004075 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004076 if (oo == NULL)
4077 return nfserr_jukebox;
4078 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004079alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004080 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004081 if (!open->op_stp)
4082 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004083
4084 if (nfsd4_has_session(cstate) &&
4085 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4086 open->op_odstate = alloc_clnt_odstate(clp);
4087 if (!open->op_odstate)
4088 return nfserr_jukebox;
4089 }
4090
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004091 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092}
4093
Al Virob37ad282006-10-19 23:28:59 -07004094static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004095nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4096{
4097 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4098 return nfserr_openmode;
4099 else
4100 return nfs_ok;
4101}
4102
Daniel Mackc47d8322011-05-16 16:38:14 +02004103static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004104{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004105 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4106}
4107
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004108static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004109{
4110 struct nfs4_stid *ret;
4111
Andrew Elble62f0b692017-11-03 14:06:31 -04004112 ret = find_stateid_by_type(cl, s,
4113 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004114 if (!ret)
4115 return NULL;
4116 return delegstateid(ret);
4117}
4118
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004119static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4120{
4121 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4122 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4123}
4124
Al Virob37ad282006-10-19 23:28:59 -07004125static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004126nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004127 struct nfs4_delegation **dp)
4128{
4129 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004130 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004131 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004132
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004133 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4134 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004135 goto out;
Andrew Elble62f0b692017-11-03 14:06:31 -04004136 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4137 nfs4_put_stid(&deleg->dl_stid);
4138 if (cl->cl_minorversion)
4139 status = nfserr_deleg_revoked;
4140 goto out;
4141 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004142 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004143 status = nfs4_check_delegmode(deleg, flags);
4144 if (status) {
4145 nfs4_put_stid(&deleg->dl_stid);
4146 goto out;
4147 }
4148 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004149out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004150 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004151 return nfs_ok;
4152 if (status)
4153 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004154 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004155 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004156}
4157
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004158static inline int nfs4_access_to_access(u32 nfs4_access)
4159{
4160 int flags = 0;
4161
4162 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4163 flags |= NFSD_MAY_READ;
4164 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4165 flags |= NFSD_MAY_WRITE;
4166 return flags;
4167}
4168
Al Virob37ad282006-10-19 23:28:59 -07004169static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4171 struct nfsd4_open *open)
4172{
4173 struct iattr iattr = {
4174 .ia_valid = ATTR_SIZE,
4175 .ia_size = 0,
4176 };
4177 if (!open->op_truncate)
4178 return 0;
4179 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004180 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4182}
4183
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004184static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004185 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4186 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004187{
Trond Myklebustde186432014-07-10 14:07:26 -04004188 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004189 __be32 status;
4190 int oflag = nfs4_access_to_omode(open->op_share_access);
4191 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004192 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004193
Trond Myklebustde186432014-07-10 14:07:26 -04004194 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004195
4196 /*
4197 * Are we trying to set a deny mode that would conflict with
4198 * current access?
4199 */
4200 status = nfs4_file_check_deny(fp, open->op_share_deny);
4201 if (status != nfs_ok) {
4202 spin_unlock(&fp->fi_lock);
4203 goto out;
4204 }
4205
4206 /* set access to the file */
4207 status = nfs4_file_get_access(fp, open->op_share_access);
4208 if (status != nfs_ok) {
4209 spin_unlock(&fp->fi_lock);
4210 goto out;
4211 }
4212
4213 /* Set access bits in stateid */
4214 old_access_bmap = stp->st_access_bmap;
4215 set_access(open->op_share_access, stp);
4216
4217 /* Set new deny mask */
4218 old_deny_bmap = stp->st_deny_bmap;
4219 set_deny(open->op_share_deny, stp);
4220 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4221
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004222 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004223 spin_unlock(&fp->fi_lock);
4224 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004225 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004226 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004227 spin_lock(&fp->fi_lock);
4228 if (!fp->fi_fds[oflag]) {
4229 fp->fi_fds[oflag] = filp;
4230 filp = NULL;
4231 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004232 }
Trond Myklebustde186432014-07-10 14:07:26 -04004233 spin_unlock(&fp->fi_lock);
4234 if (filp)
4235 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004236
4237 status = nfsd4_truncate(rqstp, cur_fh, open);
4238 if (status)
4239 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004240out:
4241 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004242out_put_access:
4243 stp->st_access_bmap = old_access_bmap;
4244 nfs4_file_put_access(fp, open->op_share_access);
4245 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4246 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004247}
4248
Al Virob37ad282006-10-19 23:28:59 -07004249static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004250nfs4_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 -07004251{
Al Virob37ad282006-10-19 23:28:59 -07004252 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004253 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004255 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004256 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004257
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004258 /* test and set deny mode */
4259 spin_lock(&fp->fi_lock);
4260 status = nfs4_file_check_deny(fp, open->op_share_deny);
4261 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004262 set_deny(open->op_share_deny, stp);
4263 fp->fi_share_deny |=
4264 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4265 }
4266 spin_unlock(&fp->fi_lock);
4267
4268 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004271 status = nfsd4_truncate(rqstp, cur_fh, open);
4272 if (status != nfs_ok)
4273 reset_union_bmap_deny(old_deny_bmap, stp);
4274 return status;
4275}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004277/* Should we give out recallable state?: */
4278static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4279{
4280 if (clp->cl_cb_state == NFSD4_CB_UP)
4281 return true;
4282 /*
4283 * In the sessions case, since we don't have to establish a
4284 * separate connection for callbacks, we assume it's OK
4285 * until we hear otherwise:
4286 */
4287 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4288}
4289
Jeff Laytond564fbe2014-07-16 10:31:58 -04004290static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004291{
4292 struct file_lock *fl;
4293
4294 fl = locks_alloc_lock();
4295 if (!fl)
4296 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004297 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004298 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004299 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4300 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04004301 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004302 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004303 return fl;
4304}
4305
Andrew Elble34ed9872015-10-15 12:07:28 -04004306/**
4307 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4308 * @dp: a pointer to the nfs4_delegation we're adding.
4309 *
4310 * Return:
4311 * On success: Return code will be 0 on success.
4312 *
4313 * On error: -EAGAIN if there was an existing delegation.
4314 * nonzero if there is an error in other cases.
4315 *
4316 */
4317
J. Bruce Fields99c41512013-05-21 16:21:25 -04004318static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004319{
Trond Myklebust11b91642014-07-29 21:34:08 -04004320 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08004321 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004322 struct file *filp;
4323 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004324
Jeff Laytond564fbe2014-07-16 10:31:58 -04004325 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004326 if (!fl)
4327 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04004328 filp = find_readable_file(fp);
4329 if (!filp) {
4330 /* We should always have a readable file here */
4331 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08004332 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04004333 return -EBADF;
4334 }
4335 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04004336 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004337 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04004338 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004339 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04004340 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04004341 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004342 spin_lock(&fp->fi_lock);
4343 /* Did the lease get broken before we took the lock? */
4344 status = -EAGAIN;
4345 if (fp->fi_had_conflict)
4346 goto out_unlock;
4347 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04004348 if (fp->fi_deleg_file) {
Andrew Elble34ed9872015-10-15 12:07:28 -04004349 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004350 goto out_unlock;
4351 }
Jeff Layton417c6622014-07-21 09:34:57 -04004352 fp->fi_deleg_file = filp;
Andrew Elble34ed9872015-10-15 12:07:28 -04004353 fp->fi_delegees = 0;
4354 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004355 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004356 spin_unlock(&state_lock);
Andrew Elble34ed9872015-10-15 12:07:28 -04004357 if (status) {
4358 /* Should never happen, this is a new fi_deleg_file */
4359 WARN_ON_ONCE(1);
4360 goto out_fput;
4361 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004362 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004363out_unlock:
4364 spin_unlock(&fp->fi_lock);
4365 spin_unlock(&state_lock);
4366out_fput:
4367 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004368 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004369}
4370
Jeff Layton0b266932014-07-25 07:34:25 -04004371static struct nfs4_delegation *
4372nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004373 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004374{
Jeff Layton0b266932014-07-25 07:34:25 -04004375 int status;
4376 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004377
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004378 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004379 return ERR_PTR(-EAGAIN);
4380
Andrew Elble34ed9872015-10-15 12:07:28 -04004381 spin_lock(&state_lock);
4382 spin_lock(&fp->fi_lock);
4383 status = nfs4_get_existing_delegation(clp, fp);
4384 spin_unlock(&fp->fi_lock);
4385 spin_unlock(&state_lock);
4386
4387 if (status)
4388 return ERR_PTR(status);
4389
Sachin Bhamare8287f002015-04-27 14:50:14 +02004390 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004391 if (!dp)
4392 return ERR_PTR(-ENOMEM);
4393
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004394 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004395 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004396 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004397 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004398 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004399 spin_unlock(&fp->fi_lock);
4400 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004401 status = nfs4_setlease(dp);
4402 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004403 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004404 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004405 status = -EAGAIN;
4406 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004407 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004408 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004409out_unlock:
4410 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004411 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004412out:
4413 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004414 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004415 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004416 return ERR_PTR(status);
4417 }
4418 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004419}
4420
Benny Halevy4aa89132012-02-21 14:16:44 -08004421static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4422{
4423 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4424 if (status == -EAGAIN)
4425 open->op_why_no_deleg = WND4_CONTENTION;
4426 else {
4427 open->op_why_no_deleg = WND4_RESOURCE;
4428 switch (open->op_deleg_want) {
4429 case NFS4_SHARE_WANT_READ_DELEG:
4430 case NFS4_SHARE_WANT_WRITE_DELEG:
4431 case NFS4_SHARE_WANT_ANY_DELEG:
4432 break;
4433 case NFS4_SHARE_WANT_CANCEL:
4434 open->op_why_no_deleg = WND4_CANCELLED;
4435 break;
4436 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004437 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004438 }
4439 }
4440}
4441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442/*
4443 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004444 *
4445 * Note we don't support write delegations, and won't until the vfs has
4446 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 */
4448static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004449nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4450 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
4452 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004453 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4454 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004455 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004456 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004458 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004459 open->op_recall = 0;
4460 switch (open->op_claim_type) {
4461 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004462 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004463 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004464 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4465 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004466 break;
4467 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004468 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004469 /*
4470 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004471 * had the chance to reclaim theirs, *and* until
4472 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004473 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004474 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004475 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004476 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004477 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004478 /*
4479 * Also, if the file was opened for write or
4480 * create, there's a good chance the client's
4481 * about to write to it, resulting in an
4482 * immediate recall (since we don't support
4483 * write delegations):
4484 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004485 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004486 goto out_no_deleg;
4487 if (open->op_create == NFS4_OPEN_CREATE)
4488 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004489 break;
4490 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004491 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004492 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004493 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004494 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004495 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004497 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004499 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004500 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004501 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004502 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004503 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004504out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004505 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4506 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004507 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004508 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004509 open->op_recall = 1;
4510 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004511
4512 /* 4.1 client asking for a delegation? */
4513 if (open->op_deleg_want)
4514 nfsd4_open_deleg_none_ext(open, status);
4515 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516}
4517
Benny Halevye27f49c2012-02-21 14:16:54 -08004518static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4519 struct nfs4_delegation *dp)
4520{
4521 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4522 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4523 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4524 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4525 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4526 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4527 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4528 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4529 }
4530 /* Otherwise the client must be confused wanting a delegation
4531 * it already has, therefore we don't return
4532 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4533 */
4534}
4535
Al Virob37ad282006-10-19 23:28:59 -07004536__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4538{
Andy Adamson66689582009-04-03 08:28:45 +03004539 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004540 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004542 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004543 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004544 __be32 status;
Trond Myklebust1c404252017-11-03 08:00:11 -04004545 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 /*
4548 * Lookup file; if found, lookup stateid and check open request,
4549 * and check for delegations in the process of being recalled.
4550 * If not found, create the nfs4_file struct
4551 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004552 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004553 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004554 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004555 if (status)
4556 goto out;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04004557 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004559 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004560 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004561 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004562 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
4564
Trond Myklebust1c404252017-11-03 08:00:11 -04004565 if (!stp) {
4566 stp = init_open_stateid(fp, open);
4567 if (!open->op_stp)
4568 new_stp = true;
4569 }
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 /*
4572 * OPEN the file, or upgrade an existing OPEN.
4573 * If truncate fails, the OPEN fails.
Trond Myklebust1c404252017-11-03 08:00:11 -04004574 *
4575 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 */
Trond Myklebust1c404252017-11-03 08:00:11 -04004577 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004579 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004580 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004581 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004585 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4586 if (status) {
Trond Myklebust1c404252017-11-03 08:00:11 -04004587 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004588 release_open_stateid(stp);
Trond Myklebust1c404252017-11-03 08:00:11 -04004589 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004590 goto out;
4591 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004592
4593 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4594 open->op_odstate);
4595 if (stp->st_clnt_odstate == open->op_odstate)
4596 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
Trond Myklebust1c404252017-11-03 08:00:11 -04004598
Jeff Layton9767feb2015-10-01 09:05:50 -04004599 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004600 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Benny Halevyd24433c2012-02-16 20:57:17 +02004602 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004603 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4604 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4605 open->op_why_no_deleg = WND4_NOT_WANTED;
4606 goto nodeleg;
4607 }
4608 }
4609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 /*
4611 * Attempt to hand out a delegation. No error return, because the
4612 * OPEN succeeds even if we fail.
4613 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004614 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004615nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 status = nfs_ok;
4617
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004618 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004619 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004621 /* 4.1 client trying to upgrade/downgrade delegation? */
4622 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004623 open->op_deleg_want)
4624 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004625
NeilBrown13cd2182005-06-23 22:03:10 -07004626 if (fp)
4627 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004628 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004629 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 /*
4631 * To finish the open response, we just need to set the rflags.
4632 */
4633 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004634 if (nfsd4_has_session(&resp->cstate))
4635 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4636 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004638
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004639 if (dp)
4640 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004641 if (stp)
4642 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
4644 return status;
4645}
4646
Jeff Layton58fb12e2014-07-29 21:34:27 -04004647void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004648 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004649{
4650 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004651 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004652
Jeff Laytond3134b12014-07-29 21:34:32 -04004653 nfsd4_cstate_assign_replay(cstate, so);
4654 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004655 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004656 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004657 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004658 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004659 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004660 if (open->op_odstate)
4661 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004662}
4663
Al Virob37ad282006-10-19 23:28:59 -07004664__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004665nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4666 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
4668 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004669 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004670 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 dprintk("process_renew(%08x/%08x): starting\n",
4673 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004674 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004675 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004677 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004679 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004680 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 goto out;
4682 status = nfs_ok;
4683out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return status;
4685}
4686
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004687void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004688nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004689{
Jeff Layton33dcc482012-04-10 11:08:48 -04004690 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004691 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004692 return;
4693
NeilBrowna76b4312005-06-23 22:04:01 -07004694 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004695 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004696 /*
4697 * If the server goes down again right now, an NFSv4
4698 * client will still be allowed to reclaim after it comes back up,
4699 * even if it hasn't yet had a chance to reclaim state this time.
4700 *
4701 */
Jeff Layton919b8042014-09-12 16:40:20 -04004702 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004703 /*
4704 * At this point, NFSv4 clients can still reclaim. But if the
4705 * server crashes, any that have not yet reclaimed will be out
4706 * of luck on the next boot.
4707 *
4708 * (NFSv4.1+ clients are considered to have reclaimed once they
4709 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4710 * have reclaimed after their first OPEN.)
4711 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004712 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004713 /*
4714 * At this point, and once lockd and/or any other containers
4715 * exit their grace period, further reclaims will fail and
4716 * regular locking can resume.
4717 */
NeilBrowna76b4312005-06-23 22:04:01 -07004718}
4719
NeilBrownfd39ca92005-06-23 22:04:03 -07004720static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004721nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
4723 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004724 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004726 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004727 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004729 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004730 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004733 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004734 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004735 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004736 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 clp = list_entry(pos, struct nfs4_client, cl_lru);
4738 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4739 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004740 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 break;
4742 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004743 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004744 dprintk("NFSD: client in use (clientid %08x)\n",
4745 clp->cl_clientid.cl_id);
4746 continue;
4747 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004748 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004749 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004750 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004751 list_for_each_safe(pos, next, &reaplist) {
4752 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 dprintk("NFSD: purging unused client (clientid %08x)\n",
4754 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004755 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 expire_client(clp);
4757 }
Benny Halevycdc97502014-05-30 09:09:30 -04004758 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004759 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4761 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004762 t = dp->dl_time - cutoff;
4763 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 break;
4765 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004766 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004767 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 }
Benny Halevycdc97502014-05-30 09:09:30 -04004769 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004770 while (!list_empty(&reaplist)) {
4771 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4772 dl_recall_lru);
4773 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004774 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 }
Jeff Layton217526e2014-07-30 08:27:11 -04004776
4777 spin_lock(&nn->client_lock);
4778 while (!list_empty(&nn->close_lru)) {
4779 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4780 oo_close_lru);
4781 if (time_after((unsigned long)oo->oo_time,
4782 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004783 t = oo->oo_time - cutoff;
4784 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 break;
4786 }
Jeff Layton217526e2014-07-30 08:27:11 -04004787 list_del_init(&oo->oo_close_lru);
4788 stp = oo->oo_last_closed_stid;
4789 oo->oo_last_closed_stid = NULL;
4790 spin_unlock(&nn->client_lock);
4791 nfs4_put_stid(&stp->st_stid);
4792 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 }
Jeff Layton217526e2014-07-30 08:27:11 -04004794 spin_unlock(&nn->client_lock);
4795
Jeff Layton7919d0a2016-09-16 16:28:25 -04004796 /*
4797 * It's possible for a client to try and acquire an already held lock
4798 * that is being held for a long time, and then lose interest in it.
4799 * So, we clean out any un-revisited request after a lease period
4800 * under the assumption that the client is no longer interested.
4801 *
4802 * RFC5661, sec. 9.6 states that the client must not rely on getting
4803 * notifications and must continue to poll for locks, even when the
4804 * server supports them. Thus this shouldn't lead to clients blocking
4805 * indefinitely once the lock does become free.
4806 */
4807 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004808 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004809 while (!list_empty(&nn->blocked_locks_lru)) {
4810 nbl = list_first_entry(&nn->blocked_locks_lru,
4811 struct nfsd4_blocked_lock, nbl_lru);
4812 if (time_after((unsigned long)nbl->nbl_time,
4813 (unsigned long)cutoff)) {
4814 t = nbl->nbl_time - cutoff;
4815 new_timeo = min(new_timeo, t);
4816 break;
4817 }
4818 list_move(&nbl->nbl_lru, &reaplist);
4819 list_del_init(&nbl->nbl_list);
4820 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004821 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004822
4823 while (!list_empty(&reaplist)) {
Naofumi Hondaaa1a6cf2017-11-09 10:57:16 -05004824 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04004825 struct nfsd4_blocked_lock, nbl_lru);
4826 list_del_init(&nbl->nbl_lru);
4827 posix_unblock_lock(&nbl->nbl_lock);
4828 free_blocked_lock(nbl);
4829 }
4830
Jeff Laytona832e7a2014-05-30 09:09:26 -04004831 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004832 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
4834
Harvey Harrisona254b242008-02-20 12:49:00 -08004835static struct workqueue_struct *laundry_wq;
4836static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004837
4838static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004839laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840{
4841 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004842 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004843 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4844 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004846 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004848 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850
Jeff Layton8fcd4612015-07-30 06:57:46 -04004851static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004852{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004853 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004854 return nfserr_bad_stateid;
4855 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856}
4857
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004859access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004861 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4862 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4863 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
4866static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004867access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004869 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4870 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871}
4872
4873static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004874__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Al Virob37ad282006-10-19 23:28:59 -07004876 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
J. Bruce Fields02921912010-07-29 15:16:59 -04004878 /* For lock stateid's, we test the parent open, not the lock: */
4879 if (stp->st_openstp)
4880 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004881 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004883 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 goto out;
4885 status = nfs_ok;
4886out:
4887 return status;
4888}
4889
Al Virob37ad282006-10-19 23:28:59 -07004890static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004891check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004893 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004895 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004896 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 * conflicting state; so we must wait out the grace period. */
4898 return nfserr_grace;
4899 } else if (flags & WR_STATE)
4900 return nfs4_share_conflict(current_fh,
4901 NFS4_SHARE_DENY_WRITE);
4902 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4903 return nfs4_share_conflict(current_fh,
4904 NFS4_SHARE_DENY_READ);
4905}
4906
4907/*
4908 * Allow READ/WRITE during grace period on recovered state only for files
4909 * that are not able to provide mandatory locking.
4910 */
4911static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004912grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004914 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915}
4916
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004917static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004918{
Andy Adamson66689582009-04-03 08:28:45 +03004919 /*
4920 * When sessions are used the stateid generation number is ignored
4921 * when it is zero.
4922 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004923 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004924 return nfs_ok;
4925
4926 if (in->si_generation == ref->si_generation)
4927 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004928
J. Bruce Fields0836f582008-01-26 19:08:12 -05004929 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004930 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004931 return nfserr_bad_stateid;
4932 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004933 * However, we could see a stateid from the past, even from a
4934 * non-buggy client. For example, if the client sends a lock
4935 * while some IO is outstanding, the lock may bump si_generation
4936 * while the IO is still in flight. The client could avoid that
4937 * situation by waiting for responses on all the IO requests,
4938 * but better performance may result in retrying IO that
4939 * receives an old_stateid error if requests are rarely
4940 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004941 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004942 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004943}
4944
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004945static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4946{
4947 if (ols->st_stateowner->so_is_open_owner &&
4948 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4949 return nfserr_bad_stateid;
4950 return nfs_ok;
4951}
4952
Chuck Lever7df302f2012-05-29 13:56:37 -04004953static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004954{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004955 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004956 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004957
Andrew Elblef25e2222017-11-09 13:41:10 -05004958 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4959 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004960 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004961 /* Client debugging aid. */
4962 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4963 char addr_str[INET6_ADDRSTRLEN];
4964 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4965 sizeof(addr_str));
4966 pr_warn_ratelimited("NFSD: client %s testing state ID "
4967 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004968 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004969 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004970 spin_lock(&cl->cl_lock);
4971 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004972 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004973 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004974 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004975 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004976 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004977 switch (s->sc_type) {
4978 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004979 status = nfs_ok;
4980 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004981 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004982 status = nfserr_deleg_revoked;
4983 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004984 case NFS4_OPEN_STID:
4985 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004986 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004987 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004988 default:
4989 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004990 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004991 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004992 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004993 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004994 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004995out_unlock:
4996 spin_unlock(&cl->cl_lock);
4997 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004998}
4999
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005000__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005001nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5002 stateid_t *stateid, unsigned char typemask,
5003 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005004{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005005 __be32 status;
Andrew Elble62f0b692017-11-03 14:06:31 -04005006 bool return_revoked = false;
5007
5008 /*
5009 * only return revoked delegations if explicitly asked.
5010 * otherwise we report revoked or bad_stateid status.
5011 */
5012 if (typemask & NFS4_REVOKED_DELEG_STID)
5013 return_revoked = true;
5014 else if (typemask & NFS4_DELEG_STID)
5015 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005016
Andrew Elblef25e2222017-11-09 13:41:10 -05005017 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5018 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005019 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005020 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005021 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005022 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005023 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005024 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005025 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005026 if (status)
5027 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005028 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005029 if (!*s)
5030 return nfserr_bad_stateid;
Andrew Elble62f0b692017-11-03 14:06:31 -04005031 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5032 nfs4_put_stid(*s);
5033 if (cstate->minorversion)
5034 return nfserr_deleg_revoked;
5035 return nfserr_bad_stateid;
5036 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005037 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005038}
5039
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005040static struct file *
5041nfs4_find_file(struct nfs4_stid *s, int flags)
5042{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005043 if (!s)
5044 return NULL;
5045
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005046 switch (s->sc_type) {
5047 case NFS4_DELEG_STID:
5048 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5049 return NULL;
5050 return get_file(s->sc_file->fi_deleg_file);
5051 case NFS4_OPEN_STID:
5052 case NFS4_LOCK_STID:
5053 if (flags & RD_STATE)
5054 return find_readable_file(s->sc_file);
5055 else
5056 return find_writeable_file(s->sc_file);
5057 break;
5058 }
5059
5060 return NULL;
5061}
5062
5063static __be32
5064nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
5065{
5066 __be32 status;
5067
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005068 status = nfsd4_check_openowner_confirmed(ols);
5069 if (status)
5070 return status;
5071 return nfs4_check_openmode(ols, flags);
5072}
5073
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005074static __be32
5075nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5076 struct file **filpp, bool *tmp_file, int flags)
5077{
5078 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5079 struct file *file;
5080 __be32 status;
5081
5082 file = nfs4_find_file(s, flags);
5083 if (file) {
5084 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5085 acc | NFSD_MAY_OWNER_OVERRIDE);
5086 if (status) {
5087 fput(file);
5088 return status;
5089 }
5090
5091 *filpp = file;
5092 } else {
5093 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5094 if (status)
5095 return status;
5096
5097 if (tmp_file)
5098 *tmp_file = true;
5099 }
5100
5101 return 0;
5102}
5103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005105 * Checks for stateid operations
5106 */
Al Virob37ad282006-10-19 23:28:59 -07005107__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005108nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005109 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5110 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005112 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005113 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005114 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005115 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005116 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 if (filpp)
5119 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005120 if (tmp_file)
5121 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005123 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 return nfserr_grace;
5125
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005126 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5127 status = check_special_stateids(net, fhp, stateid, flags);
5128 goto done;
5129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005131 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005132 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005133 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005134 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005135 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005136 status = check_stateid_generation(stateid, &s->sc_stateid,
5137 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005138 if (status)
5139 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005140
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005141 switch (s->sc_type) {
5142 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005143 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005144 break;
5145 case NFS4_OPEN_STID:
5146 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005147 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005148 break;
5149 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005150 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005153 if (status)
5154 goto out;
5155 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005156
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005157done:
5158 if (!status && filpp)
5159 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005161 if (s)
5162 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 return status;
5164}
5165
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005166/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005167 * Test if the stateid is valid
5168 */
5169__be32
5170nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5171 struct nfsd4_test_stateid *test_stateid)
5172{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005173 struct nfsd4_test_stateid_id *stateid;
5174 struct nfs4_client *cl = cstate->session->se_client;
5175
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005176 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005177 stateid->ts_id_status =
5178 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005179
Bryan Schumaker17456802011-07-13 10:50:48 -04005180 return nfs_ok;
5181}
5182
Chuck Lever42691392016-08-11 10:37:30 -04005183static __be32
5184nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5185{
5186 struct nfs4_ol_stateid *stp = openlockstateid(s);
5187 __be32 ret;
5188
5189 mutex_lock(&stp->st_mutex);
5190
5191 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5192 if (ret)
5193 goto out;
5194
5195 ret = nfserr_locks_held;
5196 if (check_for_locks(stp->st_stid.sc_file,
5197 lockowner(stp->st_stateowner)))
5198 goto out;
5199
5200 release_lock_stateid(stp);
5201 ret = nfs_ok;
5202
5203out:
5204 mutex_unlock(&stp->st_mutex);
5205 nfs4_put_stid(s);
5206 return ret;
5207}
5208
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005209__be32
5210nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5211 struct nfsd4_free_stateid *free_stateid)
5212{
5213 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005214 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005215 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005216 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005217 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005218
Jeff Layton1af71cc2014-07-29 21:34:14 -04005219 spin_lock(&cl->cl_lock);
5220 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005221 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005222 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005223 switch (s->sc_type) {
5224 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005225 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005226 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005227 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005228 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5229 if (ret)
5230 break;
5231 ret = nfserr_locks_held;
5232 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005233 case NFS4_LOCK_STID:
Chuck Lever42691392016-08-11 10:37:30 -04005234 atomic_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005235 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005236 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005237 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005238 case NFS4_REVOKED_DELEG_STID:
5239 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005240 list_del_init(&dp->dl_recall_lru);
5241 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005242 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005243 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005244 goto out;
5245 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005246 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005247out_unlock:
5248 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005249out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005250 return ret;
5251}
5252
NeilBrown4c4cd222005-07-07 17:59:27 -07005253static inline int
5254setlkflg (int type)
5255{
5256 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5257 RD_STATE : WR_STATE;
5258}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005260static __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 -04005261{
5262 struct svc_fh *current_fh = &cstate->current_fh;
5263 struct nfs4_stateowner *sop = stp->st_stateowner;
5264 __be32 status;
5265
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005266 status = nfsd4_check_seqid(cstate, sop, seqid);
5267 if (status)
5268 return status;
Trond Myklebust2a7d4a72017-11-03 08:00:15 -04005269 status = nfsd4_lock_ol_stateid(stp);
5270 if (status != nfs_ok)
5271 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005272 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005273 if (status == nfs_ok)
5274 status = nfs4_check_fh(current_fh, &stp->st_stid);
5275 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005276 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005277 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005278}
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280/*
5281 * Checks for sequence id mutating operations.
5282 */
Al Virob37ad282006-10-19 23:28:59 -07005283static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005284nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005285 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005286 struct nfs4_ol_stateid **stpp,
5287 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005289 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005290 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005291 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005293 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5294 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005295
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005297 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005298 if (status)
5299 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005300 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005301 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005303 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005304 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005305 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005306 else
5307 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005308 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005309}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005310
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005311static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5312 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005313{
5314 __be32 status;
5315 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005316 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005318 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005319 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005320 if (status)
5321 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005322 oo = openowner(stp->st_stateowner);
5323 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005324 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005325 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005326 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005327 }
5328 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005329 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330}
5331
Al Virob37ad282006-10-19 23:28:59 -07005332__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005333nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005334 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335{
Al Virob37ad282006-10-19 23:28:59 -07005336 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005337 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005338 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005339 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Al Viroa6a9f182013-09-16 10:57:01 -04005341 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5342 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005344 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005345 if (status)
5346 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005348 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005349 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005350 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005351 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005352 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005353 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005354 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005355 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005356 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005357 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005358 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005359 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005360 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005361 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005362 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005363 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005364
Jeff Layton2a4317c2012-03-21 16:42:43 -04005365 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005366 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005367put_stateid:
5368 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005370 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 return status;
5372}
5373
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005374static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005376 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005377 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005378 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005379 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005380}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005381
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005382static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5383{
5384 switch (to_access) {
5385 case NFS4_SHARE_ACCESS_READ:
5386 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5387 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5388 break;
5389 case NFS4_SHARE_ACCESS_WRITE:
5390 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5391 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5392 break;
5393 case NFS4_SHARE_ACCESS_BOTH:
5394 break;
5395 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005396 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 }
5398}
5399
Al Virob37ad282006-10-19 23:28:59 -07005400__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005401nfsd4_open_downgrade(struct svc_rqst *rqstp,
5402 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005403 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
Al Virob37ad282006-10-19 23:28:59 -07005405 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005406 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005407 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Al Viroa6a9f182013-09-16 10:57:01 -04005409 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5410 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005412 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005413 if (od->od_deleg_want)
5414 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5415 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005417 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005418 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005419 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005422 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005423 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005425 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005427 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005428 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005430 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005432 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005433 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005434 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005436put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005437 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005438 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005440 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 return status;
5442}
5443
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005444static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5445{
Trond Myklebustacf92952014-06-30 11:48:37 -04005446 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005447 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005448 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005449
Jeff Layton2c41beb2014-07-29 21:34:41 -04005450 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005451 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005452
Jeff Laytond83017f2014-07-29 21:34:42 -04005453 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005454 if (unhashed)
5455 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005456 spin_unlock(&clp->cl_lock);
5457 free_ol_stateid_reaplist(&reaplist);
5458 } else {
5459 spin_unlock(&clp->cl_lock);
5460 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005461 if (unhashed)
5462 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005463 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005464}
5465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466/*
5467 * nfs4_unlock_state() called after encode
5468 */
Al Virob37ad282006-10-19 23:28:59 -07005469__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005470nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005471 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472{
Al Virob37ad282006-10-19 23:28:59 -07005473 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005474 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005475 struct net *net = SVC_NET(rqstp);
5476 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
Al Viroa6a9f182013-09-16 10:57:01 -04005478 dprintk("NFSD: nfsd4_close on file %pd\n",
5479 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005481 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5482 &close->cl_stateid,
5483 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005484 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005485 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005486 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 goto out;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04005488
5489 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton9767feb2015-10-01 09:05:50 -04005490 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005492 nfsd4_close_open_stateid(stp);
Trond Myklebust1c8ea412017-11-03 08:00:10 -04005493 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005494
Trond Myklebust5cd35862017-11-03 08:00:12 -04005495 /* See RFC5661 sectionm 18.2.4 */
5496 if (stp->st_stid.sc_client->cl_minorversion)
5497 memcpy(&close->cl_stateid, &close_stateid,
5498 sizeof(close->cl_stateid));
5499
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005500 /* put reference from nfs4_preprocess_seqid_op */
5501 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 return status;
5504}
5505
Al Virob37ad282006-10-19 23:28:59 -07005506__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005507nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5508 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005510 struct nfs4_delegation *dp;
5511 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005512 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005513 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005514 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005516 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005517 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005519 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005520 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005521 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005522 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005523 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005524 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005525 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005526
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005527 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005528put_stateid:
5529 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530out:
5531 return status;
5532}
5533
Benny Halevy87df4de2008-12-15 19:42:03 +02005534static inline u64
5535end_offset(u64 start, u64 len)
5536{
5537 u64 end;
5538
5539 end = start + len;
5540 return end >= start ? end: NFS4_MAX_UINT64;
5541}
5542
5543/* last octet in a range */
5544static inline u64
5545last_byte_offset(u64 start, u64 len)
5546{
5547 u64 end;
5548
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005549 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005550 end = start + len;
5551 return end > start ? end - 1: NFS4_MAX_UINT64;
5552}
5553
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554/*
5555 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5556 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5557 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5558 * locking, this prevents us from being completely protocol-compliant. The
5559 * real solution to this problem is to start using unsigned file offsets in
5560 * the VFS, but this is a very deep change!
5561 */
5562static inline void
5563nfs4_transform_lock_offset(struct file_lock *lock)
5564{
5565 if (lock->fl_start < 0)
5566 lock->fl_start = OFFSET_MAX;
5567 if (lock->fl_end < 0)
5568 lock->fl_end = OFFSET_MAX;
5569}
5570
Jeff Laytoncae80b32015-04-03 09:04:04 -04005571static fl_owner_t
5572nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005573{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005574 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5575
5576 nfs4_get_stateowner(&lo->lo_owner);
5577 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005578}
5579
Jeff Laytoncae80b32015-04-03 09:04:04 -04005580static void
5581nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005582{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005583 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005584
Jeff Laytoncae80b32015-04-03 09:04:04 -04005585 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005586 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005587}
5588
Jeff Layton76d348f2016-09-16 16:28:24 -04005589static void
5590nfsd4_lm_notify(struct file_lock *fl)
5591{
5592 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5593 struct net *net = lo->lo_owner.so_client->net;
5594 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5595 struct nfsd4_blocked_lock *nbl = container_of(fl,
5596 struct nfsd4_blocked_lock, nbl_lock);
5597 bool queue = false;
5598
Jeff Layton7919d0a2016-09-16 16:28:25 -04005599 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005600 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005601 if (!list_empty(&nbl->nbl_list)) {
5602 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005603 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005604 queue = true;
5605 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005606 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005607
5608 if (queue)
5609 nfsd4_run_cb(&nbl->nbl_cb);
5610}
5611
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005612static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005613 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005614 .lm_get_owner = nfsd4_fl_get_owner,
5615 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005616};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
5618static inline void
5619nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5620{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005621 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
NeilBrownd5b90262006-04-10 22:55:22 -07005623 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005624 lo = (struct nfs4_lockowner *) fl->fl_owner;
5625 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5626 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005627 if (!deny->ld_owner.data)
5628 /* We just don't care that much */
5629 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005630 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5631 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005632 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005633nevermind:
5634 deny->ld_owner.len = 0;
5635 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005636 deny->ld_clientid.cl_boot = 0;
5637 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 }
5639 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005640 deny->ld_length = NFS4_MAX_UINT64;
5641 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5643 deny->ld_type = NFS4_READ_LT;
5644 if (fl->fl_type != F_RDLCK)
5645 deny->ld_type = NFS4_WRITE_LT;
5646}
5647
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005648static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005649find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005651 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005652 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
Trond Myklebust0a880a22014-07-30 08:27:10 -04005654 lockdep_assert_held(&clp->cl_lock);
5655
Trond Myklebustd4f04892014-07-29 21:34:36 -04005656 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5657 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005658 if (so->so_is_open_owner)
5659 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005660 if (same_owner_str(so, owner))
5661 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 }
5663 return NULL;
5664}
5665
Trond Myklebustc58c6612014-07-29 21:34:35 -04005666static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005667find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005668{
5669 struct nfs4_lockowner *lo;
5670
Trond Myklebustd4f04892014-07-29 21:34:36 -04005671 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005672 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005673 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005674 return lo;
5675}
5676
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005677static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5678{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005679 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005680}
5681
Jeff Layton6b180f02014-07-29 21:34:26 -04005682static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5683{
5684 struct nfs4_lockowner *lo = lockowner(sop);
5685
5686 kmem_cache_free(lockowner_slab, lo);
5687}
5688
5689static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005690 .so_unhash = nfs4_unhash_lockowner,
5691 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005692};
5693
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694/*
5695 * Alloc a lock owner structure.
5696 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005697 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005699 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005701static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005702alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5703 struct nfs4_ol_stateid *open_stp,
5704 struct nfsd4_lock *lock)
5705{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005706 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005708 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5709 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005711 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005712 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5713 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005714 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005715 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005716 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005717 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005718 if (ret == NULL) {
5719 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005720 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005721 ret = lo;
5722 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005723 nfs4_free_stateowner(&lo->lo_owner);
5724
Trond Myklebustd4f04892014-07-29 21:34:36 -04005725 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005726 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727}
5728
Jeff Layton356a95e2014-07-29 21:34:13 -04005729static void
5730init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5731 struct nfs4_file *fp, struct inode *inode,
5732 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005734 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Jeff Layton356a95e2014-07-29 21:34:13 -04005736 lockdep_assert_held(&clp->cl_lock);
5737
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005738 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005739 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005740 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005741 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005742 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005743 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005745 stp->st_openstp = open_stp;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005746 mutex_init(&stp->st_mutex);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005747 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005748 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005749 spin_lock(&fp->fi_lock);
5750 list_add(&stp->st_perfile, &fp->fi_stateids);
5751 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752}
5753
Jeff Laytonc53530d2014-06-30 11:48:39 -04005754static struct nfs4_ol_stateid *
5755find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5756{
5757 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005758 struct nfs4_client *clp = lo->lo_owner.so_client;
5759
5760 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005761
5762 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005763 if (lst->st_stid.sc_file == fp) {
5764 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005765 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005766 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005767 }
5768 return NULL;
5769}
5770
Jeff Layton356a95e2014-07-29 21:34:13 -04005771static struct nfs4_ol_stateid *
5772find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5773 struct inode *inode, struct nfs4_ol_stateid *ost,
5774 bool *new)
5775{
5776 struct nfs4_stid *ns = NULL;
5777 struct nfs4_ol_stateid *lst;
5778 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5779 struct nfs4_client *clp = oo->oo_owner.so_client;
5780
5781 spin_lock(&clp->cl_lock);
5782 lst = find_lock_stateid(lo, fi);
5783 if (lst == NULL) {
5784 spin_unlock(&clp->cl_lock);
Kinglong Mee743146d32017-01-18 19:04:42 +08005785 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
Jeff Layton356a95e2014-07-29 21:34:13 -04005786 if (ns == NULL)
5787 return NULL;
5788
5789 spin_lock(&clp->cl_lock);
5790 lst = find_lock_stateid(lo, fi);
5791 if (likely(!lst)) {
5792 lst = openlockstateid(ns);
5793 init_lock_stateid(lst, lo, fi, inode, ost);
5794 ns = NULL;
5795 *new = true;
5796 }
5797 }
5798 spin_unlock(&clp->cl_lock);
5799 if (ns)
5800 nfs4_put_stid(ns);
5801 return lst;
5802}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005803
NeilBrownfd39ca92005-06-23 22:04:03 -07005804static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805check_lock_length(u64 offset, u64 length)
5806{
Kinglong Meee7969312015-07-13 17:34:19 +08005807 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5808 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809}
5810
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005811static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005812{
Trond Myklebust11b91642014-07-29 21:34:08 -04005813 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005814
Jeff Layton7214e862014-07-10 14:07:33 -04005815 lockdep_assert_held(&fp->fi_lock);
5816
Jeff Layton82c5ff12012-05-11 09:45:13 -04005817 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005818 return;
Jeff Layton12659652014-07-10 14:07:28 -04005819 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005820 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005821}
5822
Jeff Layton356a95e2014-07-29 21:34:13 -04005823static __be32
5824lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5825 struct nfs4_ol_stateid *ost,
5826 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005827 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005828{
Jeff Layton5db1c032014-07-29 21:34:28 -04005829 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005830 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005831 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5832 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005833 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005834 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005835 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005836 unsigned int strhashval;
Jeff Laytondd257932016-08-11 10:37:39 -04005837 bool hashed;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005838
Kinglong Meec8623992015-07-13 17:35:37 +08005839 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005840 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005841 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005842 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5843 if (lo == NULL)
5844 return nfserr_jukebox;
5845 } else {
5846 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005847 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005848 if (!cstate->minorversion &&
5849 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005850 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005851 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005852
Jeff Laytondd257932016-08-11 10:37:39 -04005853retry:
5854 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5855 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005856 status = nfserr_jukebox;
5857 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005858 }
Jeff Laytondd257932016-08-11 10:37:39 -04005859
5860 mutex_lock(&lst->st_mutex);
5861
5862 /* See if it's still hashed to avoid race with FREE_STATEID */
5863 spin_lock(&cl->cl_lock);
5864 hashed = !list_empty(&lst->st_perfile);
5865 spin_unlock(&cl->cl_lock);
5866
5867 if (!hashed) {
5868 mutex_unlock(&lst->st_mutex);
5869 nfs4_put_stid(&lst->st_stid);
5870 goto retry;
5871 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005872 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005873 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005874out:
5875 nfs4_put_stateowner(&lo->lo_owner);
5876 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005877}
5878
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879/*
5880 * LOCK operation
5881 */
Al Virob37ad282006-10-19 23:28:59 -07005882__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005883nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005884 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005886 struct nfs4_openowner *open_sop = NULL;
5887 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005888 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005889 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005890 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005891 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005892 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005893 struct file_lock *file_lock = NULL;
5894 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005895 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005896 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005897 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005898 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005899 unsigned char fl_type;
5900 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005901 struct net *net = SVC_NET(rqstp);
5902 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
5904 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5905 (long long) lock->lk_offset,
5906 (long long) lock->lk_length);
5907
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 if (check_lock_length(lock->lk_offset, lock->lk_length))
5909 return nfserr_inval;
5910
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005911 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005912 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005913 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5914 return status;
5915 }
5916
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005918 if (nfsd4_has_session(cstate))
5919 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005920 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005921 &cstate->session->se_client->cl_clientid,
5922 sizeof(clientid_t));
5923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005925 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005929 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 lock->lk_new_open_seqid,
5931 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005932 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005933 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005935 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005936 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005937 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005938 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005939 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005940 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005941 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005942 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005943 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005944 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005945 lock->lk_old_lock_seqid,
5946 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005947 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005948 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005949 if (status)
5950 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005951 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005953 lkflg = setlkflg(lock->lk_type);
5954 status = nfs4_check_openmode(lock_stp, lkflg);
5955 if (status)
5956 goto out;
5957
NeilBrown0dd395d2005-07-07 17:59:15 -07005958 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005959 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005960 goto out;
5961 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005962 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005963 goto out;
5964
Trond Myklebust11b91642014-07-29 21:34:08 -04005965 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005968 if (nfsd4_has_session(cstate))
5969 fl_flags |= FL_SLEEP;
5970 /* Fallthrough */
5971 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005972 spin_lock(&fp->fi_lock);
5973 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005974 if (filp)
5975 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005976 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005977 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005980 if (nfsd4_has_session(cstate))
5981 fl_flags |= FL_SLEEP;
5982 /* Fallthrough */
5983 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005984 spin_lock(&fp->fi_lock);
5985 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005986 if (filp)
5987 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005988 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005989 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 default:
5992 status = nfserr_inval;
5993 goto out;
5994 }
Jeff Layton76d348f2016-09-16 16:28:24 -04005995
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005996 if (!filp) {
5997 status = nfserr_openmode;
5998 goto out;
5999 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006000
Jeff Layton76d348f2016-09-16 16:28:24 -04006001 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6002 if (!nbl) {
6003 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6004 status = nfserr_jukebox;
6005 goto out;
6006 }
6007
6008 file_lock = &nbl->nbl_lock;
6009 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006010 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006011 file_lock->fl_pid = current->tgid;
6012 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006013 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006014 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6015 file_lock->fl_start = lock->lk_offset;
6016 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6017 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
Jeff Layton21179d82012-08-21 08:03:32 -04006019 conflock = locks_alloc_lock();
6020 if (!conflock) {
6021 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6022 status = nfserr_jukebox;
6023 goto out;
6024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
Jeff Layton76d348f2016-09-16 16:28:24 -04006026 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006027 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006028 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006029 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006030 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006031 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006032 }
6033
Jeff Layton21179d82012-08-21 08:03:32 -04006034 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006035 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006037 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006038 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006039 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006040 case FILE_LOCK_DEFERRED:
6041 nbl = NULL;
6042 /* Fallthrough */
6043 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006044 status = nfserr_denied;
6045 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006046 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006047 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006048 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006050 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006051 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006052 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006053 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006057 if (nbl) {
6058 /* dequeue it if we queued it before */
6059 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006060 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006061 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006062 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006063 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006064 }
6065 free_blocked_lock(nbl);
6066 }
Trond Myklebustde186432014-07-10 14:07:26 -04006067 if (filp)
6068 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006069 if (lock_stp) {
6070 /* Bump seqid manually if the 4.0 replay owner is openowner */
6071 if (cstate->replay_owner &&
6072 cstate->replay_owner != &lock_sop->lo_owner &&
6073 seqid_mutating_err(ntohl(status)))
6074 lock_sop->lo_owner.so_seqid++;
6075
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006076 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006077
Jeff Layton5db1c032014-07-29 21:34:28 -04006078 /*
6079 * If this is a new, never-before-used stateid, and we are
6080 * returning an error, then just go ahead and release it.
6081 */
6082 if (status && new)
6083 release_lock_stateid(lock_stp);
6084
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006085 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006086 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006087 if (open_stp)
6088 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006089 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006090 if (conflock)
6091 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 return status;
6093}
6094
6095/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006096 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6097 * so we do a temporary open here just to get an open file to pass to
6098 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6099 * inode operation.)
6100 */
Al Viro04da6e92012-04-13 00:00:04 -04006101static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006102{
6103 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006104 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6105 if (!err) {
6106 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006107 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006108 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006109 return err;
6110}
6111
6112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 * LOCKT operation
6114 */
Al Virob37ad282006-10-19 23:28:59 -07006115__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006116nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6117 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118{
Jeff Layton21179d82012-08-21 08:03:32 -04006119 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006120 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006121 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006122 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006124 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 return nfserr_grace;
6126
6127 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6128 return nfserr_inval;
6129
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006130 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006131 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006132 if (status)
6133 goto out;
6134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006136 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138
Jeff Layton21179d82012-08-21 08:03:32 -04006139 file_lock = locks_alloc_lock();
6140 if (!file_lock) {
6141 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6142 status = nfserr_jukebox;
6143 goto out;
6144 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006145
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 switch (lockt->lt_type) {
6147 case NFS4_READ_LT:
6148 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006149 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 break;
6151 case NFS4_WRITE_LT:
6152 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006153 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 break;
6155 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006156 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 status = nfserr_inval;
6158 goto out;
6159 }
6160
Kinglong Meec8623992015-07-13 17:35:37 +08006161 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006162 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006163 file_lock->fl_owner = (fl_owner_t)lo;
6164 file_lock->fl_pid = current->tgid;
6165 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
Jeff Layton21179d82012-08-21 08:03:32 -04006167 file_lock->fl_start = lockt->lt_offset;
6168 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
Jeff Layton21179d82012-08-21 08:03:32 -04006170 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
Jeff Layton21179d82012-08-21 08:03:32 -04006172 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006173 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006174 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006175
Jeff Layton21179d82012-08-21 08:03:32 -04006176 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006178 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 }
6180out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006181 if (lo)
6182 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006183 if (file_lock)
6184 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 return status;
6186}
6187
Al Virob37ad282006-10-19 23:28:59 -07006188__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006189nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08006190 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006192 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006194 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006195 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006196 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006197 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6198
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6200 (long long) locku->lu_offset,
6201 (long long) locku->lu_length);
6202
6203 if (check_lock_length(locku->lu_offset, locku->lu_length))
6204 return nfserr_inval;
6205
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006206 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006207 &locku->lu_stateid, NFS4_LOCK_STID,
6208 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006209 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006211 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006212 if (!filp) {
6213 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006214 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006215 }
Jeff Layton21179d82012-08-21 08:03:32 -04006216 file_lock = locks_alloc_lock();
6217 if (!file_lock) {
6218 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6219 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006220 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006221 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006222
Jeff Layton21179d82012-08-21 08:03:32 -04006223 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006224 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006225 file_lock->fl_pid = current->tgid;
6226 file_lock->fl_file = filp;
6227 file_lock->fl_flags = FL_POSIX;
6228 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6229 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
Jeff Layton21179d82012-08-21 08:03:32 -04006231 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6232 locku->lu_length);
6233 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
Jeff Layton21179d82012-08-21 08:03:32 -04006235 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006236 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006237 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 goto out_nfserr;
6239 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006240 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006241fput:
6242 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006243put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006244 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006245 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006247 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006248 if (file_lock)
6249 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 return status;
6251
6252out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006253 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006254 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255}
6256
6257/*
6258 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006259 * true: locks held by lockowner
6260 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006262static bool
6263check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006265 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006266 int status = false;
6267 struct file *filp = find_any_file(fp);
6268 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006269 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006270
6271 if (!filp) {
6272 /* Any valid lock stateid should have some sort of access */
6273 WARN_ON_ONCE(1);
6274 return status;
6275 }
6276
6277 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006278 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006280 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006281 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006282 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6283 if (fl->fl_owner == (fl_owner_t)lowner) {
6284 status = true;
6285 break;
6286 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006287 }
Jeff Layton6109c852015-01-16 15:05:57 -05006288 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006290 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 return status;
6292}
6293
Al Virob37ad282006-10-19 23:28:59 -07006294__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006295nfsd4_release_lockowner(struct svc_rqst *rqstp,
6296 struct nfsd4_compound_state *cstate,
6297 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298{
6299 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006300 struct nfs4_stateowner *sop;
6301 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006302 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006304 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006305 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006306 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006307 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006308 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309
6310 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6311 clid->cl_boot, clid->cl_id);
6312
Jeff Layton4b24ca72014-06-30 11:48:44 -04006313 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006314 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006315 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006316
Trond Myklebustd4f04892014-07-29 21:34:36 -04006317 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006318 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006319 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006320 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006321 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006322
6323 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006324 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006325
Jeff Layton882e9d22014-07-29 21:34:37 -04006326 /* see if there are still any locks associated with it */
6327 lo = lockowner(sop);
6328 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6329 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6330 status = nfserr_locks_held;
6331 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006332 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006333 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006334 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006335
Kinglong Meeb5971af2014-08-22 10:18:43 -04006336 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006337 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006338 }
Chuck Lever88584812016-07-13 16:40:14 -04006339 if (!lo) {
6340 spin_unlock(&clp->cl_lock);
6341 return status;
6342 }
6343
6344 unhash_lockowner_locked(lo);
6345 while (!list_empty(&lo->lo_owner.so_stateids)) {
6346 stp = list_first_entry(&lo->lo_owner.so_stateids,
6347 struct nfs4_ol_stateid,
6348 st_perstateowner);
6349 WARN_ON(!unhash_lock_stateid(stp));
6350 put_ol_stateid_locked(stp, &reaplist);
6351 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006352 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006353 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone294c4c2018-03-16 11:32:02 -04006354 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006355 nfs4_put_stateowner(&lo->lo_owner);
6356
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 return status;
6358}
6359
6360static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006361alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362{
NeilBrowna55370a2005-06-23 22:03:52 -07006363 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364}
6365
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006366bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006367nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006368{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006369 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006370
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006371 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006372 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006373}
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375/*
6376 * failure => all reset bets are off, nfserr_no_grace...
6377 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006378struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006379nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380{
6381 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006382 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
NeilBrowna55370a2005-06-23 22:03:52 -07006384 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6385 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006386 if (crp) {
6387 strhashval = clientstr_hashval(name);
6388 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006389 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006390 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006391 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006392 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006393 }
6394 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395}
6396
Jeff Layton2a4317c2012-03-21 16:42:43 -04006397void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006398nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006399{
6400 list_del(&crp->cr_strhash);
6401 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006402 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006403}
6404
6405void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006406nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407{
6408 struct nfs4_client_reclaim *crp = NULL;
6409 int i;
6410
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006412 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6413 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006415 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 }
6417 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006418 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419}
6420
6421/*
6422 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006423struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006424nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425{
6426 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 struct nfs4_client_reclaim *crp = NULL;
6428
Jeff Layton278c9312012-11-12 15:00:52 -05006429 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430
Jeff Layton278c9312012-11-12 15:00:52 -05006431 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006432 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006433 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 return crp;
6435 }
6436 }
6437 return NULL;
6438}
6439
6440/*
6441* Called from OPEN. Look for clientid in reclaim list.
6442*/
Al Virob37ad282006-10-19 23:28:59 -07006443__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006444nfs4_check_open_reclaim(clientid_t *clid,
6445 struct nfsd4_compound_state *cstate,
6446 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006448 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006449
6450 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006451 status = lookup_clientid(clid, cstate, nn);
6452 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006453 return nfserr_reclaim_bad;
6454
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006455 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6456 return nfserr_no_grace;
6457
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006458 if (nfsd4_client_record_check(cstate->clp))
6459 return nfserr_reclaim_bad;
6460
6461 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462}
6463
Bryan Schumaker65178db2011-11-01 13:35:21 -04006464#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006465static inline void
6466put_client(struct nfs4_client *clp)
6467{
6468 atomic_dec(&clp->cl_refcount);
6469}
6470
Jeff Layton285abde2014-07-30 08:27:24 -04006471static struct nfs4_client *
6472nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6473{
6474 struct nfs4_client *clp;
6475 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6476 nfsd_net_id);
6477
6478 if (!nfsd_netns_ready(nn))
6479 return NULL;
6480
6481 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6482 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6483 return clp;
6484 }
6485 return NULL;
6486}
6487
Jeff Layton7ec0e362014-07-30 08:27:17 -04006488u64
Jeff Layton285abde2014-07-30 08:27:24 -04006489nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006490{
6491 struct nfs4_client *clp;
6492 u64 count = 0;
6493 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6494 nfsd_net_id);
6495 char buf[INET6_ADDRSTRLEN];
6496
6497 if (!nfsd_netns_ready(nn))
6498 return 0;
6499
6500 spin_lock(&nn->client_lock);
6501 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6502 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6503 pr_info("NFS Client: %s\n", buf);
6504 ++count;
6505 }
6506 spin_unlock(&nn->client_lock);
6507
6508 return count;
6509}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006510
Jeff Laytona0926d12014-07-30 08:27:18 -04006511u64
Jeff Layton285abde2014-07-30 08:27:24 -04006512nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006513{
6514 u64 count = 0;
6515 struct nfs4_client *clp;
6516 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6517 nfsd_net_id);
6518
6519 if (!nfsd_netns_ready(nn))
6520 return count;
6521
6522 spin_lock(&nn->client_lock);
6523 clp = nfsd_find_client(addr, addr_size);
6524 if (clp) {
6525 if (mark_client_expired_locked(clp) == nfs_ok)
6526 ++count;
6527 else
6528 clp = NULL;
6529 }
6530 spin_unlock(&nn->client_lock);
6531
6532 if (clp)
6533 expire_client(clp);
6534
6535 return count;
6536}
6537
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006538u64
Jeff Layton285abde2014-07-30 08:27:24 -04006539nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006540{
6541 u64 count = 0;
6542 struct nfs4_client *clp, *next;
6543 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6544 nfsd_net_id);
6545 LIST_HEAD(reaplist);
6546
6547 if (!nfsd_netns_ready(nn))
6548 return count;
6549
6550 spin_lock(&nn->client_lock);
6551 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6552 if (mark_client_expired_locked(clp) == nfs_ok) {
6553 list_add(&clp->cl_lru, &reaplist);
6554 if (max != 0 && ++count >= max)
6555 break;
6556 }
6557 }
6558 spin_unlock(&nn->client_lock);
6559
6560 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6561 expire_client(clp);
6562
6563 return count;
6564}
6565
Bryan Schumaker184c1842012-11-29 11:40:44 -05006566static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6567 const char *type)
6568{
6569 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006570 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006571 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6572}
6573
Jeff Layton016200c2014-07-30 08:27:21 -04006574static void
6575nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6576 struct list_head *collect)
6577{
6578 struct nfs4_client *clp = lst->st_stid.sc_client;
6579 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6580 nfsd_net_id);
6581
6582 if (!collect)
6583 return;
6584
6585 lockdep_assert_held(&nn->client_lock);
6586 atomic_inc(&clp->cl_refcount);
6587 list_add(&lst->st_locks, collect);
6588}
6589
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006590static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006591 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006592 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006593{
6594 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006595 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006596 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006597 u64 count = 0;
6598
Jeff Layton016200c2014-07-30 08:27:21 -04006599 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006600 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006601 list_for_each_entry_safe(stp, st_next,
6602 &oop->oo_owner.so_stateids, st_perstateowner) {
6603 list_for_each_entry_safe(lst, lst_next,
6604 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006605 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006606 if (func(lst))
6607 nfsd_inject_add_lock_to_list(lst,
6608 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006609 }
Jeff Layton016200c2014-07-30 08:27:21 -04006610 ++count;
6611 /*
6612 * Despite the fact that these functions deal
6613 * with 64-bit integers for "count", we must
6614 * ensure that it doesn't blow up the
6615 * clp->cl_refcount. Throw a warning if we
6616 * start to approach INT_MAX here.
6617 */
6618 WARN_ON_ONCE(count == (INT_MAX / 2));
6619 if (count == max)
6620 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006621 }
6622 }
6623 }
Jeff Layton016200c2014-07-30 08:27:21 -04006624out:
6625 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006626
6627 return count;
6628}
6629
Jeff Layton016200c2014-07-30 08:27:21 -04006630static u64
6631nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6632 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006633{
Jeff Layton016200c2014-07-30 08:27:21 -04006634 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006635}
6636
Jeff Layton016200c2014-07-30 08:27:21 -04006637static u64
6638nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006639{
Jeff Layton016200c2014-07-30 08:27:21 -04006640 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006641 nfsd_print_count(clp, count, "locked files");
6642 return count;
6643}
6644
Jeff Layton016200c2014-07-30 08:27:21 -04006645u64
Jeff Layton285abde2014-07-30 08:27:24 -04006646nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006647{
6648 struct nfs4_client *clp;
6649 u64 count = 0;
6650 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6651 nfsd_net_id);
6652
6653 if (!nfsd_netns_ready(nn))
6654 return 0;
6655
6656 spin_lock(&nn->client_lock);
6657 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6658 count += nfsd_print_client_locks(clp);
6659 spin_unlock(&nn->client_lock);
6660
6661 return count;
6662}
6663
6664static void
6665nfsd_reap_locks(struct list_head *reaplist)
6666{
6667 struct nfs4_client *clp;
6668 struct nfs4_ol_stateid *stp, *next;
6669
6670 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6671 list_del_init(&stp->st_locks);
6672 clp = stp->st_stid.sc_client;
6673 nfs4_put_stid(&stp->st_stid);
6674 put_client(clp);
6675 }
6676}
6677
6678u64
Jeff Layton285abde2014-07-30 08:27:24 -04006679nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006680{
6681 unsigned int count = 0;
6682 struct nfs4_client *clp;
6683 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6684 nfsd_net_id);
6685 LIST_HEAD(reaplist);
6686
6687 if (!nfsd_netns_ready(nn))
6688 return count;
6689
6690 spin_lock(&nn->client_lock);
6691 clp = nfsd_find_client(addr, addr_size);
6692 if (clp)
6693 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6694 spin_unlock(&nn->client_lock);
6695 nfsd_reap_locks(&reaplist);
6696 return count;
6697}
6698
6699u64
Jeff Layton285abde2014-07-30 08:27:24 -04006700nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006701{
6702 u64 count = 0;
6703 struct nfs4_client *clp;
6704 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6705 nfsd_net_id);
6706 LIST_HEAD(reaplist);
6707
6708 if (!nfsd_netns_ready(nn))
6709 return count;
6710
6711 spin_lock(&nn->client_lock);
6712 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6713 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6714 if (max != 0 && count >= max)
6715 break;
6716 }
6717 spin_unlock(&nn->client_lock);
6718 nfsd_reap_locks(&reaplist);
6719 return count;
6720}
6721
Jeff Layton82e05ef2014-07-30 08:27:22 -04006722static u64
6723nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6724 struct list_head *collect,
6725 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006726{
6727 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006728 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6729 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006730 u64 count = 0;
6731
Jeff Layton82e05ef2014-07-30 08:27:22 -04006732 lockdep_assert_held(&nn->client_lock);
6733
6734 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006735 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006736 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006737 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006738 if (collect) {
6739 atomic_inc(&clp->cl_refcount);
6740 list_add(&oop->oo_perclient, collect);
6741 }
6742 }
6743 ++count;
6744 /*
6745 * Despite the fact that these functions deal with
6746 * 64-bit integers for "count", we must ensure that
6747 * it doesn't blow up the clp->cl_refcount. Throw a
6748 * warning if we start to approach INT_MAX here.
6749 */
6750 WARN_ON_ONCE(count == (INT_MAX / 2));
6751 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006752 break;
6753 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006754 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006755
6756 return count;
6757}
6758
Jeff Layton82e05ef2014-07-30 08:27:22 -04006759static u64
6760nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006761{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006762 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6763
6764 nfsd_print_count(clp, count, "openowners");
6765 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006766}
6767
Jeff Layton82e05ef2014-07-30 08:27:22 -04006768static u64
6769nfsd_collect_client_openowners(struct nfs4_client *clp,
6770 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006771{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006772 return nfsd_foreach_client_openowner(clp, max, collect,
6773 unhash_openowner_locked);
6774}
6775
6776u64
Jeff Layton285abde2014-07-30 08:27:24 -04006777nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006778{
6779 struct nfs4_client *clp;
6780 u64 count = 0;
6781 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6782 nfsd_net_id);
6783
6784 if (!nfsd_netns_ready(nn))
6785 return 0;
6786
6787 spin_lock(&nn->client_lock);
6788 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6789 count += nfsd_print_client_openowners(clp);
6790 spin_unlock(&nn->client_lock);
6791
6792 return count;
6793}
6794
6795static void
6796nfsd_reap_openowners(struct list_head *reaplist)
6797{
6798 struct nfs4_client *clp;
6799 struct nfs4_openowner *oop, *next;
6800
6801 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6802 list_del_init(&oop->oo_perclient);
6803 clp = oop->oo_owner.so_client;
6804 release_openowner(oop);
6805 put_client(clp);
6806 }
6807}
6808
6809u64
Jeff Layton285abde2014-07-30 08:27:24 -04006810nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6811 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006812{
6813 unsigned int count = 0;
6814 struct nfs4_client *clp;
6815 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6816 nfsd_net_id);
6817 LIST_HEAD(reaplist);
6818
6819 if (!nfsd_netns_ready(nn))
6820 return count;
6821
6822 spin_lock(&nn->client_lock);
6823 clp = nfsd_find_client(addr, addr_size);
6824 if (clp)
6825 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6826 spin_unlock(&nn->client_lock);
6827 nfsd_reap_openowners(&reaplist);
6828 return count;
6829}
6830
6831u64
Jeff Layton285abde2014-07-30 08:27:24 -04006832nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006833{
6834 u64 count = 0;
6835 struct nfs4_client *clp;
6836 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6837 nfsd_net_id);
6838 LIST_HEAD(reaplist);
6839
6840 if (!nfsd_netns_ready(nn))
6841 return count;
6842
6843 spin_lock(&nn->client_lock);
6844 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6845 count += nfsd_collect_client_openowners(clp, &reaplist,
6846 max - count);
6847 if (max != 0 && count >= max)
6848 break;
6849 }
6850 spin_unlock(&nn->client_lock);
6851 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006852 return count;
6853}
6854
Bryan Schumaker269de302012-11-29 11:40:42 -05006855static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6856 struct list_head *victims)
6857{
6858 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006859 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6860 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006861 u64 count = 0;
6862
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006863 lockdep_assert_held(&nn->client_lock);
6864
6865 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006866 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006867 if (victims) {
6868 /*
6869 * It's not safe to mess with delegations that have a
6870 * non-zero dl_time. They might have already been broken
6871 * and could be processed by the laundromat outside of
6872 * the state_lock. Just leave them be.
6873 */
6874 if (dp->dl_time != 0)
6875 continue;
6876
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006877 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006878 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006879 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006880 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006881 ++count;
6882 /*
6883 * Despite the fact that these functions deal with
6884 * 64-bit integers for "count", we must ensure that
6885 * it doesn't blow up the clp->cl_refcount. Throw a
6886 * warning if we start to approach INT_MAX here.
6887 */
6888 WARN_ON_ONCE(count == (INT_MAX / 2));
6889 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006890 break;
6891 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006892 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006893 return count;
6894}
6895
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006896static u64
6897nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006898{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006899 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006900
6901 nfsd_print_count(clp, count, "delegations");
6902 return count;
6903}
6904
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006905u64
Jeff Layton285abde2014-07-30 08:27:24 -04006906nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006907{
6908 struct nfs4_client *clp;
6909 u64 count = 0;
6910 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6911 nfsd_net_id);
6912
6913 if (!nfsd_netns_ready(nn))
6914 return 0;
6915
6916 spin_lock(&nn->client_lock);
6917 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6918 count += nfsd_print_client_delegations(clp);
6919 spin_unlock(&nn->client_lock);
6920
6921 return count;
6922}
6923
6924static void
6925nfsd_forget_delegations(struct list_head *reaplist)
6926{
6927 struct nfs4_client *clp;
6928 struct nfs4_delegation *dp, *next;
6929
6930 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6931 list_del_init(&dp->dl_recall_lru);
6932 clp = dp->dl_stid.sc_client;
6933 revoke_delegation(dp);
6934 put_client(clp);
6935 }
6936}
6937
6938u64
Jeff Layton285abde2014-07-30 08:27:24 -04006939nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6940 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006941{
6942 u64 count = 0;
6943 struct nfs4_client *clp;
6944 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6945 nfsd_net_id);
6946 LIST_HEAD(reaplist);
6947
6948 if (!nfsd_netns_ready(nn))
6949 return count;
6950
6951 spin_lock(&nn->client_lock);
6952 clp = nfsd_find_client(addr, addr_size);
6953 if (clp)
6954 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6955 spin_unlock(&nn->client_lock);
6956
6957 nfsd_forget_delegations(&reaplist);
6958 return count;
6959}
6960
6961u64
Jeff Layton285abde2014-07-30 08:27:24 -04006962nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006963{
6964 u64 count = 0;
6965 struct nfs4_client *clp;
6966 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6967 nfsd_net_id);
6968 LIST_HEAD(reaplist);
6969
6970 if (!nfsd_netns_ready(nn))
6971 return count;
6972
6973 spin_lock(&nn->client_lock);
6974 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6975 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6976 if (max != 0 && count >= max)
6977 break;
6978 }
6979 spin_unlock(&nn->client_lock);
6980 nfsd_forget_delegations(&reaplist);
6981 return count;
6982}
6983
6984static void
6985nfsd_recall_delegations(struct list_head *reaplist)
6986{
6987 struct nfs4_client *clp;
6988 struct nfs4_delegation *dp, *next;
6989
6990 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6991 list_del_init(&dp->dl_recall_lru);
6992 clp = dp->dl_stid.sc_client;
6993 /*
6994 * We skipped all entries that had a zero dl_time before,
6995 * so we can now reset the dl_time back to 0. If a delegation
6996 * break comes in now, then it won't make any difference since
6997 * we're recalling it either way.
6998 */
6999 spin_lock(&state_lock);
7000 dp->dl_time = 0;
7001 spin_unlock(&state_lock);
7002 nfsd_break_one_deleg(dp);
7003 put_client(clp);
7004 }
7005}
7006
7007u64
Jeff Layton285abde2014-07-30 08:27:24 -04007008nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007009 size_t addr_size)
7010{
7011 u64 count = 0;
7012 struct nfs4_client *clp;
7013 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7014 nfsd_net_id);
7015 LIST_HEAD(reaplist);
7016
7017 if (!nfsd_netns_ready(nn))
7018 return count;
7019
7020 spin_lock(&nn->client_lock);
7021 clp = nfsd_find_client(addr, addr_size);
7022 if (clp)
7023 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7024 spin_unlock(&nn->client_lock);
7025
7026 nfsd_recall_delegations(&reaplist);
7027 return count;
7028}
7029
7030u64
Jeff Layton285abde2014-07-30 08:27:24 -04007031nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007032{
7033 u64 count = 0;
7034 struct nfs4_client *clp, *next;
7035 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7036 nfsd_net_id);
7037 LIST_HEAD(reaplist);
7038
7039 if (!nfsd_netns_ready(nn))
7040 return count;
7041
7042 spin_lock(&nn->client_lock);
7043 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7044 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7045 if (max != 0 && ++count >= max)
7046 break;
7047 }
7048 spin_unlock(&nn->client_lock);
7049 nfsd_recall_delegations(&reaplist);
7050 return count;
7051}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007052#endif /* CONFIG_NFSD_FAULT_INJECTION */
7053
Meelap Shahc2f1a552007-07-17 04:04:39 -07007054/*
7055 * Since the lifetime of a delegation isn't limited to that of an open, a
7056 * client may quite reasonably hang on to a delegation as long as it has
7057 * the inode cached. This becomes an obvious problem the first time a
7058 * client's inode cache approaches the size of the server's total memory.
7059 *
7060 * For now we avoid this problem by imposing a hard limit on the number
7061 * of delegations, which varies according to the server's memory size.
7062 */
7063static void
7064set_max_delegations(void)
7065{
7066 /*
7067 * Allow at most 4 delegations per megabyte of RAM. Quick
7068 * estimates suggest that in the worst case (where every delegation
7069 * is for a different inode), a delegation could take about 1.5K,
7070 * giving a worst case usage of about 6% of memory.
7071 */
7072 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7073}
7074
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007075static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007076{
7077 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7078 int i;
7079
7080 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
7081 CLIENT_HASH_SIZE, GFP_KERNEL);
7082 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007083 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007084 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
7085 CLIENT_HASH_SIZE, GFP_KERNEL);
7086 if (!nn->unconf_id_hashtbl)
7087 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007088 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
7089 SESSION_HASH_SIZE, GFP_KERNEL);
7090 if (!nn->sessionid_hashtbl)
7091 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007092
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007093 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007094 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007095 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007096 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007097 for (i = 0; i < SESSION_HASH_SIZE; i++)
7098 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007099 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007100 nn->unconf_name_tree = RB_ROOT;
Vasily Averin3b774232017-11-13 07:25:40 +03007101 nn->boot_time = get_seconds();
7102 nn->grace_ended = false;
7103 nn->nfsd4_manager.block_opens = true;
7104 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007105 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007106 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007107 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007108 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007109
Jeff Layton0cc11a62016-10-20 09:34:31 -04007110 spin_lock_init(&nn->blocked_locks_lock);
7111 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7112
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007113 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007114 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007115
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007116 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007117
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007118err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007119 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007120err_unconf_id:
7121 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007122err:
7123 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007124}
7125
7126static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007127nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007128{
7129 int i;
7130 struct nfs4_client *clp = NULL;
7131 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7132
7133 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7134 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7135 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7136 destroy_client(clp);
7137 }
7138 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007139
Jeff Laytone294c4c2018-03-16 11:32:02 -04007140 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7141
Kinglong Mee2b905632014-03-26 22:09:30 +08007142 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7143 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7144 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7145 destroy_client(clp);
7146 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007147 }
7148
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007149 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007150 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007151 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007152 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007153}
7154
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007155int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007156nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007157{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007158 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007159 int ret;
7160
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007161 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007162 if (ret)
7163 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007164 locks_start_grace(net, &nn->nfsd4_manager);
7165 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007166 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007167 nn->nfsd4_grace, net);
7168 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007169 return 0;
7170}
7171
7172/* initialization to perform when the nfsd service is started: */
7173
7174int
7175nfs4_state_start(void)
7176{
7177 int ret;
7178
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007179 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007180 if (ret)
Kinglong Meed4bda742017-02-05 09:57:07 +08007181 return ret;
7182
Jeff Layton51a54562015-08-20 07:17:01 -04007183 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007184 if (laundry_wq == NULL) {
7185 ret = -ENOMEM;
Kinglong Meed4bda742017-02-05 09:57:07 +08007186 goto out_cleanup_cred;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007187 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007188 ret = nfsd4_create_callback_queue();
7189 if (ret)
7190 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007191
Meelap Shahc2f1a552007-07-17 04:04:39 -07007192 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007193 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007194
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007195out_free_laundry:
7196 destroy_workqueue(laundry_wq);
Kinglong Meed4bda742017-02-05 09:57:07 +08007197out_cleanup_cred:
7198 cleanup_callback_cred();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007199 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200}
7201
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007202void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007203nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007207 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007209 cancel_delayed_work_sync(&nn->laundromat_work);
7210 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007211
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04007213 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007214 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007216 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007217 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 }
Benny Halevycdc97502014-05-30 09:09:30 -04007219 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 list_for_each_safe(pos, next, &reaplist) {
7221 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007222 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02007223 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04007224 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04007225 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 }
7227
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007228 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007229 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230}
7231
7232void
7233nfs4_state_shutdown(void)
7234{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007235 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007236 nfsd4_destroy_callback_queue();
Kinglong Meed4bda742017-02-05 09:57:07 +08007237 cleanup_callback_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007239
7240static void
7241get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7242{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007243 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7244 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007245}
7246
7247static void
7248put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7249{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007250 if (cstate->minorversion) {
7251 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7252 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7253 }
7254}
7255
7256void
7257clear_current_stateid(struct nfsd4_compound_state *cstate)
7258{
7259 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007260}
7261
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007262/*
7263 * functions to set current state id
7264 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007265void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007266nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7267{
7268 put_stateid(cstate, &odp->od_stateid);
7269}
7270
7271void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007272nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
7273{
7274 put_stateid(cstate, &open->op_stateid);
7275}
7276
7277void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007278nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7279{
7280 put_stateid(cstate, &close->cl_stateid);
7281}
7282
7283void
7284nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
7285{
7286 put_stateid(cstate, &lock->lk_resp_stateid);
7287}
7288
7289/*
7290 * functions to consume current state id
7291 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007292
7293void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007294nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7295{
7296 get_stateid(cstate, &odp->od_stateid);
7297}
7298
7299void
7300nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7301{
7302 get_stateid(cstate, &drp->dr_stateid);
7303}
7304
7305void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007306nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7307{
7308 get_stateid(cstate, &fsp->fr_stateid);
7309}
7310
7311void
7312nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7313{
7314 get_stateid(cstate, &setattr->sa_stateid);
7315}
7316
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007317void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007318nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7319{
7320 get_stateid(cstate, &close->cl_stateid);
7321}
7322
7323void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007324nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007325{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007326 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007327}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007328
7329void
7330nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7331{
7332 get_stateid(cstate, &read->rd_stateid);
7333}
7334
7335void
7336nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7337{
7338 get_stateid(cstate, &write->wr_stateid);
7339}