blob: 5c9231d5e14a0cb1e901040af1d9f3e3b335732e [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) {
Vasily Averin0ebb0562020-03-27 07:50:40 +0300249 INIT_LIST_HEAD(&nbl->nbl_list);
250 INIT_LIST_HEAD(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400251 fh_copy_shallow(&nbl->nbl_fh, fh);
252 locks_init_lock(&nbl->nbl_lock);
253 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
254 &nfsd4_cb_notify_lock_ops,
255 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
256 }
257 }
258 return nbl;
259}
260
261static void
262free_blocked_lock(struct nfsd4_blocked_lock *nbl)
263{
264 locks_release_private(&nbl->nbl_lock);
265 kfree(nbl);
266}
267
Jeff Laytone294c4c2018-03-16 11:32:02 -0400268static void
269remove_blocked_locks(struct nfs4_lockowner *lo)
270{
271 struct nfs4_client *clp = lo->lo_owner.so_client;
272 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
273 struct nfsd4_blocked_lock *nbl;
274 LIST_HEAD(reaplist);
275
276 /* Dequeue all blocked locks */
277 spin_lock(&nn->blocked_locks_lock);
278 while (!list_empty(&lo->lo_blocked)) {
279 nbl = list_first_entry(&lo->lo_blocked,
280 struct nfsd4_blocked_lock,
281 nbl_list);
282 list_del_init(&nbl->nbl_list);
283 list_move(&nbl->nbl_lru, &reaplist);
284 }
285 spin_unlock(&nn->blocked_locks_lock);
286
287 /* Now free them */
288 while (!list_empty(&reaplist)) {
289 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
290 nbl_lru);
291 list_del_init(&nbl->nbl_lru);
292 posix_unblock_lock(&nbl->nbl_lock);
293 free_blocked_lock(nbl);
294 }
295}
296
Jeff Layton76d348f2016-09-16 16:28:24 -0400297static int
298nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
299{
300 /*
301 * Since this is just an optimization, we don't try very hard if it
302 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
303 * just quit trying on anything else.
304 */
305 switch (task->tk_status) {
306 case -NFS4ERR_DELAY:
307 rpc_delay(task, 1 * HZ);
308 return 0;
309 default:
310 return 1;
311 }
312}
313
314static void
315nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
316{
317 struct nfsd4_blocked_lock *nbl = container_of(cb,
318 struct nfsd4_blocked_lock, nbl_cb);
319
320 free_blocked_lock(nbl);
321}
322
323static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
324 .done = nfsd4_cb_notify_lock_done,
325 .release = nfsd4_cb_notify_lock_release,
326};
327
Kinglong Meeb5971af2014-08-22 10:18:43 -0400328static inline struct nfs4_stateowner *
329nfs4_get_stateowner(struct nfs4_stateowner *sop)
330{
331 atomic_inc(&sop->so_count);
332 return sop;
333}
334
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400335static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400336same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400337{
338 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400339 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400340}
341
342static struct nfs4_openowner *
343find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400344 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400345{
346 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400347
Trond Myklebustd4f04892014-07-29 21:34:36 -0400348 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400349
Trond Myklebustd4f04892014-07-29 21:34:36 -0400350 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
351 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400352 if (!so->so_is_open_owner)
353 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400354 if (same_owner_str(so, &open->op_owner))
355 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400356 }
357 return NULL;
358}
359
360static struct nfs4_openowner *
361find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400362 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400363{
364 struct nfs4_openowner *oo;
365
Trond Myklebustd4f04892014-07-29 21:34:36 -0400366 spin_lock(&clp->cl_lock);
367 oo = find_openstateowner_str_locked(hashval, open, clp);
368 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400369 return oo;
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static inline u32
373opaque_hashval(const void *ptr, int nbytes)
374{
375 unsigned char *cptr = (unsigned char *) ptr;
376
377 u32 x = 0;
378 while (nbytes--) {
379 x *= 37;
380 x += *cptr++;
381 }
382 return x;
383}
384
Jeff Layton5b095e92014-10-23 08:01:02 -0400385static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400386{
Jeff Layton5b095e92014-10-23 08:01:02 -0400387 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
388
389 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400390}
391
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200392void
NeilBrown13cd2182005-06-23 22:03:10 -0700393put_nfs4_file(struct nfs4_file *fi)
394{
Jeff Layton02e12152014-07-16 10:31:57 -0400395 might_lock(&state_lock);
396
Benny Halevycdc97502014-05-30 09:09:30 -0400397 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400398 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400399 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200400 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400401 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
402 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800403 }
NeilBrown13cd2182005-06-23 22:03:10 -0700404}
405
Trond Myklebustde186432014-07-10 14:07:26 -0400406static struct file *
407__nfs4_get_fd(struct nfs4_file *f, int oflag)
408{
409 if (f->fi_fds[oflag])
410 return get_file(f->fi_fds[oflag]);
411 return NULL;
412}
413
414static struct file *
415find_writeable_file_locked(struct nfs4_file *f)
416{
417 struct file *ret;
418
419 lockdep_assert_held(&f->fi_lock);
420
421 ret = __nfs4_get_fd(f, O_WRONLY);
422 if (!ret)
423 ret = __nfs4_get_fd(f, O_RDWR);
424 return ret;
425}
426
427static struct file *
428find_writeable_file(struct nfs4_file *f)
429{
430 struct file *ret;
431
432 spin_lock(&f->fi_lock);
433 ret = find_writeable_file_locked(f);
434 spin_unlock(&f->fi_lock);
435
436 return ret;
437}
438
439static struct file *find_readable_file_locked(struct nfs4_file *f)
440{
441 struct file *ret;
442
443 lockdep_assert_held(&f->fi_lock);
444
445 ret = __nfs4_get_fd(f, O_RDONLY);
446 if (!ret)
447 ret = __nfs4_get_fd(f, O_RDWR);
448 return ret;
449}
450
451static struct file *
452find_readable_file(struct nfs4_file *f)
453{
454 struct file *ret;
455
456 spin_lock(&f->fi_lock);
457 ret = find_readable_file_locked(f);
458 spin_unlock(&f->fi_lock);
459
460 return ret;
461}
462
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500463struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400464find_any_file(struct nfs4_file *f)
465{
466 struct file *ret;
467
468 spin_lock(&f->fi_lock);
469 ret = __nfs4_get_fd(f, O_RDWR);
470 if (!ret) {
471 ret = __nfs4_get_fd(f, O_WRONLY);
472 if (!ret)
473 ret = __nfs4_get_fd(f, O_RDONLY);
474 }
475 spin_unlock(&f->fi_lock);
476 return ret;
477}
478
Trond Myklebust02a35082014-07-25 07:34:22 -0400479static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800480unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700481
482/*
483 * Open owner state (share locks)
484 */
485
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500486/* hash tables for lock and open owners */
487#define OWNER_HASH_BITS 8
488#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
489#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700490
Trond Myklebustd4f04892014-07-29 21:34:36 -0400491static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400492{
493 unsigned int ret;
494
495 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500496 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400497}
NeilBrownef0f3392006-04-10 22:55:41 -0700498
NeilBrownef0f3392006-04-10 22:55:41 -0700499/* hash table for nfs4_file */
500#define FILE_HASH_BITS 8
501#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800502
Trond Myklebustca943212014-07-23 16:17:39 -0400503static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400504{
Trond Myklebustca943212014-07-23 16:17:39 -0400505 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
506}
507
508static unsigned int file_hashval(struct knfsd_fh *fh)
509{
510 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
511}
512
Jeff Layton89876f82013-04-02 09:01:59 -0400513static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700514
Jeff Layton12659652014-07-10 14:07:28 -0400515static void
516__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400517{
Jeff Layton7214e862014-07-10 14:07:33 -0400518 lockdep_assert_held(&fp->fi_lock);
519
Jeff Layton12659652014-07-10 14:07:28 -0400520 if (access & NFS4_SHARE_ACCESS_WRITE)
521 atomic_inc(&fp->fi_access[O_WRONLY]);
522 if (access & NFS4_SHARE_ACCESS_READ)
523 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400524}
525
Jeff Layton12659652014-07-10 14:07:28 -0400526static __be32
527nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400528{
Jeff Layton7214e862014-07-10 14:07:33 -0400529 lockdep_assert_held(&fp->fi_lock);
530
Jeff Layton12659652014-07-10 14:07:28 -0400531 /* Does this access mode make sense? */
532 if (access & ~NFS4_SHARE_ACCESS_BOTH)
533 return nfserr_inval;
534
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400535 /* Does it conflict with a deny mode already set? */
536 if ((access & fp->fi_share_deny) != 0)
537 return nfserr_share_denied;
538
Jeff Layton12659652014-07-10 14:07:28 -0400539 __nfs4_file_get_access(fp, access);
540 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400541}
542
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400543static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
544{
545 /* Common case is that there is no deny mode. */
546 if (deny) {
547 /* Does this deny mode make sense? */
548 if (deny & ~NFS4_SHARE_DENY_BOTH)
549 return nfserr_inval;
550
551 if ((deny & NFS4_SHARE_DENY_READ) &&
552 atomic_read(&fp->fi_access[O_RDONLY]))
553 return nfserr_share_denied;
554
555 if ((deny & NFS4_SHARE_DENY_WRITE) &&
556 atomic_read(&fp->fi_access[O_WRONLY]))
557 return nfserr_share_denied;
558 }
559 return nfs_ok;
560}
561
J. Bruce Fields998db522010-08-07 09:21:41 -0400562static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400563{
Trond Myklebustde186432014-07-10 14:07:26 -0400564 might_lock(&fp->fi_lock);
565
566 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
567 struct file *f1 = NULL;
568 struct file *f2 = NULL;
569
Jeff Layton6d338b52014-07-10 14:07:29 -0400570 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400571 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400572 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400573 spin_unlock(&fp->fi_lock);
574 if (f1)
575 fput(f1);
576 if (f2)
577 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400578 }
579}
580
Jeff Layton12659652014-07-10 14:07:28 -0400581static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400582{
Jeff Layton12659652014-07-10 14:07:28 -0400583 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
584
585 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400586 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400587 if (access & NFS4_SHARE_ACCESS_READ)
588 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400589}
590
Sachin Bhamare8287f002015-04-27 14:50:14 +0200591/*
592 * Allocate a new open/delegation state counter. This is needed for
593 * pNFS for proper return on close semantics.
594 *
595 * Note that we only allocate it for pNFS-enabled exports, otherwise
596 * all pointers to struct nfs4_clnt_odstate are always NULL.
597 */
598static struct nfs4_clnt_odstate *
599alloc_clnt_odstate(struct nfs4_client *clp)
600{
601 struct nfs4_clnt_odstate *co;
602
603 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
604 if (co) {
605 co->co_client = clp;
606 atomic_set(&co->co_odcount, 1);
607 }
608 return co;
609}
610
611static void
612hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
613{
614 struct nfs4_file *fp = co->co_file;
615
616 lockdep_assert_held(&fp->fi_lock);
617 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
618}
619
620static inline void
621get_clnt_odstate(struct nfs4_clnt_odstate *co)
622{
623 if (co)
624 atomic_inc(&co->co_odcount);
625}
626
627static void
628put_clnt_odstate(struct nfs4_clnt_odstate *co)
629{
630 struct nfs4_file *fp;
631
632 if (!co)
633 return;
634
635 fp = co->co_file;
636 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
637 list_del(&co->co_perfile);
638 spin_unlock(&fp->fi_lock);
639
640 nfsd4_return_all_file_layouts(co->co_client, fp);
641 kmem_cache_free(odstate_slab, co);
642 }
643}
644
645static struct nfs4_clnt_odstate *
646find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
647{
648 struct nfs4_clnt_odstate *co;
649 struct nfs4_client *cl;
650
651 if (!new)
652 return NULL;
653
654 cl = new->co_client;
655
656 spin_lock(&fp->fi_lock);
657 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
658 if (co->co_client == cl) {
659 get_clnt_odstate(co);
660 goto out;
661 }
662 }
663 co = new;
664 co->co_file = fp;
665 hash_clnt_odstate_locked(new);
666out:
667 spin_unlock(&fp->fi_lock);
668 return co;
669}
670
Kinglong Mee743146d32017-01-18 19:04:42 +0800671struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
672 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400673{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500674 struct nfs4_stid *stid;
675 int new_id;
676
Trond Myklebustf8338832014-07-25 07:34:19 -0400677 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500678 if (!stid)
679 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400680
Jeff Layton4770d722014-07-29 21:34:10 -0400681 idr_preload(GFP_KERNEL);
682 spin_lock(&cl->cl_lock);
683 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
684 spin_unlock(&cl->cl_lock);
685 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700686 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500687 goto out_free;
Kinglong Mee743146d32017-01-18 19:04:42 +0800688
689 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500690 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500691 stid->sc_stateid.si_opaque.so_id = new_id;
692 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
693 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400694 atomic_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400695 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500696
J. Bruce Fields996e0932011-10-17 11:14:48 -0400697 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500698 * It shouldn't be a problem to reuse an opaque stateid value.
699 * I don't think it is for 4.1. But with 4.0 I worry that, for
700 * example, a stray write retransmission could be accepted by
701 * the server when it should have been rejected. Therefore,
702 * adopt a trick from the sctp code to attempt to maximize the
703 * amount of time until an id is reused, by ensuring they always
704 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400705 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500706 return stid;
707out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800708 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500709 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400710}
711
Jeff Laytonb49e0842014-07-29 21:34:11 -0400712static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400713{
Trond Myklebust60116952014-07-29 21:34:06 -0400714 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400715
Kinglong Mee743146d32017-01-18 19:04:42 +0800716 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400717 if (!stid)
718 return NULL;
719
Kinglong Mee743146d32017-01-18 19:04:42 +0800720 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400721}
722
723static void nfs4_free_deleg(struct nfs4_stid *stid)
724{
Trond Myklebust60116952014-07-29 21:34:06 -0400725 kmem_cache_free(deleg_slab, stid);
726 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400727}
728
NeilBrown6282cd52014-06-04 17:39:26 +1000729/*
730 * When we recall a delegation, we should be careful not to hand it
731 * out again straight away.
732 * To ensure this we keep a pair of bloom filters ('new' and 'old')
733 * in which the filehandles of recalled delegations are "stored".
734 * If a filehandle appear in either filter, a delegation is blocked.
735 * When a delegation is recalled, the filehandle is stored in the "new"
736 * filter.
737 * Every 30 seconds we swap the filters and clear the "new" one,
738 * unless both are empty of course.
739 *
740 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
741 * low 3 bytes as hash-table indices.
742 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400743 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000744 * is used to manage concurrent access. Testing does not need the lock
745 * except when swapping the two filters.
746 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400747static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000748static struct bloom_pair {
749 int entries, old_entries;
750 time_t swap_time;
751 int new; /* index into 'set' */
752 DECLARE_BITMAP(set[2], 256);
753} blocked_delegations;
754
755static int delegation_blocked(struct knfsd_fh *fh)
756{
757 u32 hash;
758 struct bloom_pair *bd = &blocked_delegations;
759
760 if (bd->entries == 0)
761 return 0;
762 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400763 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000764 if (seconds_since_boot() - bd->swap_time > 30) {
765 bd->entries -= bd->old_entries;
766 bd->old_entries = bd->entries;
767 memset(bd->set[bd->new], 0,
768 sizeof(bd->set[0]));
769 bd->new = 1-bd->new;
770 bd->swap_time = seconds_since_boot();
771 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400772 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000773 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100774 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000775 if (test_bit(hash&255, bd->set[0]) &&
776 test_bit((hash>>8)&255, bd->set[0]) &&
777 test_bit((hash>>16)&255, bd->set[0]))
778 return 1;
779
780 if (test_bit(hash&255, bd->set[1]) &&
781 test_bit((hash>>8)&255, bd->set[1]) &&
782 test_bit((hash>>16)&255, bd->set[1]))
783 return 1;
784
785 return 0;
786}
787
788static void block_delegations(struct knfsd_fh *fh)
789{
790 u32 hash;
791 struct bloom_pair *bd = &blocked_delegations;
792
Daniel Borkmann87545892014-12-10 16:33:11 +0100793 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000794
Jeff Laytonf54fe962014-07-25 07:34:26 -0400795 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000796 __set_bit(hash&255, bd->set[bd->new]);
797 __set_bit((hash>>8)&255, bd->set[bd->new]);
798 __set_bit((hash>>16)&255, bd->set[bd->new]);
799 if (bd->entries == 0)
800 bd->swap_time = seconds_since_boot();
801 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400802 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static struct nfs4_delegation *
Sachin Bhamare8287f002015-04-27 14:50:14 +0200806alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
807 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400810 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400813 n = atomic_long_inc_return(&num_delegations);
814 if (n < 0 || n > max_delegations)
815 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000816 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400817 goto out_dec;
Kinglong Mee743146d32017-01-18 19:04:42 +0800818 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700819 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400820 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400821
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400822 /*
823 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400824 * meaning of seqid 0 isn't meaningful, really, but let's avoid
825 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400826 */
827 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700828 INIT_LIST_HEAD(&dp->dl_perfile);
829 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200831 dp->dl_clnt_odstate = odstate;
832 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400833 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200834 dp->dl_retries = 1;
835 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200836 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400838out_dec:
839 atomic_long_dec(&num_delegations);
840 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843void
Trond Myklebust60116952014-07-29 21:34:06 -0400844nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Trond Myklebust11b91642014-07-29 21:34:08 -0400846 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400847 struct nfs4_client *clp = s->sc_client;
848
Jeff Layton4770d722014-07-29 21:34:10 -0400849 might_lock(&clp->cl_lock);
850
Jeff Laytonb401be222014-07-29 21:34:33 -0400851 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
852 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400853 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400854 }
Trond Myklebust60116952014-07-29 21:34:06 -0400855 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400856 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400857 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400858 if (fp)
859 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Jeff Layton9767feb2015-10-01 09:05:50 -0400862void
863nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
864{
865 stateid_t *src = &stid->sc_stateid;
866
867 spin_lock(&stid->sc_lock);
868 if (unlikely(++src->si_generation == 0))
869 src->si_generation = 1;
870 memcpy(dst, src, sizeof(*dst));
871 spin_unlock(&stid->sc_lock);
872}
873
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500874static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400876 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400877
Jeff Layton6bcc0342014-08-09 10:22:40 -0400878 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500879 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400880 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400881 spin_unlock(&fp->fi_lock);
882
883 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100884 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400885 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200889void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400890{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500891 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400892}
893
Andrew Elble34ed9872015-10-15 12:07:28 -0400894/**
895 * nfs4_get_existing_delegation - Discover if this delegation already exists
896 * @clp: a pointer to the nfs4_client we're granting a delegation to
897 * @fp: a pointer to the nfs4_file we're granting a delegation on
898 *
899 * Return:
900 * On success: NULL if an existing delegation was not found.
901 *
902 * On error: -EAGAIN if one was previously granted to this nfs4_client
903 * for this nfs4_file.
904 *
905 */
906
907static int
908nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400909{
Andrew Elble34ed9872015-10-15 12:07:28 -0400910 struct nfs4_delegation *searchdp = NULL;
911 struct nfs4_client *searchclp = NULL;
912
Benny Halevycdc97502014-05-30 09:09:30 -0400913 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400914 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400915
Andrew Elble34ed9872015-10-15 12:07:28 -0400916 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
917 searchclp = searchdp->dl_stid.sc_client;
918 if (clp == searchclp) {
919 return -EAGAIN;
920 }
921 }
922 return 0;
923}
924
925/**
926 * hash_delegation_locked - Add a delegation to the appropriate lists
927 * @dp: a pointer to the nfs4_delegation we are adding.
928 * @fp: a pointer to the nfs4_file we're granting a delegation on
929 *
930 * Return:
931 * On success: NULL if the delegation was successfully hashed.
932 *
933 * On error: -EAGAIN if one was previously granted to this
934 * nfs4_client for this nfs4_file. Delegation is not hashed.
935 *
936 */
937
938static int
939hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
940{
941 int status;
942 struct nfs4_client *clp = dp->dl_stid.sc_client;
943
944 lockdep_assert_held(&state_lock);
945 lockdep_assert_held(&fp->fi_lock);
946
947 status = nfs4_get_existing_delegation(clp, fp);
948 if (status)
949 return status;
950 ++fp->fi_delegees;
Trond Myklebust67cb1272014-07-29 21:34:17 -0400951 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400952 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400953 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -0400954 list_add(&dp->dl_perclnt, &clp->cl_delegations);
955 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -0400956}
957
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400958static bool
Jeff Layton42690672014-07-25 07:34:20 -0400959unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Trond Myklebust11b91642014-07-29 21:34:08 -0400961 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400962
Jeff Layton42690672014-07-25 07:34:20 -0400963 lockdep_assert_held(&state_lock);
964
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400965 if (list_empty(&dp->dl_perfile))
966 return false;
967
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400968 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400969 /* Ensure that deleg break won't try to requeue it */
970 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400971 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400972 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400974 list_del_init(&dp->dl_perfile);
975 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400976 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400977}
978
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400979static void destroy_delegation(struct nfs4_delegation *dp)
980{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400981 bool unhashed;
982
Jeff Layton42690672014-07-25 07:34:20 -0400983 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400984 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400985 spin_unlock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400986 if (unhashed) {
987 put_clnt_odstate(dp->dl_clnt_odstate);
988 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
989 nfs4_put_stid(&dp->dl_stid);
990 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400991}
992
993static void revoke_delegation(struct nfs4_delegation *dp)
994{
995 struct nfs4_client *clp = dp->dl_stid.sc_client;
996
Jeff Layton2d4a5322014-07-25 07:34:21 -0400997 WARN_ON(!list_empty(&dp->dl_recall_lru));
998
Sachin Bhamare8287f002015-04-27 14:50:14 +0200999 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001000 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1001
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001002 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -04001003 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001004 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001005 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -04001006 spin_lock(&clp->cl_lock);
1007 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1008 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001009 }
1010}
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012/*
1013 * SETCLIENTID state
1014 */
1015
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001016static unsigned int clientid_hashval(u32 id)
1017{
1018 return id & CLIENT_HASH_MASK;
1019}
1020
1021static unsigned int clientstr_hashval(const char *name)
1022{
1023 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001027 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1028 * st_{access,deny}_bmap field of the stateid, in order to track not
1029 * only what share bits are currently in force, but also what
1030 * combinations of share bits previous opens have used. This allows us
1031 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1032 * return an error if the client attempt to downgrade to a combination
1033 * of share bits not explicable by closing some of its previous opens.
1034 *
1035 * XXX: This enforcement is actually incomplete, since we don't keep
1036 * track of access/deny bit combinations; so, e.g., we allow:
1037 *
1038 * OPEN allow read, deny write
1039 * OPEN allow both, deny none
1040 * DOWNGRADE allow read, deny none
1041 *
1042 * which we should reject.
1043 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001044static unsigned int
1045bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001046 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001047 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001048
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001049 for (i = 1; i < 4; i++) {
1050 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001051 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001052 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001053 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001054}
1055
Jeff Layton82c5ff12012-05-11 09:45:13 -04001056/* set share access for a given stateid */
1057static inline void
1058set_access(u32 access, struct nfs4_ol_stateid *stp)
1059{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001060 unsigned char mask = 1 << access;
1061
1062 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1063 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001064}
1065
1066/* clear share access for a given stateid */
1067static inline void
1068clear_access(u32 access, struct nfs4_ol_stateid *stp)
1069{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001070 unsigned char mask = 1 << access;
1071
1072 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1073 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001074}
1075
1076/* test whether a given stateid has access */
1077static inline bool
1078test_access(u32 access, struct nfs4_ol_stateid *stp)
1079{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001080 unsigned char mask = 1 << access;
1081
1082 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001083}
1084
Jeff Laytonce0fc432012-05-11 09:45:14 -04001085/* set share deny for a given stateid */
1086static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001087set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001088{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001089 unsigned char mask = 1 << deny;
1090
1091 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1092 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001093}
1094
1095/* clear share deny for a given stateid */
1096static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001097clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001098{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001099 unsigned char mask = 1 << deny;
1100
1101 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1102 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001103}
1104
1105/* test whether a given stateid is denying specific access */
1106static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001107test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001108{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001109 unsigned char mask = 1 << deny;
1110
1111 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001112}
1113
1114static int nfs4_access_to_omode(u32 access)
1115{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001116 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001117 case NFS4_SHARE_ACCESS_READ:
1118 return O_RDONLY;
1119 case NFS4_SHARE_ACCESS_WRITE:
1120 return O_WRONLY;
1121 case NFS4_SHARE_ACCESS_BOTH:
1122 return O_RDWR;
1123 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001124 WARN_ON_ONCE(1);
1125 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001126}
1127
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001128/*
1129 * A stateid that had a deny mode associated with it is being released
1130 * or downgraded. Recalculate the deny mode on the file.
1131 */
1132static void
1133recalculate_deny_mode(struct nfs4_file *fp)
1134{
1135 struct nfs4_ol_stateid *stp;
1136
1137 spin_lock(&fp->fi_lock);
1138 fp->fi_share_deny = 0;
1139 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1140 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1141 spin_unlock(&fp->fi_lock);
1142}
1143
1144static void
1145reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1146{
1147 int i;
1148 bool change = false;
1149
1150 for (i = 1; i < 4; i++) {
1151 if ((i & deny) != i) {
1152 change = true;
1153 clear_deny(i, stp);
1154 }
1155 }
1156
1157 /* Recalculate per-file deny mode if there was a change */
1158 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001159 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001160}
1161
Jeff Layton82c5ff12012-05-11 09:45:13 -04001162/* release all access and file references for a given stateid */
1163static void
1164release_all_access(struct nfs4_ol_stateid *stp)
1165{
1166 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001167 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001168
1169 if (fp && stp->st_deny_bmap != 0)
1170 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001171
1172 for (i = 1; i < 4; i++) {
1173 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001174 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001175 clear_access(i, stp);
1176 }
1177}
1178
Kinglong Meed50ffde2015-07-16 12:05:07 +08001179static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1180{
1181 kfree(sop->so_owner.data);
1182 sop->so_ops->so_free(sop);
1183}
1184
Jeff Layton6b180f02014-07-29 21:34:26 -04001185static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1186{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001187 struct nfs4_client *clp = sop->so_client;
1188
1189 might_lock(&clp->cl_lock);
1190
1191 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001192 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001193 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001194 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001195 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001196}
1197
Jeff Laytone8568732015-08-24 12:41:47 -04001198static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001199{
Trond Myklebust11b91642014-07-29 21:34:08 -04001200 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001201
Jeff Layton1c755dc2014-07-29 21:34:12 -04001202 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1203
Jeff Laytone8568732015-08-24 12:41:47 -04001204 if (list_empty(&stp->st_perfile))
1205 return false;
1206
Trond Myklebust1d31a252014-07-10 14:07:25 -04001207 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001208 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001209 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001210 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001211 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001212}
1213
Trond Myklebust60116952014-07-29 21:34:06 -04001214static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001215{
Trond Myklebust60116952014-07-29 21:34:06 -04001216 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001217
Sachin Bhamare8287f002015-04-27 14:50:14 +02001218 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001219 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001220 if (stp->st_stateowner)
1221 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001222 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001223}
1224
Jeff Laytonb49e0842014-07-29 21:34:11 -04001225static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001226{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001227 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1228 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001229 struct file *file;
1230
Jeff Laytonb49e0842014-07-29 21:34:11 -04001231 file = find_any_file(stp->st_stid.sc_file);
1232 if (file)
1233 filp_close(file, (fl_owner_t)lo);
1234 nfs4_free_ol_stateid(stid);
1235}
1236
Jeff Layton2c41beb2014-07-29 21:34:41 -04001237/*
1238 * Put the persistent reference to an already unhashed generic stateid, while
1239 * holding the cl_lock. If it's the last reference, then put it onto the
1240 * reaplist for later destruction.
1241 */
1242static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1243 struct list_head *reaplist)
1244{
1245 struct nfs4_stid *s = &stp->st_stid;
1246 struct nfs4_client *clp = s->sc_client;
1247
1248 lockdep_assert_held(&clp->cl_lock);
1249
1250 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1251
1252 if (!atomic_dec_and_test(&s->sc_count)) {
1253 wake_up_all(&close_wq);
1254 return;
1255 }
1256
1257 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1258 list_add(&stp->st_locks, reaplist);
1259}
1260
Jeff Laytone8568732015-08-24 12:41:47 -04001261static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001262{
Chuck Leverf46c4452016-10-29 18:19:03 -04001263 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001264
1265 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001266 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001267 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001268}
1269
Jeff Layton5adfd882014-07-29 21:34:31 -04001270static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001271{
Chuck Leverf46c4452016-10-29 18:19:03 -04001272 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001273 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001274
Chuck Leverf46c4452016-10-29 18:19:03 -04001275 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001276 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001277 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001278 if (unhashed)
1279 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001280}
1281
Trond Myklebustc58c6612014-07-29 21:34:35 -04001282static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001283{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001284 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001285
Trond Myklebustd4f04892014-07-29 21:34:36 -04001286 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001287
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001288 list_del_init(&lo->lo_owner.so_strhash);
1289}
1290
Jeff Layton2c41beb2014-07-29 21:34:41 -04001291/*
1292 * Free a list of generic stateids that were collected earlier after being
1293 * fully unhashed.
1294 */
1295static void
1296free_ol_stateid_reaplist(struct list_head *reaplist)
1297{
1298 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001299 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001300
1301 might_sleep();
1302
1303 while (!list_empty(reaplist)) {
1304 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1305 st_locks);
1306 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001307 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001308 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001309 if (fp)
1310 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001311 }
1312}
1313
Jeff Laytond83017f2014-07-29 21:34:42 -04001314static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1315 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001316{
1317 struct nfs4_ol_stateid *stp;
1318
Jeff Laytone8568732015-08-24 12:41:47 -04001319 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1320
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001321 while (!list_empty(&open_stp->st_locks)) {
1322 stp = list_entry(open_stp->st_locks.next,
1323 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001324 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001325 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001326 }
1327}
1328
Jeff Laytone8568732015-08-24 12:41:47 -04001329static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001330 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001331{
Jeff Laytone8568732015-08-24 12:41:47 -04001332 bool unhashed;
1333
Jeff Layton2c41beb2014-07-29 21:34:41 -04001334 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1335
Jeff Laytone8568732015-08-24 12:41:47 -04001336 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001337 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001338 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001339}
1340
1341static void release_open_stateid(struct nfs4_ol_stateid *stp)
1342{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001343 LIST_HEAD(reaplist);
1344
1345 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001346 if (unhash_open_stateid(stp, &reaplist))
1347 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001348 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1349 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001350}
1351
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001352static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001353{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001354 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001355
Trond Myklebustd4f04892014-07-29 21:34:36 -04001356 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001357
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001358 list_del_init(&oo->oo_owner.so_strhash);
1359 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001360}
1361
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001362static void release_last_closed_stateid(struct nfs4_openowner *oo)
1363{
Jeff Layton217526e2014-07-30 08:27:11 -04001364 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1365 nfsd_net_id);
1366 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001367
Jeff Layton217526e2014-07-30 08:27:11 -04001368 spin_lock(&nn->client_lock);
1369 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001370 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001371 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001372 oo->oo_last_closed_stid = NULL;
1373 }
Jeff Layton217526e2014-07-30 08:27:11 -04001374 spin_unlock(&nn->client_lock);
1375 if (s)
1376 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001377}
1378
Jeff Layton2c41beb2014-07-29 21:34:41 -04001379static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001380{
1381 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001382 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001383 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001384
Jeff Layton2c41beb2014-07-29 21:34:41 -04001385 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001386
Trond Myklebustd4f04892014-07-29 21:34:36 -04001387 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001388 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001389 while (!list_empty(&oo->oo_owner.so_stateids)) {
1390 stp = list_first_entry(&oo->oo_owner.so_stateids,
1391 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001392 if (unhash_open_stateid(stp, &reaplist))
1393 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001394 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001395 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001396 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001397 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001398 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001399}
1400
Marc Eshel5282fd72009-04-03 08:27:52 +03001401static inline int
1402hash_sessionid(struct nfs4_sessionid *sessionid)
1403{
1404 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1405
1406 return sid->sequence % SESSION_HASH_SIZE;
1407}
1408
Mark Salter135dd002015-04-06 09:46:00 -04001409#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001410static inline void
1411dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1412{
1413 u32 *ptr = (u32 *)(&sessionid->data[0]);
1414 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1415}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001416#else
1417static inline void
1418dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1419{
1420}
1421#endif
1422
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001423/*
1424 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1425 * won't be used for replay.
1426 */
1427void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1428{
1429 struct nfs4_stateowner *so = cstate->replay_owner;
1430
1431 if (nfserr == nfserr_replay_me)
1432 return;
1433
1434 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001435 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001436 return;
1437 }
1438 if (!so)
1439 return;
1440 if (so->so_is_open_owner)
1441 release_last_closed_stateid(openowner(so));
1442 so->so_seqid++;
1443 return;
1444}
Marc Eshel5282fd72009-04-03 08:27:52 +03001445
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001446static void
1447gen_sessionid(struct nfsd4_session *ses)
1448{
1449 struct nfs4_client *clp = ses->se_client;
1450 struct nfsd4_sessionid *sid;
1451
1452 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1453 sid->clientid = clp->cl_clientid;
1454 sid->sequence = current_sessionid++;
1455 sid->reserved = 0;
1456}
1457
1458/*
Andy Adamsona6496372009-08-28 08:45:01 -04001459 * The protocol defines ca_maxresponssize_cached to include the size of
1460 * the rpc header, but all we need to cache is the data starting after
1461 * the end of the initial SEQUENCE operation--the rest we regenerate
1462 * each time. Therefore we can advertise a ca_maxresponssize_cached
1463 * value that is the number of bytes in our cache plus a few additional
1464 * bytes. In order to stay on the safe side, and not promise more than
1465 * we can cache, those additional bytes must be the minimum possible: 24
1466 * bytes of rpc header (xid through accept state, with AUTH_NULL
1467 * verifier), 12 for the compound header (with zero-length tag), and 44
1468 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001469 */
Andy Adamsona6496372009-08-28 08:45:01 -04001470#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1471
Andy Adamson557ce262009-08-28 08:45:04 -04001472static void
1473free_session_slots(struct nfsd4_session *ses)
1474{
1475 int i;
1476
J. Bruce Fields877362f2017-10-17 20:38:49 -04001477 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1478 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001479 kfree(ses->se_slots[i]);
J. Bruce Fields877362f2017-10-17 20:38:49 -04001480 }
Andy Adamson557ce262009-08-28 08:45:04 -04001481}
1482
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001483/*
1484 * We don't actually need to cache the rpc and session headers, so we
1485 * can allocate a little less for each slot:
1486 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001487static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001488{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001489 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001490
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001491 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1492 size = 0;
1493 else
1494 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1495 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001496}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001497
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001498/*
1499 * XXX: If we run out of reserved DRC memory we could (up to a point)
1500 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001501 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001502 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001503static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001504{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001505 u32 slotsize = slot_bytes(ca);
1506 u32 num = ca->maxreqs;
J. Bruce Fieldscdc106c6c2019-02-21 10:47:00 -05001507 unsigned long avail, total_avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001508
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001509 spin_lock(&nfsd_drc_lock);
J. Bruce Fieldscdc106c6c2019-02-21 10:47:00 -05001510 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1511 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fields6d548072017-09-19 19:25:41 -04001512 /*
1513 * Never use more than a third of the remaining memory,
1514 * unless it's the only way to give this client a slot:
1515 */
Paul Menzel7546e0c2019-07-03 13:28:15 +02001516 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001517 num = min_t(int, num, avail / slotsize);
1518 nfsd_drc_mem_used += num * slotsize;
1519 spin_unlock(&nfsd_drc_lock);
1520
1521 return num;
1522}
1523
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001524static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001525{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001526 int slotsize = slot_bytes(ca);
1527
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001528 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001529 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001530 spin_unlock(&nfsd_drc_lock);
1531}
1532
Kinglong Mee60810e52014-01-01 00:35:47 +08001533static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1534 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001535{
Kinglong Mee60810e52014-01-01 00:35:47 +08001536 int numslots = fattrs->maxreqs;
1537 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001538 struct nfsd4_session *new;
1539 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001540
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001541 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001542 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1543 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001544
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001545 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001546 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001547 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001548 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001549 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001550 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001551 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001552 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001553 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001554
1555 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1556 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1557
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001558 return new;
1559out_free:
1560 while (i--)
1561 kfree(new->se_slots[i]);
1562 kfree(new);
1563 return NULL;
1564}
1565
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001566static void free_conn(struct nfsd4_conn *c)
1567{
1568 svc_xprt_put(c->cn_xprt);
1569 kfree(c);
1570}
1571
1572static void nfsd4_conn_lost(struct svc_xpt_user *u)
1573{
1574 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1575 struct nfs4_client *clp = c->cn_session->se_client;
1576
1577 spin_lock(&clp->cl_lock);
1578 if (!list_empty(&c->cn_persession)) {
1579 list_del(&c->cn_persession);
1580 free_conn(c);
1581 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001582 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001583 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001584}
1585
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001586static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001587{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001588 struct nfsd4_conn *conn;
1589
1590 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1591 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001592 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001593 svc_xprt_get(rqstp->rq_xprt);
1594 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001595 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001596 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1597 return conn;
1598}
1599
J. Bruce Fields328ead22010-09-29 16:11:06 -04001600static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1601{
1602 conn->cn_session = ses;
1603 list_add(&conn->cn_persession, &ses->se_conns);
1604}
1605
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001606static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1607{
1608 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001609
1610 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001611 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001612 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001613}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001614
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001615static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001616{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001617 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001618 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001619}
1620
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001621static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001622{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001623 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001624
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001625 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001626 ret = nfsd4_register_conn(conn);
1627 if (ret)
1628 /* oops; xprt is already down: */
1629 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001630 /* We may have gained or lost a callback channel: */
1631 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001632}
1633
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001634static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001635{
1636 u32 dir = NFS4_CDFC4_FORE;
1637
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001638 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001639 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001640 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001641}
1642
1643/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001644static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001645{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001646 struct nfs4_client *clp = s->se_client;
1647 struct nfsd4_conn *c;
1648
1649 spin_lock(&clp->cl_lock);
1650 while (!list_empty(&s->se_conns)) {
1651 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1652 list_del_init(&c->cn_persession);
1653 spin_unlock(&clp->cl_lock);
1654
1655 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1656 free_conn(c);
1657
1658 spin_lock(&clp->cl_lock);
1659 }
1660 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001661}
1662
J. Bruce Fields1377b692012-09-11 21:42:40 -04001663static void __free_session(struct nfsd4_session *ses)
1664{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001665 free_session_slots(ses);
1666 kfree(ses);
1667}
1668
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001669static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001670{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001671 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001672 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001673 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001674}
Andy Adamson557ce262009-08-28 08:45:04 -04001675
Fengguang Wu135ae822012-11-10 07:20:25 -05001676static 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 -04001677{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001678 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001679 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001680
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001681 new->se_client = clp;
1682 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001683
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001684 INIT_LIST_HEAD(&new->se_conns);
1685
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001686 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001687 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001688 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001689 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001690 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001691 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001692 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001693 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001694 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001695 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001696
Chuck Leverb0d2e422014-08-22 15:10:59 -04001697 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001698 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001699 /*
1700 * This is a little silly; with sessions there's no real
1701 * use for the callback address. Use the peer address
1702 * as a reasonable default for now, but consider fixing
1703 * the rpc client not to require an address in the
1704 * future:
1705 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001706 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1707 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001708 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001709}
1710
Benny Halevy9089f1b2010-05-12 00:12:26 +03001711/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001712static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001713__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001714{
1715 struct nfsd4_session *elem;
1716 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001717 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001718
Trond Myklebust0a880a22014-07-30 08:27:10 -04001719 lockdep_assert_held(&nn->client_lock);
1720
Marc Eshel5282fd72009-04-03 08:27:52 +03001721 dump_sessionid(__func__, sessionid);
1722 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001723 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001724 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001725 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1726 NFS4_MAX_SESSIONID_LEN)) {
1727 return elem;
1728 }
1729 }
1730
1731 dprintk("%s: session not found\n", __func__);
1732 return NULL;
1733}
1734
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001735static struct nfsd4_session *
1736find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1737 __be32 *ret)
1738{
1739 struct nfsd4_session *session;
1740 __be32 status = nfserr_badsession;
1741
1742 session = __find_in_sessionid_hashtbl(sessionid, net);
1743 if (!session)
1744 goto out;
1745 status = nfsd4_get_session_locked(session);
1746 if (status)
1747 session = NULL;
1748out:
1749 *ret = status;
1750 return session;
1751}
1752
Benny Halevy9089f1b2010-05-12 00:12:26 +03001753/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001754static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001755unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001756{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001757 struct nfs4_client *clp = ses->se_client;
1758 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1759
1760 lockdep_assert_held(&nn->client_lock);
1761
Andy Adamson7116ed62009-04-03 08:27:43 +03001762 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001763 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001764 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001765 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1769static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001770STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001772 /*
1773 * We're assuming the clid was not given out from a boot
1774 * precisely 2^32 (about 136 years) before this one. That seems
1775 * a safe assumption:
1776 */
1777 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001779 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001780 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return 1;
1782}
1783
1784/*
1785 * XXX Should we use a slab cache ?
1786 * This type of memory management is somewhat inefficient, but we use it
1787 * anyway since SETCLIENTID is not a common operation.
1788 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001789static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001792 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001794 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1795 if (clp == NULL)
1796 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001797 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001798 if (clp->cl_name.data == NULL)
1799 goto err_no_name;
1800 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1801 OWNER_HASH_SIZE, GFP_KERNEL);
1802 if (!clp->cl_ownerstr_hashtbl)
1803 goto err_no_hashtbl;
1804 for (i = 0; i < OWNER_HASH_SIZE; i++)
1805 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001806 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001807 INIT_LIST_HEAD(&clp->cl_sessions);
1808 idr_init(&clp->cl_stateids);
1809 atomic_set(&clp->cl_refcount, 0);
1810 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1811 INIT_LIST_HEAD(&clp->cl_idhash);
1812 INIT_LIST_HEAD(&clp->cl_openowners);
1813 INIT_LIST_HEAD(&clp->cl_delegations);
1814 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001815 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001816#ifdef CONFIG_NFSD_PNFS
1817 INIT_LIST_HEAD(&clp->cl_lo_states);
1818#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001819 spin_lock_init(&clp->cl_lock);
1820 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001822err_no_hashtbl:
1823 kfree(clp->cl_name.data);
1824err_no_name:
1825 kfree(clp);
1826 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001829static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830free_client(struct nfs4_client *clp)
1831{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001832 while (!list_empty(&clp->cl_sessions)) {
1833 struct nfsd4_session *ses;
1834 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1835 se_perclnt);
1836 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001837 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1838 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001839 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001840 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001841 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001842 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001844 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 kfree(clp);
1846}
1847
Benny Halevy84d38ac2010-05-12 00:13:16 +03001848/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001849static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001850unhash_client_locked(struct nfs4_client *clp)
1851{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001852 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001853 struct nfsd4_session *ses;
1854
Trond Myklebust0a880a22014-07-30 08:27:10 -04001855 lockdep_assert_held(&nn->client_lock);
1856
Trond Myklebust4beb3452014-07-30 08:27:02 -04001857 /* Mark the client as expired! */
1858 clp->cl_time = 0;
1859 /* Make it invisible */
1860 if (!list_empty(&clp->cl_idhash)) {
1861 list_del_init(&clp->cl_idhash);
1862 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1863 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1864 else
1865 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1866 }
1867 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001868 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001869 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1870 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001871 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001875unhash_client(struct nfs4_client *clp)
1876{
1877 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1878
1879 spin_lock(&nn->client_lock);
1880 unhash_client_locked(clp);
1881 spin_unlock(&nn->client_lock);
1882}
1883
Jeff Layton97403d92014-07-30 08:27:12 -04001884static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1885{
1886 if (atomic_read(&clp->cl_refcount))
1887 return nfserr_jukebox;
1888 unhash_client_locked(clp);
1889 return nfs_ok;
1890}
1891
Trond Myklebust4beb3452014-07-30 08:27:02 -04001892static void
1893__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Jeff Laytone294c4c2018-03-16 11:32:02 -04001895 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001896 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 struct list_head reaplist;
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001901 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001902 while (!list_empty(&clp->cl_delegations)) {
1903 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001904 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001905 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Benny Halevycdc97502014-05-30 09:09:30 -04001907 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 while (!list_empty(&reaplist)) {
1909 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001910 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001911 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001912 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001913 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001915 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001916 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001917 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001918 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001919 }
NeilBrownea1da632005-06-23 22:04:17 -07001920 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001921 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001922 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001923 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
Jeff Laytone294c4c2018-03-16 11:32:02 -04001925 for (i = 0; i < OWNER_HASH_SIZE; i++) {
1926 struct nfs4_stateowner *so, *tmp;
1927
1928 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
1929 so_strhash) {
1930 /* Should be no openowners at this point */
1931 WARN_ON_ONCE(so->so_is_open_owner);
1932 remove_blocked_locks(lockowner(so));
1933 }
1934 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001935 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001936 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001937 if (clp->cl_cb_conn.cb_xprt)
1938 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001939 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Trond Myklebust4beb3452014-07-30 08:27:02 -04001942static void
1943destroy_client(struct nfs4_client *clp)
1944{
1945 unhash_client(clp);
1946 __destroy_client(clp);
1947}
1948
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001949static void expire_client(struct nfs4_client *clp)
1950{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001951 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001952 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001953 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001954}
1955
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001956static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1957{
1958 memcpy(target->cl_verifier.data, source->data,
1959 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001962static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1963{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1965 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1966}
1967
J. Bruce Fields50043852015-11-20 15:58:37 -05001968int strdup_if_nonnull(char **target, char *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001969{
J. Bruce Fields50043852015-11-20 15:58:37 -05001970 if (source) {
1971 *target = kstrdup(source, GFP_KERNEL);
1972 if (!*target)
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001973 return -ENOMEM;
1974 } else
J. Bruce Fields50043852015-11-20 15:58:37 -05001975 *target = NULL;
1976 return 0;
1977}
1978
1979static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1980{
1981 int ret;
1982
1983 ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
1984 if (ret)
1985 return ret;
J. Bruce Fields414ca012015-11-20 10:48:02 -05001986 ret = strdup_if_nonnull(&target->cr_raw_principal,
1987 source->cr_raw_principal);
1988 if (ret)
1989 return ret;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001990 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 target->cr_uid = source->cr_uid;
1992 target->cr_gid = source->cr_gid;
1993 target->cr_group_info = source->cr_group_info;
1994 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001995 target->cr_gss_mech = source->cr_gss_mech;
1996 if (source->cr_gss_mech)
1997 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002001static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002002compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2003{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002004 if (o1->len < o2->len)
2005 return -1;
2006 if (o1->len > o2->len)
2007 return 1;
2008 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002009}
2010
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002011static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002012{
NeilBrowna55370a2005-06-23 22:03:52 -07002013 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
2016static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002017same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2018{
2019 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
2022static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002023same_clid(clientid_t *cl1, clientid_t *cl2)
2024{
2025 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002028static bool groups_equal(struct group_info *g1, struct group_info *g2)
2029{
2030 int i;
2031
2032 if (g1->ngroups != g2->ngroups)
2033 return false;
2034 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002035 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002036 return false;
2037 return true;
2038}
2039
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002040/*
2041 * RFC 3530 language requires clid_inuse be returned when the
2042 * "principal" associated with a requests differs from that previously
2043 * used. We use uid, gid's, and gss principal string as our best
2044 * approximation. We also don't want to allow non-gss use of a client
2045 * established using gss: in theory cr_principal should catch that
2046 * change, but in practice cr_principal can be null even in the gss case
2047 * since gssd doesn't always pass down a principal string.
2048 */
2049static bool is_gss_cred(struct svc_cred *cr)
2050{
2051 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2052 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2053}
2054
2055
Vivek Trivedi5559b502012-07-24 21:18:20 +05302056static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002057same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2058{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002059 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002060 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2061 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002062 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2063 return false;
2064 if (cr1->cr_principal == cr2->cr_principal)
2065 return true;
2066 if (!cr1->cr_principal || !cr2->cr_principal)
2067 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302068 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
J. Bruce Fields57266a62013-04-13 14:27:29 -04002071static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2072{
2073 struct svc_cred *cr = &rqstp->rq_cred;
2074 u32 service;
2075
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002076 if (!cr->cr_gss_mech)
2077 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002078 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2079 return service == RPC_GSS_SVC_INTEGRITY ||
2080 service == RPC_GSS_SVC_PRIVACY;
2081}
2082
Andrew Elblededeb132016-06-15 12:52:08 -04002083bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002084{
2085 struct svc_cred *cr = &rqstp->rq_cred;
2086
2087 if (!cl->cl_mach_cred)
2088 return true;
2089 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2090 return false;
2091 if (!svc_rqst_integrity_protected(rqstp))
2092 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002093 if (cl->cl_cred.cr_raw_principal)
2094 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2095 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002096 if (!cr->cr_principal)
2097 return false;
2098 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2099}
2100
Jeff Layton294ac322014-07-30 08:27:15 -04002101static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002102{
Chuck Leverab4684d2012-03-02 17:13:50 -05002103 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Jeff Laytonf4199922014-06-17 07:44:11 -04002105 /*
2106 * This is opaque to client, so no need to byte-swap. Use
2107 * __force to keep sparse happy
2108 */
2109 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002110 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002111 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Jeff Layton294ac322014-07-30 08:27:15 -04002114static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2115{
2116 clp->cl_clientid.cl_boot = nn->boot_time;
2117 clp->cl_clientid.cl_id = nn->clientid_counter++;
2118 gen_confirm(clp, nn);
2119}
2120
Jeff Layton4770d722014-07-29 21:34:10 -04002121static struct nfs4_stid *
2122find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002123{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002124 struct nfs4_stid *ret;
2125
2126 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2127 if (!ret || !ret->sc_type)
2128 return NULL;
2129 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002130}
2131
Jeff Layton4770d722014-07-29 21:34:10 -04002132static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002133find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002134{
2135 struct nfs4_stid *s;
2136
Jeff Layton4770d722014-07-29 21:34:10 -04002137 spin_lock(&cl->cl_lock);
2138 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002139 if (s != NULL) {
2140 if (typemask & s->sc_type)
2141 atomic_inc(&s->sc_count);
2142 else
2143 s = NULL;
2144 }
Jeff Layton4770d722014-07-29 21:34:10 -04002145 spin_unlock(&cl->cl_lock);
2146 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002147}
2148
Jeff Layton2216d442012-11-12 15:00:57 -05002149static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002150 struct svc_rqst *rqstp, nfs4_verifier *verf)
2151{
2152 struct nfs4_client *clp;
2153 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002154 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002155 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002156
2157 clp = alloc_client(name);
2158 if (clp == NULL)
2159 return NULL;
2160
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002161 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2162 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002163 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002164 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002165 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002166 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002167 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002168 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002169 copy_verf(clp, verf);
2170 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002171 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002172 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002173 return clp;
2174}
2175
NeilBrownfd39ca92005-06-23 22:04:03 -07002176static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002177add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2178{
2179 struct rb_node **new = &(root->rb_node), *parent = NULL;
2180 struct nfs4_client *clp;
2181
2182 while (*new) {
2183 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2184 parent = *new;
2185
2186 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2187 new = &((*new)->rb_left);
2188 else
2189 new = &((*new)->rb_right);
2190 }
2191
2192 rb_link_node(&new_clp->cl_namenode, parent, new);
2193 rb_insert_color(&new_clp->cl_namenode, root);
2194}
2195
2196static struct nfs4_client *
2197find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2198{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002199 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002200 struct rb_node *node = root->rb_node;
2201 struct nfs4_client *clp;
2202
2203 while (node) {
2204 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2205 cmp = compare_blob(&clp->cl_name, name);
2206 if (cmp > 0)
2207 node = node->rb_left;
2208 else if (cmp < 0)
2209 node = node->rb_right;
2210 else
2211 return clp;
2212 }
2213 return NULL;
2214}
2215
2216static void
2217add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002220 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Trond Myklebust0a880a22014-07-30 08:27:10 -04002222 lockdep_assert_held(&nn->client_lock);
2223
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002224 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002225 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002227 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002228 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
NeilBrownfd39ca92005-06-23 22:04:03 -07002231static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232move_to_confirmed(struct nfs4_client *clp)
2233{
2234 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002235 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Trond Myklebust0a880a22014-07-30 08:27:10 -04002237 lockdep_assert_held(&nn->client_lock);
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002240 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002241 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002242 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002243 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002244 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
2247static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002248find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 struct nfs4_client *clp;
2251 unsigned int idhashval = clientid_hashval(clid->cl_id);
2252
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002253 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002254 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002255 if ((bool)clp->cl_minorversion != sessions)
2256 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002257 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261 return NULL;
2262}
2263
2264static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002265find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2266{
2267 struct list_head *tbl = nn->conf_id_hashtbl;
2268
Trond Myklebust0a880a22014-07-30 08:27:10 -04002269 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002270 return find_client_in_id_table(tbl, clid, sessions);
2271}
2272
2273static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002274find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002276 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Trond Myklebust0a880a22014-07-30 08:27:10 -04002278 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002279 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002282static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002283{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002284 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002285}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002286
NeilBrown28ce6052005-06-23 22:03:56 -07002287static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002288find_confirmed_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 Kinsbursky382a62e2012-11-14 18:21:26 +03002291 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002292}
2293
2294static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002295find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002296{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002297 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002298 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002299}
2300
NeilBrownfd39ca92005-06-23 22:04:03 -07002301static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002302gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002304 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002305 struct sockaddr *sa = svc_addr(rqstp);
2306 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002307 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Jeff Layton7077ecb2009-08-14 12:57:58 -04002309 /* Currently, we only support tcp and tcp6 for the callback channel */
2310 if (se->se_callback_netid_len == 3 &&
2311 !memcmp(se->se_callback_netid_val, "tcp", 3))
2312 expected_family = AF_INET;
2313 else if (se->se_callback_netid_len == 4 &&
2314 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2315 expected_family = AF_INET6;
2316 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 goto out_err;
2318
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002319 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002320 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002321 (struct sockaddr *)&conn->cb_addr,
2322 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002323
J. Bruce Fields07263f12010-05-31 19:09:40 -04002324 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002326
J. Bruce Fields07263f12010-05-31 19:09:40 -04002327 if (conn->cb_addr.ss_family == AF_INET6)
2328 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002329
J. Bruce Fields07263f12010-05-31 19:09:40 -04002330 conn->cb_prog = se->se_callback_prog;
2331 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002332 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return;
2334out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002335 conn->cb_addr.ss_family = AF_UNSPEC;
2336 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002337 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 "will not receive delegations\n",
2339 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return;
2342}
2343
Andy Adamson074fe892009-04-03 08:28:15 +03002344/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002345 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002346 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002347static void
Andy Adamson074fe892009-04-03 08:28:15 +03002348nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2349{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002350 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002351 struct nfsd4_slot *slot = resp->cstate.slot;
2352 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002353
Andy Adamson557ce262009-08-28 08:45:04 -04002354 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002355
J. Bruce Fieldsf92c45b2017-10-18 16:17:18 -04002356 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002357 slot->sl_opcnt = resp->opcnt;
2358 slot->sl_status = resp->cstate.status;
J. Bruce Fields877362f2017-10-17 20:38:49 -04002359 free_svc_cred(&slot->sl_cred);
2360 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002361
J. Bruce Fieldsf92c45b2017-10-18 16:17:18 -04002362 if (!nfsd4_cache_this(resp)) {
2363 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002364 return;
2365 }
J. Bruce Fieldsf92c45b2017-10-18 16:17:18 -04002366 slot->sl_flags |= NFSD4_SLOT_CACHED;
2367
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002368 base = resp->cstate.data_offset;
2369 slot->sl_datalen = buf->len - base;
2370 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002371 WARN(1, "%s: sessions DRC could not cache compound\n",
2372 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002373 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002374}
2375
2376/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002377 * Encode the replay sequence operation from the slot values.
2378 * If cachethis is FALSE encode the uncached rep error on the next
2379 * operation which sets resp->p and increments resp->opcnt for
2380 * nfs4svc_encode_compoundres.
2381 *
Andy Adamson074fe892009-04-03 08:28:15 +03002382 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002383static __be32
2384nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2385 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002386{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002387 struct nfsd4_op *op;
2388 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002389
Andy Adamsonabfabf82009-07-23 19:02:18 -04002390 /* Encode the replayed sequence operation */
2391 op = &args->ops[resp->opcnt - 1];
2392 nfsd4_encode_operation(resp, op);
2393
J. Bruce Fieldsf92c45b2017-10-18 16:17:18 -04002394 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2395 return op->status;
2396 if (args->opcnt == 1) {
2397 /*
2398 * The original operation wasn't a solo sequence--we
2399 * always cache those--so this retry must not match the
2400 * original:
2401 */
2402 op->status = nfserr_seq_false_retry;
2403 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002404 op = &args->ops[resp->opcnt++];
2405 op->status = nfserr_retry_uncached_rep;
2406 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002407 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002408 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002409}
2410
2411/*
Andy Adamson557ce262009-08-28 08:45:04 -04002412 * The sequence operation is not cached because we can use the slot and
2413 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002414 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002415static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002416nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2417 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002418{
Andy Adamson557ce262009-08-28 08:45:04 -04002419 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002420 struct xdr_stream *xdr = &resp->xdr;
2421 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002422 __be32 status;
2423
Andy Adamson557ce262009-08-28 08:45:04 -04002424 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002425
Andy Adamsonabfabf82009-07-23 19:02:18 -04002426 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002427 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002428 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002429
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002430 p = xdr_reserve_space(xdr, slot->sl_datalen);
2431 if (!p) {
2432 WARN_ON_ONCE(1);
2433 return nfserr_serverfault;
2434 }
2435 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2436 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002437
Andy Adamson557ce262009-08-28 08:45:04 -04002438 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002439 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002440}
2441
Andy Adamson0733d212009-04-03 08:28:01 +03002442/*
2443 * Set the exchange_id flags returned by the server.
2444 */
2445static void
2446nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2447{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002448#ifdef CONFIG_NFSD_PNFS
2449 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2450#else
Andy Adamson0733d212009-04-03 08:28:01 +03002451 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002452#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002453
2454 /* Referrals are supported, Migration is not. */
2455 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2456
2457 /* set the wire flags to return to client. */
2458 clid->flags = new->cl_exchange_flags;
2459}
2460
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002461static bool client_has_openowners(struct nfs4_client *clp)
2462{
2463 struct nfs4_openowner *oo;
2464
2465 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2466 if (!list_empty(&oo->oo_owner.so_stateids))
2467 return true;
2468 }
2469 return false;
2470}
2471
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002472static bool client_has_state(struct nfs4_client *clp)
2473{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002474 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002475#ifdef CONFIG_NFSD_PNFS
2476 || !list_empty(&clp->cl_lo_states)
2477#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002478 || !list_empty(&clp->cl_delegations)
2479 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002480}
2481
Al Virob37ad282006-10-19 23:28:59 -07002482__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002483nfsd4_exchange_id(struct svc_rqst *rqstp,
2484 struct nfsd4_compound_state *cstate,
2485 struct nfsd4_exchange_id *exid)
2486{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002487 struct nfs4_client *conf, *new;
2488 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002489 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002490 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002491 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002492 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002493 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002494 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002495
Jeff Layton363168b2009-08-14 12:57:56 -04002496 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002497 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002498 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002499 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002500 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002501
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002502 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002503 return nfserr_inval;
2504
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002505 new = create_client(exid->clname, rqstp, &verf);
2506 if (new == NULL)
2507 return nfserr_jukebox;
2508
Andy Adamson0733d212009-04-03 08:28:01 +03002509 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002510 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002511 exid->spo_must_enforce[0] = 0;
2512 exid->spo_must_enforce[1] = (
2513 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2514 1 << (OP_EXCHANGE_ID - 32) |
2515 1 << (OP_CREATE_SESSION - 32) |
2516 1 << (OP_DESTROY_SESSION - 32) |
2517 1 << (OP_DESTROY_CLIENTID - 32));
2518
2519 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2520 1 << (OP_OPEN_DOWNGRADE) |
2521 1 << (OP_LOCKU) |
2522 1 << (OP_DELEGRETURN));
2523
2524 exid->spo_must_allow[1] &= (
2525 1 << (OP_TEST_STATEID - 32) |
2526 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002527 if (!svc_rqst_integrity_protected(rqstp)) {
2528 status = nfserr_inval;
2529 goto out_nolock;
2530 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002531 /*
2532 * Sometimes userspace doesn't give us a principal.
2533 * Which is a bug, really. Anyway, we can't enforce
2534 * MACH_CRED in that case, better to give up now:
2535 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002536 if (!new->cl_cred.cr_principal &&
2537 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002538 status = nfserr_serverfault;
2539 goto out_nolock;
2540 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002541 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002542 case SP4_NONE:
2543 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002544 default: /* checked by xdr code */
2545 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002546 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002547 status = nfserr_encr_alg_unsupp;
2548 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002549 }
2550
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002551 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002552 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002553 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002554 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002555 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2556 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2557
J. Bruce Fields136e6582012-05-12 20:37:23 -04002558 if (update) {
2559 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002560 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002561 goto out;
2562 }
Andrew Elblededeb132016-06-15 12:52:08 -04002563 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002564 status = nfserr_wrong_cred;
2565 goto out;
2566 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002567 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002568 status = nfserr_perm;
2569 goto out;
2570 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002571 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002572 status = nfserr_not_same;
2573 goto out;
2574 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002575 /* case 6 */
2576 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002577 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002578 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002579 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002580 if (client_has_state(conf)) {
2581 status = nfserr_clid_inuse;
2582 goto out;
2583 }
Andy Adamson0733d212009-04-03 08:28:01 +03002584 goto out_new;
2585 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002586 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002587 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002588 goto out_copy;
2589 }
2590 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002591 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002592 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002593 }
2594
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002595 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002596 status = nfserr_noent;
2597 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002598 }
2599
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002600 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002601 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002602 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002603
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002604 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002605out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002606 if (conf) {
2607 status = mark_client_expired_locked(conf);
2608 if (status)
2609 goto out;
2610 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002611 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002612 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2613 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002614
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002615 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002616 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002617 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002618out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002619 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2620 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002621
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002622 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2623 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002624
2625 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002626 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002627 status = nfs_ok;
2628
2629out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002630 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002631out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002632 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002633 expire_client(new);
2634 if (unconf)
2635 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002636 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002637}
2638
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002639static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002640check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002641{
Andy Adamson88e588d2009-07-23 19:02:15 -04002642 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2643 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002644
2645 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002646 if (slot_inuse) {
2647 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002648 return nfserr_jukebox;
2649 else
2650 return nfserr_seq_misordered;
2651 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002652 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002653 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002654 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002655 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002656 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002657 return nfserr_seq_misordered;
2658}
2659
Andy Adamson49557cc2009-07-23 19:02:16 -04002660/*
2661 * Cache the create session result into the create session single DRC
2662 * slot cache by saving the xdr structure. sl_seqid has been set.
2663 * Do this for solo or embedded create session operations.
2664 */
2665static void
2666nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002667 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002668{
2669 slot->sl_status = nfserr;
2670 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2671}
2672
2673static __be32
2674nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2675 struct nfsd4_clid_slot *slot)
2676{
2677 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2678 return slot->sl_status;
2679}
2680
Mi Jinlong1b74c252011-07-14 14:50:17 +08002681#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2682 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2683 1 + /* MIN tag is length with zero, only length */ \
2684 3 + /* version, opcount, opcode */ \
2685 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2686 /* seqid, slotID, slotID, cache */ \
2687 4 ) * sizeof(__be32))
2688
2689#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2690 2 + /* verifier: AUTH_NULL, length 0 */\
2691 1 + /* status */ \
2692 1 + /* MIN tag is length with zero, only length */ \
2693 3 + /* opcount, opcode, opstatus*/ \
2694 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2695 /* seqid, slotID, slotID, slotID, status */ \
2696 5 ) * sizeof(__be32))
2697
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002698static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002699{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002700 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2701
J. Bruce Fields373cd402013-04-08 15:42:12 -04002702 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2703 return nfserr_toosmall;
2704 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2705 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002706 ca->headerpadsz = 0;
2707 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2708 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2709 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2710 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2711 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2712 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2713 /*
2714 * Note decreasing slot size below client's request may make it
2715 * difficult for client to function correctly, whereas
2716 * decreasing the number of slots will (just?) affect
2717 * performance. When short on memory we therefore prefer to
2718 * decrease number of slots instead of their size. Clients that
2719 * request larger slots than they need will get poor results:
2720 */
2721 ca->maxreqs = nfsd4_get_drc_mem(ca);
2722 if (!ca->maxreqs)
2723 return nfserr_jukebox;
2724
J. Bruce Fields373cd402013-04-08 15:42:12 -04002725 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002726}
2727
Chuck Lever45006322016-03-01 13:06:02 -05002728/*
2729 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2730 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2731 */
2732#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2733 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2734
2735#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2736 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2737
Kinglong Mee8a891632013-12-23 18:11:02 +08002738#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002739 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002740#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002741 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2742 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002743
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002744static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2745{
2746 ca->headerpadsz = 0;
2747
Kinglong Mee8a891632013-12-23 18:11:02 +08002748 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002749 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002750 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002751 return nfserr_toosmall;
2752 ca->maxresp_cached = 0;
2753 if (ca->maxops < 2)
2754 return nfserr_toosmall;
2755
2756 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002757}
2758
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002759static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2760{
2761 switch (cbs->flavor) {
2762 case RPC_AUTH_NULL:
2763 case RPC_AUTH_UNIX:
2764 return nfs_ok;
2765 default:
2766 /*
2767 * GSS case: the spec doesn't allow us to return this
2768 * error. But it also doesn't allow us not to support
2769 * GSS.
2770 * I'd rather this fail hard than return some error the
2771 * client might think it can already handle:
2772 */
2773 return nfserr_encr_alg_unsupp;
2774 }
2775}
2776
Andy Adamson069b6ad2009-04-03 08:27:58 +03002777__be32
2778nfsd4_create_session(struct svc_rqst *rqstp,
2779 struct nfsd4_compound_state *cstate,
2780 struct nfsd4_create_session *cr_ses)
2781{
Jeff Layton363168b2009-08-14 12:57:56 -04002782 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002783 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002784 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002785 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002786 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002787 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002788 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002789 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002790
Mi Jinlonga62573d2011-03-23 17:57:07 +08002791 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2792 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002793 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2794 if (status)
2795 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002796 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002797 if (status)
2798 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002799 status = check_backchannel_attrs(&cr_ses->back_channel);
2800 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002801 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002802 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002803 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002804 if (!new)
2805 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002806 conn = alloc_conn_from_crses(rqstp, cr_ses);
2807 if (!conn)
2808 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002809
Jeff Laytond20c11d2014-07-30 08:27:07 -04002810 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002811 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002812 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002813 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002814
2815 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002816 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002817 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002818 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002819 cs_slot = &conf->cl_cs_slot;
2820 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002821 if (status) {
2822 if (status == nfserr_replay_cache)
2823 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002824 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002825 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002826 } else if (unconf) {
2827 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002828 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002829 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002830 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002831 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002832 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002833 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002834 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002835 cs_slot = &unconf->cl_cs_slot;
2836 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002837 if (status) {
2838 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002839 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002840 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002841 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002842 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002843 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002844 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002845 if (status) {
2846 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002847 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002848 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002849 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002850 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002851 conf = unconf;
2852 } else {
2853 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002854 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002855 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002856 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002857 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002858 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002859 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002860 cr_ses->flags &= ~SESSION4_RDMA;
2861
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002862 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002863 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002864
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002865 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002866 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002867 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002868 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002869
Jeff Laytond20c11d2014-07-30 08:27:07 -04002870 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002871 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002872 spin_unlock(&nn->client_lock);
2873 /* init connection and backchannel */
2874 nfsd4_init_conn(rqstp, conn, new);
2875 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002876 if (old)
2877 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002878 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002879out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002880 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002881 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002882 if (old)
2883 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002884out_free_session:
2885 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002886out_release_drc_mem:
2887 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002888 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002889}
2890
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002891static __be32 nfsd4_map_bcts_dir(u32 *dir)
2892{
2893 switch (*dir) {
2894 case NFS4_CDFC4_FORE:
2895 case NFS4_CDFC4_BACK:
2896 return nfs_ok;
2897 case NFS4_CDFC4_FORE_OR_BOTH:
2898 case NFS4_CDFC4_BACK_OR_BOTH:
2899 *dir = NFS4_CDFC4_BOTH;
2900 return nfs_ok;
2901 };
2902 return nfserr_inval;
2903}
2904
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002905__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2906{
2907 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002908 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002909 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002910
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002911 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2912 if (status)
2913 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002914 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002915 session->se_cb_prog = bc->bc_cb_program;
2916 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002917 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002918
2919 nfsd4_probe_callback(session->se_client);
2920
2921 return nfs_ok;
2922}
2923
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002924__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2925 struct nfsd4_compound_state *cstate,
2926 struct nfsd4_bind_conn_to_session *bcts)
2927{
2928 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002929 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002930 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002931 struct net *net = SVC_NET(rqstp);
2932 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002933
2934 if (!nfsd4_last_compound_op(rqstp))
2935 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002936 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002937 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002938 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002939 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002940 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002941 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002942 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002943 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002944 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002945 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002946 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002947 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002948 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002949 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002950 goto out;
2951 nfsd4_init_conn(rqstp, conn, session);
2952 status = nfs_ok;
2953out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002954 nfsd4_put_session(session);
2955out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002956 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002957}
2958
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002959static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2960{
2961 if (!session)
2962 return 0;
2963 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2964}
2965
Andy Adamson069b6ad2009-04-03 08:27:58 +03002966__be32
2967nfsd4_destroy_session(struct svc_rqst *r,
2968 struct nfsd4_compound_state *cstate,
2969 struct nfsd4_destroy_session *sessionid)
2970{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002971 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002972 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002973 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002974 struct net *net = SVC_NET(r);
2975 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002976
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002977 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002978 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002979 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002980 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002981 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002982 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002983 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002984 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002985 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002986 if (!ses)
2987 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002988 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002989 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002990 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002991 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002992 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002993 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002994 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002995 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002996
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002997 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002998
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002999 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003000 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003001out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003002 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003003out_client_lock:
3004 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003005out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003006 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003007}
3008
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003009static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003010{
3011 struct nfsd4_conn *c;
3012
3013 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003014 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003015 return c;
3016 }
3017 }
3018 return NULL;
3019}
3020
J. Bruce Fields57266a62013-04-13 14:27:29 -04003021static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003022{
3023 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003024 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003025 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003026 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003027
3028 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003029 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003030 if (c)
3031 goto out_free;
3032 status = nfserr_conn_not_bound_to_session;
3033 if (clp->cl_mach_cred)
3034 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003035 __nfsd4_hash_conn(new, ses);
3036 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003037 ret = nfsd4_register_conn(new);
3038 if (ret)
3039 /* oops; xprt is already down: */
3040 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003041 return nfs_ok;
3042out_free:
3043 spin_unlock(&clp->cl_lock);
3044 free_conn(new);
3045 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003046}
3047
Mi Jinlong868b89c2011-04-27 09:09:58 +08003048static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3049{
3050 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3051
3052 return args->opcnt > session->se_fchannel.maxops;
3053}
3054
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003055static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3056 struct nfsd4_session *session)
3057{
3058 struct xdr_buf *xb = &rqstp->rq_arg;
3059
3060 return xb->len > session->se_fchannel.maxreq_sz;
3061}
3062
J. Bruce Fields877362f2017-10-17 20:38:49 -04003063static bool replay_matches_cache(struct svc_rqst *rqstp,
3064 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3065{
3066 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3067
3068 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3069 (bool)seq->cachethis)
3070 return false;
3071 /*
Scott Mayhewaf49ad52019-10-09 15:11:37 -04003072 * If there's an error then the reply can have fewer ops than
3073 * the call.
J. Bruce Fields877362f2017-10-17 20:38:49 -04003074 */
Scott Mayhewaf49ad52019-10-09 15:11:37 -04003075 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3076 return false;
3077 /*
3078 * But if we cached a reply with *more* ops than the call you're
3079 * sending us now, then this new call is clearly not really a
3080 * replay of the old one:
3081 */
3082 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields877362f2017-10-17 20:38:49 -04003083 return false;
3084 /* This is the only check explicitly called by spec: */
3085 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3086 return false;
3087 /*
3088 * There may be more comparisons we could actually do, but the
3089 * spec doesn't require us to catch every case where the calls
3090 * don't match (that would require caching the call as well as
3091 * the reply), so we don't bother.
3092 */
3093 return true;
3094}
3095
Andy Adamson069b6ad2009-04-03 08:27:58 +03003096__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03003097nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003098 struct nfsd4_compound_state *cstate,
3099 struct nfsd4_sequence *seq)
3100{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003101 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003102 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003103 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003104 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003105 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003106 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003107 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003108 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003109 struct net *net = SVC_NET(rqstp);
3110 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003111
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003112 if (resp->opcnt != 1)
3113 return nfserr_sequence_pos;
3114
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003115 /*
3116 * Will be either used or freed by nfsd4_sequence_check_conn
3117 * below.
3118 */
3119 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3120 if (!conn)
3121 return nfserr_jukebox;
3122
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003123 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003124 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003125 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003126 goto out_no_session;
3127 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003128
Mi Jinlong868b89c2011-04-27 09:09:58 +08003129 status = nfserr_too_many_ops;
3130 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003131 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003132
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003133 status = nfserr_req_too_big;
3134 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003135 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003136
Benny Halevyb85d4c02009-04-03 08:28:08 +03003137 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003138 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003139 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003140
Andy Adamson557ce262009-08-28 08:45:04 -04003141 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003142 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3143
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003144 /* We do not negotiate the number of slots yet, so set the
3145 * maxslots to the session maxreqs which is used to encode
3146 * sr_highest_slotid and the sr_target_slot id to maxslots */
3147 seq->maxslots = session->se_fchannel.maxreqs;
3148
J. Bruce Fields73e79482012-02-13 16:39:00 -05003149 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3150 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003151 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003152 status = nfserr_seq_misordered;
3153 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003154 goto out_put_session;
J. Bruce Fields877362f2017-10-17 20:38:49 -04003155 status = nfserr_seq_false_retry;
3156 if (!replay_matches_cache(rqstp, seq, slot))
3157 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003158 cstate->slot = slot;
3159 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003160 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003161 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003162 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003163 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003164 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003165 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003166 }
3167 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003168 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003169
J. Bruce Fields57266a62013-04-13 14:27:29 -04003170 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003171 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003172 if (status)
3173 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003174
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003175 buflen = (seq->cachethis) ?
3176 session->se_fchannel.maxresp_cached :
3177 session->se_fchannel.maxresp_sz;
3178 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3179 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003180 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003181 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003182 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003183
3184 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003185 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003186 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003187 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003188 if (seq->cachethis)
3189 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003190 else
3191 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003192
3193 cstate->slot = slot;
3194 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003195 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003196
Benny Halevyb85d4c02009-04-03 08:28:08 +03003197out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003198 switch (clp->cl_cb_state) {
3199 case NFSD4_CB_DOWN:
3200 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3201 break;
3202 case NFSD4_CB_FAULT:
3203 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3204 break;
3205 default:
3206 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003207 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003208 if (!list_empty(&clp->cl_revoked))
3209 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003210out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003211 if (conn)
3212 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003213 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003214 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003215out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003216 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003217 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003218}
3219
Trond Myklebustb6076642014-06-30 11:48:35 -04003220void
3221nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3222{
3223 struct nfsd4_compound_state *cs = &resp->cstate;
3224
3225 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003226 if (cs->status != nfserr_replay_cache) {
3227 nfsd4_store_cache_entry(resp);
3228 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3229 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003230 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003231 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003232 } else if (cs->clp)
3233 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003234}
3235
Mi Jinlong345c2842011-10-20 17:51:39 +08003236__be32
3237nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3238{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003239 struct nfs4_client *conf, *unconf;
3240 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003241 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003242 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003243
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003244 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003245 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003246 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003247 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003248
3249 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003250 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003251 status = nfserr_clientid_busy;
3252 goto out;
3253 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003254 status = mark_client_expired_locked(conf);
3255 if (status)
3256 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003257 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003258 } else if (unconf)
3259 clp = unconf;
3260 else {
3261 status = nfserr_stale_clientid;
3262 goto out;
3263 }
Andrew Elblededeb132016-06-15 12:52:08 -04003264 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003265 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003266 status = nfserr_wrong_cred;
3267 goto out;
3268 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003269 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003270out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003271 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003272 if (clp)
3273 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003274 return status;
3275}
3276
Andy Adamson069b6ad2009-04-03 08:27:58 +03003277__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003278nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3279{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003280 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003281
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003282 if (rc->rca_one_fs) {
3283 if (!cstate->current_fh.fh_dentry)
3284 return nfserr_nofilehandle;
3285 /*
3286 * We don't take advantage of the rca_one_fs case.
3287 * That's OK, it's optional, we can safely ignore it.
3288 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003289 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003290 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003291
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003292 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003293 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3294 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003295 goto out;
3296
3297 status = nfserr_stale_clientid;
3298 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003299 /*
3300 * The following error isn't really legal.
3301 * But we only get here if the client just explicitly
3302 * destroyed the client. Surely it no longer cares what
3303 * error it gets back on an operation for the dead
3304 * client.
3305 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003306 goto out;
3307
3308 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003309 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003310out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003311 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003312}
3313
3314__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003315nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3316 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003318 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003320 struct nfs4_client *conf, *new;
3321 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003322 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003323 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3324
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003325 new = create_client(clname, rqstp, &clverifier);
3326 if (new == NULL)
3327 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003328 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003329 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003330 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003331 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003332 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003334 if (clp_used_exchangeid(conf))
3335 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003336 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003337 char addr_str[INET6_ADDRSTRLEN];
3338 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3339 sizeof(addr_str));
3340 dprintk("NFSD: setclientid: string in use by client "
3341 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 goto out;
3343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003345 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003346 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003347 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003348 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003349 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003351 gen_confirm(new, nn);
3352 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003353 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003354 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003355 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003356 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3358 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3359 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003360 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 status = nfs_ok;
3362out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003363 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003364 if (new)
3365 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003366 if (unconf)
3367 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return status;
3369}
3370
3371
Al Virob37ad282006-10-19 23:28:59 -07003372__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003373nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3374 struct nfsd4_compound_state *cstate,
3375 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
NeilBrown21ab45a2005-06-23 22:04:14 -07003377 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003378 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3380 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003381 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003382 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003384 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003386
Jeff Laytond20c11d2014-07-30 08:27:07 -04003387 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003388 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003389 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003390 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003391 * We try hard to give out unique clientid's, so if we get an
3392 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003393 * the client may be buggy; this should never happen.
3394 *
3395 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003396 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003397 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003398 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3399 goto out;
3400 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3401 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003402 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003403 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003404 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3405 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003407 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003408 status = nfserr_stale_clientid;
3409 goto out;
3410 }
3411 status = nfs_ok;
3412 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003413 old = unconf;
3414 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003415 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003416 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003417 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3418 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003419 status = nfserr_clid_inuse;
3420 if (client_has_state(old)
3421 && !same_creds(&unconf->cl_cred,
3422 &old->cl_cred))
3423 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003424 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003425 if (status) {
3426 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003427 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003428 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003429 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003430 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003431 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003432 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003433 get_client_locked(conf);
3434 spin_unlock(&nn->client_lock);
3435 nfsd4_probe_callback(conf);
3436 spin_lock(&nn->client_lock);
3437 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003439 spin_unlock(&nn->client_lock);
3440 if (old)
3441 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return status;
3443}
3444
J. Bruce Fields32513b42011-10-13 16:00:16 -04003445static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003447 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3448}
3449
3450/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003451static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3452 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003453{
Trond Myklebust950e0112014-06-30 11:48:31 -04003454 lockdep_assert_held(&state_lock);
3455
J. Bruce Fields32513b42011-10-13 16:00:16 -04003456 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003457 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003458 INIT_LIST_HEAD(&fp->fi_stateids);
3459 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003460 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003461 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003462 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003463 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003464 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003465 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3466 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003467#ifdef CONFIG_NFSD_PNFS
3468 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003469 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003470#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003471 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472}
3473
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003474void
NeilBrowne60d4392005-06-23 22:03:01 -07003475nfsd4_free_slabs(void)
3476{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003477 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003478 kmem_cache_destroy(openowner_slab);
3479 kmem_cache_destroy(lockowner_slab);
3480 kmem_cache_destroy(file_slab);
3481 kmem_cache_destroy(stateid_slab);
3482 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Bryan Schumaker72083392011-11-01 15:24:59 -04003485int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486nfsd4_init_slabs(void)
3487{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003488 openowner_slab = kmem_cache_create("nfsd4_openowners",
3489 sizeof(struct nfs4_openowner), 0, 0, NULL);
3490 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003491 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003492 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003493 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003494 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003495 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003496 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003497 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003498 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003499 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003500 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003501 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003502 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003503 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003504 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003505 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003506 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003507 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003508 odstate_slab = kmem_cache_create("nfsd4_odstate",
3509 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3510 if (odstate_slab == NULL)
3511 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003513
Sachin Bhamare8287f002015-04-27 14:50:14 +02003514out_free_deleg_slab:
3515 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003516out_free_stateid_slab:
3517 kmem_cache_destroy(stateid_slab);
3518out_free_file_slab:
3519 kmem_cache_destroy(file_slab);
3520out_free_lockowner_slab:
3521 kmem_cache_destroy(lockowner_slab);
3522out_free_openowner_slab:
3523 kmem_cache_destroy(openowner_slab);
3524out:
NeilBrowne60d4392005-06-23 22:03:01 -07003525 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3526 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003529static void init_nfs4_replay(struct nfs4_replay *rp)
3530{
3531 rp->rp_status = nfserr_serverfault;
3532 rp->rp_buflen = 0;
3533 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003534 mutex_init(&rp->rp_mutex);
3535}
3536
3537static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3538 struct nfs4_stateowner *so)
3539{
3540 if (!nfsd4_has_session(cstate)) {
3541 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003542 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003543 }
3544}
3545
3546void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3547{
3548 struct nfs4_stateowner *so = cstate->replay_owner;
3549
3550 if (so != NULL) {
3551 cstate->replay_owner = NULL;
3552 mutex_unlock(&so->so_replay.rp_mutex);
3553 nfs4_put_stateowner(so);
3554 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003555}
3556
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003557static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
3559 struct nfs4_stateowner *sop;
3560
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003561 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003562 if (!sop)
3563 return NULL;
3564
3565 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3566 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003567 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003568 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003570 sop->so_owner.len = owner->len;
3571
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003572 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003573 sop->so_client = clp;
3574 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003575 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003576 return sop;
3577}
3578
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003579static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003580{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003581 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003582
Trond Myklebustd4f04892014-07-29 21:34:36 -04003583 list_add(&oo->oo_owner.so_strhash,
3584 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003585 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003588static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3589{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003590 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003591}
3592
Jeff Layton6b180f02014-07-29 21:34:26 -04003593static void nfs4_free_openowner(struct nfs4_stateowner *so)
3594{
3595 struct nfs4_openowner *oo = openowner(so);
3596
3597 kmem_cache_free(openowner_slab, oo);
3598}
3599
3600static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003601 .so_unhash = nfs4_unhash_openowner,
3602 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003603};
3604
Andrew Elble7fc05642015-11-05 20:42:43 -05003605static struct nfs4_ol_stateid *
3606nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3607{
3608 struct nfs4_ol_stateid *local, *ret = NULL;
3609 struct nfs4_openowner *oo = open->op_openowner;
3610
3611 lockdep_assert_held(&fp->fi_lock);
3612
3613 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3614 /* ignore lock owners */
3615 if (local->st_stateowner->so_is_open_owner == 0)
3616 continue;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003617 if (local->st_stateowner != &oo->oo_owner)
3618 continue;
3619 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003620 ret = local;
3621 atomic_inc(&ret->st_stid.sc_count);
3622 break;
3623 }
3624 }
3625 return ret;
3626}
3627
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003628static __be32
3629nfsd4_verify_open_stid(struct nfs4_stid *s)
3630{
3631 __be32 ret = nfs_ok;
3632
3633 switch (s->sc_type) {
3634 default:
3635 break;
3636 case NFS4_CLOSED_STID:
3637 case NFS4_CLOSED_DELEG_STID:
3638 ret = nfserr_bad_stateid;
3639 break;
3640 case NFS4_REVOKED_DELEG_STID:
3641 ret = nfserr_deleg_revoked;
3642 }
3643 return ret;
3644}
3645
3646/* Lock the stateid st_mutex, and deal with races with CLOSE */
3647static __be32
3648nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3649{
3650 __be32 ret;
3651
3652 mutex_lock(&stp->st_mutex);
3653 ret = nfsd4_verify_open_stid(&stp->st_stid);
3654 if (ret != nfs_ok)
3655 mutex_unlock(&stp->st_mutex);
3656 return ret;
3657}
3658
3659static struct nfs4_ol_stateid *
3660nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3661{
3662 struct nfs4_ol_stateid *stp;
3663 for (;;) {
3664 spin_lock(&fp->fi_lock);
3665 stp = nfsd4_find_existing_open(fp, open);
3666 spin_unlock(&fp->fi_lock);
3667 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3668 break;
3669 nfs4_put_stid(&stp->st_stid);
3670 }
3671 return stp;
3672}
3673
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003674static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003675alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003676 struct nfsd4_compound_state *cstate)
3677{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003678 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003679 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003681 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3682 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003684 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003685 oo->oo_owner.so_is_open_owner = 1;
3686 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003687 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003688 if (nfsd4_has_session(cstate))
3689 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003690 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003691 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003692 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003693 spin_lock(&clp->cl_lock);
3694 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003695 if (ret == NULL) {
3696 hash_openowner(oo, clp, strhashval);
3697 ret = oo;
3698 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003699 nfs4_free_stateowner(&oo->oo_owner);
3700
Trond Myklebustd4f04892014-07-29 21:34:36 -04003701 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}
3704
Andrew Elble7fc05642015-11-05 20:42:43 -05003705static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003706init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003707{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Andrew Elble7fc05642015-11-05 20:42:43 -05003709 struct nfs4_openowner *oo = open->op_openowner;
3710 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003711 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003712
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003713 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003714 /* We are moving these outside of the spinlocks to avoid the warnings */
3715 mutex_init(&stp->st_mutex);
3716 mutex_lock(&stp->st_mutex);
3717
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003718retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05003719 spin_lock(&oo->oo_owner.so_client->cl_lock);
3720 spin_lock(&fp->fi_lock);
3721
3722 retstp = nfsd4_find_existing_open(fp, open);
3723 if (retstp)
3724 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003725
3726 open->op_stp = NULL;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003727 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003728 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003729 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003730 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003731 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003732 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 stp->st_access_bmap = 0;
3734 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003735 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003736 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003737 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003738
3739out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003740 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003741 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003742 if (retstp) {
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003743 /* Handle races with CLOSE */
3744 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3745 nfs4_put_stid(&retstp->st_stid);
3746 goto retry;
3747 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003748 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003749 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003750 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003751 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003752 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
Jeff Laytond3134b12014-07-29 21:34:32 -04003755/*
3756 * In the 4.0 case we need to keep the owners around a little while to handle
3757 * CLOSE replay. We still do need to release any file access that is held by
3758 * them before returning however.
3759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003761move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
Jeff Layton217526e2014-07-30 08:27:11 -04003763 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003764 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3765 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3766 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003767
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003768 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Jeff Laytonb401be222014-07-29 21:34:33 -04003770 /*
3771 * We know that we hold one reference via nfsd4_close, and another
3772 * "persistent" reference for the client. If the refcount is higher
3773 * than 2, then there are still calls in progress that are using this
3774 * stateid. We can't put the sc_file reference until they are finished.
3775 * Wait for the refcount to drop to 2. Since it has been unhashed,
3776 * there should be no danger of the refcount going back up again at
3777 * this point.
3778 */
3779 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3780
Jeff Laytond3134b12014-07-29 21:34:32 -04003781 release_all_access(s);
3782 if (s->st_stid.sc_file) {
3783 put_nfs4_file(s->st_stid.sc_file);
3784 s->st_stid.sc_file = NULL;
3785 }
Jeff Layton217526e2014-07-30 08:27:11 -04003786
3787 spin_lock(&nn->client_lock);
3788 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003789 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003790 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003791 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003792 spin_unlock(&nn->client_lock);
3793 if (last)
3794 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797/* search file_hashtbl[] for file */
3798static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003799find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 struct nfs4_file *fp;
3802
Jeff Layton5b095e92014-10-23 08:01:02 -04003803 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003804 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003805 if (atomic_inc_not_zero(&fp->fi_ref))
3806 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
3809 return NULL;
3810}
3811
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003812struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003813find_file(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);
Trond Myklebust950e0112014-06-30 11:48:31 -04003817
Jeff Layton5b095e92014-10-23 08:01:02 -04003818 rcu_read_lock();
3819 fp = find_file_locked(fh, hashval);
3820 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003821 return fp;
3822}
3823
3824static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003825find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003826{
3827 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003828 unsigned int hashval = file_hashval(fh);
3829
3830 rcu_read_lock();
3831 fp = find_file_locked(fh, hashval);
3832 rcu_read_unlock();
3833 if (fp)
3834 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003835
3836 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003837 fp = find_file_locked(fh, hashval);
3838 if (likely(fp == NULL)) {
3839 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003840 fp = new;
3841 }
3842 spin_unlock(&state_lock);
3843
3844 return fp;
3845}
3846
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003847/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 * Called to check deny when READ with all zero stateid or
3849 * WRITE with all zero or all one stateid
3850 */
Al Virob37ad282006-10-19 23:28:59 -07003851static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3853{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003855 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Trond Myklebustca943212014-07-23 16:17:39 -04003857 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003858 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003859 return ret;
3860 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003861 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003862 if (fp->fi_share_deny & deny_type)
3863 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003864 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003865 put_nfs4_file(fp);
3866 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003869static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003871 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003872 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3873 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003874
Trond Myklebust11b91642014-07-29 21:34:08 -04003875 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003876
Jeff Layton02e12152014-07-16 10:31:57 -04003877 /*
3878 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003879 * already holding inode->i_lock.
3880 *
Jeff Laytondff13992014-07-08 14:02:49 -04003881 * If the dl_time != 0, then we know that it has already been
3882 * queued for a lease break. Don't queue it again.
3883 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003884 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003885 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003886 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003887 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003888 }
Jeff Layton02e12152014-07-16 10:31:57 -04003889 spin_unlock(&state_lock);
3890}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003892static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3893 struct rpc_task *task)
3894{
3895 struct nfs4_delegation *dp = cb_to_delegation(cb);
3896
Andrew Elblea4579742015-08-31 12:06:41 -04003897 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3898 return 1;
3899
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003900 switch (task->tk_status) {
3901 case 0:
3902 return 1;
3903 case -EBADHANDLE:
3904 case -NFS4ERR_BAD_STATEID:
3905 /*
3906 * Race: client probably got cb_recall before open reply
3907 * granting delegation.
3908 */
3909 if (dp->dl_retries--) {
3910 rpc_delay(task, 2 * HZ);
3911 return 0;
3912 }
3913 /*FALLTHRU*/
3914 default:
3915 return -1;
3916 }
3917}
3918
3919static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3920{
3921 struct nfs4_delegation *dp = cb_to_delegation(cb);
3922
3923 nfs4_put_stid(&dp->dl_stid);
3924}
3925
Julia Lawallc4cb8972015-11-21 22:57:39 +01003926static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003927 .prepare = nfsd4_cb_recall_prepare,
3928 .done = nfsd4_cb_recall_done,
3929 .release = nfsd4_cb_recall_release,
3930};
3931
Jeff Layton02e12152014-07-16 10:31:57 -04003932static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3933{
3934 /*
3935 * We're assuming the state code never drops its reference
3936 * without first removing the lease. Since we're in this lease
3937 * callback (and since the lease code is serialized by the kernel
3938 * lock) we know the server hasn't removed the lease yet, we know
3939 * it's safe to take a reference.
3940 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003941 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003942 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003943}
3944
Jeff Layton1c8c6012013-06-21 08:58:15 -04003945/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003946static bool
3947nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003948{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003949 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003950 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3951 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003952
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003953 if (!fp) {
3954 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003955 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003956 }
3957 if (fp->fi_had_conflict) {
3958 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003959 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003960 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003961 /*
3962 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003963 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003964 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003965 */
3966 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Jeff Layton02e12152014-07-16 10:31:57 -04003968 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003969 fp->fi_had_conflict = true;
3970 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003971 * If there are no delegations on the list, then return true
3972 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003973 */
3974 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003975 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003976 else
3977 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3978 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003979 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003980 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
Jeff Laytonc45198e2014-09-01 07:12:07 -04003983static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003984nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3985 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
3987 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003988 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 else
3990 return -EAGAIN;
3991}
3992
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003993static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003994 .lm_break = nfsd_break_deleg_cb,
3995 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996};
3997
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003998static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3999{
4000 if (nfsd4_has_session(cstate))
4001 return nfs_ok;
4002 if (seqid == so->so_seqid - 1)
4003 return nfserr_replay_me;
4004 if (seqid == so->so_seqid)
4005 return nfs_ok;
4006 return nfserr_bad_seqid;
4007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Jeff Layton4b24ca72014-06-30 11:48:44 -04004009static __be32 lookup_clientid(clientid_t *clid,
4010 struct nfsd4_compound_state *cstate,
4011 struct nfsd_net *nn)
4012{
4013 struct nfs4_client *found;
4014
4015 if (cstate->clp) {
4016 found = cstate->clp;
4017 if (!same_clid(&found->cl_clientid, clid))
4018 return nfserr_stale_clientid;
4019 return nfs_ok;
4020 }
4021
4022 if (STALE_CLIENTID(clid, nn))
4023 return nfserr_stale_clientid;
4024
4025 /*
4026 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4027 * cached already then we know this is for is for v4.0 and "sessions"
4028 * will be false.
4029 */
4030 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004031 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004032 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004033 if (!found) {
4034 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004035 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004036 }
4037 atomic_inc(&found->cl_refcount);
4038 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004039
4040 /* Cache the nfs4_client in cstate! */
4041 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004042 return nfs_ok;
4043}
4044
Al Virob37ad282006-10-19 23:28:59 -07004045__be32
Andy Adamson66689582009-04-03 08:28:45 +03004046nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004047 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 clientid_t *clientid = &open->op_clientid;
4050 struct nfs4_client *clp = NULL;
4051 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004052 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004053 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004055 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004057 /*
4058 * In case we need it later, after we've already created the
4059 * file and don't want to risk a further failure:
4060 */
4061 open->op_file = nfsd4_alloc_file();
4062 if (open->op_file == NULL)
4063 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Trond Myklebust2d91e892014-06-30 11:48:46 -04004065 status = lookup_clientid(clientid, cstate, nn);
4066 if (status)
4067 return status;
4068 clp = cstate->clp;
4069
Trond Myklebustd4f04892014-07-29 21:34:36 -04004070 strhashval = ownerstr_hashval(&open->op_owner);
4071 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004072 open->op_openowner = oo;
4073 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004074 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004076 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004077 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004078 release_openowner(oo);
4079 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004080 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004081 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004082 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4083 if (status)
4084 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004085 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004086new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004087 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004088 if (oo == NULL)
4089 return nfserr_jukebox;
4090 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004091alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004092 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004093 if (!open->op_stp)
4094 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004095
4096 if (nfsd4_has_session(cstate) &&
4097 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4098 open->op_odstate = alloc_clnt_odstate(clp);
4099 if (!open->op_odstate)
4100 return nfserr_jukebox;
4101 }
4102
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004103 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
Al Virob37ad282006-10-19 23:28:59 -07004106static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004107nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4108{
4109 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4110 return nfserr_openmode;
4111 else
4112 return nfs_ok;
4113}
4114
Daniel Mackc47d8322011-05-16 16:38:14 +02004115static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004116{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004117 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4118}
4119
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004120static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004121{
4122 struct nfs4_stid *ret;
4123
Andrew Elble62f0b692017-11-03 14:06:31 -04004124 ret = find_stateid_by_type(cl, s,
4125 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004126 if (!ret)
4127 return NULL;
4128 return delegstateid(ret);
4129}
4130
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004131static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4132{
4133 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4134 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4135}
4136
Al Virob37ad282006-10-19 23:28:59 -07004137static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004138nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004139 struct nfs4_delegation **dp)
4140{
4141 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004142 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004143 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004144
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004145 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4146 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004147 goto out;
Andrew Elble62f0b692017-11-03 14:06:31 -04004148 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4149 nfs4_put_stid(&deleg->dl_stid);
4150 if (cl->cl_minorversion)
4151 status = nfserr_deleg_revoked;
4152 goto out;
4153 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004154 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004155 status = nfs4_check_delegmode(deleg, flags);
4156 if (status) {
4157 nfs4_put_stid(&deleg->dl_stid);
4158 goto out;
4159 }
4160 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004161out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004162 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004163 return nfs_ok;
4164 if (status)
4165 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004166 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004167 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004168}
4169
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004170static inline int nfs4_access_to_access(u32 nfs4_access)
4171{
4172 int flags = 0;
4173
4174 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4175 flags |= NFSD_MAY_READ;
4176 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4177 flags |= NFSD_MAY_WRITE;
4178 return flags;
4179}
4180
Al Virob37ad282006-10-19 23:28:59 -07004181static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4183 struct nfsd4_open *open)
4184{
4185 struct iattr iattr = {
4186 .ia_valid = ATTR_SIZE,
4187 .ia_size = 0,
4188 };
4189 if (!open->op_truncate)
4190 return 0;
4191 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004192 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4194}
4195
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004196static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004197 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4198 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004199{
Trond Myklebustde186432014-07-10 14:07:26 -04004200 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004201 __be32 status;
4202 int oflag = nfs4_access_to_omode(open->op_share_access);
4203 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004204 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004205
Trond Myklebustde186432014-07-10 14:07:26 -04004206 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004207
4208 /*
4209 * Are we trying to set a deny mode that would conflict with
4210 * current access?
4211 */
4212 status = nfs4_file_check_deny(fp, open->op_share_deny);
4213 if (status != nfs_ok) {
4214 spin_unlock(&fp->fi_lock);
4215 goto out;
4216 }
4217
4218 /* set access to the file */
4219 status = nfs4_file_get_access(fp, open->op_share_access);
4220 if (status != nfs_ok) {
4221 spin_unlock(&fp->fi_lock);
4222 goto out;
4223 }
4224
4225 /* Set access bits in stateid */
4226 old_access_bmap = stp->st_access_bmap;
4227 set_access(open->op_share_access, stp);
4228
4229 /* Set new deny mask */
4230 old_deny_bmap = stp->st_deny_bmap;
4231 set_deny(open->op_share_deny, stp);
4232 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4233
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004234 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004235 spin_unlock(&fp->fi_lock);
4236 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004237 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004238 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004239 spin_lock(&fp->fi_lock);
4240 if (!fp->fi_fds[oflag]) {
4241 fp->fi_fds[oflag] = filp;
4242 filp = NULL;
4243 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004244 }
Trond Myklebustde186432014-07-10 14:07:26 -04004245 spin_unlock(&fp->fi_lock);
4246 if (filp)
4247 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004248
4249 status = nfsd4_truncate(rqstp, cur_fh, open);
4250 if (status)
4251 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004252out:
4253 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004254out_put_access:
4255 stp->st_access_bmap = old_access_bmap;
4256 nfs4_file_put_access(fp, open->op_share_access);
4257 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4258 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004259}
4260
Al Virob37ad282006-10-19 23:28:59 -07004261static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004262nfs4_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 -07004263{
Al Virob37ad282006-10-19 23:28:59 -07004264 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004265 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004267 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004268 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004269
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004270 /* test and set deny mode */
4271 spin_lock(&fp->fi_lock);
4272 status = nfs4_file_check_deny(fp, open->op_share_deny);
4273 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004274 set_deny(open->op_share_deny, stp);
4275 fp->fi_share_deny |=
4276 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4277 }
4278 spin_unlock(&fp->fi_lock);
4279
4280 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004283 status = nfsd4_truncate(rqstp, cur_fh, open);
4284 if (status != nfs_ok)
4285 reset_union_bmap_deny(old_deny_bmap, stp);
4286 return status;
4287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004289/* Should we give out recallable state?: */
4290static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4291{
4292 if (clp->cl_cb_state == NFSD4_CB_UP)
4293 return true;
4294 /*
4295 * In the sessions case, since we don't have to establish a
4296 * separate connection for callbacks, we assume it's OK
4297 * until we hear otherwise:
4298 */
4299 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4300}
4301
Jeff Laytond564fbe2014-07-16 10:31:58 -04004302static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004303{
4304 struct file_lock *fl;
4305
4306 fl = locks_alloc_lock();
4307 if (!fl)
4308 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004309 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004310 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004311 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4312 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04004313 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004314 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004315 return fl;
4316}
4317
Andrew Elble34ed9872015-10-15 12:07:28 -04004318/**
4319 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4320 * @dp: a pointer to the nfs4_delegation we're adding.
4321 *
4322 * Return:
4323 * On success: Return code will be 0 on success.
4324 *
4325 * On error: -EAGAIN if there was an existing delegation.
4326 * nonzero if there is an error in other cases.
4327 *
4328 */
4329
J. Bruce Fields99c41512013-05-21 16:21:25 -04004330static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004331{
Trond Myklebust11b91642014-07-29 21:34:08 -04004332 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08004333 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004334 struct file *filp;
4335 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004336
Jeff Laytond564fbe2014-07-16 10:31:58 -04004337 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004338 if (!fl)
4339 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04004340 filp = find_readable_file(fp);
4341 if (!filp) {
4342 /* We should always have a readable file here */
4343 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08004344 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04004345 return -EBADF;
4346 }
4347 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04004348 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004349 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04004350 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004351 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04004352 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04004353 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004354 spin_lock(&fp->fi_lock);
4355 /* Did the lease get broken before we took the lock? */
4356 status = -EAGAIN;
4357 if (fp->fi_had_conflict)
4358 goto out_unlock;
4359 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04004360 if (fp->fi_deleg_file) {
Andrew Elble34ed9872015-10-15 12:07:28 -04004361 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004362 goto out_unlock;
4363 }
Jeff Layton417c6622014-07-21 09:34:57 -04004364 fp->fi_deleg_file = filp;
Andrew Elble34ed9872015-10-15 12:07:28 -04004365 fp->fi_delegees = 0;
4366 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004367 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004368 spin_unlock(&state_lock);
Andrew Elble34ed9872015-10-15 12:07:28 -04004369 if (status) {
4370 /* Should never happen, this is a new fi_deleg_file */
4371 WARN_ON_ONCE(1);
4372 goto out_fput;
4373 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004374 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004375out_unlock:
4376 spin_unlock(&fp->fi_lock);
4377 spin_unlock(&state_lock);
4378out_fput:
4379 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004380 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004381}
4382
Jeff Layton0b266932014-07-25 07:34:25 -04004383static struct nfs4_delegation *
4384nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004385 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004386{
Jeff Layton0b266932014-07-25 07:34:25 -04004387 int status;
4388 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004389
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004390 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004391 return ERR_PTR(-EAGAIN);
4392
Andrew Elble34ed9872015-10-15 12:07:28 -04004393 spin_lock(&state_lock);
4394 spin_lock(&fp->fi_lock);
4395 status = nfs4_get_existing_delegation(clp, fp);
4396 spin_unlock(&fp->fi_lock);
4397 spin_unlock(&state_lock);
4398
4399 if (status)
4400 return ERR_PTR(status);
4401
Sachin Bhamare8287f002015-04-27 14:50:14 +02004402 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004403 if (!dp)
4404 return ERR_PTR(-ENOMEM);
4405
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004406 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004407 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004408 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004409 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004410 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004411 spin_unlock(&fp->fi_lock);
4412 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004413 status = nfs4_setlease(dp);
4414 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004415 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004416 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004417 status = -EAGAIN;
4418 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004419 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004420 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004421out_unlock:
4422 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004423 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004424out:
4425 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004426 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004427 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004428 return ERR_PTR(status);
4429 }
4430 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004431}
4432
Benny Halevy4aa89132012-02-21 14:16:44 -08004433static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4434{
4435 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4436 if (status == -EAGAIN)
4437 open->op_why_no_deleg = WND4_CONTENTION;
4438 else {
4439 open->op_why_no_deleg = WND4_RESOURCE;
4440 switch (open->op_deleg_want) {
4441 case NFS4_SHARE_WANT_READ_DELEG:
4442 case NFS4_SHARE_WANT_WRITE_DELEG:
4443 case NFS4_SHARE_WANT_ANY_DELEG:
4444 break;
4445 case NFS4_SHARE_WANT_CANCEL:
4446 open->op_why_no_deleg = WND4_CANCELLED;
4447 break;
4448 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004449 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004450 }
4451 }
4452}
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454/*
4455 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004456 *
4457 * Note we don't support write delegations, and won't until the vfs has
4458 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 */
4460static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004461nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4462 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
4464 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004465 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4466 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004467 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004468 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004470 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004471 open->op_recall = 0;
4472 switch (open->op_claim_type) {
4473 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004474 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004475 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004476 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4477 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004478 break;
4479 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004480 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004481 /*
4482 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004483 * had the chance to reclaim theirs, *and* until
4484 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004485 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004486 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004487 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004488 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004489 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004490 /*
4491 * Also, if the file was opened for write or
4492 * create, there's a good chance the client's
4493 * about to write to it, resulting in an
4494 * immediate recall (since we don't support
4495 * write delegations):
4496 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004497 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004498 goto out_no_deleg;
4499 if (open->op_create == NFS4_OPEN_CREATE)
4500 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004501 break;
4502 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004503 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004504 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004505 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004506 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004507 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004509 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004511 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004512 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004513 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004514 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004515 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004516out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004517 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4518 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004519 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004520 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004521 open->op_recall = 1;
4522 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004523
4524 /* 4.1 client asking for a delegation? */
4525 if (open->op_deleg_want)
4526 nfsd4_open_deleg_none_ext(open, status);
4527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
4529
Benny Halevye27f49c2012-02-21 14:16:54 -08004530static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4531 struct nfs4_delegation *dp)
4532{
4533 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4534 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4535 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4536 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4537 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4538 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4539 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4540 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4541 }
4542 /* Otherwise the client must be confused wanting a delegation
4543 * it already has, therefore we don't return
4544 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4545 */
4546}
4547
Al Virob37ad282006-10-19 23:28:59 -07004548__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4550{
Andy Adamson66689582009-04-03 08:28:45 +03004551 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004552 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004554 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004555 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004556 __be32 status;
Trond Myklebust1c404252017-11-03 08:00:11 -04004557 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 /*
4560 * Lookup file; if found, lookup stateid and check open request,
4561 * and check for delegations in the process of being recalled.
4562 * If not found, create the nfs4_file struct
4563 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004564 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004565 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004566 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004567 if (status)
4568 goto out;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04004569 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004571 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004572 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004573 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 }
4576
Trond Myklebust1c404252017-11-03 08:00:11 -04004577 if (!stp) {
4578 stp = init_open_stateid(fp, open);
4579 if (!open->op_stp)
4580 new_stp = true;
4581 }
4582
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 /*
4584 * OPEN the file, or upgrade an existing OPEN.
4585 * If truncate fails, the OPEN fails.
Trond Myklebust1c404252017-11-03 08:00:11 -04004586 *
4587 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 */
Trond Myklebust1c404252017-11-03 08:00:11 -04004589 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004591 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004592 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004593 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004597 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4598 if (status) {
Trond Myklebust1c404252017-11-03 08:00:11 -04004599 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004600 release_open_stateid(stp);
Trond Myklebust1c404252017-11-03 08:00:11 -04004601 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004602 goto out;
4603 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004604
4605 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4606 open->op_odstate);
4607 if (stp->st_clnt_odstate == open->op_odstate)
4608 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
Trond Myklebust1c404252017-11-03 08:00:11 -04004610
Jeff Layton9767feb2015-10-01 09:05:50 -04004611 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004612 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Benny Halevyd24433c2012-02-16 20:57:17 +02004614 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004615 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4616 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4617 open->op_why_no_deleg = WND4_NOT_WANTED;
4618 goto nodeleg;
4619 }
4620 }
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 /*
4623 * Attempt to hand out a delegation. No error return, because the
4624 * OPEN succeeds even if we fail.
4625 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004626 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004627nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 status = nfs_ok;
4629
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004630 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004631 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004633 /* 4.1 client trying to upgrade/downgrade delegation? */
4634 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004635 open->op_deleg_want)
4636 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004637
NeilBrown13cd2182005-06-23 22:03:10 -07004638 if (fp)
4639 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004640 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004641 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 /*
4643 * To finish the open response, we just need to set the rflags.
4644 */
4645 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004646 if (nfsd4_has_session(&resp->cstate))
4647 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4648 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004650
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004651 if (dp)
4652 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004653 if (stp)
4654 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
4656 return status;
4657}
4658
Jeff Layton58fb12e2014-07-29 21:34:27 -04004659void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004660 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004661{
4662 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004663 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004664
Jeff Laytond3134b12014-07-29 21:34:32 -04004665 nfsd4_cstate_assign_replay(cstate, so);
4666 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004667 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004668 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004669 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004670 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004671 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004672 if (open->op_odstate)
4673 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004674}
4675
Al Virob37ad282006-10-19 23:28:59 -07004676__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004677nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4678 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
4680 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004681 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004682 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 dprintk("process_renew(%08x/%08x): starting\n",
4685 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004686 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004687 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004689 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004691 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004692 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 goto out;
4694 status = nfs_ok;
4695out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return status;
4697}
4698
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004699void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004700nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004701{
Jeff Layton33dcc482012-04-10 11:08:48 -04004702 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004703 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004704 return;
4705
NeilBrowna76b4312005-06-23 22:04:01 -07004706 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004707 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004708 /*
4709 * If the server goes down again right now, an NFSv4
4710 * client will still be allowed to reclaim after it comes back up,
4711 * even if it hasn't yet had a chance to reclaim state this time.
4712 *
4713 */
Jeff Layton919b8042014-09-12 16:40:20 -04004714 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004715 /*
4716 * At this point, NFSv4 clients can still reclaim. But if the
4717 * server crashes, any that have not yet reclaimed will be out
4718 * of luck on the next boot.
4719 *
4720 * (NFSv4.1+ clients are considered to have reclaimed once they
4721 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4722 * have reclaimed after their first OPEN.)
4723 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004724 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004725 /*
4726 * At this point, and once lockd and/or any other containers
4727 * exit their grace period, further reclaims will fail and
4728 * regular locking can resume.
4729 */
NeilBrowna76b4312005-06-23 22:04:01 -07004730}
4731
NeilBrownfd39ca92005-06-23 22:04:03 -07004732static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004733nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
4735 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004736 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004738 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004739 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004741 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004742 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004745 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004746 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004747 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004748 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 clp = list_entry(pos, struct nfs4_client, cl_lru);
4750 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4751 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004752 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 break;
4754 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004755 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004756 dprintk("NFSD: client in use (clientid %08x)\n",
4757 clp->cl_clientid.cl_id);
4758 continue;
4759 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004760 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004761 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004762 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004763 list_for_each_safe(pos, next, &reaplist) {
4764 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 dprintk("NFSD: purging unused client (clientid %08x)\n",
4766 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004767 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 expire_client(clp);
4769 }
Benny Halevycdc97502014-05-30 09:09:30 -04004770 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004771 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4773 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004774 t = dp->dl_time - cutoff;
4775 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 break;
4777 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004778 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004779 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 }
Benny Halevycdc97502014-05-30 09:09:30 -04004781 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004782 while (!list_empty(&reaplist)) {
4783 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4784 dl_recall_lru);
4785 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004786 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 }
Jeff Layton217526e2014-07-30 08:27:11 -04004788
4789 spin_lock(&nn->client_lock);
4790 while (!list_empty(&nn->close_lru)) {
4791 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4792 oo_close_lru);
4793 if (time_after((unsigned long)oo->oo_time,
4794 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004795 t = oo->oo_time - cutoff;
4796 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 break;
4798 }
Jeff Layton217526e2014-07-30 08:27:11 -04004799 list_del_init(&oo->oo_close_lru);
4800 stp = oo->oo_last_closed_stid;
4801 oo->oo_last_closed_stid = NULL;
4802 spin_unlock(&nn->client_lock);
4803 nfs4_put_stid(&stp->st_stid);
4804 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 }
Jeff Layton217526e2014-07-30 08:27:11 -04004806 spin_unlock(&nn->client_lock);
4807
Jeff Layton7919d0a2016-09-16 16:28:25 -04004808 /*
4809 * It's possible for a client to try and acquire an already held lock
4810 * that is being held for a long time, and then lose interest in it.
4811 * So, we clean out any un-revisited request after a lease period
4812 * under the assumption that the client is no longer interested.
4813 *
4814 * RFC5661, sec. 9.6 states that the client must not rely on getting
4815 * notifications and must continue to poll for locks, even when the
4816 * server supports them. Thus this shouldn't lead to clients blocking
4817 * indefinitely once the lock does become free.
4818 */
4819 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004820 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004821 while (!list_empty(&nn->blocked_locks_lru)) {
4822 nbl = list_first_entry(&nn->blocked_locks_lru,
4823 struct nfsd4_blocked_lock, nbl_lru);
4824 if (time_after((unsigned long)nbl->nbl_time,
4825 (unsigned long)cutoff)) {
4826 t = nbl->nbl_time - cutoff;
4827 new_timeo = min(new_timeo, t);
4828 break;
4829 }
4830 list_move(&nbl->nbl_lru, &reaplist);
4831 list_del_init(&nbl->nbl_list);
4832 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004833 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004834
4835 while (!list_empty(&reaplist)) {
Naofumi Hondaaa1a6cf2017-11-09 10:57:16 -05004836 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04004837 struct nfsd4_blocked_lock, nbl_lru);
4838 list_del_init(&nbl->nbl_lru);
4839 posix_unblock_lock(&nbl->nbl_lock);
4840 free_blocked_lock(nbl);
4841 }
4842
Jeff Laytona832e7a2014-05-30 09:09:26 -04004843 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004844 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845}
4846
Harvey Harrisona254b242008-02-20 12:49:00 -08004847static struct workqueue_struct *laundry_wq;
4848static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004849
4850static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004851laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
4853 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004854 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004855 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4856 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004858 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004860 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861}
4862
Jeff Layton8fcd4612015-07-30 06:57:46 -04004863static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004864{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004865 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004866 return nfserr_bad_stateid;
4867 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004871access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004873 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4874 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4875 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
4878static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004879access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004881 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4882 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883}
4884
4885static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004886__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887{
Al Virob37ad282006-10-19 23:28:59 -07004888 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
J. Bruce Fields02921912010-07-29 15:16:59 -04004890 /* For lock stateid's, we test the parent open, not the lock: */
4891 if (stp->st_openstp)
4892 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004893 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004895 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 goto out;
4897 status = nfs_ok;
4898out:
4899 return status;
4900}
4901
Al Virob37ad282006-10-19 23:28:59 -07004902static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004903check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004905 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004907 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004908 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 * conflicting state; so we must wait out the grace period. */
4910 return nfserr_grace;
4911 } else if (flags & WR_STATE)
4912 return nfs4_share_conflict(current_fh,
4913 NFS4_SHARE_DENY_WRITE);
4914 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4915 return nfs4_share_conflict(current_fh,
4916 NFS4_SHARE_DENY_READ);
4917}
4918
4919/*
4920 * Allow READ/WRITE during grace period on recovered state only for files
4921 * that are not able to provide mandatory locking.
4922 */
4923static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004924grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004926 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927}
4928
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004929static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004930{
Andy Adamson66689582009-04-03 08:28:45 +03004931 /*
4932 * When sessions are used the stateid generation number is ignored
4933 * when it is zero.
4934 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004935 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004936 return nfs_ok;
4937
4938 if (in->si_generation == ref->si_generation)
4939 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004940
J. Bruce Fields0836f582008-01-26 19:08:12 -05004941 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004942 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004943 return nfserr_bad_stateid;
4944 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004945 * However, we could see a stateid from the past, even from a
4946 * non-buggy client. For example, if the client sends a lock
4947 * while some IO is outstanding, the lock may bump si_generation
4948 * while the IO is still in flight. The client could avoid that
4949 * situation by waiting for responses on all the IO requests,
4950 * but better performance may result in retrying IO that
4951 * receives an old_stateid error if requests are rarely
4952 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004953 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004954 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004955}
4956
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004957static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4958{
4959 if (ols->st_stateowner->so_is_open_owner &&
4960 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4961 return nfserr_bad_stateid;
4962 return nfs_ok;
4963}
4964
Chuck Lever7df302f2012-05-29 13:56:37 -04004965static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004966{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004967 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004968 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004969
Andrew Elblef25e2222017-11-09 13:41:10 -05004970 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4971 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004972 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004973 /* Client debugging aid. */
4974 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4975 char addr_str[INET6_ADDRSTRLEN];
4976 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4977 sizeof(addr_str));
4978 pr_warn_ratelimited("NFSD: client %s testing state ID "
4979 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004980 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004981 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004982 spin_lock(&cl->cl_lock);
4983 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004984 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004985 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004986 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004987 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004988 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004989 switch (s->sc_type) {
4990 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004991 status = nfs_ok;
4992 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004993 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004994 status = nfserr_deleg_revoked;
4995 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004996 case NFS4_OPEN_STID:
4997 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004998 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004999 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005000 default:
5001 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005002 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005003 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005004 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005005 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005006 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005007out_unlock:
5008 spin_unlock(&cl->cl_lock);
5009 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005010}
5011
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005012__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005013nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5014 stateid_t *stateid, unsigned char typemask,
5015 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005016{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005017 __be32 status;
Andrew Elble62f0b692017-11-03 14:06:31 -04005018 bool return_revoked = false;
5019
5020 /*
5021 * only return revoked delegations if explicitly asked.
5022 * otherwise we report revoked or bad_stateid status.
5023 */
5024 if (typemask & NFS4_REVOKED_DELEG_STID)
5025 return_revoked = true;
5026 else if (typemask & NFS4_DELEG_STID)
5027 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005028
Andrew Elblef25e2222017-11-09 13:41:10 -05005029 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5030 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005031 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005032 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005033 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005034 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005035 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005036 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005037 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005038 if (status)
5039 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005040 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005041 if (!*s)
5042 return nfserr_bad_stateid;
Andrew Elble62f0b692017-11-03 14:06:31 -04005043 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5044 nfs4_put_stid(*s);
5045 if (cstate->minorversion)
5046 return nfserr_deleg_revoked;
5047 return nfserr_bad_stateid;
5048 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005049 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005050}
5051
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005052static struct file *
5053nfs4_find_file(struct nfs4_stid *s, int flags)
5054{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005055 if (!s)
5056 return NULL;
5057
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005058 switch (s->sc_type) {
5059 case NFS4_DELEG_STID:
5060 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5061 return NULL;
5062 return get_file(s->sc_file->fi_deleg_file);
5063 case NFS4_OPEN_STID:
5064 case NFS4_LOCK_STID:
5065 if (flags & RD_STATE)
5066 return find_readable_file(s->sc_file);
5067 else
5068 return find_writeable_file(s->sc_file);
5069 break;
5070 }
5071
5072 return NULL;
5073}
5074
5075static __be32
5076nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
5077{
5078 __be32 status;
5079
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005080 status = nfsd4_check_openowner_confirmed(ols);
5081 if (status)
5082 return status;
5083 return nfs4_check_openmode(ols, flags);
5084}
5085
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005086static __be32
5087nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5088 struct file **filpp, bool *tmp_file, int flags)
5089{
5090 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5091 struct file *file;
5092 __be32 status;
5093
5094 file = nfs4_find_file(s, flags);
5095 if (file) {
5096 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5097 acc | NFSD_MAY_OWNER_OVERRIDE);
5098 if (status) {
5099 fput(file);
5100 return status;
5101 }
5102
5103 *filpp = file;
5104 } else {
5105 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5106 if (status)
5107 return status;
5108
5109 if (tmp_file)
5110 *tmp_file = true;
5111 }
5112
5113 return 0;
5114}
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005117 * Checks for stateid operations
5118 */
Al Virob37ad282006-10-19 23:28:59 -07005119__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005120nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005121 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5122 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005124 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005125 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005126 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005127 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005128 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 if (filpp)
5131 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005132 if (tmp_file)
5133 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005135 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 return nfserr_grace;
5137
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005138 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5139 status = check_special_stateids(net, fhp, stateid, flags);
5140 goto done;
5141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005143 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005144 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005145 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005146 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005147 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005148 status = check_stateid_generation(stateid, &s->sc_stateid,
5149 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005150 if (status)
5151 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005152
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005153 switch (s->sc_type) {
5154 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005155 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005156 break;
5157 case NFS4_OPEN_STID:
5158 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005159 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005160 break;
5161 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005162 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005165 if (status)
5166 goto out;
5167 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005168
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005169done:
5170 if (!status && filpp)
5171 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005173 if (s)
5174 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 return status;
5176}
5177
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005178/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005179 * Test if the stateid is valid
5180 */
5181__be32
5182nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5183 struct nfsd4_test_stateid *test_stateid)
5184{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005185 struct nfsd4_test_stateid_id *stateid;
5186 struct nfs4_client *cl = cstate->session->se_client;
5187
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005188 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005189 stateid->ts_id_status =
5190 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005191
Bryan Schumaker17456802011-07-13 10:50:48 -04005192 return nfs_ok;
5193}
5194
Chuck Lever42691392016-08-11 10:37:30 -04005195static __be32
5196nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5197{
5198 struct nfs4_ol_stateid *stp = openlockstateid(s);
5199 __be32 ret;
5200
5201 mutex_lock(&stp->st_mutex);
5202
5203 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5204 if (ret)
5205 goto out;
5206
5207 ret = nfserr_locks_held;
5208 if (check_for_locks(stp->st_stid.sc_file,
5209 lockowner(stp->st_stateowner)))
5210 goto out;
5211
5212 release_lock_stateid(stp);
5213 ret = nfs_ok;
5214
5215out:
5216 mutex_unlock(&stp->st_mutex);
5217 nfs4_put_stid(s);
5218 return ret;
5219}
5220
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005221__be32
5222nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5223 struct nfsd4_free_stateid *free_stateid)
5224{
5225 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005226 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005227 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005228 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005229 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005230
Jeff Layton1af71cc2014-07-29 21:34:14 -04005231 spin_lock(&cl->cl_lock);
5232 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005233 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005234 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005235 switch (s->sc_type) {
5236 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005237 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005238 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005239 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005240 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5241 if (ret)
5242 break;
5243 ret = nfserr_locks_held;
5244 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005245 case NFS4_LOCK_STID:
Chuck Lever42691392016-08-11 10:37:30 -04005246 atomic_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005247 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005248 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005249 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005250 case NFS4_REVOKED_DELEG_STID:
5251 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005252 list_del_init(&dp->dl_recall_lru);
5253 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005254 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005255 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005256 goto out;
5257 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005258 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005259out_unlock:
5260 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005261out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005262 return ret;
5263}
5264
NeilBrown4c4cd222005-07-07 17:59:27 -07005265static inline int
5266setlkflg (int type)
5267{
5268 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5269 RD_STATE : WR_STATE;
5270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005272static __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 -04005273{
5274 struct svc_fh *current_fh = &cstate->current_fh;
5275 struct nfs4_stateowner *sop = stp->st_stateowner;
5276 __be32 status;
5277
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005278 status = nfsd4_check_seqid(cstate, sop, seqid);
5279 if (status)
5280 return status;
Trond Myklebust2a7d4a72017-11-03 08:00:15 -04005281 status = nfsd4_lock_ol_stateid(stp);
5282 if (status != nfs_ok)
5283 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005284 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005285 if (status == nfs_ok)
5286 status = nfs4_check_fh(current_fh, &stp->st_stid);
5287 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005288 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005289 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005290}
5291
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292/*
5293 * Checks for sequence id mutating operations.
5294 */
Al Virob37ad282006-10-19 23:28:59 -07005295static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005296nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005297 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005298 struct nfs4_ol_stateid **stpp,
5299 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005301 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005302 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005303 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005305 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5306 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005307
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005309 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005310 if (status)
5311 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005312 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005313 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005315 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005316 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005317 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005318 else
5319 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005320 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005321}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005322
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005323static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5324 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005325{
5326 __be32 status;
5327 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005328 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005330 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005331 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005332 if (status)
5333 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005334 oo = openowner(stp->st_stateowner);
5335 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005336 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005337 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005338 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005339 }
5340 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005341 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
Al Virob37ad282006-10-19 23:28:59 -07005344__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005345nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005346 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347{
Al Virob37ad282006-10-19 23:28:59 -07005348 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005349 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005350 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005351 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
Al Viroa6a9f182013-09-16 10:57:01 -04005353 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5354 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005356 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005357 if (status)
5358 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005360 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005361 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005362 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005363 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005364 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005365 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005366 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005367 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005368 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005369 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005370 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005371 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005372 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005373 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005374 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005375 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005376
Jeff Layton2a4317c2012-03-21 16:42:43 -04005377 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005378 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005379put_stateid:
5380 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005382 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 return status;
5384}
5385
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005386static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005388 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005389 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005390 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005391 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005392}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005393
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005394static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5395{
5396 switch (to_access) {
5397 case NFS4_SHARE_ACCESS_READ:
5398 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5399 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5400 break;
5401 case NFS4_SHARE_ACCESS_WRITE:
5402 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5403 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5404 break;
5405 case NFS4_SHARE_ACCESS_BOTH:
5406 break;
5407 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005408 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 }
5410}
5411
Al Virob37ad282006-10-19 23:28:59 -07005412__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005413nfsd4_open_downgrade(struct svc_rqst *rqstp,
5414 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005415 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
Al Virob37ad282006-10-19 23:28:59 -07005417 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005418 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005419 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
Al Viroa6a9f182013-09-16 10:57:01 -04005421 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5422 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005424 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005425 if (od->od_deleg_want)
5426 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5427 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005429 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005430 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005431 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005434 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005435 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005437 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005439 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005440 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005442 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005444 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005445 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005446 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005448put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005449 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005450 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005452 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 return status;
5454}
5455
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005456static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5457{
Trond Myklebustacf92952014-06-30 11:48:37 -04005458 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005459 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005460 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005461
Jeff Layton2c41beb2014-07-29 21:34:41 -04005462 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005463 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005464
Jeff Laytond83017f2014-07-29 21:34:42 -04005465 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005466 if (unhashed)
5467 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005468 spin_unlock(&clp->cl_lock);
5469 free_ol_stateid_reaplist(&reaplist);
5470 } else {
5471 spin_unlock(&clp->cl_lock);
5472 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005473 if (unhashed)
5474 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005475 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005476}
5477
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478/*
5479 * nfs4_unlock_state() called after encode
5480 */
Al Virob37ad282006-10-19 23:28:59 -07005481__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005482nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005483 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484{
Al Virob37ad282006-10-19 23:28:59 -07005485 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005486 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005487 struct net *net = SVC_NET(rqstp);
5488 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Al Viroa6a9f182013-09-16 10:57:01 -04005490 dprintk("NFSD: nfsd4_close on file %pd\n",
5491 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005493 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5494 &close->cl_stateid,
5495 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005496 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005497 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005498 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 goto out;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04005500
5501 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton9767feb2015-10-01 09:05:50 -04005502 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005504 nfsd4_close_open_stateid(stp);
Trond Myklebust1c8ea412017-11-03 08:00:10 -04005505 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005506
Trond Myklebust5cd35862017-11-03 08:00:12 -04005507 /* See RFC5661 sectionm 18.2.4 */
5508 if (stp->st_stid.sc_client->cl_minorversion)
5509 memcpy(&close->cl_stateid, &close_stateid,
5510 sizeof(close->cl_stateid));
5511
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005512 /* put reference from nfs4_preprocess_seqid_op */
5513 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 return status;
5516}
5517
Al Virob37ad282006-10-19 23:28:59 -07005518__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005519nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5520 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005522 struct nfs4_delegation *dp;
5523 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005524 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005525 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005526 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005528 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005529 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005531 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005532 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005533 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005534 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005535 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005536 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005537 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005538
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005539 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005540put_stateid:
5541 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542out:
5543 return status;
5544}
5545
Benny Halevy87df4de2008-12-15 19:42:03 +02005546static inline u64
5547end_offset(u64 start, u64 len)
5548{
5549 u64 end;
5550
5551 end = start + len;
5552 return end >= start ? end: NFS4_MAX_UINT64;
5553}
5554
5555/* last octet in a range */
5556static inline u64
5557last_byte_offset(u64 start, u64 len)
5558{
5559 u64 end;
5560
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005561 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005562 end = start + len;
5563 return end > start ? end - 1: NFS4_MAX_UINT64;
5564}
5565
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566/*
5567 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5568 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5569 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5570 * locking, this prevents us from being completely protocol-compliant. The
5571 * real solution to this problem is to start using unsigned file offsets in
5572 * the VFS, but this is a very deep change!
5573 */
5574static inline void
5575nfs4_transform_lock_offset(struct file_lock *lock)
5576{
5577 if (lock->fl_start < 0)
5578 lock->fl_start = OFFSET_MAX;
5579 if (lock->fl_end < 0)
5580 lock->fl_end = OFFSET_MAX;
5581}
5582
Jeff Laytoncae80b32015-04-03 09:04:04 -04005583static fl_owner_t
5584nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005585{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005586 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5587
5588 nfs4_get_stateowner(&lo->lo_owner);
5589 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005590}
5591
Jeff Laytoncae80b32015-04-03 09:04:04 -04005592static void
5593nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005594{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005595 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005596
Jeff Laytoncae80b32015-04-03 09:04:04 -04005597 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005598 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005599}
5600
Jeff Layton76d348f2016-09-16 16:28:24 -04005601static void
5602nfsd4_lm_notify(struct file_lock *fl)
5603{
5604 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5605 struct net *net = lo->lo_owner.so_client->net;
5606 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5607 struct nfsd4_blocked_lock *nbl = container_of(fl,
5608 struct nfsd4_blocked_lock, nbl_lock);
5609 bool queue = false;
5610
Jeff Layton7919d0a2016-09-16 16:28:25 -04005611 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005612 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005613 if (!list_empty(&nbl->nbl_list)) {
5614 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005615 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005616 queue = true;
5617 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005618 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005619
5620 if (queue)
5621 nfsd4_run_cb(&nbl->nbl_cb);
5622}
5623
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005624static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005625 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005626 .lm_get_owner = nfsd4_fl_get_owner,
5627 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005628};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
5630static inline void
5631nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5632{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005633 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
NeilBrownd5b90262006-04-10 22:55:22 -07005635 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005636 lo = (struct nfs4_lockowner *) fl->fl_owner;
5637 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5638 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005639 if (!deny->ld_owner.data)
5640 /* We just don't care that much */
5641 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005642 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5643 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005644 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005645nevermind:
5646 deny->ld_owner.len = 0;
5647 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005648 deny->ld_clientid.cl_boot = 0;
5649 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 }
5651 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005652 deny->ld_length = NFS4_MAX_UINT64;
5653 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5655 deny->ld_type = NFS4_READ_LT;
5656 if (fl->fl_type != F_RDLCK)
5657 deny->ld_type = NFS4_WRITE_LT;
5658}
5659
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005660static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005661find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005663 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005664 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Trond Myklebust0a880a22014-07-30 08:27:10 -04005666 lockdep_assert_held(&clp->cl_lock);
5667
Trond Myklebustd4f04892014-07-29 21:34:36 -04005668 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5669 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005670 if (so->so_is_open_owner)
5671 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005672 if (same_owner_str(so, owner))
5673 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 }
5675 return NULL;
5676}
5677
Trond Myklebustc58c6612014-07-29 21:34:35 -04005678static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005679find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005680{
5681 struct nfs4_lockowner *lo;
5682
Trond Myklebustd4f04892014-07-29 21:34:36 -04005683 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005684 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005685 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005686 return lo;
5687}
5688
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005689static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5690{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005691 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005692}
5693
Jeff Layton6b180f02014-07-29 21:34:26 -04005694static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5695{
5696 struct nfs4_lockowner *lo = lockowner(sop);
5697
5698 kmem_cache_free(lockowner_slab, lo);
5699}
5700
5701static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005702 .so_unhash = nfs4_unhash_lockowner,
5703 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005704};
5705
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706/*
5707 * Alloc a lock owner structure.
5708 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005709 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005711 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005713static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005714alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5715 struct nfs4_ol_stateid *open_stp,
5716 struct nfsd4_lock *lock)
5717{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005718 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005720 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5721 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005723 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005724 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5725 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005726 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005727 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005728 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005729 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005730 if (ret == NULL) {
5731 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005732 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005733 ret = lo;
5734 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005735 nfs4_free_stateowner(&lo->lo_owner);
5736
Trond Myklebustd4f04892014-07-29 21:34:36 -04005737 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005738 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739}
5740
Jeff Layton356a95e2014-07-29 21:34:13 -04005741static void
5742init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5743 struct nfs4_file *fp, struct inode *inode,
5744 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005746 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Jeff Layton356a95e2014-07-29 21:34:13 -04005748 lockdep_assert_held(&clp->cl_lock);
5749
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005750 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005751 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005752 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005753 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005754 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005755 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005757 stp->st_openstp = open_stp;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005758 mutex_init(&stp->st_mutex);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005759 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005760 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005761 spin_lock(&fp->fi_lock);
5762 list_add(&stp->st_perfile, &fp->fi_stateids);
5763 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764}
5765
Jeff Laytonc53530d2014-06-30 11:48:39 -04005766static struct nfs4_ol_stateid *
5767find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5768{
5769 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005770 struct nfs4_client *clp = lo->lo_owner.so_client;
5771
5772 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005773
5774 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005775 if (lst->st_stid.sc_file == fp) {
5776 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005777 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005778 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005779 }
5780 return NULL;
5781}
5782
Jeff Layton356a95e2014-07-29 21:34:13 -04005783static struct nfs4_ol_stateid *
5784find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5785 struct inode *inode, struct nfs4_ol_stateid *ost,
5786 bool *new)
5787{
5788 struct nfs4_stid *ns = NULL;
5789 struct nfs4_ol_stateid *lst;
5790 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5791 struct nfs4_client *clp = oo->oo_owner.so_client;
5792
5793 spin_lock(&clp->cl_lock);
5794 lst = find_lock_stateid(lo, fi);
5795 if (lst == NULL) {
5796 spin_unlock(&clp->cl_lock);
Kinglong Mee743146d32017-01-18 19:04:42 +08005797 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
Jeff Layton356a95e2014-07-29 21:34:13 -04005798 if (ns == NULL)
5799 return NULL;
5800
5801 spin_lock(&clp->cl_lock);
5802 lst = find_lock_stateid(lo, fi);
5803 if (likely(!lst)) {
5804 lst = openlockstateid(ns);
5805 init_lock_stateid(lst, lo, fi, inode, ost);
5806 ns = NULL;
5807 *new = true;
5808 }
5809 }
5810 spin_unlock(&clp->cl_lock);
5811 if (ns)
5812 nfs4_put_stid(ns);
5813 return lst;
5814}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005815
NeilBrownfd39ca92005-06-23 22:04:03 -07005816static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817check_lock_length(u64 offset, u64 length)
5818{
Kinglong Meee7969312015-07-13 17:34:19 +08005819 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5820 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821}
5822
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005823static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005824{
Trond Myklebust11b91642014-07-29 21:34:08 -04005825 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005826
Jeff Layton7214e862014-07-10 14:07:33 -04005827 lockdep_assert_held(&fp->fi_lock);
5828
Jeff Layton82c5ff12012-05-11 09:45:13 -04005829 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005830 return;
Jeff Layton12659652014-07-10 14:07:28 -04005831 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005832 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005833}
5834
Jeff Layton356a95e2014-07-29 21:34:13 -04005835static __be32
5836lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5837 struct nfs4_ol_stateid *ost,
5838 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005839 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005840{
Jeff Layton5db1c032014-07-29 21:34:28 -04005841 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005842 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005843 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5844 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005845 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005846 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005847 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005848 unsigned int strhashval;
Jeff Laytondd257932016-08-11 10:37:39 -04005849 bool hashed;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005850
Kinglong Meec8623992015-07-13 17:35:37 +08005851 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005852 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005853 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005854 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5855 if (lo == NULL)
5856 return nfserr_jukebox;
5857 } else {
5858 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005859 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005860 if (!cstate->minorversion &&
5861 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005862 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005863 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005864
Jeff Laytondd257932016-08-11 10:37:39 -04005865retry:
5866 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5867 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005868 status = nfserr_jukebox;
5869 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005870 }
Jeff Laytondd257932016-08-11 10:37:39 -04005871
5872 mutex_lock(&lst->st_mutex);
5873
5874 /* See if it's still hashed to avoid race with FREE_STATEID */
5875 spin_lock(&cl->cl_lock);
5876 hashed = !list_empty(&lst->st_perfile);
5877 spin_unlock(&cl->cl_lock);
5878
5879 if (!hashed) {
5880 mutex_unlock(&lst->st_mutex);
5881 nfs4_put_stid(&lst->st_stid);
5882 goto retry;
5883 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005884 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005885 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005886out:
5887 nfs4_put_stateowner(&lo->lo_owner);
5888 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005889}
5890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891/*
5892 * LOCK operation
5893 */
Al Virob37ad282006-10-19 23:28:59 -07005894__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005895nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005896 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005898 struct nfs4_openowner *open_sop = NULL;
5899 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005900 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005901 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005902 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005903 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005904 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005905 struct file_lock *file_lock = NULL;
5906 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005907 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005908 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005909 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005910 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005911 unsigned char fl_type;
5912 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005913 struct net *net = SVC_NET(rqstp);
5914 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
5916 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5917 (long long) lock->lk_offset,
5918 (long long) lock->lk_length);
5919
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 if (check_lock_length(lock->lk_offset, lock->lk_length))
5921 return nfserr_inval;
5922
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005923 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005924 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005925 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5926 return status;
5927 }
5928
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005930 if (nfsd4_has_session(cstate))
5931 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005932 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005933 &cstate->session->se_client->cl_clientid,
5934 sizeof(clientid_t));
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005937 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005941 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 lock->lk_new_open_seqid,
5943 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005944 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005945 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005947 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005948 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005949 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005950 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005951 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005952 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005953 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005954 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005955 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005956 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005957 lock->lk_old_lock_seqid,
5958 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005959 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005960 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005961 if (status)
5962 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005963 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005965 lkflg = setlkflg(lock->lk_type);
5966 status = nfs4_check_openmode(lock_stp, lkflg);
5967 if (status)
5968 goto out;
5969
NeilBrown0dd395d2005-07-07 17:59:15 -07005970 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005971 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005972 goto out;
5973 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005974 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005975 goto out;
5976
Trond Myklebust11b91642014-07-29 21:34:08 -04005977 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005980 if (nfsd4_has_session(cstate))
5981 fl_flags |= FL_SLEEP;
5982 /* Fallthrough */
5983 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005984 spin_lock(&fp->fi_lock);
5985 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005986 if (filp)
5987 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005988 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005989 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005992 if (nfsd4_has_session(cstate))
5993 fl_flags |= FL_SLEEP;
5994 /* Fallthrough */
5995 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005996 spin_lock(&fp->fi_lock);
5997 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005998 if (filp)
5999 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006000 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006001 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006002 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 default:
6004 status = nfserr_inval;
6005 goto out;
6006 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006007
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006008 if (!filp) {
6009 status = nfserr_openmode;
6010 goto out;
6011 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006012
Jeff Layton76d348f2016-09-16 16:28:24 -04006013 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6014 if (!nbl) {
6015 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6016 status = nfserr_jukebox;
6017 goto out;
6018 }
6019
6020 file_lock = &nbl->nbl_lock;
6021 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006022 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006023 file_lock->fl_pid = current->tgid;
6024 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006025 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006026 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6027 file_lock->fl_start = lock->lk_offset;
6028 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6029 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Jeff Layton21179d82012-08-21 08:03:32 -04006031 conflock = locks_alloc_lock();
6032 if (!conflock) {
6033 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6034 status = nfserr_jukebox;
6035 goto out;
6036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
Jeff Layton76d348f2016-09-16 16:28:24 -04006038 if (fl_flags & FL_SLEEP) {
Arnd Bergmann669210a2019-11-04 14:43:17 +01006039 nbl->nbl_time = get_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006040 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006041 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006042 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006043 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006044 }
6045
Jeff Layton21179d82012-08-21 08:03:32 -04006046 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006047 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006049 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006050 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006051 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006052 case FILE_LOCK_DEFERRED:
6053 nbl = NULL;
6054 /* Fallthrough */
6055 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006056 status = nfserr_denied;
6057 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006058 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006059 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006060 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006062 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006063 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006064 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006065 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006069 if (nbl) {
6070 /* dequeue it if we queued it before */
6071 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006072 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006073 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006074 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006075 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006076 }
6077 free_blocked_lock(nbl);
6078 }
Trond Myklebustde186432014-07-10 14:07:26 -04006079 if (filp)
6080 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006081 if (lock_stp) {
6082 /* Bump seqid manually if the 4.0 replay owner is openowner */
6083 if (cstate->replay_owner &&
6084 cstate->replay_owner != &lock_sop->lo_owner &&
6085 seqid_mutating_err(ntohl(status)))
6086 lock_sop->lo_owner.so_seqid++;
6087
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006088 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006089
Jeff Layton5db1c032014-07-29 21:34:28 -04006090 /*
6091 * If this is a new, never-before-used stateid, and we are
6092 * returning an error, then just go ahead and release it.
6093 */
6094 if (status && new)
6095 release_lock_stateid(lock_stp);
6096
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006097 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006098 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006099 if (open_stp)
6100 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006101 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006102 if (conflock)
6103 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return status;
6105}
6106
6107/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006108 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6109 * so we do a temporary open here just to get an open file to pass to
6110 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6111 * inode operation.)
6112 */
Al Viro04da6e92012-04-13 00:00:04 -04006113static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006114{
6115 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006116 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6117 if (!err) {
6118 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006119 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006120 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006121 return err;
6122}
6123
6124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 * LOCKT operation
6126 */
Al Virob37ad282006-10-19 23:28:59 -07006127__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006128nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6129 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130{
Jeff Layton21179d82012-08-21 08:03:32 -04006131 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006132 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006133 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006134 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006136 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 return nfserr_grace;
6138
6139 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6140 return nfserr_inval;
6141
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006142 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006143 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006144 if (status)
6145 goto out;
6146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006148 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
Jeff Layton21179d82012-08-21 08:03:32 -04006151 file_lock = locks_alloc_lock();
6152 if (!file_lock) {
6153 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6154 status = nfserr_jukebox;
6155 goto out;
6156 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006157
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 switch (lockt->lt_type) {
6159 case NFS4_READ_LT:
6160 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006161 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 break;
6163 case NFS4_WRITE_LT:
6164 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006165 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 break;
6167 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006168 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 status = nfserr_inval;
6170 goto out;
6171 }
6172
Kinglong Meec8623992015-07-13 17:35:37 +08006173 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006174 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006175 file_lock->fl_owner = (fl_owner_t)lo;
6176 file_lock->fl_pid = current->tgid;
6177 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
Jeff Layton21179d82012-08-21 08:03:32 -04006179 file_lock->fl_start = lockt->lt_offset;
6180 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Jeff Layton21179d82012-08-21 08:03:32 -04006182 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
Jeff Layton21179d82012-08-21 08:03:32 -04006184 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006185 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006186 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006187
Jeff Layton21179d82012-08-21 08:03:32 -04006188 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006190 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 }
6192out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006193 if (lo)
6194 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006195 if (file_lock)
6196 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 return status;
6198}
6199
Al Virob37ad282006-10-19 23:28:59 -07006200__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006201nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08006202 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006204 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006206 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006207 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006208 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006209 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6210
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6212 (long long) locku->lu_offset,
6213 (long long) locku->lu_length);
6214
6215 if (check_lock_length(locku->lu_offset, locku->lu_length))
6216 return nfserr_inval;
6217
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006218 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006219 &locku->lu_stateid, NFS4_LOCK_STID,
6220 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006221 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006223 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006224 if (!filp) {
6225 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006226 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006227 }
Jeff Layton21179d82012-08-21 08:03:32 -04006228 file_lock = locks_alloc_lock();
6229 if (!file_lock) {
6230 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6231 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006232 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006233 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006234
Jeff Layton21179d82012-08-21 08:03:32 -04006235 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006236 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006237 file_lock->fl_pid = current->tgid;
6238 file_lock->fl_file = filp;
6239 file_lock->fl_flags = FL_POSIX;
6240 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6241 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
Jeff Layton21179d82012-08-21 08:03:32 -04006243 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6244 locku->lu_length);
6245 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
Jeff Layton21179d82012-08-21 08:03:32 -04006247 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006248 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006249 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 goto out_nfserr;
6251 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006252 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006253fput:
6254 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006255put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006256 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006257 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006259 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006260 if (file_lock)
6261 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 return status;
6263
6264out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006265 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006266 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267}
6268
6269/*
6270 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006271 * true: locks held by lockowner
6272 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006274static bool
6275check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006277 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006278 int status = false;
6279 struct file *filp = find_any_file(fp);
6280 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006281 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006282
6283 if (!filp) {
6284 /* Any valid lock stateid should have some sort of access */
6285 WARN_ON_ONCE(1);
6286 return status;
6287 }
6288
6289 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006290 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006292 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006293 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006294 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6295 if (fl->fl_owner == (fl_owner_t)lowner) {
6296 status = true;
6297 break;
6298 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006299 }
Jeff Layton6109c852015-01-16 15:05:57 -05006300 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006302 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 return status;
6304}
6305
Al Virob37ad282006-10-19 23:28:59 -07006306__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006307nfsd4_release_lockowner(struct svc_rqst *rqstp,
6308 struct nfsd4_compound_state *cstate,
6309 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310{
6311 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006312 struct nfs4_stateowner *sop;
6313 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006314 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006316 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006317 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006318 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006319 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006320 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
6322 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6323 clid->cl_boot, clid->cl_id);
6324
Jeff Layton4b24ca72014-06-30 11:48:44 -04006325 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006326 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006327 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006328
Trond Myklebustd4f04892014-07-29 21:34:36 -04006329 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006330 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006331 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006332 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006333 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006334
6335 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006336 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006337
Jeff Layton882e9d22014-07-29 21:34:37 -04006338 /* see if there are still any locks associated with it */
6339 lo = lockowner(sop);
6340 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6341 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6342 status = nfserr_locks_held;
6343 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006344 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006345 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006346 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006347
Kinglong Meeb5971af2014-08-22 10:18:43 -04006348 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006349 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006350 }
Chuck Lever88584812016-07-13 16:40:14 -04006351 if (!lo) {
6352 spin_unlock(&clp->cl_lock);
6353 return status;
6354 }
6355
6356 unhash_lockowner_locked(lo);
6357 while (!list_empty(&lo->lo_owner.so_stateids)) {
6358 stp = list_first_entry(&lo->lo_owner.so_stateids,
6359 struct nfs4_ol_stateid,
6360 st_perstateowner);
6361 WARN_ON(!unhash_lock_stateid(stp));
6362 put_ol_stateid_locked(stp, &reaplist);
6363 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006364 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006365 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone294c4c2018-03-16 11:32:02 -04006366 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006367 nfs4_put_stateowner(&lo->lo_owner);
6368
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 return status;
6370}
6371
6372static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006373alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374{
NeilBrowna55370a2005-06-23 22:03:52 -07006375 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376}
6377
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006378bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006379nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006380{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006381 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006382
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006383 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006384 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006385}
6386
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387/*
6388 * failure => all reset bets are off, nfserr_no_grace...
6389 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006390struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006391nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392{
6393 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006394 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
NeilBrowna55370a2005-06-23 22:03:52 -07006396 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6397 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006398 if (crp) {
6399 strhashval = clientstr_hashval(name);
6400 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006401 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006402 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006403 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006404 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006405 }
6406 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407}
6408
Jeff Layton2a4317c2012-03-21 16:42:43 -04006409void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006410nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006411{
6412 list_del(&crp->cr_strhash);
6413 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006414 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006415}
6416
6417void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006418nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419{
6420 struct nfs4_client_reclaim *crp = NULL;
6421 int i;
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006424 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6425 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006427 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 }
6429 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006430 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431}
6432
6433/*
6434 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006435struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006436nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437{
6438 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 struct nfs4_client_reclaim *crp = NULL;
6440
Jeff Layton278c9312012-11-12 15:00:52 -05006441 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
Jeff Layton278c9312012-11-12 15:00:52 -05006443 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006444 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006445 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 return crp;
6447 }
6448 }
6449 return NULL;
6450}
6451
6452/*
6453* Called from OPEN. Look for clientid in reclaim list.
6454*/
Al Virob37ad282006-10-19 23:28:59 -07006455__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006456nfs4_check_open_reclaim(clientid_t *clid,
6457 struct nfsd4_compound_state *cstate,
6458 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006460 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006461
6462 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006463 status = lookup_clientid(clid, cstate, nn);
6464 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006465 return nfserr_reclaim_bad;
6466
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006467 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6468 return nfserr_no_grace;
6469
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006470 if (nfsd4_client_record_check(cstate->clp))
6471 return nfserr_reclaim_bad;
6472
6473 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474}
6475
Bryan Schumaker65178db2011-11-01 13:35:21 -04006476#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006477static inline void
6478put_client(struct nfs4_client *clp)
6479{
6480 atomic_dec(&clp->cl_refcount);
6481}
6482
Jeff Layton285abde2014-07-30 08:27:24 -04006483static struct nfs4_client *
6484nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6485{
6486 struct nfs4_client *clp;
6487 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6488 nfsd_net_id);
6489
6490 if (!nfsd_netns_ready(nn))
6491 return NULL;
6492
6493 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6494 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6495 return clp;
6496 }
6497 return NULL;
6498}
6499
Jeff Layton7ec0e362014-07-30 08:27:17 -04006500u64
Jeff Layton285abde2014-07-30 08:27:24 -04006501nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006502{
6503 struct nfs4_client *clp;
6504 u64 count = 0;
6505 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6506 nfsd_net_id);
6507 char buf[INET6_ADDRSTRLEN];
6508
6509 if (!nfsd_netns_ready(nn))
6510 return 0;
6511
6512 spin_lock(&nn->client_lock);
6513 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6514 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6515 pr_info("NFS Client: %s\n", buf);
6516 ++count;
6517 }
6518 spin_unlock(&nn->client_lock);
6519
6520 return count;
6521}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006522
Jeff Laytona0926d12014-07-30 08:27:18 -04006523u64
Jeff Layton285abde2014-07-30 08:27:24 -04006524nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006525{
6526 u64 count = 0;
6527 struct nfs4_client *clp;
6528 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6529 nfsd_net_id);
6530
6531 if (!nfsd_netns_ready(nn))
6532 return count;
6533
6534 spin_lock(&nn->client_lock);
6535 clp = nfsd_find_client(addr, addr_size);
6536 if (clp) {
6537 if (mark_client_expired_locked(clp) == nfs_ok)
6538 ++count;
6539 else
6540 clp = NULL;
6541 }
6542 spin_unlock(&nn->client_lock);
6543
6544 if (clp)
6545 expire_client(clp);
6546
6547 return count;
6548}
6549
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006550u64
Jeff Layton285abde2014-07-30 08:27:24 -04006551nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006552{
6553 u64 count = 0;
6554 struct nfs4_client *clp, *next;
6555 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6556 nfsd_net_id);
6557 LIST_HEAD(reaplist);
6558
6559 if (!nfsd_netns_ready(nn))
6560 return count;
6561
6562 spin_lock(&nn->client_lock);
6563 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6564 if (mark_client_expired_locked(clp) == nfs_ok) {
6565 list_add(&clp->cl_lru, &reaplist);
6566 if (max != 0 && ++count >= max)
6567 break;
6568 }
6569 }
6570 spin_unlock(&nn->client_lock);
6571
6572 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6573 expire_client(clp);
6574
6575 return count;
6576}
6577
Bryan Schumaker184c1842012-11-29 11:40:44 -05006578static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6579 const char *type)
6580{
6581 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006582 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006583 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6584}
6585
Jeff Layton016200c2014-07-30 08:27:21 -04006586static void
6587nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6588 struct list_head *collect)
6589{
6590 struct nfs4_client *clp = lst->st_stid.sc_client;
6591 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6592 nfsd_net_id);
6593
6594 if (!collect)
6595 return;
6596
6597 lockdep_assert_held(&nn->client_lock);
6598 atomic_inc(&clp->cl_refcount);
6599 list_add(&lst->st_locks, collect);
6600}
6601
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006602static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006603 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006604 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006605{
6606 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006607 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006608 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006609 u64 count = 0;
6610
Jeff Layton016200c2014-07-30 08:27:21 -04006611 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006612 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006613 list_for_each_entry_safe(stp, st_next,
6614 &oop->oo_owner.so_stateids, st_perstateowner) {
6615 list_for_each_entry_safe(lst, lst_next,
6616 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006617 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006618 if (func(lst))
6619 nfsd_inject_add_lock_to_list(lst,
6620 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006621 }
Jeff Layton016200c2014-07-30 08:27:21 -04006622 ++count;
6623 /*
6624 * Despite the fact that these functions deal
6625 * with 64-bit integers for "count", we must
6626 * ensure that it doesn't blow up the
6627 * clp->cl_refcount. Throw a warning if we
6628 * start to approach INT_MAX here.
6629 */
6630 WARN_ON_ONCE(count == (INT_MAX / 2));
6631 if (count == max)
6632 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006633 }
6634 }
6635 }
Jeff Layton016200c2014-07-30 08:27:21 -04006636out:
6637 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006638
6639 return count;
6640}
6641
Jeff Layton016200c2014-07-30 08:27:21 -04006642static u64
6643nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6644 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006645{
Jeff Layton016200c2014-07-30 08:27:21 -04006646 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006647}
6648
Jeff Layton016200c2014-07-30 08:27:21 -04006649static u64
6650nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006651{
Jeff Layton016200c2014-07-30 08:27:21 -04006652 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006653 nfsd_print_count(clp, count, "locked files");
6654 return count;
6655}
6656
Jeff Layton016200c2014-07-30 08:27:21 -04006657u64
Jeff Layton285abde2014-07-30 08:27:24 -04006658nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006659{
6660 struct nfs4_client *clp;
6661 u64 count = 0;
6662 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6663 nfsd_net_id);
6664
6665 if (!nfsd_netns_ready(nn))
6666 return 0;
6667
6668 spin_lock(&nn->client_lock);
6669 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6670 count += nfsd_print_client_locks(clp);
6671 spin_unlock(&nn->client_lock);
6672
6673 return count;
6674}
6675
6676static void
6677nfsd_reap_locks(struct list_head *reaplist)
6678{
6679 struct nfs4_client *clp;
6680 struct nfs4_ol_stateid *stp, *next;
6681
6682 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6683 list_del_init(&stp->st_locks);
6684 clp = stp->st_stid.sc_client;
6685 nfs4_put_stid(&stp->st_stid);
6686 put_client(clp);
6687 }
6688}
6689
6690u64
Jeff Layton285abde2014-07-30 08:27:24 -04006691nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006692{
6693 unsigned int count = 0;
6694 struct nfs4_client *clp;
6695 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6696 nfsd_net_id);
6697 LIST_HEAD(reaplist);
6698
6699 if (!nfsd_netns_ready(nn))
6700 return count;
6701
6702 spin_lock(&nn->client_lock);
6703 clp = nfsd_find_client(addr, addr_size);
6704 if (clp)
6705 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6706 spin_unlock(&nn->client_lock);
6707 nfsd_reap_locks(&reaplist);
6708 return count;
6709}
6710
6711u64
Jeff Layton285abde2014-07-30 08:27:24 -04006712nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006713{
6714 u64 count = 0;
6715 struct nfs4_client *clp;
6716 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6717 nfsd_net_id);
6718 LIST_HEAD(reaplist);
6719
6720 if (!nfsd_netns_ready(nn))
6721 return count;
6722
6723 spin_lock(&nn->client_lock);
6724 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6725 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6726 if (max != 0 && count >= max)
6727 break;
6728 }
6729 spin_unlock(&nn->client_lock);
6730 nfsd_reap_locks(&reaplist);
6731 return count;
6732}
6733
Jeff Layton82e05ef2014-07-30 08:27:22 -04006734static u64
6735nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6736 struct list_head *collect,
6737 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006738{
6739 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006740 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6741 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006742 u64 count = 0;
6743
Jeff Layton82e05ef2014-07-30 08:27:22 -04006744 lockdep_assert_held(&nn->client_lock);
6745
6746 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006747 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006748 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006749 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006750 if (collect) {
6751 atomic_inc(&clp->cl_refcount);
6752 list_add(&oop->oo_perclient, collect);
6753 }
6754 }
6755 ++count;
6756 /*
6757 * Despite the fact that these functions deal with
6758 * 64-bit integers for "count", we must ensure that
6759 * it doesn't blow up the clp->cl_refcount. Throw a
6760 * warning if we start to approach INT_MAX here.
6761 */
6762 WARN_ON_ONCE(count == (INT_MAX / 2));
6763 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006764 break;
6765 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006766 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006767
6768 return count;
6769}
6770
Jeff Layton82e05ef2014-07-30 08:27:22 -04006771static u64
6772nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006773{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006774 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6775
6776 nfsd_print_count(clp, count, "openowners");
6777 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006778}
6779
Jeff Layton82e05ef2014-07-30 08:27:22 -04006780static u64
6781nfsd_collect_client_openowners(struct nfs4_client *clp,
6782 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006783{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006784 return nfsd_foreach_client_openowner(clp, max, collect,
6785 unhash_openowner_locked);
6786}
6787
6788u64
Jeff Layton285abde2014-07-30 08:27:24 -04006789nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006790{
6791 struct nfs4_client *clp;
6792 u64 count = 0;
6793 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6794 nfsd_net_id);
6795
6796 if (!nfsd_netns_ready(nn))
6797 return 0;
6798
6799 spin_lock(&nn->client_lock);
6800 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6801 count += nfsd_print_client_openowners(clp);
6802 spin_unlock(&nn->client_lock);
6803
6804 return count;
6805}
6806
6807static void
6808nfsd_reap_openowners(struct list_head *reaplist)
6809{
6810 struct nfs4_client *clp;
6811 struct nfs4_openowner *oop, *next;
6812
6813 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6814 list_del_init(&oop->oo_perclient);
6815 clp = oop->oo_owner.so_client;
6816 release_openowner(oop);
6817 put_client(clp);
6818 }
6819}
6820
6821u64
Jeff Layton285abde2014-07-30 08:27:24 -04006822nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6823 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006824{
6825 unsigned int count = 0;
6826 struct nfs4_client *clp;
6827 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6828 nfsd_net_id);
6829 LIST_HEAD(reaplist);
6830
6831 if (!nfsd_netns_ready(nn))
6832 return count;
6833
6834 spin_lock(&nn->client_lock);
6835 clp = nfsd_find_client(addr, addr_size);
6836 if (clp)
6837 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6838 spin_unlock(&nn->client_lock);
6839 nfsd_reap_openowners(&reaplist);
6840 return count;
6841}
6842
6843u64
Jeff Layton285abde2014-07-30 08:27:24 -04006844nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006845{
6846 u64 count = 0;
6847 struct nfs4_client *clp;
6848 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6849 nfsd_net_id);
6850 LIST_HEAD(reaplist);
6851
6852 if (!nfsd_netns_ready(nn))
6853 return count;
6854
6855 spin_lock(&nn->client_lock);
6856 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6857 count += nfsd_collect_client_openowners(clp, &reaplist,
6858 max - count);
6859 if (max != 0 && count >= max)
6860 break;
6861 }
6862 spin_unlock(&nn->client_lock);
6863 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006864 return count;
6865}
6866
Bryan Schumaker269de302012-11-29 11:40:42 -05006867static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6868 struct list_head *victims)
6869{
6870 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006871 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6872 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006873 u64 count = 0;
6874
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006875 lockdep_assert_held(&nn->client_lock);
6876
6877 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006878 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006879 if (victims) {
6880 /*
6881 * It's not safe to mess with delegations that have a
6882 * non-zero dl_time. They might have already been broken
6883 * and could be processed by the laundromat outside of
6884 * the state_lock. Just leave them be.
6885 */
6886 if (dp->dl_time != 0)
6887 continue;
6888
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006889 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006890 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006891 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006892 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006893 ++count;
6894 /*
6895 * Despite the fact that these functions deal with
6896 * 64-bit integers for "count", we must ensure that
6897 * it doesn't blow up the clp->cl_refcount. Throw a
6898 * warning if we start to approach INT_MAX here.
6899 */
6900 WARN_ON_ONCE(count == (INT_MAX / 2));
6901 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006902 break;
6903 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006904 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006905 return count;
6906}
6907
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006908static u64
6909nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006910{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006911 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006912
6913 nfsd_print_count(clp, count, "delegations");
6914 return count;
6915}
6916
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006917u64
Jeff Layton285abde2014-07-30 08:27:24 -04006918nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006919{
6920 struct nfs4_client *clp;
6921 u64 count = 0;
6922 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6923 nfsd_net_id);
6924
6925 if (!nfsd_netns_ready(nn))
6926 return 0;
6927
6928 spin_lock(&nn->client_lock);
6929 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6930 count += nfsd_print_client_delegations(clp);
6931 spin_unlock(&nn->client_lock);
6932
6933 return count;
6934}
6935
6936static void
6937nfsd_forget_delegations(struct list_head *reaplist)
6938{
6939 struct nfs4_client *clp;
6940 struct nfs4_delegation *dp, *next;
6941
6942 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6943 list_del_init(&dp->dl_recall_lru);
6944 clp = dp->dl_stid.sc_client;
6945 revoke_delegation(dp);
6946 put_client(clp);
6947 }
6948}
6949
6950u64
Jeff Layton285abde2014-07-30 08:27:24 -04006951nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6952 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006953{
6954 u64 count = 0;
6955 struct nfs4_client *clp;
6956 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6957 nfsd_net_id);
6958 LIST_HEAD(reaplist);
6959
6960 if (!nfsd_netns_ready(nn))
6961 return count;
6962
6963 spin_lock(&nn->client_lock);
6964 clp = nfsd_find_client(addr, addr_size);
6965 if (clp)
6966 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6967 spin_unlock(&nn->client_lock);
6968
6969 nfsd_forget_delegations(&reaplist);
6970 return count;
6971}
6972
6973u64
Jeff Layton285abde2014-07-30 08:27:24 -04006974nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006975{
6976 u64 count = 0;
6977 struct nfs4_client *clp;
6978 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6979 nfsd_net_id);
6980 LIST_HEAD(reaplist);
6981
6982 if (!nfsd_netns_ready(nn))
6983 return count;
6984
6985 spin_lock(&nn->client_lock);
6986 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6987 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6988 if (max != 0 && count >= max)
6989 break;
6990 }
6991 spin_unlock(&nn->client_lock);
6992 nfsd_forget_delegations(&reaplist);
6993 return count;
6994}
6995
6996static void
6997nfsd_recall_delegations(struct list_head *reaplist)
6998{
6999 struct nfs4_client *clp;
7000 struct nfs4_delegation *dp, *next;
7001
7002 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7003 list_del_init(&dp->dl_recall_lru);
7004 clp = dp->dl_stid.sc_client;
7005 /*
7006 * We skipped all entries that had a zero dl_time before,
7007 * so we can now reset the dl_time back to 0. If a delegation
7008 * break comes in now, then it won't make any difference since
7009 * we're recalling it either way.
7010 */
7011 spin_lock(&state_lock);
7012 dp->dl_time = 0;
7013 spin_unlock(&state_lock);
7014 nfsd_break_one_deleg(dp);
7015 put_client(clp);
7016 }
7017}
7018
7019u64
Jeff Layton285abde2014-07-30 08:27:24 -04007020nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007021 size_t addr_size)
7022{
7023 u64 count = 0;
7024 struct nfs4_client *clp;
7025 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7026 nfsd_net_id);
7027 LIST_HEAD(reaplist);
7028
7029 if (!nfsd_netns_ready(nn))
7030 return count;
7031
7032 spin_lock(&nn->client_lock);
7033 clp = nfsd_find_client(addr, addr_size);
7034 if (clp)
7035 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7036 spin_unlock(&nn->client_lock);
7037
7038 nfsd_recall_delegations(&reaplist);
7039 return count;
7040}
7041
7042u64
Jeff Layton285abde2014-07-30 08:27:24 -04007043nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007044{
7045 u64 count = 0;
7046 struct nfs4_client *clp, *next;
7047 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7048 nfsd_net_id);
7049 LIST_HEAD(reaplist);
7050
7051 if (!nfsd_netns_ready(nn))
7052 return count;
7053
7054 spin_lock(&nn->client_lock);
7055 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7056 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7057 if (max != 0 && ++count >= max)
7058 break;
7059 }
7060 spin_unlock(&nn->client_lock);
7061 nfsd_recall_delegations(&reaplist);
7062 return count;
7063}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007064#endif /* CONFIG_NFSD_FAULT_INJECTION */
7065
Meelap Shahc2f1a552007-07-17 04:04:39 -07007066/*
7067 * Since the lifetime of a delegation isn't limited to that of an open, a
7068 * client may quite reasonably hang on to a delegation as long as it has
7069 * the inode cached. This becomes an obvious problem the first time a
7070 * client's inode cache approaches the size of the server's total memory.
7071 *
7072 * For now we avoid this problem by imposing a hard limit on the number
7073 * of delegations, which varies according to the server's memory size.
7074 */
7075static void
7076set_max_delegations(void)
7077{
7078 /*
7079 * Allow at most 4 delegations per megabyte of RAM. Quick
7080 * estimates suggest that in the worst case (where every delegation
7081 * is for a different inode), a delegation could take about 1.5K,
7082 * giving a worst case usage of about 6% of memory.
7083 */
7084 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7085}
7086
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007087static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007088{
7089 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7090 int i;
7091
7092 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
7093 CLIENT_HASH_SIZE, GFP_KERNEL);
7094 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007095 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007096 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
7097 CLIENT_HASH_SIZE, GFP_KERNEL);
7098 if (!nn->unconf_id_hashtbl)
7099 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007100 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
7101 SESSION_HASH_SIZE, GFP_KERNEL);
7102 if (!nn->sessionid_hashtbl)
7103 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007104
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007105 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007106 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007107 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007108 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007109 for (i = 0; i < SESSION_HASH_SIZE; i++)
7110 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007111 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007112 nn->unconf_name_tree = RB_ROOT;
Vasily Averin3b774232017-11-13 07:25:40 +03007113 nn->boot_time = get_seconds();
7114 nn->grace_ended = false;
7115 nn->nfsd4_manager.block_opens = true;
7116 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007117 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007118 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007119 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007120 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007121
Jeff Layton0cc11a62016-10-20 09:34:31 -04007122 spin_lock_init(&nn->blocked_locks_lock);
7123 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7124
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007125 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007126 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007127
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007128 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007129
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007130err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007131 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007132err_unconf_id:
7133 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007134err:
7135 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007136}
7137
7138static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007139nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007140{
7141 int i;
7142 struct nfs4_client *clp = NULL;
7143 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7144
7145 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7146 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7147 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7148 destroy_client(clp);
7149 }
7150 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007151
Jeff Laytone294c4c2018-03-16 11:32:02 -04007152 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7153
Kinglong Mee2b905632014-03-26 22:09:30 +08007154 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7155 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7156 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7157 destroy_client(clp);
7158 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007159 }
7160
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007161 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007162 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007163 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007164 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007165}
7166
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007167int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007168nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007169{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007170 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007171 int ret;
7172
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007173 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007174 if (ret)
7175 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007176 locks_start_grace(net, &nn->nfsd4_manager);
7177 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007178 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007179 nn->nfsd4_grace, net);
7180 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007181 return 0;
7182}
7183
7184/* initialization to perform when the nfsd service is started: */
7185
7186int
7187nfs4_state_start(void)
7188{
7189 int ret;
7190
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007191 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007192 if (ret)
Kinglong Meed4bda742017-02-05 09:57:07 +08007193 return ret;
7194
Jeff Layton51a54562015-08-20 07:17:01 -04007195 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007196 if (laundry_wq == NULL) {
7197 ret = -ENOMEM;
Kinglong Meed4bda742017-02-05 09:57:07 +08007198 goto out_cleanup_cred;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007199 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007200 ret = nfsd4_create_callback_queue();
7201 if (ret)
7202 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007203
Meelap Shahc2f1a552007-07-17 04:04:39 -07007204 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007205 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007206
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007207out_free_laundry:
7208 destroy_workqueue(laundry_wq);
Kinglong Meed4bda742017-02-05 09:57:07 +08007209out_cleanup_cred:
7210 cleanup_callback_cred();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212}
7213
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007214void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007215nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007219 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007221 cancel_delayed_work_sync(&nn->laundromat_work);
7222 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007223
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04007225 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007226 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007228 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007229 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 }
Benny Halevycdc97502014-05-30 09:09:30 -04007231 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 list_for_each_safe(pos, next, &reaplist) {
7233 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007234 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02007235 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04007236 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04007237 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 }
7239
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007240 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007241 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242}
7243
7244void
7245nfs4_state_shutdown(void)
7246{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007247 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007248 nfsd4_destroy_callback_queue();
Kinglong Meed4bda742017-02-05 09:57:07 +08007249 cleanup_callback_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007251
7252static void
7253get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7254{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007255 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7256 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007257}
7258
7259static void
7260put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7261{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007262 if (cstate->minorversion) {
7263 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7264 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7265 }
7266}
7267
7268void
7269clear_current_stateid(struct nfsd4_compound_state *cstate)
7270{
7271 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007272}
7273
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007274/*
7275 * functions to set current state id
7276 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007277void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007278nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7279{
7280 put_stateid(cstate, &odp->od_stateid);
7281}
7282
7283void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007284nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
7285{
7286 put_stateid(cstate, &open->op_stateid);
7287}
7288
7289void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007290nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7291{
7292 put_stateid(cstate, &close->cl_stateid);
7293}
7294
7295void
7296nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
7297{
7298 put_stateid(cstate, &lock->lk_resp_stateid);
7299}
7300
7301/*
7302 * functions to consume current state id
7303 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007304
7305void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007306nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7307{
7308 get_stateid(cstate, &odp->od_stateid);
7309}
7310
7311void
7312nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7313{
7314 get_stateid(cstate, &drp->dr_stateid);
7315}
7316
7317void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007318nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7319{
7320 get_stateid(cstate, &fsp->fr_stateid);
7321}
7322
7323void
7324nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7325{
7326 get_stateid(cstate, &setattr->sa_stateid);
7327}
7328
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007329void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007330nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7331{
7332 get_stateid(cstate, &close->cl_stateid);
7333}
7334
7335void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007336nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007337{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007338 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007339}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007340
7341void
7342nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7343{
7344 get_stateid(cstate, &read->rd_stateid);
7345}
7346
7347void
7348nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7349{
7350 get_stateid(cstate, &write->wr_stateid);
7351}