blob: c548190ef5ef0ecaa82d4ed930430af8d622f31d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020045#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040046#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050047#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040048#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040050#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020051#include "pnfs.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define NFSDDBG_FACILITY NFSDDBG_PROC
54
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050055#define all_ones {{~0,~0},~0}
56static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59};
60static const stateid_t zero_stateid = {
61 /* all fields zero */
62};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010063static const stateid_t currentstateid = {
64 .si_generation = 1,
65};
Trond Myklebust5cd35862017-11-03 08:00:12 -040066static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
68};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050069
Andy Adamsonec6b5d72009-04-03 08:28:28 +030070static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070071
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050072#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010074#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elblef25e2222017-11-09 13:41:10 -050075#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040078static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040079static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
J. Bruce Fields8b671b82009-02-22 14:51:34 -080081/* Locking: */
82
J. Bruce Fields8b671b82009-02-22 14:51:34 -080083/*
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
87 */
Benny Halevycdc97502014-05-30 09:09:30 -040088static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080089
Jeff Laytonb401be222014-07-29 21:34:33 -040090/*
91 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
92 * the refcount on the open stateid to drop.
93 */
94static DECLARE_WAIT_QUEUE_HEAD(close_wq);
95
Christoph Hellwigabf11352014-05-21 07:43:03 -070096static struct kmem_cache *openowner_slab;
97static struct kmem_cache *lockowner_slab;
98static struct kmem_cache *file_slab;
99static struct kmem_cache *stateid_slab;
100static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200101static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700102
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400103static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800104
Julia Lawallc4cb8972015-11-21 22:57:39 +0100105static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400106static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200107
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400108static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800109{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400110 return ses->se_flags & NFS4_SESSION_DEAD;
111}
112
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400113static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
114{
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
118 return nfs_ok;
119}
120
J. Bruce Fields221a6872013-04-01 22:23:49 -0400121static bool is_client_expired(struct nfs4_client *clp)
122{
123 return clp->cl_time == 0;
124}
125
J. Bruce Fields221a6872013-04-01 22:23:49 -0400126static __be32 get_client_locked(struct nfs4_client *clp)
127{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400128 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
129
130 lockdep_assert_held(&nn->client_lock);
131
J. Bruce Fields221a6872013-04-01 22:23:49 -0400132 if (is_client_expired(clp))
133 return nfserr_expired;
134 atomic_inc(&clp->cl_refcount);
135 return nfs_ok;
136}
137
138/* must be called under the client_lock */
139static inline void
140renew_client_locked(struct nfs4_client *clp)
141{
142 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
143
144 if (is_client_expired(clp)) {
145 WARN_ON(1);
146 printk("%s: client (clientid %08x/%08x) already expired\n",
147 __func__,
148 clp->cl_clientid.cl_boot,
149 clp->cl_clientid.cl_id);
150 return;
151 }
152
153 dprintk("renewing client (clientid %08x/%08x)\n",
154 clp->cl_clientid.cl_boot,
155 clp->cl_clientid.cl_id);
156 list_move_tail(&clp->cl_lru, &nn->client_lru);
157 clp->cl_time = get_seconds();
158}
159
Fengguang Wuba138432013-04-16 22:14:15 -0400160static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400161{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400162 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
163
164 lockdep_assert_held(&nn->client_lock);
165
J. Bruce Fields221a6872013-04-01 22:23:49 -0400166 if (!atomic_dec_and_test(&clp->cl_refcount))
167 return;
168 if (!is_client_expired(clp))
169 renew_client_locked(clp);
170}
171
Jeff Layton4b24ca72014-06-30 11:48:44 -0400172static void put_client_renew(struct nfs4_client *clp)
173{
174 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
175
Jeff Laytond6c249b2014-07-08 14:02:50 -0400176 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
177 return;
178 if (!is_client_expired(clp))
179 renew_client_locked(clp);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400180 spin_unlock(&nn->client_lock);
181}
182
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400183static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
184{
185 __be32 status;
186
187 if (is_session_dead(ses))
188 return nfserr_badsession;
189 status = get_client_locked(ses->se_client);
190 if (status)
191 return status;
192 atomic_inc(&ses->se_ref);
193 return nfs_ok;
194}
195
196static void nfsd4_put_session_locked(struct nfsd4_session *ses)
197{
198 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400199 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
200
201 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400202
203 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
204 free_session(ses);
205 put_client_renew_locked(clp);
206}
207
208static void nfsd4_put_session(struct nfsd4_session *ses)
209{
210 struct nfs4_client *clp = ses->se_client;
211 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
212
213 spin_lock(&nn->client_lock);
214 nfsd4_put_session_locked(ses);
215 spin_unlock(&nn->client_lock);
216}
217
Jeff Layton76d348f2016-09-16 16:28:24 -0400218static struct nfsd4_blocked_lock *
219find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
220 struct nfsd_net *nn)
221{
222 struct nfsd4_blocked_lock *cur, *found = NULL;
223
Jeff Layton0cc11a62016-10-20 09:34:31 -0400224 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400225 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
226 if (fh_match(fh, &cur->nbl_fh)) {
227 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400228 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400229 found = cur;
230 break;
231 }
232 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400233 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400234 if (found)
235 posix_unblock_lock(&found->nbl_lock);
236 return found;
237}
238
239static struct nfsd4_blocked_lock *
240find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
241 struct nfsd_net *nn)
242{
243 struct nfsd4_blocked_lock *nbl;
244
245 nbl = find_blocked_lock(lo, fh, nn);
246 if (!nbl) {
247 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
248 if (nbl) {
249 fh_copy_shallow(&nbl->nbl_fh, fh);
250 locks_init_lock(&nbl->nbl_lock);
251 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
252 &nfsd4_cb_notify_lock_ops,
253 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
254 }
255 }
256 return nbl;
257}
258
259static void
260free_blocked_lock(struct nfsd4_blocked_lock *nbl)
261{
262 locks_release_private(&nbl->nbl_lock);
263 kfree(nbl);
264}
265
266static int
267nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
268{
269 /*
270 * Since this is just an optimization, we don't try very hard if it
271 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
272 * just quit trying on anything else.
273 */
274 switch (task->tk_status) {
275 case -NFS4ERR_DELAY:
276 rpc_delay(task, 1 * HZ);
277 return 0;
278 default:
279 return 1;
280 }
281}
282
283static void
284nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
285{
286 struct nfsd4_blocked_lock *nbl = container_of(cb,
287 struct nfsd4_blocked_lock, nbl_cb);
288
289 free_blocked_lock(nbl);
290}
291
292static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
293 .done = nfsd4_cb_notify_lock_done,
294 .release = nfsd4_cb_notify_lock_release,
295};
296
Kinglong Meeb5971af2014-08-22 10:18:43 -0400297static inline struct nfs4_stateowner *
298nfs4_get_stateowner(struct nfs4_stateowner *sop)
299{
300 atomic_inc(&sop->so_count);
301 return sop;
302}
303
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400304static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400305same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400306{
307 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400308 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400309}
310
311static struct nfs4_openowner *
312find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400313 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400314{
315 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400316
Trond Myklebustd4f04892014-07-29 21:34:36 -0400317 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400318
Trond Myklebustd4f04892014-07-29 21:34:36 -0400319 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
320 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400321 if (!so->so_is_open_owner)
322 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400323 if (same_owner_str(so, &open->op_owner))
324 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400325 }
326 return NULL;
327}
328
329static struct nfs4_openowner *
330find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400331 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400332{
333 struct nfs4_openowner *oo;
334
Trond Myklebustd4f04892014-07-29 21:34:36 -0400335 spin_lock(&clp->cl_lock);
336 oo = find_openstateowner_str_locked(hashval, open, clp);
337 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400338 return oo;
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static inline u32
342opaque_hashval(const void *ptr, int nbytes)
343{
344 unsigned char *cptr = (unsigned char *) ptr;
345
346 u32 x = 0;
347 while (nbytes--) {
348 x *= 37;
349 x += *cptr++;
350 }
351 return x;
352}
353
Jeff Layton5b095e92014-10-23 08:01:02 -0400354static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400355{
Jeff Layton5b095e92014-10-23 08:01:02 -0400356 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
357
358 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400359}
360
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200361void
NeilBrown13cd2182005-06-23 22:03:10 -0700362put_nfs4_file(struct nfs4_file *fi)
363{
Jeff Layton02e12152014-07-16 10:31:57 -0400364 might_lock(&state_lock);
365
Benny Halevycdc97502014-05-30 09:09:30 -0400366 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400367 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc97502014-05-30 09:09:30 -0400368 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200369 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400370 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
371 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800372 }
NeilBrown13cd2182005-06-23 22:03:10 -0700373}
374
Trond Myklebustde186432014-07-10 14:07:26 -0400375static struct file *
376__nfs4_get_fd(struct nfs4_file *f, int oflag)
377{
378 if (f->fi_fds[oflag])
379 return get_file(f->fi_fds[oflag]);
380 return NULL;
381}
382
383static struct file *
384find_writeable_file_locked(struct nfs4_file *f)
385{
386 struct file *ret;
387
388 lockdep_assert_held(&f->fi_lock);
389
390 ret = __nfs4_get_fd(f, O_WRONLY);
391 if (!ret)
392 ret = __nfs4_get_fd(f, O_RDWR);
393 return ret;
394}
395
396static struct file *
397find_writeable_file(struct nfs4_file *f)
398{
399 struct file *ret;
400
401 spin_lock(&f->fi_lock);
402 ret = find_writeable_file_locked(f);
403 spin_unlock(&f->fi_lock);
404
405 return ret;
406}
407
408static struct file *find_readable_file_locked(struct nfs4_file *f)
409{
410 struct file *ret;
411
412 lockdep_assert_held(&f->fi_lock);
413
414 ret = __nfs4_get_fd(f, O_RDONLY);
415 if (!ret)
416 ret = __nfs4_get_fd(f, O_RDWR);
417 return ret;
418}
419
420static struct file *
421find_readable_file(struct nfs4_file *f)
422{
423 struct file *ret;
424
425 spin_lock(&f->fi_lock);
426 ret = find_readable_file_locked(f);
427 spin_unlock(&f->fi_lock);
428
429 return ret;
430}
431
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500432struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400433find_any_file(struct nfs4_file *f)
434{
435 struct file *ret;
436
437 spin_lock(&f->fi_lock);
438 ret = __nfs4_get_fd(f, O_RDWR);
439 if (!ret) {
440 ret = __nfs4_get_fd(f, O_WRONLY);
441 if (!ret)
442 ret = __nfs4_get_fd(f, O_RDONLY);
443 }
444 spin_unlock(&f->fi_lock);
445 return ret;
446}
447
Trond Myklebust02a35082014-07-25 07:34:22 -0400448static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800449unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700450
451/*
452 * Open owner state (share locks)
453 */
454
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500455/* hash tables for lock and open owners */
456#define OWNER_HASH_BITS 8
457#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
458#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700459
Trond Myklebustd4f04892014-07-29 21:34:36 -0400460static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400461{
462 unsigned int ret;
463
464 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500465 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400466}
NeilBrownef0f3392006-04-10 22:55:41 -0700467
NeilBrownef0f3392006-04-10 22:55:41 -0700468/* hash table for nfs4_file */
469#define FILE_HASH_BITS 8
470#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800471
Trond Myklebustca943212014-07-23 16:17:39 -0400472static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400473{
Trond Myklebustca943212014-07-23 16:17:39 -0400474 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
475}
476
477static unsigned int file_hashval(struct knfsd_fh *fh)
478{
479 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
480}
481
Jeff Layton89876f82013-04-02 09:01:59 -0400482static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700483
Jeff Layton12659652014-07-10 14:07:28 -0400484static void
485__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400486{
Jeff Layton7214e862014-07-10 14:07:33 -0400487 lockdep_assert_held(&fp->fi_lock);
488
Jeff Layton12659652014-07-10 14:07:28 -0400489 if (access & NFS4_SHARE_ACCESS_WRITE)
490 atomic_inc(&fp->fi_access[O_WRONLY]);
491 if (access & NFS4_SHARE_ACCESS_READ)
492 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400493}
494
Jeff Layton12659652014-07-10 14:07:28 -0400495static __be32
496nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400497{
Jeff Layton7214e862014-07-10 14:07:33 -0400498 lockdep_assert_held(&fp->fi_lock);
499
Jeff Layton12659652014-07-10 14:07:28 -0400500 /* Does this access mode make sense? */
501 if (access & ~NFS4_SHARE_ACCESS_BOTH)
502 return nfserr_inval;
503
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400504 /* Does it conflict with a deny mode already set? */
505 if ((access & fp->fi_share_deny) != 0)
506 return nfserr_share_denied;
507
Jeff Layton12659652014-07-10 14:07:28 -0400508 __nfs4_file_get_access(fp, access);
509 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400510}
511
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400512static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
513{
514 /* Common case is that there is no deny mode. */
515 if (deny) {
516 /* Does this deny mode make sense? */
517 if (deny & ~NFS4_SHARE_DENY_BOTH)
518 return nfserr_inval;
519
520 if ((deny & NFS4_SHARE_DENY_READ) &&
521 atomic_read(&fp->fi_access[O_RDONLY]))
522 return nfserr_share_denied;
523
524 if ((deny & NFS4_SHARE_DENY_WRITE) &&
525 atomic_read(&fp->fi_access[O_WRONLY]))
526 return nfserr_share_denied;
527 }
528 return nfs_ok;
529}
530
J. Bruce Fields998db522010-08-07 09:21:41 -0400531static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400532{
Trond Myklebustde186432014-07-10 14:07:26 -0400533 might_lock(&fp->fi_lock);
534
535 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
536 struct file *f1 = NULL;
537 struct file *f2 = NULL;
538
Jeff Layton6d338b52014-07-10 14:07:29 -0400539 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400540 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400541 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400542 spin_unlock(&fp->fi_lock);
543 if (f1)
544 fput(f1);
545 if (f2)
546 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400547 }
548}
549
Jeff Layton12659652014-07-10 14:07:28 -0400550static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400551{
Jeff Layton12659652014-07-10 14:07:28 -0400552 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
553
554 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400555 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400556 if (access & NFS4_SHARE_ACCESS_READ)
557 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400558}
559
Sachin Bhamare8287f002015-04-27 14:50:14 +0200560/*
561 * Allocate a new open/delegation state counter. This is needed for
562 * pNFS for proper return on close semantics.
563 *
564 * Note that we only allocate it for pNFS-enabled exports, otherwise
565 * all pointers to struct nfs4_clnt_odstate are always NULL.
566 */
567static struct nfs4_clnt_odstate *
568alloc_clnt_odstate(struct nfs4_client *clp)
569{
570 struct nfs4_clnt_odstate *co;
571
572 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
573 if (co) {
574 co->co_client = clp;
575 atomic_set(&co->co_odcount, 1);
576 }
577 return co;
578}
579
580static void
581hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
582{
583 struct nfs4_file *fp = co->co_file;
584
585 lockdep_assert_held(&fp->fi_lock);
586 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
587}
588
589static inline void
590get_clnt_odstate(struct nfs4_clnt_odstate *co)
591{
592 if (co)
593 atomic_inc(&co->co_odcount);
594}
595
596static void
597put_clnt_odstate(struct nfs4_clnt_odstate *co)
598{
599 struct nfs4_file *fp;
600
601 if (!co)
602 return;
603
604 fp = co->co_file;
605 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
606 list_del(&co->co_perfile);
607 spin_unlock(&fp->fi_lock);
608
609 nfsd4_return_all_file_layouts(co->co_client, fp);
610 kmem_cache_free(odstate_slab, co);
611 }
612}
613
614static struct nfs4_clnt_odstate *
615find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
616{
617 struct nfs4_clnt_odstate *co;
618 struct nfs4_client *cl;
619
620 if (!new)
621 return NULL;
622
623 cl = new->co_client;
624
625 spin_lock(&fp->fi_lock);
626 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
627 if (co->co_client == cl) {
628 get_clnt_odstate(co);
629 goto out;
630 }
631 }
632 co = new;
633 co->co_file = fp;
634 hash_clnt_odstate_locked(new);
635out:
636 spin_unlock(&fp->fi_lock);
637 return co;
638}
639
Kinglong Mee743146d32017-01-18 19:04:42 +0800640struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
641 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400642{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500643 struct nfs4_stid *stid;
644 int new_id;
645
Trond Myklebustf8338832014-07-25 07:34:19 -0400646 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500647 if (!stid)
648 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400649
Jeff Layton4770d722014-07-29 21:34:10 -0400650 idr_preload(GFP_KERNEL);
651 spin_lock(&cl->cl_lock);
652 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
653 spin_unlock(&cl->cl_lock);
654 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700655 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500656 goto out_free;
Kinglong Mee743146d32017-01-18 19:04:42 +0800657
658 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500659 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500660 stid->sc_stateid.si_opaque.so_id = new_id;
661 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
662 /* Will be incremented before return to client: */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -0400663 atomic_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400664 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500665
J. Bruce Fields996e0932011-10-17 11:14:48 -0400666 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500667 * It shouldn't be a problem to reuse an opaque stateid value.
668 * I don't think it is for 4.1. But with 4.0 I worry that, for
669 * example, a stray write retransmission could be accepted by
670 * the server when it should have been rejected. Therefore,
671 * adopt a trick from the sctp code to attempt to maximize the
672 * amount of time until an id is reused, by ensuring they always
673 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400674 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500675 return stid;
676out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800677 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500678 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400679}
680
Jeff Laytonb49e0842014-07-29 21:34:11 -0400681static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400682{
Trond Myklebust60116952014-07-29 21:34:06 -0400683 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400684
Kinglong Mee743146d32017-01-18 19:04:42 +0800685 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400686 if (!stid)
687 return NULL;
688
Kinglong Mee743146d32017-01-18 19:04:42 +0800689 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400690}
691
692static void nfs4_free_deleg(struct nfs4_stid *stid)
693{
Trond Myklebust60116952014-07-29 21:34:06 -0400694 kmem_cache_free(deleg_slab, stid);
695 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400696}
697
NeilBrown6282cd52014-06-04 17:39:26 +1000698/*
699 * When we recall a delegation, we should be careful not to hand it
700 * out again straight away.
701 * To ensure this we keep a pair of bloom filters ('new' and 'old')
702 * in which the filehandles of recalled delegations are "stored".
703 * If a filehandle appear in either filter, a delegation is blocked.
704 * When a delegation is recalled, the filehandle is stored in the "new"
705 * filter.
706 * Every 30 seconds we swap the filters and clear the "new" one,
707 * unless both are empty of course.
708 *
709 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
710 * low 3 bytes as hash-table indices.
711 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400712 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000713 * is used to manage concurrent access. Testing does not need the lock
714 * except when swapping the two filters.
715 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400716static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000717static struct bloom_pair {
718 int entries, old_entries;
719 time_t swap_time;
720 int new; /* index into 'set' */
721 DECLARE_BITMAP(set[2], 256);
722} blocked_delegations;
723
724static int delegation_blocked(struct knfsd_fh *fh)
725{
726 u32 hash;
727 struct bloom_pair *bd = &blocked_delegations;
728
729 if (bd->entries == 0)
730 return 0;
731 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400732 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000733 if (seconds_since_boot() - bd->swap_time > 30) {
734 bd->entries -= bd->old_entries;
735 bd->old_entries = bd->entries;
736 memset(bd->set[bd->new], 0,
737 sizeof(bd->set[0]));
738 bd->new = 1-bd->new;
739 bd->swap_time = seconds_since_boot();
740 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400741 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000742 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100743 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000744 if (test_bit(hash&255, bd->set[0]) &&
745 test_bit((hash>>8)&255, bd->set[0]) &&
746 test_bit((hash>>16)&255, bd->set[0]))
747 return 1;
748
749 if (test_bit(hash&255, bd->set[1]) &&
750 test_bit((hash>>8)&255, bd->set[1]) &&
751 test_bit((hash>>16)&255, bd->set[1]))
752 return 1;
753
754 return 0;
755}
756
757static void block_delegations(struct knfsd_fh *fh)
758{
759 u32 hash;
760 struct bloom_pair *bd = &blocked_delegations;
761
Daniel Borkmann87545892014-12-10 16:33:11 +0100762 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000763
Jeff Laytonf54fe962014-07-25 07:34:26 -0400764 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000765 __set_bit(hash&255, bd->set[bd->new]);
766 __set_bit((hash>>8)&255, bd->set[bd->new]);
767 __set_bit((hash>>16)&255, bd->set[bd->new]);
768 if (bd->entries == 0)
769 bd->swap_time = seconds_since_boot();
770 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400771 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static struct nfs4_delegation *
Sachin Bhamare8287f002015-04-27 14:50:14 +0200775alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
776 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400779 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400782 n = atomic_long_inc_return(&num_delegations);
783 if (n < 0 || n > max_delegations)
784 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000785 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400786 goto out_dec;
Kinglong Mee743146d32017-01-18 19:04:42 +0800787 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700788 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400789 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400790
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400791 /*
792 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400793 * meaning of seqid 0 isn't meaningful, really, but let's avoid
794 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400795 */
796 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700797 INIT_LIST_HEAD(&dp->dl_perfile);
798 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200800 dp->dl_clnt_odstate = odstate;
801 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400802 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200803 dp->dl_retries = 1;
804 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200805 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400807out_dec:
808 atomic_long_dec(&num_delegations);
809 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812void
Trond Myklebust60116952014-07-29 21:34:06 -0400813nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Trond Myklebust11b91642014-07-29 21:34:08 -0400815 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400816 struct nfs4_client *clp = s->sc_client;
817
Jeff Layton4770d722014-07-29 21:34:10 -0400818 might_lock(&clp->cl_lock);
819
Jeff Laytonb401be222014-07-29 21:34:33 -0400820 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
821 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400822 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400823 }
Trond Myklebust60116952014-07-29 21:34:06 -0400824 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400825 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400826 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400827 if (fp)
828 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Jeff Layton9767feb2015-10-01 09:05:50 -0400831void
832nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
833{
834 stateid_t *src = &stid->sc_stateid;
835
836 spin_lock(&stid->sc_lock);
837 if (unlikely(++src->si_generation == 0))
838 src->si_generation = 1;
839 memcpy(dst, src, sizeof(*dst));
840 spin_unlock(&stid->sc_lock);
841}
842
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500843static void nfs4_put_deleg_lease(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400845 struct file *filp = NULL;
Jeff Layton417c6622014-07-21 09:34:57 -0400846
Jeff Layton6bcc0342014-08-09 10:22:40 -0400847 spin_lock(&fp->fi_lock);
Jeff Layton67db1032014-12-13 09:11:40 -0500848 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
Jeff Layton6bcc0342014-08-09 10:22:40 -0400849 swap(filp, fp->fi_deleg_file);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400850 spin_unlock(&fp->fi_lock);
851
852 if (filp) {
Christoph Hellwig2ab99ee2015-01-21 19:14:02 +0100853 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
Jeff Layton6bcc0342014-08-09 10:22:40 -0400854 fput(filp);
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -0500855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200858void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400859{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500860 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400861}
862
Andrew Elble34ed9872015-10-15 12:07:28 -0400863/**
864 * nfs4_get_existing_delegation - Discover if this delegation already exists
865 * @clp: a pointer to the nfs4_client we're granting a delegation to
866 * @fp: a pointer to the nfs4_file we're granting a delegation on
867 *
868 * Return:
869 * On success: NULL if an existing delegation was not found.
870 *
871 * On error: -EAGAIN if one was previously granted to this nfs4_client
872 * for this nfs4_file.
873 *
874 */
875
876static int
877nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400878{
Andrew Elble34ed9872015-10-15 12:07:28 -0400879 struct nfs4_delegation *searchdp = NULL;
880 struct nfs4_client *searchclp = NULL;
881
Benny Halevycdc97502014-05-30 09:09:30 -0400882 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400883 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400884
Andrew Elble34ed9872015-10-15 12:07:28 -0400885 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
886 searchclp = searchdp->dl_stid.sc_client;
887 if (clp == searchclp) {
888 return -EAGAIN;
889 }
890 }
891 return 0;
892}
893
894/**
895 * hash_delegation_locked - Add a delegation to the appropriate lists
896 * @dp: a pointer to the nfs4_delegation we are adding.
897 * @fp: a pointer to the nfs4_file we're granting a delegation on
898 *
899 * Return:
900 * On success: NULL if the delegation was successfully hashed.
901 *
902 * On error: -EAGAIN if one was previously granted to this
903 * nfs4_client for this nfs4_file. Delegation is not hashed.
904 *
905 */
906
907static int
908hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
909{
910 int status;
911 struct nfs4_client *clp = dp->dl_stid.sc_client;
912
913 lockdep_assert_held(&state_lock);
914 lockdep_assert_held(&fp->fi_lock);
915
916 status = nfs4_get_existing_delegation(clp, fp);
917 if (status)
918 return status;
919 ++fp->fi_delegees;
Trond Myklebust67cb1272014-07-29 21:34:17 -0400920 atomic_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -0400921 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -0400922 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -0400923 list_add(&dp->dl_perclnt, &clp->cl_delegations);
924 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -0400925}
926
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400927static bool
Jeff Layton42690672014-07-25 07:34:20 -0400928unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Trond Myklebust11b91642014-07-29 21:34:08 -0400930 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -0400931
Jeff Layton42690672014-07-25 07:34:20 -0400932 lockdep_assert_held(&state_lock);
933
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400934 if (list_empty(&dp->dl_perfile))
935 return false;
936
Trond Myklebustb0fc29d2014-07-16 10:31:59 -0400937 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -0400938 /* Ensure that deleg break won't try to requeue it */
939 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -0400940 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400941 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -0400943 list_del_init(&dp->dl_perfile);
944 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400945 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400946}
947
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400948static void destroy_delegation(struct nfs4_delegation *dp)
949{
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400950 bool unhashed;
951
Jeff Layton42690672014-07-25 07:34:20 -0400952 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400953 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -0400954 spin_unlock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -0400955 if (unhashed) {
956 put_clnt_odstate(dp->dl_clnt_odstate);
957 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
958 nfs4_put_stid(&dp->dl_stid);
959 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400960}
961
962static void revoke_delegation(struct nfs4_delegation *dp)
963{
964 struct nfs4_client *clp = dp->dl_stid.sc_client;
965
Jeff Layton2d4a5322014-07-25 07:34:21 -0400966 WARN_ON(!list_empty(&dp->dl_recall_lru));
967
Sachin Bhamare8287f002015-04-27 14:50:14 +0200968 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -0400969 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
970
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400971 if (clp->cl_minorversion == 0)
Trond Myklebust60116952014-07-29 21:34:06 -0400972 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400973 else {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400974 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
Jeff Layton2d4a5322014-07-25 07:34:21 -0400975 spin_lock(&clp->cl_lock);
976 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
977 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -0400978 }
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/*
982 * SETCLIENTID state
983 */
984
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400985static unsigned int clientid_hashval(u32 id)
986{
987 return id & CLIENT_HASH_MASK;
988}
989
990static unsigned int clientstr_hashval(const char *name)
991{
992 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400996 * We store the NONE, READ, WRITE, and BOTH bits separately in the
997 * st_{access,deny}_bmap field of the stateid, in order to track not
998 * only what share bits are currently in force, but also what
999 * combinations of share bits previous opens have used. This allows us
1000 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1001 * return an error if the client attempt to downgrade to a combination
1002 * of share bits not explicable by closing some of its previous opens.
1003 *
1004 * XXX: This enforcement is actually incomplete, since we don't keep
1005 * track of access/deny bit combinations; so, e.g., we allow:
1006 *
1007 * OPEN allow read, deny write
1008 * OPEN allow both, deny none
1009 * DOWNGRADE allow read, deny none
1010 *
1011 * which we should reject.
1012 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001013static unsigned int
1014bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001015 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001016 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001017
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001018 for (i = 1; i < 4; i++) {
1019 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001020 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001021 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001022 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001023}
1024
Jeff Layton82c5ff12012-05-11 09:45:13 -04001025/* set share access for a given stateid */
1026static inline void
1027set_access(u32 access, struct nfs4_ol_stateid *stp)
1028{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001029 unsigned char mask = 1 << access;
1030
1031 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1032 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001033}
1034
1035/* clear share access for a given stateid */
1036static inline void
1037clear_access(u32 access, struct nfs4_ol_stateid *stp)
1038{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001039 unsigned char mask = 1 << access;
1040
1041 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1042 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001043}
1044
1045/* test whether a given stateid has access */
1046static inline bool
1047test_access(u32 access, struct nfs4_ol_stateid *stp)
1048{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001049 unsigned char mask = 1 << access;
1050
1051 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001052}
1053
Jeff Laytonce0fc432012-05-11 09:45:14 -04001054/* set share deny for a given stateid */
1055static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001056set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001057{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001058 unsigned char mask = 1 << deny;
1059
1060 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1061 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001062}
1063
1064/* clear share deny for a given stateid */
1065static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001066clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001067{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001068 unsigned char mask = 1 << deny;
1069
1070 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1071 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001072}
1073
1074/* test whether a given stateid is denying specific access */
1075static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001076test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001077{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001078 unsigned char mask = 1 << deny;
1079
1080 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001081}
1082
1083static int nfs4_access_to_omode(u32 access)
1084{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001085 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001086 case NFS4_SHARE_ACCESS_READ:
1087 return O_RDONLY;
1088 case NFS4_SHARE_ACCESS_WRITE:
1089 return O_WRONLY;
1090 case NFS4_SHARE_ACCESS_BOTH:
1091 return O_RDWR;
1092 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001093 WARN_ON_ONCE(1);
1094 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001095}
1096
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001097/*
1098 * A stateid that had a deny mode associated with it is being released
1099 * or downgraded. Recalculate the deny mode on the file.
1100 */
1101static void
1102recalculate_deny_mode(struct nfs4_file *fp)
1103{
1104 struct nfs4_ol_stateid *stp;
1105
1106 spin_lock(&fp->fi_lock);
1107 fp->fi_share_deny = 0;
1108 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1109 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1110 spin_unlock(&fp->fi_lock);
1111}
1112
1113static void
1114reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1115{
1116 int i;
1117 bool change = false;
1118
1119 for (i = 1; i < 4; i++) {
1120 if ((i & deny) != i) {
1121 change = true;
1122 clear_deny(i, stp);
1123 }
1124 }
1125
1126 /* Recalculate per-file deny mode if there was a change */
1127 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001128 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001129}
1130
Jeff Layton82c5ff12012-05-11 09:45:13 -04001131/* release all access and file references for a given stateid */
1132static void
1133release_all_access(struct nfs4_ol_stateid *stp)
1134{
1135 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001136 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001137
1138 if (fp && stp->st_deny_bmap != 0)
1139 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001140
1141 for (i = 1; i < 4; i++) {
1142 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001143 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001144 clear_access(i, stp);
1145 }
1146}
1147
Kinglong Meed50ffde2015-07-16 12:05:07 +08001148static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1149{
1150 kfree(sop->so_owner.data);
1151 sop->so_ops->so_free(sop);
1152}
1153
Jeff Layton6b180f02014-07-29 21:34:26 -04001154static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1155{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001156 struct nfs4_client *clp = sop->so_client;
1157
1158 might_lock(&clp->cl_lock);
1159
1160 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001161 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001162 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001163 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001164 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001165}
1166
Jeff Laytone8568732015-08-24 12:41:47 -04001167static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001168{
Trond Myklebust11b91642014-07-29 21:34:08 -04001169 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001170
Jeff Layton1c755dc2014-07-29 21:34:12 -04001171 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1172
Jeff Laytone8568732015-08-24 12:41:47 -04001173 if (list_empty(&stp->st_perfile))
1174 return false;
1175
Trond Myklebust1d31a252014-07-10 14:07:25 -04001176 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001177 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001178 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001179 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001180 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001181}
1182
Trond Myklebust60116952014-07-29 21:34:06 -04001183static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001184{
Trond Myklebust60116952014-07-29 21:34:06 -04001185 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001186
Sachin Bhamare8287f002015-04-27 14:50:14 +02001187 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001188 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001189 if (stp->st_stateowner)
1190 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001191 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001192}
1193
Jeff Laytonb49e0842014-07-29 21:34:11 -04001194static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001195{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001196 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1197 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001198 struct file *file;
1199
Jeff Laytonb49e0842014-07-29 21:34:11 -04001200 file = find_any_file(stp->st_stid.sc_file);
1201 if (file)
1202 filp_close(file, (fl_owner_t)lo);
1203 nfs4_free_ol_stateid(stid);
1204}
1205
Jeff Layton2c41beb2014-07-29 21:34:41 -04001206/*
1207 * Put the persistent reference to an already unhashed generic stateid, while
1208 * holding the cl_lock. If it's the last reference, then put it onto the
1209 * reaplist for later destruction.
1210 */
1211static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1212 struct list_head *reaplist)
1213{
1214 struct nfs4_stid *s = &stp->st_stid;
1215 struct nfs4_client *clp = s->sc_client;
1216
1217 lockdep_assert_held(&clp->cl_lock);
1218
1219 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1220
1221 if (!atomic_dec_and_test(&s->sc_count)) {
1222 wake_up_all(&close_wq);
1223 return;
1224 }
1225
1226 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1227 list_add(&stp->st_locks, reaplist);
1228}
1229
Jeff Laytone8568732015-08-24 12:41:47 -04001230static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001231{
Chuck Leverf46c4452016-10-29 18:19:03 -04001232 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001233
1234 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001235 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001236 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001237}
1238
Jeff Layton5adfd882014-07-29 21:34:31 -04001239static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001240{
Chuck Leverf46c4452016-10-29 18:19:03 -04001241 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001242 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001243
Chuck Leverf46c4452016-10-29 18:19:03 -04001244 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001245 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001246 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001247 if (unhashed)
1248 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001249}
1250
Trond Myklebustc58c6612014-07-29 21:34:35 -04001251static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001252{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001253 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001254
Trond Myklebustd4f04892014-07-29 21:34:36 -04001255 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001256
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001257 list_del_init(&lo->lo_owner.so_strhash);
1258}
1259
Jeff Layton2c41beb2014-07-29 21:34:41 -04001260/*
1261 * Free a list of generic stateids that were collected earlier after being
1262 * fully unhashed.
1263 */
1264static void
1265free_ol_stateid_reaplist(struct list_head *reaplist)
1266{
1267 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001268 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001269
1270 might_sleep();
1271
1272 while (!list_empty(reaplist)) {
1273 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1274 st_locks);
1275 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001276 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001277 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001278 if (fp)
1279 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001280 }
1281}
1282
Jeff Laytond83017f2014-07-29 21:34:42 -04001283static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1284 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001285{
1286 struct nfs4_ol_stateid *stp;
1287
Jeff Laytone8568732015-08-24 12:41:47 -04001288 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1289
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001290 while (!list_empty(&open_stp->st_locks)) {
1291 stp = list_entry(open_stp->st_locks.next,
1292 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001293 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001294 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001295 }
1296}
1297
Jeff Laytone8568732015-08-24 12:41:47 -04001298static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001299 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001300{
Jeff Laytone8568732015-08-24 12:41:47 -04001301 bool unhashed;
1302
Jeff Layton2c41beb2014-07-29 21:34:41 -04001303 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1304
Jeff Laytone8568732015-08-24 12:41:47 -04001305 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001306 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001307 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001308}
1309
1310static void release_open_stateid(struct nfs4_ol_stateid *stp)
1311{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001312 LIST_HEAD(reaplist);
1313
1314 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001315 if (unhash_open_stateid(stp, &reaplist))
1316 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001317 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1318 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001319}
1320
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001321static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001322{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001323 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001324
Trond Myklebustd4f04892014-07-29 21:34:36 -04001325 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001326
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001327 list_del_init(&oo->oo_owner.so_strhash);
1328 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001329}
1330
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001331static void release_last_closed_stateid(struct nfs4_openowner *oo)
1332{
Jeff Layton217526e2014-07-30 08:27:11 -04001333 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1334 nfsd_net_id);
1335 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001336
Jeff Layton217526e2014-07-30 08:27:11 -04001337 spin_lock(&nn->client_lock);
1338 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001339 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001340 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001341 oo->oo_last_closed_stid = NULL;
1342 }
Jeff Layton217526e2014-07-30 08:27:11 -04001343 spin_unlock(&nn->client_lock);
1344 if (s)
1345 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001346}
1347
Jeff Layton2c41beb2014-07-29 21:34:41 -04001348static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001349{
1350 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001351 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001352 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001353
Jeff Layton2c41beb2014-07-29 21:34:41 -04001354 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001355
Trond Myklebustd4f04892014-07-29 21:34:36 -04001356 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001357 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001358 while (!list_empty(&oo->oo_owner.so_stateids)) {
1359 stp = list_first_entry(&oo->oo_owner.so_stateids,
1360 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001361 if (unhash_open_stateid(stp, &reaplist))
1362 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001363 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001364 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001365 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001366 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001367 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001368}
1369
Marc Eshel5282fd72009-04-03 08:27:52 +03001370static inline int
1371hash_sessionid(struct nfs4_sessionid *sessionid)
1372{
1373 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1374
1375 return sid->sequence % SESSION_HASH_SIZE;
1376}
1377
Mark Salter135dd002015-04-06 09:46:00 -04001378#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001379static inline void
1380dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1381{
1382 u32 *ptr = (u32 *)(&sessionid->data[0]);
1383 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1384}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001385#else
1386static inline void
1387dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1388{
1389}
1390#endif
1391
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001392/*
1393 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1394 * won't be used for replay.
1395 */
1396void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1397{
1398 struct nfs4_stateowner *so = cstate->replay_owner;
1399
1400 if (nfserr == nfserr_replay_me)
1401 return;
1402
1403 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001404 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001405 return;
1406 }
1407 if (!so)
1408 return;
1409 if (so->so_is_open_owner)
1410 release_last_closed_stateid(openowner(so));
1411 so->so_seqid++;
1412 return;
1413}
Marc Eshel5282fd72009-04-03 08:27:52 +03001414
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001415static void
1416gen_sessionid(struct nfsd4_session *ses)
1417{
1418 struct nfs4_client *clp = ses->se_client;
1419 struct nfsd4_sessionid *sid;
1420
1421 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1422 sid->clientid = clp->cl_clientid;
1423 sid->sequence = current_sessionid++;
1424 sid->reserved = 0;
1425}
1426
1427/*
Andy Adamsona6496372009-08-28 08:45:01 -04001428 * The protocol defines ca_maxresponssize_cached to include the size of
1429 * the rpc header, but all we need to cache is the data starting after
1430 * the end of the initial SEQUENCE operation--the rest we regenerate
1431 * each time. Therefore we can advertise a ca_maxresponssize_cached
1432 * value that is the number of bytes in our cache plus a few additional
1433 * bytes. In order to stay on the safe side, and not promise more than
1434 * we can cache, those additional bytes must be the minimum possible: 24
1435 * bytes of rpc header (xid through accept state, with AUTH_NULL
1436 * verifier), 12 for the compound header (with zero-length tag), and 44
1437 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001438 */
Andy Adamsona6496372009-08-28 08:45:01 -04001439#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1440
Andy Adamson557ce262009-08-28 08:45:04 -04001441static void
1442free_session_slots(struct nfsd4_session *ses)
1443{
1444 int i;
1445
1446 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1447 kfree(ses->se_slots[i]);
1448}
1449
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001450/*
1451 * We don't actually need to cache the rpc and session headers, so we
1452 * can allocate a little less for each slot:
1453 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001454static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001455{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001456 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001457
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001458 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1459 size = 0;
1460 else
1461 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1462 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001463}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001464
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001465/*
1466 * XXX: If we run out of reserved DRC memory we could (up to a point)
1467 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001468 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001469 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001470static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001471{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001472 u32 slotsize = slot_bytes(ca);
1473 u32 num = ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001474 int avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001475
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001476 spin_lock(&nfsd_drc_lock);
Zhang Yanfei697ce9b2013-02-22 16:35:47 -08001477 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1478 nfsd_drc_max_mem - nfsd_drc_mem_used);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001479 num = min_t(int, num, avail / slotsize);
1480 nfsd_drc_mem_used += num * slotsize;
1481 spin_unlock(&nfsd_drc_lock);
1482
1483 return num;
1484}
1485
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001486static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001487{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001488 int slotsize = slot_bytes(ca);
1489
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001490 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001491 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001492 spin_unlock(&nfsd_drc_lock);
1493}
1494
Kinglong Mee60810e52014-01-01 00:35:47 +08001495static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1496 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001497{
Kinglong Mee60810e52014-01-01 00:35:47 +08001498 int numslots = fattrs->maxreqs;
1499 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001500 struct nfsd4_session *new;
1501 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001502
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001503 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001504 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1505 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001506
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001507 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001508 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001509 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001510 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001511 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001512 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001513 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001514 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001515 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001516
1517 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1518 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1519
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001520 return new;
1521out_free:
1522 while (i--)
1523 kfree(new->se_slots[i]);
1524 kfree(new);
1525 return NULL;
1526}
1527
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001528static void free_conn(struct nfsd4_conn *c)
1529{
1530 svc_xprt_put(c->cn_xprt);
1531 kfree(c);
1532}
1533
1534static void nfsd4_conn_lost(struct svc_xpt_user *u)
1535{
1536 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1537 struct nfs4_client *clp = c->cn_session->se_client;
1538
1539 spin_lock(&clp->cl_lock);
1540 if (!list_empty(&c->cn_persession)) {
1541 list_del(&c->cn_persession);
1542 free_conn(c);
1543 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001544 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001545 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001546}
1547
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001548static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001549{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001550 struct nfsd4_conn *conn;
1551
1552 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1553 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001554 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001555 svc_xprt_get(rqstp->rq_xprt);
1556 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001557 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001558 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1559 return conn;
1560}
1561
J. Bruce Fields328ead22010-09-29 16:11:06 -04001562static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1563{
1564 conn->cn_session = ses;
1565 list_add(&conn->cn_persession, &ses->se_conns);
1566}
1567
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001568static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1569{
1570 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001571
1572 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001573 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001574 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001575}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001576
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001577static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001578{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001579 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001580 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001581}
1582
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001583static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001584{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001585 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001586
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001587 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001588 ret = nfsd4_register_conn(conn);
1589 if (ret)
1590 /* oops; xprt is already down: */
1591 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001592 /* We may have gained or lost a callback channel: */
1593 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001594}
1595
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001596static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001597{
1598 u32 dir = NFS4_CDFC4_FORE;
1599
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001600 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001601 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001602 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001603}
1604
1605/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001606static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001607{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001608 struct nfs4_client *clp = s->se_client;
1609 struct nfsd4_conn *c;
1610
1611 spin_lock(&clp->cl_lock);
1612 while (!list_empty(&s->se_conns)) {
1613 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1614 list_del_init(&c->cn_persession);
1615 spin_unlock(&clp->cl_lock);
1616
1617 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1618 free_conn(c);
1619
1620 spin_lock(&clp->cl_lock);
1621 }
1622 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001623}
1624
J. Bruce Fields1377b692012-09-11 21:42:40 -04001625static void __free_session(struct nfsd4_session *ses)
1626{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001627 free_session_slots(ses);
1628 kfree(ses);
1629}
1630
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001631static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001632{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001633 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001634 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001635 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001636}
Andy Adamson557ce262009-08-28 08:45:04 -04001637
Fengguang Wu135ae822012-11-10 07:20:25 -05001638static 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 -04001639{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001640 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001641 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001642
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001643 new->se_client = clp;
1644 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001645
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001646 INIT_LIST_HEAD(&new->se_conns);
1647
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04001648 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001649 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001650 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001651 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001652 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001653 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001654 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001655 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001656 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001657 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001658
Chuck Leverb0d2e422014-08-22 15:10:59 -04001659 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001660 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001661 /*
1662 * This is a little silly; with sessions there's no real
1663 * use for the callback address. Use the peer address
1664 * as a reasonable default for now, but consider fixing
1665 * the rpc client not to require an address in the
1666 * future:
1667 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001668 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1669 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001670 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001671}
1672
Benny Halevy9089f1b2010-05-12 00:12:26 +03001673/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001674static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001675__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001676{
1677 struct nfsd4_session *elem;
1678 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001679 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001680
Trond Myklebust0a880a22014-07-30 08:27:10 -04001681 lockdep_assert_held(&nn->client_lock);
1682
Marc Eshel5282fd72009-04-03 08:27:52 +03001683 dump_sessionid(__func__, sessionid);
1684 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001685 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001686 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001687 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1688 NFS4_MAX_SESSIONID_LEN)) {
1689 return elem;
1690 }
1691 }
1692
1693 dprintk("%s: session not found\n", __func__);
1694 return NULL;
1695}
1696
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001697static struct nfsd4_session *
1698find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1699 __be32 *ret)
1700{
1701 struct nfsd4_session *session;
1702 __be32 status = nfserr_badsession;
1703
1704 session = __find_in_sessionid_hashtbl(sessionid, net);
1705 if (!session)
1706 goto out;
1707 status = nfsd4_get_session_locked(session);
1708 if (status)
1709 session = NULL;
1710out:
1711 *ret = status;
1712 return session;
1713}
1714
Benny Halevy9089f1b2010-05-12 00:12:26 +03001715/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001716static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001717unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001718{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001719 struct nfs4_client *clp = ses->se_client;
1720 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1721
1722 lockdep_assert_held(&nn->client_lock);
1723
Andy Adamson7116ed62009-04-03 08:27:43 +03001724 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001725 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001726 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001727 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1731static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001732STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001734 /*
1735 * We're assuming the clid was not given out from a boot
1736 * precisely 2^32 (about 136 years) before this one. That seems
1737 * a safe assumption:
1738 */
1739 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001741 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001742 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return 1;
1744}
1745
1746/*
1747 * XXX Should we use a slab cache ?
1748 * This type of memory management is somewhat inefficient, but we use it
1749 * anyway since SETCLIENTID is not a common operation.
1750 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001751static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001754 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001756 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1757 if (clp == NULL)
1758 return NULL;
Thomas Meyer67114fe2011-11-17 23:43:40 +01001759 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001760 if (clp->cl_name.data == NULL)
1761 goto err_no_name;
1762 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1763 OWNER_HASH_SIZE, GFP_KERNEL);
1764 if (!clp->cl_ownerstr_hashtbl)
1765 goto err_no_hashtbl;
1766 for (i = 0; i < OWNER_HASH_SIZE; i++)
1767 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001768 clp->cl_name.len = name.len;
Trond Myklebust5694c932014-04-18 14:43:56 -04001769 INIT_LIST_HEAD(&clp->cl_sessions);
1770 idr_init(&clp->cl_stateids);
1771 atomic_set(&clp->cl_refcount, 0);
1772 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1773 INIT_LIST_HEAD(&clp->cl_idhash);
1774 INIT_LIST_HEAD(&clp->cl_openowners);
1775 INIT_LIST_HEAD(&clp->cl_delegations);
1776 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001777 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001778#ifdef CONFIG_NFSD_PNFS
1779 INIT_LIST_HEAD(&clp->cl_lo_states);
1780#endif
Trond Myklebust5694c932014-04-18 14:43:56 -04001781 spin_lock_init(&clp->cl_lock);
1782 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001784err_no_hashtbl:
1785 kfree(clp->cl_name.data);
1786err_no_name:
1787 kfree(clp);
1788 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001791static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792free_client(struct nfs4_client *clp)
1793{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001794 while (!list_empty(&clp->cl_sessions)) {
1795 struct nfsd4_session *ses;
1796 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1797 se_perclnt);
1798 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001799 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1800 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001801 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001802 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001803 free_svc_cred(&clp->cl_cred);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001804 kfree(clp->cl_ownerstr_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 kfree(clp->cl_name.data);
majianpeng2d32b292013-01-29 13:16:06 +08001806 idr_destroy(&clp->cl_stateids);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 kfree(clp);
1808}
1809
Benny Halevy84d38ac2010-05-12 00:13:16 +03001810/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001811static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001812unhash_client_locked(struct nfs4_client *clp)
1813{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001814 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001815 struct nfsd4_session *ses;
1816
Trond Myklebust0a880a22014-07-30 08:27:10 -04001817 lockdep_assert_held(&nn->client_lock);
1818
Trond Myklebust4beb3452014-07-30 08:27:02 -04001819 /* Mark the client as expired! */
1820 clp->cl_time = 0;
1821 /* Make it invisible */
1822 if (!list_empty(&clp->cl_idhash)) {
1823 list_del_init(&clp->cl_idhash);
1824 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1825 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1826 else
1827 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1828 }
1829 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001830 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001831 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1832 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001833 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001837unhash_client(struct nfs4_client *clp)
1838{
1839 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1840
1841 spin_lock(&nn->client_lock);
1842 unhash_client_locked(clp);
1843 spin_unlock(&nn->client_lock);
1844}
1845
Jeff Layton97403d92014-07-30 08:27:12 -04001846static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1847{
1848 if (atomic_read(&clp->cl_refcount))
1849 return nfserr_jukebox;
1850 unhash_client_locked(clp);
1851 return nfs_ok;
1852}
1853
Trond Myklebust4beb3452014-07-30 08:27:02 -04001854static void
1855__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001857 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 struct list_head reaplist;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04001862 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001863 while (!list_empty(&clp->cl_delegations)) {
1864 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001865 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001866 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
Benny Halevycdc97502014-05-30 09:09:30 -04001868 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 while (!list_empty(&reaplist)) {
1870 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001871 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001872 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04001873 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04001874 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001876 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001877 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001878 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001879 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02001880 }
NeilBrownea1da632005-06-23 22:04:17 -07001881 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001882 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04001883 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001884 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001886 nfsd4_return_all_client_layouts(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001887 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05001888 if (clp->cl_cb_conn.cb_xprt)
1889 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04001890 free_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Trond Myklebust4beb3452014-07-30 08:27:02 -04001893static void
1894destroy_client(struct nfs4_client *clp)
1895{
1896 unhash_client(clp);
1897 __destroy_client(clp);
1898}
1899
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001900static void expire_client(struct nfs4_client *clp)
1901{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001902 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001903 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04001904 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04001905}
1906
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001907static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1908{
1909 memcpy(target->cl_verifier.data, source->data,
1910 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001913static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1916 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1917}
1918
J. Bruce Fields50043852015-11-20 15:58:37 -05001919int strdup_if_nonnull(char **target, char *source)
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001920{
J. Bruce Fields50043852015-11-20 15:58:37 -05001921 if (source) {
1922 *target = kstrdup(source, GFP_KERNEL);
1923 if (!*target)
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001924 return -ENOMEM;
1925 } else
J. Bruce Fields50043852015-11-20 15:58:37 -05001926 *target = NULL;
1927 return 0;
1928}
1929
1930static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1931{
1932 int ret;
1933
1934 ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
1935 if (ret)
1936 return ret;
J. Bruce Fields414ca012015-11-20 10:48:02 -05001937 ret = strdup_if_nonnull(&target->cr_raw_principal,
1938 source->cr_raw_principal);
1939 if (ret)
1940 return ret;
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04001941 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 target->cr_uid = source->cr_uid;
1943 target->cr_gid = source->cr_gid;
1944 target->cr_group_info = source->cr_group_info;
1945 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04001946 target->cr_gss_mech = source->cr_gss_mech;
1947 if (source->cr_gss_mech)
1948 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04001949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001952static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001953compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1954{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01001955 if (o1->len < o2->len)
1956 return -1;
1957 if (o1->len > o2->len)
1958 return 1;
1959 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05001960}
1961
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001962static int same_name(const char *n1, const char *n2)
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001963{
NeilBrowna55370a2005-06-23 22:03:52 -07001964 return 0 == memcmp(n1, n2, HEXDIR_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001968same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1969{
1970 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04001974same_clid(clientid_t *cl1, clientid_t *cl2)
1975{
1976 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001979static bool groups_equal(struct group_info *g1, struct group_info *g2)
1980{
1981 int i;
1982
1983 if (g1->ngroups != g2->ngroups)
1984 return false;
1985 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07001986 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04001987 return false;
1988 return true;
1989}
1990
J. Bruce Fields68eb3502012-08-21 12:48:30 -04001991/*
1992 * RFC 3530 language requires clid_inuse be returned when the
1993 * "principal" associated with a requests differs from that previously
1994 * used. We use uid, gid's, and gss principal string as our best
1995 * approximation. We also don't want to allow non-gss use of a client
1996 * established using gss: in theory cr_principal should catch that
1997 * change, but in practice cr_principal can be null even in the gss case
1998 * since gssd doesn't always pass down a principal string.
1999 */
2000static bool is_gss_cred(struct svc_cred *cr)
2001{
2002 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2003 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2004}
2005
2006
Vivek Trivedi5559b502012-07-24 21:18:20 +05302007static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002008same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2009{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002010 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002011 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2012 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002013 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2014 return false;
2015 if (cr1->cr_principal == cr2->cr_principal)
2016 return true;
2017 if (!cr1->cr_principal || !cr2->cr_principal)
2018 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302019 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
J. Bruce Fields57266a62013-04-13 14:27:29 -04002022static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2023{
2024 struct svc_cred *cr = &rqstp->rq_cred;
2025 u32 service;
2026
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002027 if (!cr->cr_gss_mech)
2028 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002029 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2030 return service == RPC_GSS_SVC_INTEGRITY ||
2031 service == RPC_GSS_SVC_PRIVACY;
2032}
2033
Andrew Elblededeb132016-06-15 12:52:08 -04002034bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002035{
2036 struct svc_cred *cr = &rqstp->rq_cred;
2037
2038 if (!cl->cl_mach_cred)
2039 return true;
2040 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2041 return false;
2042 if (!svc_rqst_integrity_protected(rqstp))
2043 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002044 if (cl->cl_cred.cr_raw_principal)
2045 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2046 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002047 if (!cr->cr_principal)
2048 return false;
2049 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2050}
2051
Jeff Layton294ac322014-07-30 08:27:15 -04002052static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002053{
Chuck Leverab4684d2012-03-02 17:13:50 -05002054 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Jeff Laytonf4199922014-06-17 07:44:11 -04002056 /*
2057 * This is opaque to client, so no need to byte-swap. Use
2058 * __force to keep sparse happy
2059 */
2060 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002061 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002062 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Jeff Layton294ac322014-07-30 08:27:15 -04002065static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2066{
2067 clp->cl_clientid.cl_boot = nn->boot_time;
2068 clp->cl_clientid.cl_id = nn->clientid_counter++;
2069 gen_confirm(clp, nn);
2070}
2071
Jeff Layton4770d722014-07-29 21:34:10 -04002072static struct nfs4_stid *
2073find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002074{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002075 struct nfs4_stid *ret;
2076
2077 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2078 if (!ret || !ret->sc_type)
2079 return NULL;
2080 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002081}
2082
Jeff Layton4770d722014-07-29 21:34:10 -04002083static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002084find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002085{
2086 struct nfs4_stid *s;
2087
Jeff Layton4770d722014-07-29 21:34:10 -04002088 spin_lock(&cl->cl_lock);
2089 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002090 if (s != NULL) {
2091 if (typemask & s->sc_type)
2092 atomic_inc(&s->sc_count);
2093 else
2094 s = NULL;
2095 }
Jeff Layton4770d722014-07-29 21:34:10 -04002096 spin_unlock(&cl->cl_lock);
2097 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002098}
2099
Jeff Layton2216d442012-11-12 15:00:57 -05002100static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002101 struct svc_rqst *rqstp, nfs4_verifier *verf)
2102{
2103 struct nfs4_client *clp;
2104 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002105 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002106 struct net *net = SVC_NET(rqstp);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002107
2108 clp = alloc_client(name);
2109 if (clp == NULL)
2110 return NULL;
2111
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002112 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2113 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002114 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002115 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002116 }
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002117 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002118 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002119 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002120 copy_verf(clp, verf);
2121 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002122 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002123 clp->net = net;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002124 return clp;
2125}
2126
NeilBrownfd39ca92005-06-23 22:04:03 -07002127static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002128add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2129{
2130 struct rb_node **new = &(root->rb_node), *parent = NULL;
2131 struct nfs4_client *clp;
2132
2133 while (*new) {
2134 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2135 parent = *new;
2136
2137 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2138 new = &((*new)->rb_left);
2139 else
2140 new = &((*new)->rb_right);
2141 }
2142
2143 rb_link_node(&new_clp->cl_namenode, parent, new);
2144 rb_insert_color(&new_clp->cl_namenode, root);
2145}
2146
2147static struct nfs4_client *
2148find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2149{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002150 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002151 struct rb_node *node = root->rb_node;
2152 struct nfs4_client *clp;
2153
2154 while (node) {
2155 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2156 cmp = compare_blob(&clp->cl_name, name);
2157 if (cmp > 0)
2158 node = node->rb_left;
2159 else if (cmp < 0)
2160 node = node->rb_right;
2161 else
2162 return clp;
2163 }
2164 return NULL;
2165}
2166
2167static void
2168add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002171 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Trond Myklebust0a880a22014-07-30 08:27:10 -04002173 lockdep_assert_held(&nn->client_lock);
2174
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002175 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002176 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002178 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002179 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
NeilBrownfd39ca92005-06-23 22:04:03 -07002182static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183move_to_confirmed(struct nfs4_client *clp)
2184{
2185 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002186 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Trond Myklebust0a880a22014-07-30 08:27:10 -04002188 lockdep_assert_held(&nn->client_lock);
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002191 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002192 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002193 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002194 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002195 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002199find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 struct nfs4_client *clp;
2202 unsigned int idhashval = clientid_hashval(clid->cl_id);
2203
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002204 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002205 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002206 if ((bool)clp->cl_minorversion != sessions)
2207 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002208 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212 return NULL;
2213}
2214
2215static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002216find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2217{
2218 struct list_head *tbl = nn->conf_id_hashtbl;
2219
Trond Myklebust0a880a22014-07-30 08:27:10 -04002220 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002221 return find_client_in_id_table(tbl, clid, sessions);
2222}
2223
2224static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002225find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002227 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Trond Myklebust0a880a22014-07-30 08:27:10 -04002229 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002230 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002233static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002234{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002235 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002236}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002237
NeilBrown28ce6052005-06-23 22:03:56 -07002238static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002239find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002240{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002241 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002242 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002243}
2244
2245static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002246find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002247{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002248 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002249 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002250}
2251
NeilBrownfd39ca92005-06-23 22:04:03 -07002252static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002253gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002255 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002256 struct sockaddr *sa = svc_addr(rqstp);
2257 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002258 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Jeff Layton7077ecb2009-08-14 12:57:58 -04002260 /* Currently, we only support tcp and tcp6 for the callback channel */
2261 if (se->se_callback_netid_len == 3 &&
2262 !memcmp(se->se_callback_netid_val, "tcp", 3))
2263 expected_family = AF_INET;
2264 else if (se->se_callback_netid_len == 4 &&
2265 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2266 expected_family = AF_INET6;
2267 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 goto out_err;
2269
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002270 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002271 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002272 (struct sockaddr *)&conn->cb_addr,
2273 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002274
J. Bruce Fields07263f12010-05-31 19:09:40 -04002275 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002277
J. Bruce Fields07263f12010-05-31 19:09:40 -04002278 if (conn->cb_addr.ss_family == AF_INET6)
2279 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002280
J. Bruce Fields07263f12010-05-31 19:09:40 -04002281 conn->cb_prog = se->se_callback_prog;
2282 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002283 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return;
2285out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002286 conn->cb_addr.ss_family = AF_UNSPEC;
2287 conn->cb_addrlen = 0;
Neil Brown849823c2005-09-13 01:25:36 -07002288 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 "will not receive delegations\n",
2290 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return;
2293}
2294
Andy Adamson074fe892009-04-03 08:28:15 +03002295/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002296 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002297 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002298static void
Andy Adamson074fe892009-04-03 08:28:15 +03002299nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2300{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002301 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002302 struct nfsd4_slot *slot = resp->cstate.slot;
2303 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002304
Andy Adamson557ce262009-08-28 08:45:04 -04002305 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002306
Andy Adamson557ce262009-08-28 08:45:04 -04002307 slot->sl_opcnt = resp->opcnt;
2308 slot->sl_status = resp->cstate.status;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002309
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05002310 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002311 if (nfsd4_not_cached(resp)) {
Andy Adamson557ce262009-08-28 08:45:04 -04002312 slot->sl_datalen = 0;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002313 return;
2314 }
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002315 base = resp->cstate.data_offset;
2316 slot->sl_datalen = buf->len - base;
2317 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002318 WARN(1, "%s: sessions DRC could not cache compound\n",
2319 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002320 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002321}
2322
2323/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002324 * Encode the replay sequence operation from the slot values.
2325 * If cachethis is FALSE encode the uncached rep error on the next
2326 * operation which sets resp->p and increments resp->opcnt for
2327 * nfs4svc_encode_compoundres.
2328 *
Andy Adamson074fe892009-04-03 08:28:15 +03002329 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002330static __be32
2331nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2332 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002333{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002334 struct nfsd4_op *op;
2335 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002336
Andy Adamsonabfabf82009-07-23 19:02:18 -04002337 /* Encode the replayed sequence operation */
2338 op = &args->ops[resp->opcnt - 1];
2339 nfsd4_encode_operation(resp, op);
2340
2341 /* Return nfserr_retry_uncached_rep in next operation. */
J. Bruce Fields73e79482012-02-13 16:39:00 -05002342 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002343 op = &args->ops[resp->opcnt++];
2344 op->status = nfserr_retry_uncached_rep;
2345 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002346 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002347 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002348}
2349
2350/*
Andy Adamson557ce262009-08-28 08:45:04 -04002351 * The sequence operation is not cached because we can use the slot and
2352 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002353 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002354static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002355nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2356 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002357{
Andy Adamson557ce262009-08-28 08:45:04 -04002358 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002359 struct xdr_stream *xdr = &resp->xdr;
2360 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002361 __be32 status;
2362
Andy Adamson557ce262009-08-28 08:45:04 -04002363 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002364
Andy Adamsonabfabf82009-07-23 19:02:18 -04002365 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002366 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002367 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002368
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002369 p = xdr_reserve_space(xdr, slot->sl_datalen);
2370 if (!p) {
2371 WARN_ON_ONCE(1);
2372 return nfserr_serverfault;
2373 }
2374 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2375 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002376
Andy Adamson557ce262009-08-28 08:45:04 -04002377 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002378 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002379}
2380
Andy Adamson0733d212009-04-03 08:28:01 +03002381/*
2382 * Set the exchange_id flags returned by the server.
2383 */
2384static void
2385nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2386{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002387#ifdef CONFIG_NFSD_PNFS
2388 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2389#else
Andy Adamson0733d212009-04-03 08:28:01 +03002390 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002391#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002392
2393 /* Referrals are supported, Migration is not. */
2394 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2395
2396 /* set the wire flags to return to client. */
2397 clid->flags = new->cl_exchange_flags;
2398}
2399
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002400static bool client_has_openowners(struct nfs4_client *clp)
2401{
2402 struct nfs4_openowner *oo;
2403
2404 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2405 if (!list_empty(&oo->oo_owner.so_stateids))
2406 return true;
2407 }
2408 return false;
2409}
2410
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002411static bool client_has_state(struct nfs4_client *clp)
2412{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002413 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002414#ifdef CONFIG_NFSD_PNFS
2415 || !list_empty(&clp->cl_lo_states)
2416#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002417 || !list_empty(&clp->cl_delegations)
2418 || !list_empty(&clp->cl_sessions);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002419}
2420
Al Virob37ad282006-10-19 23:28:59 -07002421__be32
Andy Adamson069b6ad2009-04-03 08:27:58 +03002422nfsd4_exchange_id(struct svc_rqst *rqstp,
2423 struct nfsd4_compound_state *cstate,
2424 struct nfsd4_exchange_id *exid)
2425{
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002426 struct nfs4_client *conf, *new;
2427 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002428 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002429 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002430 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002431 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002432 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002433 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002434
Jeff Layton363168b2009-08-14 12:57:56 -04002435 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002436 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002437 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002438 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002439 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002440
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002441 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002442 return nfserr_inval;
2443
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002444 new = create_client(exid->clname, rqstp, &verf);
2445 if (new == NULL)
2446 return nfserr_jukebox;
2447
Andy Adamson0733d212009-04-03 08:28:01 +03002448 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002449 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002450 exid->spo_must_enforce[0] = 0;
2451 exid->spo_must_enforce[1] = (
2452 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2453 1 << (OP_EXCHANGE_ID - 32) |
2454 1 << (OP_CREATE_SESSION - 32) |
2455 1 << (OP_DESTROY_SESSION - 32) |
2456 1 << (OP_DESTROY_CLIENTID - 32));
2457
2458 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2459 1 << (OP_OPEN_DOWNGRADE) |
2460 1 << (OP_LOCKU) |
2461 1 << (OP_DELEGRETURN));
2462
2463 exid->spo_must_allow[1] &= (
2464 1 << (OP_TEST_STATEID - 32) |
2465 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002466 if (!svc_rqst_integrity_protected(rqstp)) {
2467 status = nfserr_inval;
2468 goto out_nolock;
2469 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002470 /*
2471 * Sometimes userspace doesn't give us a principal.
2472 * Which is a bug, really. Anyway, we can't enforce
2473 * MACH_CRED in that case, better to give up now:
2474 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002475 if (!new->cl_cred.cr_principal &&
2476 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002477 status = nfserr_serverfault;
2478 goto out_nolock;
2479 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002480 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002481 case SP4_NONE:
2482 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002483 default: /* checked by xdr code */
2484 WARN_ON_ONCE(1);
Andy Adamson0733d212009-04-03 08:28:01 +03002485 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002486 status = nfserr_encr_alg_unsupp;
2487 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002488 }
2489
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002490 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002491 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002492 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002493 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002494 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2495 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2496
J. Bruce Fields136e6582012-05-12 20:37:23 -04002497 if (update) {
2498 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002499 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002500 goto out;
2501 }
Andrew Elblededeb132016-06-15 12:52:08 -04002502 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002503 status = nfserr_wrong_cred;
2504 goto out;
2505 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002506 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002507 status = nfserr_perm;
2508 goto out;
2509 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002510 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002511 status = nfserr_not_same;
2512 goto out;
2513 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002514 /* case 6 */
2515 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002516 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03002517 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002518 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002519 if (client_has_state(conf)) {
2520 status = nfserr_clid_inuse;
2521 goto out;
2522 }
Andy Adamson0733d212009-04-03 08:28:01 +03002523 goto out_new;
2524 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002525 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04002526 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002527 goto out_copy;
2528 }
2529 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002530 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04002531 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002532 }
2533
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002534 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03002535 status = nfserr_noent;
2536 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03002537 }
2538
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002539 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002540 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002541 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002542
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002543 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03002544out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04002545 if (conf) {
2546 status = mark_client_expired_locked(conf);
2547 if (status)
2548 goto out;
2549 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04002550 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04002551 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
2552 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03002553
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002554 gen_clid(new, nn);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002555 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002556 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03002557out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002558 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2559 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03002560
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002561 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2562 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03002563
2564 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002565 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03002566 status = nfs_ok;
2567
2568out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002569 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002570out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04002571 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002572 expire_client(new);
2573 if (unconf)
2574 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03002575 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002576}
2577
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002578static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04002579check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002580{
Andy Adamson88e588d2009-07-23 19:02:15 -04002581 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2582 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03002583
2584 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04002585 if (slot_inuse) {
2586 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002587 return nfserr_jukebox;
2588 else
2589 return nfserr_seq_misordered;
2590 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05002591 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04002592 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03002593 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04002594 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03002595 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03002596 return nfserr_seq_misordered;
2597}
2598
Andy Adamson49557cc2009-07-23 19:02:16 -04002599/*
2600 * Cache the create session result into the create session single DRC
2601 * slot cache by saving the xdr structure. sl_seqid has been set.
2602 * Do this for solo or embedded create session operations.
2603 */
2604static void
2605nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002606 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04002607{
2608 slot->sl_status = nfserr;
2609 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2610}
2611
2612static __be32
2613nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2614 struct nfsd4_clid_slot *slot)
2615{
2616 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2617 return slot->sl_status;
2618}
2619
Mi Jinlong1b74c252011-07-14 14:50:17 +08002620#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2621 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2622 1 + /* MIN tag is length with zero, only length */ \
2623 3 + /* version, opcount, opcode */ \
2624 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2625 /* seqid, slotID, slotID, cache */ \
2626 4 ) * sizeof(__be32))
2627
2628#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2629 2 + /* verifier: AUTH_NULL, length 0 */\
2630 1 + /* status */ \
2631 1 + /* MIN tag is length with zero, only length */ \
2632 3 + /* opcount, opcode, opstatus*/ \
2633 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2634 /* seqid, slotID, slotID, slotID, status */ \
2635 5 ) * sizeof(__be32))
2636
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002637static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08002638{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002639 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2640
J. Bruce Fields373cd402013-04-08 15:42:12 -04002641 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2642 return nfserr_toosmall;
2643 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2644 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002645 ca->headerpadsz = 0;
2646 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2647 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2648 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2649 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2650 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2651 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2652 /*
2653 * Note decreasing slot size below client's request may make it
2654 * difficult for client to function correctly, whereas
2655 * decreasing the number of slots will (just?) affect
2656 * performance. When short on memory we therefore prefer to
2657 * decrease number of slots instead of their size. Clients that
2658 * request larger slots than they need will get poor results:
2659 */
2660 ca->maxreqs = nfsd4_get_drc_mem(ca);
2661 if (!ca->maxreqs)
2662 return nfserr_jukebox;
2663
J. Bruce Fields373cd402013-04-08 15:42:12 -04002664 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08002665}
2666
Chuck Lever45006322016-03-01 13:06:02 -05002667/*
2668 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2669 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2670 */
2671#define RPC_MAX_HEADER_WITH_AUTH_SYS \
2672 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2673
2674#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2675 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2676
Kinglong Mee8a891632013-12-23 18:11:02 +08002677#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002678 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002679#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05002680 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2681 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08002682
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002683static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2684{
2685 ca->headerpadsz = 0;
2686
Kinglong Mee8a891632013-12-23 18:11:02 +08002687 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002688 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08002689 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002690 return nfserr_toosmall;
2691 ca->maxresp_cached = 0;
2692 if (ca->maxops < 2)
2693 return nfserr_toosmall;
2694
2695 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002696}
2697
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002698static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2699{
2700 switch (cbs->flavor) {
2701 case RPC_AUTH_NULL:
2702 case RPC_AUTH_UNIX:
2703 return nfs_ok;
2704 default:
2705 /*
2706 * GSS case: the spec doesn't allow us to return this
2707 * error. But it also doesn't allow us not to support
2708 * GSS.
2709 * I'd rather this fail hard than return some error the
2710 * client might think it can already handle:
2711 */
2712 return nfserr_encr_alg_unsupp;
2713 }
2714}
2715
Andy Adamson069b6ad2009-04-03 08:27:58 +03002716__be32
2717nfsd4_create_session(struct svc_rqst *rqstp,
2718 struct nfsd4_compound_state *cstate,
2719 struct nfsd4_create_session *cr_ses)
2720{
Jeff Layton363168b2009-08-14 12:57:56 -04002721 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002722 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04002723 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002724 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002725 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002726 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002727 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002728 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002729
Mi Jinlonga62573d2011-03-23 17:57:07 +08002730 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2731 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002732 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2733 if (status)
2734 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002735 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04002736 if (status)
2737 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04002738 status = check_backchannel_attrs(&cr_ses->back_channel);
2739 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08002740 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002741 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08002742 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002743 if (!new)
2744 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002745 conn = alloc_conn_from_crses(rqstp, cr_ses);
2746 if (!conn)
2747 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08002748
Jeff Laytond20c11d2014-07-30 08:27:07 -04002749 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002750 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002751 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04002752 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002753
2754 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002755 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002756 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002757 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002758 cs_slot = &conf->cl_cs_slot;
2759 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08002760 if (status) {
2761 if (status == nfserr_replay_cache)
2762 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002763 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002764 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002765 } else if (unconf) {
2766 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04002767 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002768 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002769 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002770 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04002771 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002772 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002773 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04002774 cs_slot = &unconf->cl_cs_slot;
2775 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03002776 if (status) {
2777 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002778 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002779 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002780 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002781 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002782 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04002783 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04002784 if (status) {
2785 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04002786 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04002787 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04002788 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04002789 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002790 conf = unconf;
2791 } else {
2792 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002793 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002794 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002795 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002796 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002797 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05002798 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04002799 cr_ses->flags &= ~SESSION4_RDMA;
2800
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002801 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002802 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002803
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -04002804 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002805 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04002806 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04002807 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002808
Jeff Laytond20c11d2014-07-30 08:27:07 -04002809 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04002810 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002811 spin_unlock(&nn->client_lock);
2812 /* init connection and backchannel */
2813 nfsd4_init_conn(rqstp, conn, new);
2814 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002815 if (old)
2816 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03002817 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002818out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04002819 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002820 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04002821 if (old)
2822 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04002823out_free_session:
2824 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04002825out_release_drc_mem:
2826 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04002827 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002828}
2829
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002830static __be32 nfsd4_map_bcts_dir(u32 *dir)
2831{
2832 switch (*dir) {
2833 case NFS4_CDFC4_FORE:
2834 case NFS4_CDFC4_BACK:
2835 return nfs_ok;
2836 case NFS4_CDFC4_FORE_OR_BOTH:
2837 case NFS4_CDFC4_BACK_OR_BOTH:
2838 *dir = NFS4_CDFC4_BOTH;
2839 return nfs_ok;
2840 };
2841 return nfserr_inval;
2842}
2843
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002844__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2845{
2846 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002847 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002848 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002849
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04002850 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2851 if (status)
2852 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002853 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002854 session->se_cb_prog = bc->bc_cb_program;
2855 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002856 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002857
2858 nfsd4_probe_callback(session->se_client);
2859
2860 return nfs_ok;
2861}
2862
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002863__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2864 struct nfsd4_compound_state *cstate,
2865 struct nfsd4_bind_conn_to_session *bcts)
2866{
2867 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002868 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002869 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002870 struct net *net = SVC_NET(rqstp);
2871 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002872
2873 if (!nfsd4_last_compound_op(rqstp))
2874 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002875 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002876 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002877 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002878 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002879 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002880 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002881 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04002882 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002883 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002884 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002885 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002886 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002887 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04002888 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002889 goto out;
2890 nfsd4_init_conn(rqstp, conn, session);
2891 status = nfs_ok;
2892out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002893 nfsd4_put_session(session);
2894out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04002895 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002896}
2897
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002898static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2899{
2900 if (!session)
2901 return 0;
2902 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2903}
2904
Andy Adamson069b6ad2009-04-03 08:27:58 +03002905__be32
2906nfsd4_destroy_session(struct svc_rqst *r,
2907 struct nfsd4_compound_state *cstate,
2908 struct nfsd4_destroy_session *sessionid)
2909{
Benny Halevye10e0cf2009-04-03 08:28:38 +03002910 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002911 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002912 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002913 struct net *net = SVC_NET(r);
2914 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002915
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002916 status = nfserr_not_only_op;
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04002917 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04002918 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002919 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002920 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04002921 }
Benny Halevye10e0cf2009-04-03 08:28:38 +03002922 dump_sessionid(__func__, &sessionid->sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002923 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002924 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002925 if (!ses)
2926 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002927 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04002928 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002929 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002930 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002931 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002932 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03002933 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002934 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002935
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05002936 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04002937
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03002938 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002939 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04002940out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002941 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04002942out_client_lock:
2943 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03002944out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03002945 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03002946}
2947
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002948static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002949{
2950 struct nfsd4_conn *c;
2951
2952 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002953 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04002954 return c;
2955 }
2956 }
2957 return NULL;
2958}
2959
J. Bruce Fields57266a62013-04-13 14:27:29 -04002960static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04002961{
2962 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002963 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002964 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002965 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002966
2967 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04002968 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002969 if (c)
2970 goto out_free;
2971 status = nfserr_conn_not_bound_to_session;
2972 if (clp->cl_mach_cred)
2973 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002974 __nfsd4_hash_conn(new, ses);
2975 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04002976 ret = nfsd4_register_conn(new);
2977 if (ret)
2978 /* oops; xprt is already down: */
2979 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002980 return nfs_ok;
2981out_free:
2982 spin_unlock(&clp->cl_lock);
2983 free_conn(new);
2984 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04002985}
2986
Mi Jinlong868b89c2011-04-27 09:09:58 +08002987static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2988{
2989 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2990
2991 return args->opcnt > session->se_fchannel.maxops;
2992}
2993
Mi Jinlongae82a8d2011-07-14 14:56:02 +08002994static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2995 struct nfsd4_session *session)
2996{
2997 struct xdr_buf *xb = &rqstp->rq_arg;
2998
2999 return xb->len > session->se_fchannel.maxreq_sz;
3000}
3001
Andy Adamson069b6ad2009-04-03 08:27:58 +03003002__be32
Benny Halevyb85d4c02009-04-03 08:28:08 +03003003nfsd4_sequence(struct svc_rqst *rqstp,
Andy Adamson069b6ad2009-04-03 08:27:58 +03003004 struct nfsd4_compound_state *cstate,
3005 struct nfsd4_sequence *seq)
3006{
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003007 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003008 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003009 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003010 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003011 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003012 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003013 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003014 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003015 struct net *net = SVC_NET(rqstp);
3016 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003017
Andy Adamsonf9bb94c2009-04-03 08:28:12 +03003018 if (resp->opcnt != 1)
3019 return nfserr_sequence_pos;
3020
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003021 /*
3022 * Will be either used or freed by nfsd4_sequence_check_conn
3023 * below.
3024 */
3025 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3026 if (!conn)
3027 return nfserr_jukebox;
3028
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003029 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003030 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003031 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003032 goto out_no_session;
3033 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003034
Mi Jinlong868b89c2011-04-27 09:09:58 +08003035 status = nfserr_too_many_ops;
3036 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003037 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003038
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003039 status = nfserr_req_too_big;
3040 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003041 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003042
Benny Halevyb85d4c02009-04-03 08:28:08 +03003043 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003044 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003045 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003046
Andy Adamson557ce262009-08-28 08:45:04 -04003047 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003048 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3049
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003050 /* We do not negotiate the number of slots yet, so set the
3051 * maxslots to the session maxreqs which is used to encode
3052 * sr_highest_slotid and the sr_target_slot id to maxslots */
3053 seq->maxslots = session->se_fchannel.maxreqs;
3054
J. Bruce Fields73e79482012-02-13 16:39:00 -05003055 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3056 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003057 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003058 status = nfserr_seq_misordered;
3059 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003060 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003061 cstate->slot = slot;
3062 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003063 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003064 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003065 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003066 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003067 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003068 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003069 }
3070 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003071 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003072
J. Bruce Fields57266a62013-04-13 14:27:29 -04003073 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003074 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003075 if (status)
3076 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003077
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003078 buflen = (seq->cachethis) ?
3079 session->se_fchannel.maxresp_cached :
3080 session->se_fchannel.maxresp_sz;
3081 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3082 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003083 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003084 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003085 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003086
3087 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003088 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003089 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003090 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003091 if (seq->cachethis)
3092 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003093 else
3094 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003095
3096 cstate->slot = slot;
3097 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003098 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003099
Benny Halevyb85d4c02009-04-03 08:28:08 +03003100out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003101 switch (clp->cl_cb_state) {
3102 case NFSD4_CB_DOWN:
3103 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3104 break;
3105 case NFSD4_CB_FAULT:
3106 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3107 break;
3108 default:
3109 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003110 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003111 if (!list_empty(&clp->cl_revoked))
3112 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003113out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003114 if (conn)
3115 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003116 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003117 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003118out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003119 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003120 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003121}
3122
Trond Myklebustb6076642014-06-30 11:48:35 -04003123void
3124nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3125{
3126 struct nfsd4_compound_state *cs = &resp->cstate;
3127
3128 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003129 if (cs->status != nfserr_replay_cache) {
3130 nfsd4_store_cache_entry(resp);
3131 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3132 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003133 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003134 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003135 } else if (cs->clp)
3136 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003137}
3138
Mi Jinlong345c2842011-10-20 17:51:39 +08003139__be32
3140nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3141{
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003142 struct nfs4_client *conf, *unconf;
3143 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003144 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003145 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003146
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003147 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003148 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003149 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003150 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003151
3152 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003153 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003154 status = nfserr_clientid_busy;
3155 goto out;
3156 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003157 status = mark_client_expired_locked(conf);
3158 if (status)
3159 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003160 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003161 } else if (unconf)
3162 clp = unconf;
3163 else {
3164 status = nfserr_stale_clientid;
3165 goto out;
3166 }
Andrew Elblededeb132016-06-15 12:52:08 -04003167 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003168 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003169 status = nfserr_wrong_cred;
3170 goto out;
3171 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003172 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003173out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003174 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003175 if (clp)
3176 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003177 return status;
3178}
3179
Andy Adamson069b6ad2009-04-03 08:27:58 +03003180__be32
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003181nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3182{
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003183 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003184
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003185 if (rc->rca_one_fs) {
3186 if (!cstate->current_fh.fh_dentry)
3187 return nfserr_nofilehandle;
3188 /*
3189 * We don't take advantage of the rca_one_fs case.
3190 * That's OK, it's optional, we can safely ignore it.
3191 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003192 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003193 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003194
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003195 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003196 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3197 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003198 goto out;
3199
3200 status = nfserr_stale_clientid;
3201 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003202 /*
3203 * The following error isn't really legal.
3204 * But we only get here if the client just explicitly
3205 * destroyed the client. Surely it no longer cares what
3206 * error it gets back on an operation for the dead
3207 * client.
3208 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003209 goto out;
3210
3211 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003212 nfsd4_client_record_create(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003213out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003214 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003215}
3216
3217__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003218nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3219 struct nfsd4_setclientid *setclid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003221 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003223 struct nfs4_client *conf, *new;
3224 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003225 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003226 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3227
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003228 new = create_client(clname, rqstp, &clverifier);
3229 if (new == NULL)
3230 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003231 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003232 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003233 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003234 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003235 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003237 if (clp_used_exchangeid(conf))
3238 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003239 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003240 char addr_str[INET6_ADDRSTRLEN];
3241 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3242 sizeof(addr_str));
3243 dprintk("NFSD: setclientid: string in use by client "
3244 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 goto out;
3246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003248 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003249 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003250 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003251 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003252 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003254 gen_confirm(new, nn);
3255 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003256 gen_clid(new, nn);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003257 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003258 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003259 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3261 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3262 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003263 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 status = nfs_ok;
3265out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003266 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003267 if (new)
3268 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003269 if (unconf)
3270 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return status;
3272}
3273
3274
Al Virob37ad282006-10-19 23:28:59 -07003275__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003276nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3277 struct nfsd4_compound_state *cstate,
3278 struct nfsd4_setclientid_confirm *setclientid_confirm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
NeilBrown21ab45a2005-06-23 22:04:14 -07003280 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003281 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3283 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003284 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003285 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003287 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003289
Jeff Laytond20c11d2014-07-30 08:27:07 -04003290 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003291 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003292 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003293 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003294 * We try hard to give out unique clientid's, so if we get an
3295 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003296 * the client may be buggy; this should never happen.
3297 *
3298 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003299 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003300 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003301 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3302 goto out;
3303 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3304 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003305 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003306 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003307 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3308 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003310 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003311 status = nfserr_stale_clientid;
3312 goto out;
3313 }
3314 status = nfs_ok;
3315 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003316 old = unconf;
3317 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003318 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003319 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003320 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3321 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003322 status = nfserr_clid_inuse;
3323 if (client_has_state(old)
3324 && !same_creds(&unconf->cl_cred,
3325 &old->cl_cred))
3326 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003327 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003328 if (status) {
3329 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003330 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003331 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003332 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003333 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003334 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003335 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003336 get_client_locked(conf);
3337 spin_unlock(&nn->client_lock);
3338 nfsd4_probe_callback(conf);
3339 spin_lock(&nn->client_lock);
3340 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003342 spin_unlock(&nn->client_lock);
3343 if (old)
3344 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 return status;
3346}
3347
J. Bruce Fields32513b42011-10-13 16:00:16 -04003348static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003350 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3351}
3352
3353/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003354static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3355 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003356{
Trond Myklebust950e0112014-06-30 11:48:31 -04003357 lockdep_assert_held(&state_lock);
3358
J. Bruce Fields32513b42011-10-13 16:00:16 -04003359 atomic_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003360 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003361 INIT_LIST_HEAD(&fp->fi_stateids);
3362 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003363 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003364 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003365 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003366 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003367 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003368 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3369 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003370#ifdef CONFIG_NFSD_PNFS
3371 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003372 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003373#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003374 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003377void
NeilBrowne60d4392005-06-23 22:03:01 -07003378nfsd4_free_slabs(void)
3379{
Sachin Bhamare8287f002015-04-27 14:50:14 +02003380 kmem_cache_destroy(odstate_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003381 kmem_cache_destroy(openowner_slab);
3382 kmem_cache_destroy(lockowner_slab);
3383 kmem_cache_destroy(file_slab);
3384 kmem_cache_destroy(stateid_slab);
3385 kmem_cache_destroy(deleg_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Bryan Schumaker72083392011-11-01 15:24:59 -04003388int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389nfsd4_init_slabs(void)
3390{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003391 openowner_slab = kmem_cache_create("nfsd4_openowners",
3392 sizeof(struct nfs4_openowner), 0, 0, NULL);
3393 if (openowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003394 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003395 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003396 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003397 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003398 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003399 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003400 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003401 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003402 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003403 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003404 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003405 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003406 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003407 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003408 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003409 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003410 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003411 odstate_slab = kmem_cache_create("nfsd4_odstate",
3412 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3413 if (odstate_slab == NULL)
3414 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003416
Sachin Bhamare8287f002015-04-27 14:50:14 +02003417out_free_deleg_slab:
3418 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003419out_free_stateid_slab:
3420 kmem_cache_destroy(stateid_slab);
3421out_free_file_slab:
3422 kmem_cache_destroy(file_slab);
3423out_free_lockowner_slab:
3424 kmem_cache_destroy(lockowner_slab);
3425out_free_openowner_slab:
3426 kmem_cache_destroy(openowner_slab);
3427out:
NeilBrowne60d4392005-06-23 22:03:01 -07003428 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3429 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003432static void init_nfs4_replay(struct nfs4_replay *rp)
3433{
3434 rp->rp_status = nfserr_serverfault;
3435 rp->rp_buflen = 0;
3436 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003437 mutex_init(&rp->rp_mutex);
3438}
3439
3440static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3441 struct nfs4_stateowner *so)
3442{
3443 if (!nfsd4_has_session(cstate)) {
3444 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003445 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003446 }
3447}
3448
3449void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3450{
3451 struct nfs4_stateowner *so = cstate->replay_owner;
3452
3453 if (so != NULL) {
3454 cstate->replay_owner = NULL;
3455 mutex_unlock(&so->so_replay.rp_mutex);
3456 nfs4_put_stateowner(so);
3457 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003458}
3459
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003460static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
3462 struct nfs4_stateowner *sop;
3463
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003464 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003465 if (!sop)
3466 return NULL;
3467
3468 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3469 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003470 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003471 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003473 sop->so_owner.len = owner->len;
3474
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003475 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003476 sop->so_client = clp;
3477 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04003478 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003479 return sop;
3480}
3481
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003482static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003483{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003484 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03003485
Trond Myklebustd4f04892014-07-29 21:34:36 -04003486 list_add(&oo->oo_owner.so_strhash,
3487 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003488 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003491static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3492{
Trond Myklebustd4f04892014-07-29 21:34:36 -04003493 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003494}
3495
Jeff Layton6b180f02014-07-29 21:34:26 -04003496static void nfs4_free_openowner(struct nfs4_stateowner *so)
3497{
3498 struct nfs4_openowner *oo = openowner(so);
3499
3500 kmem_cache_free(openowner_slab, oo);
3501}
3502
3503static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04003504 .so_unhash = nfs4_unhash_openowner,
3505 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04003506};
3507
Andrew Elble7fc05642015-11-05 20:42:43 -05003508static struct nfs4_ol_stateid *
3509nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3510{
3511 struct nfs4_ol_stateid *local, *ret = NULL;
3512 struct nfs4_openowner *oo = open->op_openowner;
3513
3514 lockdep_assert_held(&fp->fi_lock);
3515
3516 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3517 /* ignore lock owners */
3518 if (local->st_stateowner->so_is_open_owner == 0)
3519 continue;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003520 if (local->st_stateowner != &oo->oo_owner)
3521 continue;
3522 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05003523 ret = local;
3524 atomic_inc(&ret->st_stid.sc_count);
3525 break;
3526 }
3527 }
3528 return ret;
3529}
3530
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003531static __be32
3532nfsd4_verify_open_stid(struct nfs4_stid *s)
3533{
3534 __be32 ret = nfs_ok;
3535
3536 switch (s->sc_type) {
3537 default:
3538 break;
3539 case NFS4_CLOSED_STID:
3540 case NFS4_CLOSED_DELEG_STID:
3541 ret = nfserr_bad_stateid;
3542 break;
3543 case NFS4_REVOKED_DELEG_STID:
3544 ret = nfserr_deleg_revoked;
3545 }
3546 return ret;
3547}
3548
3549/* Lock the stateid st_mutex, and deal with races with CLOSE */
3550static __be32
3551nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3552{
3553 __be32 ret;
3554
3555 mutex_lock(&stp->st_mutex);
3556 ret = nfsd4_verify_open_stid(&stp->st_stid);
3557 if (ret != nfs_ok)
3558 mutex_unlock(&stp->st_mutex);
3559 return ret;
3560}
3561
3562static struct nfs4_ol_stateid *
3563nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3564{
3565 struct nfs4_ol_stateid *stp;
3566 for (;;) {
3567 spin_lock(&fp->fi_lock);
3568 stp = nfsd4_find_existing_open(fp, open);
3569 spin_unlock(&fp->fi_lock);
3570 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3571 break;
3572 nfs4_put_stid(&stp->st_stid);
3573 }
3574 return stp;
3575}
3576
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003577static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04003578alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003579 struct nfsd4_compound_state *cstate)
3580{
Trond Myklebust13d6f662014-06-30 11:48:45 -04003581 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003582 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003584 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3585 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04003587 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003588 oo->oo_owner.so_is_open_owner = 1;
3589 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003590 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04003591 if (nfsd4_has_session(cstate))
3592 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003593 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04003594 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003595 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04003596 spin_lock(&clp->cl_lock);
3597 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04003598 if (ret == NULL) {
3599 hash_openowner(oo, clp, strhashval);
3600 ret = oo;
3601 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08003602 nfs4_free_stateowner(&oo->oo_owner);
3603
Trond Myklebustd4f04892014-07-29 21:34:36 -04003604 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04003605 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607
Andrew Elble7fc05642015-11-05 20:42:43 -05003608static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003609init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05003610{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Andrew Elble7fc05642015-11-05 20:42:43 -05003612 struct nfs4_openowner *oo = open->op_openowner;
3613 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003614 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05003615
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003616 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003617 /* We are moving these outside of the spinlocks to avoid the warnings */
3618 mutex_init(&stp->st_mutex);
3619 mutex_lock(&stp->st_mutex);
3620
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003621retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05003622 spin_lock(&oo->oo_owner.so_client->cl_lock);
3623 spin_lock(&fp->fi_lock);
3624
3625 retstp = nfsd4_find_existing_open(fp, open);
3626 if (retstp)
3627 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003628
3629 open->op_stp = NULL;
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04003630 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05003631 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04003632 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003633 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07003634 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04003635 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 stp->st_access_bmap = 0;
3637 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07003638 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04003639 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003640 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05003641
3642out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04003643 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04003644 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003645 if (retstp) {
Trond Myklebust1c8ea412017-11-03 08:00:10 -04003646 /* Handle races with CLOSE */
3647 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3648 nfs4_put_stid(&retstp->st_stid);
3649 goto retry;
3650 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003651 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003652 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003653 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04003654 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04003655 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}
3657
Jeff Laytond3134b12014-07-29 21:34:32 -04003658/*
3659 * In the 4.0 case we need to keep the owners around a little while to handle
3660 * CLOSE replay. We still do need to release any file access that is held by
3661 * them before returning however.
3662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663static void
Jeff Laytond3134b12014-07-29 21:34:32 -04003664move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Jeff Layton217526e2014-07-30 08:27:11 -04003666 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04003667 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3668 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3669 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003670
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003671 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Jeff Laytonb401be222014-07-29 21:34:33 -04003673 /*
3674 * We know that we hold one reference via nfsd4_close, and another
3675 * "persistent" reference for the client. If the refcount is higher
3676 * than 2, then there are still calls in progress that are using this
3677 * stateid. We can't put the sc_file reference until they are finished.
3678 * Wait for the refcount to drop to 2. Since it has been unhashed,
3679 * there should be no danger of the refcount going back up again at
3680 * this point.
3681 */
3682 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3683
Jeff Laytond3134b12014-07-29 21:34:32 -04003684 release_all_access(s);
3685 if (s->st_stid.sc_file) {
3686 put_nfs4_file(s->st_stid.sc_file);
3687 s->st_stid.sc_file = NULL;
3688 }
Jeff Layton217526e2014-07-30 08:27:11 -04003689
3690 spin_lock(&nn->client_lock);
3691 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04003692 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03003693 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003694 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04003695 spin_unlock(&nn->client_lock);
3696 if (last)
3697 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700/* search file_hashtbl[] for file */
3701static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04003702find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 struct nfs4_file *fp;
3705
Jeff Layton5b095e92014-10-23 08:01:02 -04003706 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02003707 if (fh_match(&fp->fi_fhandle, fh)) {
Jeff Layton5b095e92014-10-23 08:01:02 -04003708 if (atomic_inc_not_zero(&fp->fi_ref))
3709 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07003710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 }
3712 return NULL;
3713}
3714
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02003715struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04003716find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003717{
3718 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003719 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04003720
Jeff Layton5b095e92014-10-23 08:01:02 -04003721 rcu_read_lock();
3722 fp = find_file_locked(fh, hashval);
3723 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04003724 return fp;
3725}
3726
3727static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04003728find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04003729{
3730 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04003731 unsigned int hashval = file_hashval(fh);
3732
3733 rcu_read_lock();
3734 fp = find_file_locked(fh, hashval);
3735 rcu_read_unlock();
3736 if (fp)
3737 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04003738
3739 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04003740 fp = find_file_locked(fh, hashval);
3741 if (likely(fp == NULL)) {
3742 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04003743 fp = new;
3744 }
3745 spin_unlock(&state_lock);
3746
3747 return fp;
3748}
3749
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04003750/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 * Called to check deny when READ with all zero stateid or
3752 * WRITE with all zero or all one stateid
3753 */
Al Virob37ad282006-10-19 23:28:59 -07003754static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3756{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003758 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Trond Myklebustca943212014-07-23 16:17:39 -04003760 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07003761 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003762 return ret;
3763 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04003764 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003765 if (fp->fi_share_deny & deny_type)
3766 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04003767 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07003768 put_nfs4_file(fp);
3769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003772static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003774 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04003775 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3776 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04003777
Trond Myklebust11b91642014-07-29 21:34:08 -04003778 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04003779
Jeff Layton02e12152014-07-16 10:31:57 -04003780 /*
3781 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04003782 * already holding inode->i_lock.
3783 *
Jeff Laytondff13992014-07-08 14:02:49 -04003784 * If the dl_time != 0, then we know that it has already been
3785 * queued for a lease break. Don't queue it again.
3786 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04003787 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04003788 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04003789 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04003790 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04003791 }
Jeff Layton02e12152014-07-16 10:31:57 -04003792 spin_unlock(&state_lock);
3793}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003795static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3796 struct rpc_task *task)
3797{
3798 struct nfs4_delegation *dp = cb_to_delegation(cb);
3799
Andrew Elblea4579742015-08-31 12:06:41 -04003800 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3801 return 1;
3802
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003803 switch (task->tk_status) {
3804 case 0:
3805 return 1;
3806 case -EBADHANDLE:
3807 case -NFS4ERR_BAD_STATEID:
3808 /*
3809 * Race: client probably got cb_recall before open reply
3810 * granting delegation.
3811 */
3812 if (dp->dl_retries--) {
3813 rpc_delay(task, 2 * HZ);
3814 return 0;
3815 }
3816 /*FALLTHRU*/
3817 default:
3818 return -1;
3819 }
3820}
3821
3822static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3823{
3824 struct nfs4_delegation *dp = cb_to_delegation(cb);
3825
3826 nfs4_put_stid(&dp->dl_stid);
3827}
3828
Julia Lawallc4cb8972015-11-21 22:57:39 +01003829static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02003830 .prepare = nfsd4_cb_recall_prepare,
3831 .done = nfsd4_cb_recall_done,
3832 .release = nfsd4_cb_recall_release,
3833};
3834
Jeff Layton02e12152014-07-16 10:31:57 -04003835static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3836{
3837 /*
3838 * We're assuming the state code never drops its reference
3839 * without first removing the lease. Since we're in this lease
3840 * callback (and since the lease code is serialized by the kernel
3841 * lock) we know the server hasn't removed the lease yet, we know
3842 * it's safe to take a reference.
3843 */
Trond Myklebust72c0b0f2014-07-21 09:34:58 -04003844 atomic_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02003845 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003846}
3847
Jeff Layton1c8c6012013-06-21 08:58:15 -04003848/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003849static bool
3850nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003851{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003852 bool ret = false;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003853 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3854 struct nfs4_delegation *dp;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003855
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003856 if (!fp) {
3857 WARN(1, "(%p)->fl_owner NULL\n", fl);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003858 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003859 }
3860 if (fp->fi_had_conflict) {
3861 WARN(1, "duplicate break on %p\n", fp);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003862 return ret;
J. Bruce Fields7fa10cd2012-10-16 12:39:33 -04003863 }
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003864 /*
3865 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05003866 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05003867 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04003868 */
3869 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Jeff Layton02e12152014-07-16 10:31:57 -04003871 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04003872 fp->fi_had_conflict = true;
3873 /*
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003874 * If there are no delegations on the list, then return true
3875 * so that the lease code will go ahead and delete it.
Jeff Layton417c6622014-07-21 09:34:57 -04003876 */
3877 if (list_empty(&fp->fi_delegations))
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003878 ret = true;
Jeff Layton417c6622014-07-21 09:34:57 -04003879 else
3880 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3881 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04003882 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04003883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
Jeff Laytonc45198e2014-09-01 07:12:07 -04003886static int
Jeff Layton7448cc32015-01-16 15:05:57 -05003887nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3888 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
3890 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04003891 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 else
3893 return -EAGAIN;
3894}
3895
Alexey Dobriyan7b021962009-09-21 17:01:12 -07003896static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04003897 .lm_break = nfsd_break_deleg_cb,
3898 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899};
3900
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04003901static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3902{
3903 if (nfsd4_has_session(cstate))
3904 return nfs_ok;
3905 if (seqid == so->so_seqid - 1)
3906 return nfserr_replay_me;
3907 if (seqid == so->so_seqid)
3908 return nfs_ok;
3909 return nfserr_bad_seqid;
3910}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
Jeff Layton4b24ca72014-06-30 11:48:44 -04003912static __be32 lookup_clientid(clientid_t *clid,
3913 struct nfsd4_compound_state *cstate,
3914 struct nfsd_net *nn)
3915{
3916 struct nfs4_client *found;
3917
3918 if (cstate->clp) {
3919 found = cstate->clp;
3920 if (!same_clid(&found->cl_clientid, clid))
3921 return nfserr_stale_clientid;
3922 return nfs_ok;
3923 }
3924
3925 if (STALE_CLIENTID(clid, nn))
3926 return nfserr_stale_clientid;
3927
3928 /*
3929 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3930 * cached already then we know this is for is for v4.0 and "sessions"
3931 * will be false.
3932 */
3933 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003934 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003935 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04003936 if (!found) {
3937 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003938 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04003939 }
3940 atomic_inc(&found->cl_refcount);
3941 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003942
3943 /* Cache the nfs4_client in cstate! */
3944 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003945 return nfs_ok;
3946}
3947
Al Virob37ad282006-10-19 23:28:59 -07003948__be32
Andy Adamson66689582009-04-03 08:28:45 +03003949nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03003950 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 clientid_t *clientid = &open->op_clientid;
3953 struct nfs4_client *clp = NULL;
3954 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003955 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003956 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003958 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003960 /*
3961 * In case we need it later, after we've already created the
3962 * file and don't want to risk a further failure:
3963 */
3964 open->op_file = nfsd4_alloc_file();
3965 if (open->op_file == NULL)
3966 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Trond Myklebust2d91e892014-06-30 11:48:46 -04003968 status = lookup_clientid(clientid, cstate, nn);
3969 if (status)
3970 return status;
3971 clp = cstate->clp;
3972
Trond Myklebustd4f04892014-07-29 21:34:36 -04003973 strhashval = ownerstr_hashval(&open->op_owner);
3974 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003975 open->op_openowner = oo;
3976 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003977 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04003979 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003980 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003981 release_openowner(oo);
3982 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003983 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08003984 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003985 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3986 if (status)
3987 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003988 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003989new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04003990 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04003991 if (oo == NULL)
3992 return nfserr_jukebox;
3993 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003994alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04003995 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04003996 if (!open->op_stp)
3997 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003998
3999 if (nfsd4_has_session(cstate) &&
4000 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4001 open->op_odstate = alloc_clnt_odstate(clp);
4002 if (!open->op_odstate)
4003 return nfserr_jukebox;
4004 }
4005
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004006 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
Al Virob37ad282006-10-19 23:28:59 -07004009static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004010nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4011{
4012 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4013 return nfserr_openmode;
4014 else
4015 return nfs_ok;
4016}
4017
Daniel Mackc47d8322011-05-16 16:38:14 +02004018static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004019{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004020 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4021}
4022
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004023static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004024{
4025 struct nfs4_stid *ret;
4026
Andrew Elble62f0b692017-11-03 14:06:31 -04004027 ret = find_stateid_by_type(cl, s,
4028 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004029 if (!ret)
4030 return NULL;
4031 return delegstateid(ret);
4032}
4033
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004034static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4035{
4036 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4037 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4038}
4039
Al Virob37ad282006-10-19 23:28:59 -07004040static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004041nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004042 struct nfs4_delegation **dp)
4043{
4044 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004045 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004046 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004047
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004048 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4049 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004050 goto out;
Andrew Elble62f0b692017-11-03 14:06:31 -04004051 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4052 nfs4_put_stid(&deleg->dl_stid);
4053 if (cl->cl_minorversion)
4054 status = nfserr_deleg_revoked;
4055 goto out;
4056 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004057 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004058 status = nfs4_check_delegmode(deleg, flags);
4059 if (status) {
4060 nfs4_put_stid(&deleg->dl_stid);
4061 goto out;
4062 }
4063 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004064out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004065 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004066 return nfs_ok;
4067 if (status)
4068 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004069 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004070 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004071}
4072
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004073static inline int nfs4_access_to_access(u32 nfs4_access)
4074{
4075 int flags = 0;
4076
4077 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4078 flags |= NFSD_MAY_READ;
4079 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4080 flags |= NFSD_MAY_WRITE;
4081 return flags;
4082}
4083
Al Virob37ad282006-10-19 23:28:59 -07004084static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4086 struct nfsd4_open *open)
4087{
4088 struct iattr iattr = {
4089 .ia_valid = ATTR_SIZE,
4090 .ia_size = 0,
4091 };
4092 if (!open->op_truncate)
4093 return 0;
4094 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004095 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4097}
4098
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004099static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004100 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4101 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004102{
Trond Myklebustde186432014-07-10 14:07:26 -04004103 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004104 __be32 status;
4105 int oflag = nfs4_access_to_omode(open->op_share_access);
4106 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004107 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004108
Trond Myklebustde186432014-07-10 14:07:26 -04004109 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004110
4111 /*
4112 * Are we trying to set a deny mode that would conflict with
4113 * current access?
4114 */
4115 status = nfs4_file_check_deny(fp, open->op_share_deny);
4116 if (status != nfs_ok) {
4117 spin_unlock(&fp->fi_lock);
4118 goto out;
4119 }
4120
4121 /* set access to the file */
4122 status = nfs4_file_get_access(fp, open->op_share_access);
4123 if (status != nfs_ok) {
4124 spin_unlock(&fp->fi_lock);
4125 goto out;
4126 }
4127
4128 /* Set access bits in stateid */
4129 old_access_bmap = stp->st_access_bmap;
4130 set_access(open->op_share_access, stp);
4131
4132 /* Set new deny mask */
4133 old_deny_bmap = stp->st_deny_bmap;
4134 set_deny(open->op_share_deny, stp);
4135 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4136
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004137 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004138 spin_unlock(&fp->fi_lock);
4139 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004140 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004141 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004142 spin_lock(&fp->fi_lock);
4143 if (!fp->fi_fds[oflag]) {
4144 fp->fi_fds[oflag] = filp;
4145 filp = NULL;
4146 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004147 }
Trond Myklebustde186432014-07-10 14:07:26 -04004148 spin_unlock(&fp->fi_lock);
4149 if (filp)
4150 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004151
4152 status = nfsd4_truncate(rqstp, cur_fh, open);
4153 if (status)
4154 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004155out:
4156 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004157out_put_access:
4158 stp->st_access_bmap = old_access_bmap;
4159 nfs4_file_put_access(fp, open->op_share_access);
4160 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4161 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004162}
4163
Al Virob37ad282006-10-19 23:28:59 -07004164static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004165nfs4_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 -07004166{
Al Virob37ad282006-10-19 23:28:59 -07004167 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004168 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004170 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004171 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004172
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004173 /* test and set deny mode */
4174 spin_lock(&fp->fi_lock);
4175 status = nfs4_file_check_deny(fp, open->op_share_deny);
4176 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004177 set_deny(open->op_share_deny, stp);
4178 fp->fi_share_deny |=
4179 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4180 }
4181 spin_unlock(&fp->fi_lock);
4182
4183 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004186 status = nfsd4_truncate(rqstp, cur_fh, open);
4187 if (status != nfs_ok)
4188 reset_union_bmap_deny(old_deny_bmap, stp);
4189 return status;
4190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004192/* Should we give out recallable state?: */
4193static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4194{
4195 if (clp->cl_cb_state == NFSD4_CB_UP)
4196 return true;
4197 /*
4198 * In the sessions case, since we don't have to establish a
4199 * separate connection for callbacks, we assume it's OK
4200 * until we hear otherwise:
4201 */
4202 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4203}
4204
Jeff Laytond564fbe2014-07-16 10:31:58 -04004205static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004206{
4207 struct file_lock *fl;
4208
4209 fl = locks_alloc_lock();
4210 if (!fl)
4211 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004212 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004213 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004214 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4215 fl->fl_end = OFFSET_MAX;
Jeff Laytond564fbe2014-07-16 10:31:58 -04004216 fl->fl_owner = (fl_owner_t)fp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004217 fl->fl_pid = current->tgid;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004218 return fl;
4219}
4220
Andrew Elble34ed9872015-10-15 12:07:28 -04004221/**
4222 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4223 * @dp: a pointer to the nfs4_delegation we're adding.
4224 *
4225 * Return:
4226 * On success: Return code will be 0 on success.
4227 *
4228 * On error: -EAGAIN if there was an existing delegation.
4229 * nonzero if there is an error in other cases.
4230 *
4231 */
4232
J. Bruce Fields99c41512013-05-21 16:21:25 -04004233static int nfs4_setlease(struct nfs4_delegation *dp)
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004234{
Trond Myklebust11b91642014-07-29 21:34:08 -04004235 struct nfs4_file *fp = dp->dl_stid.sc_file;
Kinglong Meeefde6b42015-07-13 17:33:24 +08004236 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004237 struct file *filp;
4238 int status = 0;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004239
Jeff Laytond564fbe2014-07-16 10:31:58 -04004240 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004241 if (!fl)
4242 return -ENOMEM;
Jeff Layton417c6622014-07-21 09:34:57 -04004243 filp = find_readable_file(fp);
4244 if (!filp) {
4245 /* We should always have a readable file here */
4246 WARN_ON_ONCE(1);
Kinglong Meeaf9dbaf2015-07-13 17:30:51 +08004247 locks_free_lock(fl);
Jeff Layton417c6622014-07-21 09:34:57 -04004248 return -EBADF;
4249 }
4250 fl->fl_file = filp;
Jeff Laytone6f5c782014-08-22 10:40:25 -04004251 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004252 if (fl)
Jeff Layton417c6622014-07-21 09:34:57 -04004253 locks_free_lock(fl);
Jeff Layton1c7dd2f2014-08-22 10:55:47 -04004254 if (status)
Jeff Layton417c6622014-07-21 09:34:57 -04004255 goto out_fput;
Benny Halevycdc97502014-05-30 09:09:30 -04004256 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004257 spin_lock(&fp->fi_lock);
4258 /* Did the lease get broken before we took the lock? */
4259 status = -EAGAIN;
4260 if (fp->fi_had_conflict)
4261 goto out_unlock;
4262 /* Race breaker */
Jeff Layton0c637be2014-08-22 12:05:43 -04004263 if (fp->fi_deleg_file) {
Andrew Elble34ed9872015-10-15 12:07:28 -04004264 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004265 goto out_unlock;
4266 }
Jeff Layton417c6622014-07-21 09:34:57 -04004267 fp->fi_deleg_file = filp;
Andrew Elble34ed9872015-10-15 12:07:28 -04004268 fp->fi_delegees = 0;
4269 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004270 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004271 spin_unlock(&state_lock);
Andrew Elble34ed9872015-10-15 12:07:28 -04004272 if (status) {
4273 /* Should never happen, this is a new fi_deleg_file */
4274 WARN_ON_ONCE(1);
4275 goto out_fput;
4276 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004277 return 0;
Jeff Layton417c6622014-07-21 09:34:57 -04004278out_unlock:
4279 spin_unlock(&fp->fi_lock);
4280 spin_unlock(&state_lock);
4281out_fput:
4282 fput(filp);
J. Bruce Fieldse8730882012-01-23 13:52:01 -05004283 return status;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004284}
4285
Jeff Layton0b266932014-07-25 07:34:25 -04004286static struct nfs4_delegation *
4287nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004288 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004289{
Jeff Layton0b266932014-07-25 07:34:25 -04004290 int status;
4291 struct nfs4_delegation *dp;
Jeff Layton417c6622014-07-21 09:34:57 -04004292
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004293 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004294 return ERR_PTR(-EAGAIN);
4295
Andrew Elble34ed9872015-10-15 12:07:28 -04004296 spin_lock(&state_lock);
4297 spin_lock(&fp->fi_lock);
4298 status = nfs4_get_existing_delegation(clp, fp);
4299 spin_unlock(&fp->fi_lock);
4300 spin_unlock(&state_lock);
4301
4302 if (status)
4303 return ERR_PTR(status);
4304
Sachin Bhamare8287f002015-04-27 14:50:14 +02004305 dp = alloc_init_deleg(clp, fh, odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004306 if (!dp)
4307 return ERR_PTR(-ENOMEM);
4308
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004309 get_nfs4_file(fp);
Benny Halevycdc97502014-05-30 09:09:30 -04004310 spin_lock(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004311 spin_lock(&fp->fi_lock);
Trond Myklebust11b91642014-07-29 21:34:08 -04004312 dp->dl_stid.sc_file = fp;
Jeff Layton0c637be2014-08-22 12:05:43 -04004313 if (!fp->fi_deleg_file) {
Jeff Layton417c6622014-07-21 09:34:57 -04004314 spin_unlock(&fp->fi_lock);
4315 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004316 status = nfs4_setlease(dp);
4317 goto out;
Jeff Layton417c6622014-07-21 09:34:57 -04004318 }
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004319 if (fp->fi_had_conflict) {
Jeff Layton417c6622014-07-21 09:34:57 -04004320 status = -EAGAIN;
4321 goto out_unlock;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004322 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004323 status = hash_delegation_locked(dp, fp);
Jeff Layton417c6622014-07-21 09:34:57 -04004324out_unlock:
4325 spin_unlock(&fp->fi_lock);
Benny Halevycdc97502014-05-30 09:09:30 -04004326 spin_unlock(&state_lock);
Jeff Layton0b266932014-07-25 07:34:25 -04004327out:
4328 if (status) {
Sachin Bhamare8287f002015-04-27 14:50:14 +02004329 put_clnt_odstate(dp->dl_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04004330 nfs4_put_stid(&dp->dl_stid);
Jeff Layton0b266932014-07-25 07:34:25 -04004331 return ERR_PTR(status);
4332 }
4333 return dp;
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004334}
4335
Benny Halevy4aa89132012-02-21 14:16:44 -08004336static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4337{
4338 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4339 if (status == -EAGAIN)
4340 open->op_why_no_deleg = WND4_CONTENTION;
4341 else {
4342 open->op_why_no_deleg = WND4_RESOURCE;
4343 switch (open->op_deleg_want) {
4344 case NFS4_SHARE_WANT_READ_DELEG:
4345 case NFS4_SHARE_WANT_WRITE_DELEG:
4346 case NFS4_SHARE_WANT_ANY_DELEG:
4347 break;
4348 case NFS4_SHARE_WANT_CANCEL:
4349 open->op_why_no_deleg = WND4_CANCELLED;
4350 break;
4351 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004352 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004353 }
4354 }
4355}
4356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357/*
4358 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004359 *
4360 * Note we don't support write delegations, and won't until the vfs has
4361 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 */
4363static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004364nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4365 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004368 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4369 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004370 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004371 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004373 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004374 open->op_recall = 0;
4375 switch (open->op_claim_type) {
4376 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004377 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004378 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004379 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4380 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004381 break;
4382 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004383 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004384 /*
4385 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004386 * had the chance to reclaim theirs, *and* until
4387 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004388 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004389 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004390 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004391 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004392 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004393 /*
4394 * Also, if the file was opened for write or
4395 * create, there's a good chance the client's
4396 * about to write to it, resulting in an
4397 * immediate recall (since we don't support
4398 * write delegations):
4399 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004400 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004401 goto out_no_deleg;
4402 if (open->op_create == NFS4_OPEN_CREATE)
4403 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004404 break;
4405 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004406 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004407 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004408 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004409 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004410 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004412 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004414 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004415 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004416 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004417 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004418 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004419out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004420 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4421 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004422 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004423 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004424 open->op_recall = 1;
4425 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004426
4427 /* 4.1 client asking for a delegation? */
4428 if (open->op_deleg_want)
4429 nfsd4_open_deleg_none_ext(open, status);
4430 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
4432
Benny Halevye27f49c2012-02-21 14:16:54 -08004433static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4434 struct nfs4_delegation *dp)
4435{
4436 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4437 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4438 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4439 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4440 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4441 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4442 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4443 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4444 }
4445 /* Otherwise the client must be confused wanting a delegation
4446 * it already has, therefore we don't return
4447 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4448 */
4449}
4450
Al Virob37ad282006-10-19 23:28:59 -07004451__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4453{
Andy Adamson66689582009-04-03 08:28:45 +03004454 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004455 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004457 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004458 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004459 __be32 status;
Trond Myklebust1c404252017-11-03 08:00:11 -04004460 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 /*
4463 * Lookup file; if found, lookup stateid and check open request,
4464 * and check for delegations in the process of being recalled.
4465 * If not found, create the nfs4_file struct
4466 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004467 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004468 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004469 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004470 if (status)
4471 goto out;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04004472 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004474 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004475 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004476 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 }
4479
Trond Myklebust1c404252017-11-03 08:00:11 -04004480 if (!stp) {
4481 stp = init_open_stateid(fp, open);
4482 if (!open->op_stp)
4483 new_stp = true;
4484 }
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 /*
4487 * OPEN the file, or upgrade an existing OPEN.
4488 * If truncate fails, the OPEN fails.
Trond Myklebust1c404252017-11-03 08:00:11 -04004489 *
4490 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 */
Trond Myklebust1c404252017-11-03 08:00:11 -04004492 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004494 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004495 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004496 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004500 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4501 if (status) {
Trond Myklebust1c404252017-11-03 08:00:11 -04004502 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004503 release_open_stateid(stp);
Trond Myklebust1c404252017-11-03 08:00:11 -04004504 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004505 goto out;
4506 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004507
4508 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4509 open->op_odstate);
4510 if (stp->st_clnt_odstate == open->op_odstate)
4511 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 }
Trond Myklebust1c404252017-11-03 08:00:11 -04004513
Jeff Layton9767feb2015-10-01 09:05:50 -04004514 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004515 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Benny Halevyd24433c2012-02-16 20:57:17 +02004517 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02004518 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4519 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4520 open->op_why_no_deleg = WND4_NOT_WANTED;
4521 goto nodeleg;
4522 }
4523 }
4524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 /*
4526 * Attempt to hand out a delegation. No error return, because the
4527 * OPEN succeeds even if we fail.
4528 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004529 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004530nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 status = nfs_ok;
4532
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004533 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004534 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535out:
Benny Halevyd24433c2012-02-16 20:57:17 +02004536 /* 4.1 client trying to upgrade/downgrade delegation? */
4537 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08004538 open->op_deleg_want)
4539 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02004540
NeilBrown13cd2182005-06-23 22:03:10 -07004541 if (fp)
4542 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07004543 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08004544 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 /*
4546 * To finish the open response, we just need to set the rflags.
4547 */
4548 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04004549 if (nfsd4_has_session(&resp->cstate))
4550 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
4551 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04004553
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004554 if (dp)
4555 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04004556 if (stp)
4557 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559 return status;
4560}
4561
Jeff Layton58fb12e2014-07-29 21:34:27 -04004562void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04004563 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004564{
4565 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04004566 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004567
Jeff Laytond3134b12014-07-29 21:34:32 -04004568 nfsd4_cstate_assign_replay(cstate, so);
4569 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004570 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04004571 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04004572 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004573 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04004574 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004575 if (open->op_odstate)
4576 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04004577}
4578
Al Virob37ad282006-10-19 23:28:59 -07004579__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004580nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4581 clientid_t *clid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
4583 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07004584 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004585 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 dprintk("process_renew(%08x/%08x): starting\n",
4588 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004589 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05004590 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004592 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07004594 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04004595 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 goto out;
4597 status = nfs_ok;
4598out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 return status;
4600}
4601
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04004602void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004603nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07004604{
Jeff Layton33dcc482012-04-10 11:08:48 -04004605 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004606 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04004607 return;
4608
NeilBrowna76b4312005-06-23 22:04:01 -07004609 dprintk("NFSD: end of grace period\n");
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04004610 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04004611 /*
4612 * If the server goes down again right now, an NFSv4
4613 * client will still be allowed to reclaim after it comes back up,
4614 * even if it hasn't yet had a chance to reclaim state this time.
4615 *
4616 */
Jeff Layton919b8042014-09-12 16:40:20 -04004617 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004618 /*
4619 * At this point, NFSv4 clients can still reclaim. But if the
4620 * server crashes, any that have not yet reclaimed will be out
4621 * of luck on the next boot.
4622 *
4623 * (NFSv4.1+ clients are considered to have reclaimed once they
4624 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4625 * have reclaimed after their first OPEN.)
4626 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04004627 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04004628 /*
4629 * At this point, and once lockd and/or any other containers
4630 * exit their grace period, further reclaims will fail and
4631 * regular locking can resume.
4632 */
NeilBrowna76b4312005-06-23 22:04:01 -07004633}
4634
NeilBrownfd39ca92005-06-23 22:04:03 -07004635static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004636nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
4638 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004639 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04004641 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04004642 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03004644 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004645 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 dprintk("NFSD: laundromat service - starting\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03004648 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03004649 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004650 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03004651 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 clp = list_entry(pos, struct nfs4_client, cl_lru);
4653 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4654 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04004655 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 break;
4657 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004658 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03004659 dprintk("NFSD: client in use (clientid %08x)\n",
4660 clp->cl_clientid.cl_id);
4661 continue;
4662 }
Trond Myklebust4864af92014-07-30 08:27:03 -04004663 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03004664 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004665 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03004666 list_for_each_safe(pos, next, &reaplist) {
4667 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 dprintk("NFSD: purging unused client (clientid %08x)\n",
4669 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04004670 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 expire_client(clp);
4672 }
Benny Halevycdc97502014-05-30 09:09:30 -04004673 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004674 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4676 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004677 t = dp->dl_time - cutoff;
4678 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 break;
4680 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04004681 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04004682 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 }
Benny Halevycdc97502014-05-30 09:09:30 -04004684 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04004685 while (!list_empty(&reaplist)) {
4686 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4687 dl_recall_lru);
4688 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004689 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 }
Jeff Layton217526e2014-07-30 08:27:11 -04004691
4692 spin_lock(&nn->client_lock);
4693 while (!list_empty(&nn->close_lru)) {
4694 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4695 oo_close_lru);
4696 if (time_after((unsigned long)oo->oo_time,
4697 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04004698 t = oo->oo_time - cutoff;
4699 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 break;
4701 }
Jeff Layton217526e2014-07-30 08:27:11 -04004702 list_del_init(&oo->oo_close_lru);
4703 stp = oo->oo_last_closed_stid;
4704 oo->oo_last_closed_stid = NULL;
4705 spin_unlock(&nn->client_lock);
4706 nfs4_put_stid(&stp->st_stid);
4707 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 }
Jeff Layton217526e2014-07-30 08:27:11 -04004709 spin_unlock(&nn->client_lock);
4710
Jeff Layton7919d0a2016-09-16 16:28:25 -04004711 /*
4712 * It's possible for a client to try and acquire an already held lock
4713 * that is being held for a long time, and then lose interest in it.
4714 * So, we clean out any un-revisited request after a lease period
4715 * under the assumption that the client is no longer interested.
4716 *
4717 * RFC5661, sec. 9.6 states that the client must not rely on getting
4718 * notifications and must continue to poll for locks, even when the
4719 * server supports them. Thus this shouldn't lead to clients blocking
4720 * indefinitely once the lock does become free.
4721 */
4722 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04004723 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004724 while (!list_empty(&nn->blocked_locks_lru)) {
4725 nbl = list_first_entry(&nn->blocked_locks_lru,
4726 struct nfsd4_blocked_lock, nbl_lru);
4727 if (time_after((unsigned long)nbl->nbl_time,
4728 (unsigned long)cutoff)) {
4729 t = nbl->nbl_time - cutoff;
4730 new_timeo = min(new_timeo, t);
4731 break;
4732 }
4733 list_move(&nbl->nbl_lru, &reaplist);
4734 list_del_init(&nbl->nbl_list);
4735 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04004736 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04004737
4738 while (!list_empty(&reaplist)) {
Naofumi Hondaaa1a6cf2017-11-09 10:57:16 -05004739 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04004740 struct nfsd4_blocked_lock, nbl_lru);
4741 list_del_init(&nbl->nbl_lru);
4742 posix_unblock_lock(&nbl->nbl_lock);
4743 free_blocked_lock(nbl);
4744 }
4745
Jeff Laytona832e7a2014-05-30 09:09:26 -04004746 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04004747 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
4749
Harvey Harrisona254b242008-02-20 12:49:00 -08004750static struct workqueue_struct *laundry_wq;
4751static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08004752
4753static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004754laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755{
4756 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08004757 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004758 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4759 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004761 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03004763 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
Jeff Layton8fcd4612015-07-30 06:57:46 -04004766static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07004767{
Jeff Layton8fcd4612015-07-30 06:57:46 -04004768 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04004769 return nfserr_bad_stateid;
4770 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771}
4772
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004774access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004776 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4777 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4778 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779}
4780
4781static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04004782access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Jeff Layton82c5ff12012-05-11 09:45:13 -04004784 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4785 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
4788static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004789__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Al Virob37ad282006-10-19 23:28:59 -07004791 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
J. Bruce Fields02921912010-07-29 15:16:59 -04004793 /* For lock stateid's, we test the parent open, not the lock: */
4794 if (stp->st_openstp)
4795 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004796 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04004798 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 goto out;
4800 status = nfs_ok;
4801out:
4802 return status;
4803}
4804
Al Virob37ad282006-10-19 23:28:59 -07004805static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004806check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08004808 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004810 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004811 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 * conflicting state; so we must wait out the grace period. */
4813 return nfserr_grace;
4814 } else if (flags & WR_STATE)
4815 return nfs4_share_conflict(current_fh,
4816 NFS4_SHARE_DENY_WRITE);
4817 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4818 return nfs4_share_conflict(current_fh,
4819 NFS4_SHARE_DENY_READ);
4820}
4821
4822/*
4823 * Allow READ/WRITE during grace period on recovered state only for files
4824 * that are not able to provide mandatory locking.
4825 */
4826static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04004827grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004829 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830}
4831
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004832static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05004833{
Andy Adamson66689582009-04-03 08:28:45 +03004834 /*
4835 * When sessions are used the stateid generation number is ignored
4836 * when it is zero.
4837 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04004838 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04004839 return nfs_ok;
4840
4841 if (in->si_generation == ref->si_generation)
4842 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03004843
J. Bruce Fields0836f582008-01-26 19:08:12 -05004844 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04004845 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05004846 return nfserr_bad_stateid;
4847 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04004848 * However, we could see a stateid from the past, even from a
4849 * non-buggy client. For example, if the client sends a lock
4850 * while some IO is outstanding, the lock may bump si_generation
4851 * while the IO is still in flight. The client could avoid that
4852 * situation by waiting for responses on all the IO requests,
4853 * but better performance may result in retrying IO that
4854 * receives an old_stateid error if requests are rarely
4855 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05004856 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04004857 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05004858}
4859
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004860static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4861{
4862 if (ols->st_stateowner->so_is_open_owner &&
4863 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4864 return nfserr_bad_stateid;
4865 return nfs_ok;
4866}
4867
Chuck Lever7df302f2012-05-29 13:56:37 -04004868static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04004869{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004870 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04004871 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04004872
Andrew Elblef25e2222017-11-09 13:41:10 -05004873 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4874 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04004875 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004876 /* Client debugging aid. */
4877 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4878 char addr_str[INET6_ADDRSTRLEN];
4879 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4880 sizeof(addr_str));
4881 pr_warn_ratelimited("NFSD: client %s testing state ID "
4882 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04004883 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04004884 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004885 spin_lock(&cl->cl_lock);
4886 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04004887 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004888 goto out_unlock;
J. Bruce Fields36279ac2011-09-26 12:53:00 -04004889 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04004890 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04004891 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04004892 switch (s->sc_type) {
4893 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004894 status = nfs_ok;
4895 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004896 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004897 status = nfserr_deleg_revoked;
4898 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004899 case NFS4_OPEN_STID:
4900 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02004901 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04004902 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04004903 default:
4904 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004905 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04004906 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04004907 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04004908 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04004909 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04004910out_unlock:
4911 spin_unlock(&cl->cl_lock);
4912 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004913}
4914
Christoph Hellwigcd61c522014-08-14 08:44:57 +02004915__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04004916nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4917 stateid_t *stateid, unsigned char typemask,
4918 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004919{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004920 __be32 status;
Andrew Elble62f0b692017-11-03 14:06:31 -04004921 bool return_revoked = false;
4922
4923 /*
4924 * only return revoked delegations if explicitly asked.
4925 * otherwise we report revoked or bad_stateid status.
4926 */
4927 if (typemask & NFS4_REVOKED_DELEG_STID)
4928 return_revoked = true;
4929 else if (typemask & NFS4_DELEG_STID)
4930 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004931
Andrew Elblef25e2222017-11-09 13:41:10 -05004932 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4933 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004934 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004935 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004936 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04004937 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004938 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004939 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04004940 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04004941 if (status)
4942 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004943 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004944 if (!*s)
4945 return nfserr_bad_stateid;
Andrew Elble62f0b692017-11-03 14:06:31 -04004946 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
4947 nfs4_put_stid(*s);
4948 if (cstate->minorversion)
4949 return nfserr_deleg_revoked;
4950 return nfserr_bad_stateid;
4951 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004952 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004953}
4954
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004955static struct file *
4956nfs4_find_file(struct nfs4_stid *s, int flags)
4957{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004958 if (!s)
4959 return NULL;
4960
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004961 switch (s->sc_type) {
4962 case NFS4_DELEG_STID:
4963 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4964 return NULL;
4965 return get_file(s->sc_file->fi_deleg_file);
4966 case NFS4_OPEN_STID:
4967 case NFS4_LOCK_STID:
4968 if (flags & RD_STATE)
4969 return find_readable_file(s->sc_file);
4970 else
4971 return find_writeable_file(s->sc_file);
4972 break;
4973 }
4974
4975 return NULL;
4976}
4977
4978static __be32
4979nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4980{
4981 __be32 status;
4982
Christoph Hellwiga0649b22015-06-18 16:44:59 +02004983 status = nfsd4_check_openowner_confirmed(ols);
4984 if (status)
4985 return status;
4986 return nfs4_check_openmode(ols, flags);
4987}
4988
Christoph Hellwigaf90f702015-06-18 16:45:00 +02004989static __be32
4990nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4991 struct file **filpp, bool *tmp_file, int flags)
4992{
4993 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4994 struct file *file;
4995 __be32 status;
4996
4997 file = nfs4_find_file(s, flags);
4998 if (file) {
4999 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5000 acc | NFSD_MAY_OWNER_OVERRIDE);
5001 if (status) {
5002 fput(file);
5003 return status;
5004 }
5005
5006 *filpp = file;
5007 } else {
5008 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5009 if (status)
5010 return status;
5011
5012 if (tmp_file)
5013 *tmp_file = true;
5014 }
5015
5016 return 0;
5017}
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005020 * Checks for stateid operations
5021 */
Al Virob37ad282006-10-19 23:28:59 -07005022__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005023nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005024 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5025 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005027 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005028 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005029 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005030 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005031 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 if (filpp)
5034 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005035 if (tmp_file)
5036 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005038 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 return nfserr_grace;
5040
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005041 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5042 status = check_special_stateids(net, fhp, stateid, flags);
5043 goto done;
5044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005046 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005047 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005048 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005049 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005050 return status;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005051 status = check_stateid_generation(stateid, &s->sc_stateid,
5052 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005053 if (status)
5054 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005055
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005056 switch (s->sc_type) {
5057 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005058 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005059 break;
5060 case NFS4_OPEN_STID:
5061 case NFS4_LOCK_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005062 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005063 break;
5064 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005065 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005068 if (status)
5069 goto out;
5070 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005071
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005072done:
5073 if (!status && filpp)
5074 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005076 if (s)
5077 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return status;
5079}
5080
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005081/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005082 * Test if the stateid is valid
5083 */
5084__be32
5085nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5086 struct nfsd4_test_stateid *test_stateid)
5087{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005088 struct nfsd4_test_stateid_id *stateid;
5089 struct nfs4_client *cl = cstate->session->se_client;
5090
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005091 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005092 stateid->ts_id_status =
5093 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005094
Bryan Schumaker17456802011-07-13 10:50:48 -04005095 return nfs_ok;
5096}
5097
Chuck Lever42691392016-08-11 10:37:30 -04005098static __be32
5099nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5100{
5101 struct nfs4_ol_stateid *stp = openlockstateid(s);
5102 __be32 ret;
5103
5104 mutex_lock(&stp->st_mutex);
5105
5106 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5107 if (ret)
5108 goto out;
5109
5110 ret = nfserr_locks_held;
5111 if (check_for_locks(stp->st_stid.sc_file,
5112 lockowner(stp->st_stateowner)))
5113 goto out;
5114
5115 release_lock_stateid(stp);
5116 ret = nfs_ok;
5117
5118out:
5119 mutex_unlock(&stp->st_mutex);
5120 nfs4_put_stid(s);
5121 return ret;
5122}
5123
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005124__be32
5125nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5126 struct nfsd4_free_stateid *free_stateid)
5127{
5128 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005129 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005130 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005131 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005132 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005133
Jeff Layton1af71cc2014-07-29 21:34:14 -04005134 spin_lock(&cl->cl_lock);
5135 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005136 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005137 goto out_unlock;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005138 switch (s->sc_type) {
5139 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005140 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005141 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005142 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005143 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5144 if (ret)
5145 break;
5146 ret = nfserr_locks_held;
5147 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005148 case NFS4_LOCK_STID:
Chuck Lever42691392016-08-11 10:37:30 -04005149 atomic_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005150 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005151 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005152 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005153 case NFS4_REVOKED_DELEG_STID:
5154 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005155 list_del_init(&dp->dl_recall_lru);
5156 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005157 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005158 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005159 goto out;
5160 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005161 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005162out_unlock:
5163 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005164out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005165 return ret;
5166}
5167
NeilBrown4c4cd222005-07-07 17:59:27 -07005168static inline int
5169setlkflg (int type)
5170{
5171 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5172 RD_STATE : WR_STATE;
5173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005175static __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 -04005176{
5177 struct svc_fh *current_fh = &cstate->current_fh;
5178 struct nfs4_stateowner *sop = stp->st_stateowner;
5179 __be32 status;
5180
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005181 status = nfsd4_check_seqid(cstate, sop, seqid);
5182 if (status)
5183 return status;
Trond Myklebust2a7d4a72017-11-03 08:00:15 -04005184 status = nfsd4_lock_ol_stateid(stp);
5185 if (status != nfs_ok)
5186 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005187 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005188 if (status == nfs_ok)
5189 status = nfs4_check_fh(current_fh, &stp->st_stid);
5190 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005191 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005192 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005193}
5194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195/*
5196 * Checks for sequence id mutating operations.
5197 */
Al Virob37ad282006-10-19 23:28:59 -07005198static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005199nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005200 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005201 struct nfs4_ol_stateid **stpp,
5202 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005204 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005205 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005206 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005208 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5209 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005212 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005213 if (status)
5214 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005215 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005216 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005218 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005219 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005220 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005221 else
5222 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005223 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005224}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005225
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005226static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5227 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005228{
5229 __be32 status;
5230 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005231 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005233 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005234 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005235 if (status)
5236 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005237 oo = openowner(stp->st_stateowner);
5238 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005239 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005240 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005241 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005242 }
5243 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005244 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245}
5246
Al Virob37ad282006-10-19 23:28:59 -07005247__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005248nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005249 struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250{
Al Virob37ad282006-10-19 23:28:59 -07005251 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005252 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005253 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005254 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
Al Viroa6a9f182013-09-16 10:57:01 -04005256 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5257 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005259 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005260 if (status)
5261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005263 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005264 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005265 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005266 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005267 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005268 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005269 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005270 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005271 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005272 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005273 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005274 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005275 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005276 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005277 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005278 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005279
Jeff Layton2a4317c2012-03-21 16:42:43 -04005280 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005281 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005282put_stateid:
5283 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005285 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 return status;
5287}
5288
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005289static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005291 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005292 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005293 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005294 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005295}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005296
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005297static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5298{
5299 switch (to_access) {
5300 case NFS4_SHARE_ACCESS_READ:
5301 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5302 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5303 break;
5304 case NFS4_SHARE_ACCESS_WRITE:
5305 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5306 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5307 break;
5308 case NFS4_SHARE_ACCESS_BOTH:
5309 break;
5310 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005311 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 }
5313}
5314
Al Virob37ad282006-10-19 23:28:59 -07005315__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005316nfsd4_open_downgrade(struct svc_rqst *rqstp,
5317 struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005318 struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319{
Al Virob37ad282006-10-19 23:28:59 -07005320 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005321 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005322 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
Al Viroa6a9f182013-09-16 10:57:01 -04005324 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5325 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005327 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005328 if (od->od_deleg_want)
5329 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5330 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005332 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005333 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005334 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005337 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005338 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005340 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005342 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005343 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005345 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005347 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005348 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005349 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005351put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005352 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005353 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005355 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 return status;
5357}
5358
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005359static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5360{
Trond Myklebustacf92952014-06-30 11:48:37 -04005361 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005362 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005363 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005364
Jeff Layton2c41beb2014-07-29 21:34:41 -04005365 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005366 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005367
Jeff Laytond83017f2014-07-29 21:34:42 -04005368 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005369 if (unhashed)
5370 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005371 spin_unlock(&clp->cl_lock);
5372 free_ol_stateid_reaplist(&reaplist);
5373 } else {
5374 spin_unlock(&clp->cl_lock);
5375 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005376 if (unhashed)
5377 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005378 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005379}
5380
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381/*
5382 * nfs4_unlock_state() called after encode
5383 */
Al Virob37ad282006-10-19 23:28:59 -07005384__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005385nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005386 struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387{
Al Virob37ad282006-10-19 23:28:59 -07005388 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005389 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005390 struct net *net = SVC_NET(rqstp);
5391 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Al Viroa6a9f182013-09-16 10:57:01 -04005393 dprintk("NFSD: nfsd4_close on file %pd\n",
5394 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005396 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5397 &close->cl_stateid,
5398 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005399 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005400 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005401 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 goto out;
Trond Myklebust1c8ea412017-11-03 08:00:10 -04005403
5404 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton9767feb2015-10-01 09:05:50 -04005405 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005407 nfsd4_close_open_stateid(stp);
Trond Myklebust1c8ea412017-11-03 08:00:10 -04005408 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005409
Trond Myklebust5cd35862017-11-03 08:00:12 -04005410 /* See RFC5661 sectionm 18.2.4 */
5411 if (stp->st_stid.sc_client->cl_minorversion)
5412 memcpy(&close->cl_stateid, &close_stateid,
5413 sizeof(close->cl_stateid));
5414
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005415 /* put reference from nfs4_preprocess_seqid_op */
5416 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 return status;
5419}
5420
Al Virob37ad282006-10-19 23:28:59 -07005421__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005422nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5423 struct nfsd4_delegreturn *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005425 struct nfs4_delegation *dp;
5426 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005427 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005428 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005429 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005431 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005432 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005434 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005435 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005436 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005437 dp = delegstateid(s);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005438 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005439 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005440 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005441
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005442 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005443put_stateid:
5444 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445out:
5446 return status;
5447}
5448
Benny Halevy87df4de2008-12-15 19:42:03 +02005449static inline u64
5450end_offset(u64 start, u64 len)
5451{
5452 u64 end;
5453
5454 end = start + len;
5455 return end >= start ? end: NFS4_MAX_UINT64;
5456}
5457
5458/* last octet in a range */
5459static inline u64
5460last_byte_offset(u64 start, u64 len)
5461{
5462 u64 end;
5463
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005464 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02005465 end = start + len;
5466 return end > start ? end - 1: NFS4_MAX_UINT64;
5467}
5468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469/*
5470 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5471 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5472 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5473 * locking, this prevents us from being completely protocol-compliant. The
5474 * real solution to this problem is to start using unsigned file offsets in
5475 * the VFS, but this is a very deep change!
5476 */
5477static inline void
5478nfs4_transform_lock_offset(struct file_lock *lock)
5479{
5480 if (lock->fl_start < 0)
5481 lock->fl_start = OFFSET_MAX;
5482 if (lock->fl_end < 0)
5483 lock->fl_end = OFFSET_MAX;
5484}
5485
Jeff Laytoncae80b32015-04-03 09:04:04 -04005486static fl_owner_t
5487nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005488{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005489 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5490
5491 nfs4_get_stateowner(&lo->lo_owner);
5492 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005493}
5494
Jeff Laytoncae80b32015-04-03 09:04:04 -04005495static void
5496nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04005497{
Jeff Laytoncae80b32015-04-03 09:04:04 -04005498 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04005499
Jeff Laytoncae80b32015-04-03 09:04:04 -04005500 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04005501 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04005502}
5503
Jeff Layton76d348f2016-09-16 16:28:24 -04005504static void
5505nfsd4_lm_notify(struct file_lock *fl)
5506{
5507 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
5508 struct net *net = lo->lo_owner.so_client->net;
5509 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5510 struct nfsd4_blocked_lock *nbl = container_of(fl,
5511 struct nfsd4_blocked_lock, nbl_lock);
5512 bool queue = false;
5513
Jeff Layton7919d0a2016-09-16 16:28:25 -04005514 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04005515 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005516 if (!list_empty(&nbl->nbl_list)) {
5517 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005518 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04005519 queue = true;
5520 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005521 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005522
5523 if (queue)
5524 nfsd4_run_cb(&nbl->nbl_cb);
5525}
5526
Alexey Dobriyan7b021962009-09-21 17:01:12 -07005527static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04005528 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04005529 .lm_get_owner = nfsd4_fl_get_owner,
5530 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07005531};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
5533static inline void
5534nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5535{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005536 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
NeilBrownd5b90262006-04-10 22:55:22 -07005538 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005539 lo = (struct nfs4_lockowner *) fl->fl_owner;
5540 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5541 lo->lo_owner.so_owner.len, GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005542 if (!deny->ld_owner.data)
5543 /* We just don't care that much */
5544 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005545 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5546 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07005547 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04005548nevermind:
5549 deny->ld_owner.len = 0;
5550 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07005551 deny->ld_clientid.cl_boot = 0;
5552 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 }
5554 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02005555 deny->ld_length = NFS4_MAX_UINT64;
5556 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5558 deny->ld_type = NFS4_READ_LT;
5559 if (fl->fl_type != F_RDLCK)
5560 deny->ld_type = NFS4_WRITE_LT;
5561}
5562
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005563static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005564find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565{
Trond Myklebustd4f04892014-07-29 21:34:36 -04005566 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005567 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
Trond Myklebust0a880a22014-07-30 08:27:10 -04005569 lockdep_assert_held(&clp->cl_lock);
5570
Trond Myklebustd4f04892014-07-29 21:34:36 -04005571 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5572 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04005573 if (so->so_is_open_owner)
5574 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005575 if (same_owner_str(so, owner))
5576 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 }
5578 return NULL;
5579}
5580
Trond Myklebustc58c6612014-07-29 21:34:35 -04005581static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08005582find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04005583{
5584 struct nfs4_lockowner *lo;
5585
Trond Myklebustd4f04892014-07-29 21:34:36 -04005586 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005587 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04005588 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005589 return lo;
5590}
5591
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005592static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5593{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005594 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005595}
5596
Jeff Layton6b180f02014-07-29 21:34:26 -04005597static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5598{
5599 struct nfs4_lockowner *lo = lockowner(sop);
5600
5601 kmem_cache_free(lockowner_slab, lo);
5602}
5603
5604static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04005605 .so_unhash = nfs4_unhash_lockowner,
5606 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04005607};
5608
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609/*
5610 * Alloc a lock owner structure.
5611 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005612 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05005614 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005616static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04005617alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5618 struct nfs4_ol_stateid *open_stp,
5619 struct nfsd4_lock *lock)
5620{
Trond Myklebustc58c6612014-07-29 21:34:35 -04005621 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005623 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5624 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005626 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005627 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5628 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04005629 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04005630 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04005631 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08005632 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005633 if (ret == NULL) {
5634 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04005635 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04005636 ret = lo;
5637 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08005638 nfs4_free_stateowner(&lo->lo_owner);
5639
Trond Myklebustd4f04892014-07-29 21:34:36 -04005640 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04005641 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642}
5643
Jeff Layton356a95e2014-07-29 21:34:13 -04005644static void
5645init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5646 struct nfs4_file *fp, struct inode *inode,
5647 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04005649 struct nfs4_client *clp = lo->lo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Jeff Layton356a95e2014-07-29 21:34:13 -04005651 lockdep_assert_held(&clp->cl_lock);
5652
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005653 atomic_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05005654 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04005655 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07005656 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04005657 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005658 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07005660 stp->st_openstp = open_stp;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005661 mutex_init(&stp->st_mutex);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04005662 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04005663 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04005664 spin_lock(&fp->fi_lock);
5665 list_add(&stp->st_perfile, &fp->fi_stateids);
5666 spin_unlock(&fp->fi_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667}
5668
Jeff Laytonc53530d2014-06-30 11:48:39 -04005669static struct nfs4_ol_stateid *
5670find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5671{
5672 struct nfs4_ol_stateid *lst;
Jeff Layton356a95e2014-07-29 21:34:13 -04005673 struct nfs4_client *clp = lo->lo_owner.so_client;
5674
5675 lockdep_assert_held(&clp->cl_lock);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005676
5677 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005678 if (lst->st_stid.sc_file == fp) {
5679 atomic_inc(&lst->st_stid.sc_count);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005680 return lst;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005681 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005682 }
5683 return NULL;
5684}
5685
Jeff Layton356a95e2014-07-29 21:34:13 -04005686static struct nfs4_ol_stateid *
5687find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5688 struct inode *inode, struct nfs4_ol_stateid *ost,
5689 bool *new)
5690{
5691 struct nfs4_stid *ns = NULL;
5692 struct nfs4_ol_stateid *lst;
5693 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5694 struct nfs4_client *clp = oo->oo_owner.so_client;
5695
5696 spin_lock(&clp->cl_lock);
5697 lst = find_lock_stateid(lo, fi);
5698 if (lst == NULL) {
5699 spin_unlock(&clp->cl_lock);
Kinglong Mee743146d32017-01-18 19:04:42 +08005700 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
Jeff Layton356a95e2014-07-29 21:34:13 -04005701 if (ns == NULL)
5702 return NULL;
5703
5704 spin_lock(&clp->cl_lock);
5705 lst = find_lock_stateid(lo, fi);
5706 if (likely(!lst)) {
5707 lst = openlockstateid(ns);
5708 init_lock_stateid(lst, lo, fi, inode, ost);
5709 ns = NULL;
5710 *new = true;
5711 }
5712 }
5713 spin_unlock(&clp->cl_lock);
5714 if (ns)
5715 nfs4_put_stid(ns);
5716 return lst;
5717}
Jeff Laytonc53530d2014-06-30 11:48:39 -04005718
NeilBrownfd39ca92005-06-23 22:04:03 -07005719static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720check_lock_length(u64 offset, u64 length)
5721{
Kinglong Meee7969312015-07-13 17:34:19 +08005722 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5723 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724}
5725
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005726static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05005727{
Trond Myklebust11b91642014-07-29 21:34:08 -04005728 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05005729
Jeff Layton7214e862014-07-10 14:07:33 -04005730 lockdep_assert_held(&fp->fi_lock);
5731
Jeff Layton82c5ff12012-05-11 09:45:13 -04005732 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05005733 return;
Jeff Layton12659652014-07-10 14:07:28 -04005734 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005735 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005736}
5737
Jeff Layton356a95e2014-07-29 21:34:13 -04005738static __be32
5739lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5740 struct nfs4_ol_stateid *ost,
5741 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04005742 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005743{
Jeff Layton5db1c032014-07-29 21:34:28 -04005744 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04005745 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005746 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5747 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00005748 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005749 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04005750 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005751 unsigned int strhashval;
Jeff Laytondd257932016-08-11 10:37:39 -04005752 bool hashed;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005753
Kinglong Meec8623992015-07-13 17:35:37 +08005754 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005755 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005756 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04005757 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5758 if (lo == NULL)
5759 return nfserr_jukebox;
5760 } else {
5761 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04005762 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04005763 if (!cstate->minorversion &&
5764 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04005765 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005766 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04005767
Jeff Laytondd257932016-08-11 10:37:39 -04005768retry:
5769 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5770 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04005771 status = nfserr_jukebox;
5772 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005773 }
Jeff Laytondd257932016-08-11 10:37:39 -04005774
5775 mutex_lock(&lst->st_mutex);
5776
5777 /* See if it's still hashed to avoid race with FREE_STATEID */
5778 spin_lock(&cl->cl_lock);
5779 hashed = !list_empty(&lst->st_perfile);
5780 spin_unlock(&cl->cl_lock);
5781
5782 if (!hashed) {
5783 mutex_unlock(&lst->st_mutex);
5784 nfs4_put_stid(&lst->st_stid);
5785 goto retry;
5786 }
Jeff Layton5db1c032014-07-29 21:34:28 -04005787 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04005788 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04005789out:
5790 nfs4_put_stateowner(&lo->lo_owner);
5791 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005792}
5793
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794/*
5795 * LOCK operation
5796 */
Al Virob37ad282006-10-19 23:28:59 -07005797__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005798nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08005799 struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005801 struct nfs4_openowner *open_sop = NULL;
5802 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005803 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005804 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04005805 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04005806 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04005807 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04005808 struct file_lock *file_lock = NULL;
5809 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005810 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005811 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07005812 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04005813 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04005814 unsigned char fl_type;
5815 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005816 struct net *net = SVC_NET(rqstp);
5817 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
5819 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5820 (long long) lock->lk_offset,
5821 (long long) lock->lk_length);
5822
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 if (check_lock_length(lock->lk_offset, lock->lk_length))
5824 return nfserr_inval;
5825
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005826 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02005827 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08005828 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5829 return status;
5830 }
5831
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04005833 if (nfsd4_has_session(cstate))
5834 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005835 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04005836 &cstate->session->se_client->cl_clientid,
5837 sizeof(clientid_t));
5838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04005840 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005844 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 lock->lk_new_open_seqid,
5846 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005847 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07005848 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005850 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005851 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005852 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04005853 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08005854 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005855 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005856 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04005857 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005858 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03005859 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005860 lock->lk_old_lock_seqid,
5861 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005862 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04005863 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04005864 if (status)
5865 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04005866 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04005868 lkflg = setlkflg(lock->lk_type);
5869 status = nfs4_check_openmode(lock_stp, lkflg);
5870 if (status)
5871 goto out;
5872
NeilBrown0dd395d2005-07-07 17:59:15 -07005873 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005874 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005875 goto out;
5876 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005877 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07005878 goto out;
5879
Trond Myklebust11b91642014-07-29 21:34:08 -04005880 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005883 if (nfsd4_has_session(cstate))
5884 fl_flags |= FL_SLEEP;
5885 /* Fallthrough */
5886 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005887 spin_lock(&fp->fi_lock);
5888 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005889 if (filp)
5890 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04005891 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005892 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04005895 if (nfsd4_has_session(cstate))
5896 fl_flags |= FL_SLEEP;
5897 /* Fallthrough */
5898 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04005899 spin_lock(&fp->fi_lock);
5900 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05005901 if (filp)
5902 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04005903 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005904 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05005905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 default:
5907 status = nfserr_inval;
5908 goto out;
5909 }
Jeff Layton76d348f2016-09-16 16:28:24 -04005910
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005911 if (!filp) {
5912 status = nfserr_openmode;
5913 goto out;
5914 }
Kinglong Meeaef95832014-08-22 10:18:44 -04005915
Jeff Layton76d348f2016-09-16 16:28:24 -04005916 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
5917 if (!nbl) {
5918 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
5919 status = nfserr_jukebox;
5920 goto out;
5921 }
5922
5923 file_lock = &nbl->nbl_lock;
5924 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04005925 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04005926 file_lock->fl_pid = current->tgid;
5927 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04005928 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04005929 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5930 file_lock->fl_start = lock->lk_offset;
5931 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5932 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
Jeff Layton21179d82012-08-21 08:03:32 -04005934 conflock = locks_alloc_lock();
5935 if (!conflock) {
5936 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5937 status = nfserr_jukebox;
5938 goto out;
5939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Jeff Layton76d348f2016-09-16 16:28:24 -04005941 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04005942 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04005943 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005944 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005945 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04005946 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005947 }
5948
Jeff Layton21179d82012-08-21 08:03:32 -04005949 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005950 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04005952 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07005953 status = 0;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005954 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005955 case FILE_LOCK_DEFERRED:
5956 nbl = NULL;
5957 /* Fallthrough */
5958 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005959 status = nfserr_denied;
5960 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04005961 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005962 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04005963 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005965 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04005966 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05005967 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04005968 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08005969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971out:
Jeff Layton76d348f2016-09-16 16:28:24 -04005972 if (nbl) {
5973 /* dequeue it if we queued it before */
5974 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04005975 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005976 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005977 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04005978 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04005979 }
5980 free_blocked_lock(nbl);
5981 }
Trond Myklebustde186432014-07-10 14:07:26 -04005982 if (filp)
5983 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04005984 if (lock_stp) {
5985 /* Bump seqid manually if the 4.0 replay owner is openowner */
5986 if (cstate->replay_owner &&
5987 cstate->replay_owner != &lock_sop->lo_owner &&
5988 seqid_mutating_err(ntohl(status)))
5989 lock_sop->lo_owner.so_seqid++;
5990
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005991 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005992
Jeff Layton5db1c032014-07-29 21:34:28 -04005993 /*
5994 * If this is a new, never-before-used stateid, and we are
5995 * returning an error, then just go ahead and release it.
5996 */
5997 if (status && new)
5998 release_lock_stateid(lock_stp);
5999
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006000 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006001 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006002 if (open_stp)
6003 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006004 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006005 if (conflock)
6006 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 return status;
6008}
6009
6010/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006011 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6012 * so we do a temporary open here just to get an open file to pass to
6013 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6014 * inode operation.)
6015 */
Al Viro04da6e92012-04-13 00:00:04 -04006016static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006017{
6018 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006019 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6020 if (!err) {
6021 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006022 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006023 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006024 return err;
6025}
6026
6027/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 * LOCKT operation
6029 */
Al Virob37ad282006-10-19 23:28:59 -07006030__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006031nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6032 struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
Jeff Layton21179d82012-08-21 08:03:32 -04006034 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006035 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006036 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006037 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006039 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 return nfserr_grace;
6041
6042 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6043 return nfserr_inval;
6044
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006045 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006046 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006047 if (status)
6048 goto out;
6049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006051 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Jeff Layton21179d82012-08-21 08:03:32 -04006054 file_lock = locks_alloc_lock();
6055 if (!file_lock) {
6056 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6057 status = nfserr_jukebox;
6058 goto out;
6059 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006060
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 switch (lockt->lt_type) {
6062 case NFS4_READ_LT:
6063 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006064 file_lock->fl_type = F_RDLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 break;
6066 case NFS4_WRITE_LT:
6067 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006068 file_lock->fl_type = F_WRLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 break;
6070 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006071 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 status = nfserr_inval;
6073 goto out;
6074 }
6075
Kinglong Meec8623992015-07-13 17:35:37 +08006076 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006077 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006078 file_lock->fl_owner = (fl_owner_t)lo;
6079 file_lock->fl_pid = current->tgid;
6080 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Jeff Layton21179d82012-08-21 08:03:32 -04006082 file_lock->fl_start = lockt->lt_offset;
6083 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
Jeff Layton21179d82012-08-21 08:03:32 -04006085 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Jeff Layton21179d82012-08-21 08:03:32 -04006087 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006088 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006089 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006090
Jeff Layton21179d82012-08-21 08:03:32 -04006091 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006093 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 }
6095out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006096 if (lo)
6097 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006098 if (file_lock)
6099 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 return status;
6101}
6102
Al Virob37ad282006-10-19 23:28:59 -07006103__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006104nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
J.Bruce Fieldsa4f1706a92006-12-13 00:35:28 -08006105 struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106{
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006107 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006109 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006110 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006111 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006112 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6113
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6115 (long long) locku->lu_offset,
6116 (long long) locku->lu_length);
6117
6118 if (check_lock_length(locku->lu_offset, locku->lu_length))
6119 return nfserr_inval;
6120
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006121 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006122 &locku->lu_stateid, NFS4_LOCK_STID,
6123 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006124 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006126 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006127 if (!filp) {
6128 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006129 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006130 }
Jeff Layton21179d82012-08-21 08:03:32 -04006131 file_lock = locks_alloc_lock();
6132 if (!file_lock) {
6133 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6134 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006135 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006136 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006137
Jeff Layton21179d82012-08-21 08:03:32 -04006138 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006139 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006140 file_lock->fl_pid = current->tgid;
6141 file_lock->fl_file = filp;
6142 file_lock->fl_flags = FL_POSIX;
6143 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6144 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Jeff Layton21179d82012-08-21 08:03:32 -04006146 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6147 locku->lu_length);
6148 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
Jeff Layton21179d82012-08-21 08:03:32 -04006150 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006151 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006152 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 goto out_nfserr;
6154 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006155 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006156fput:
6157 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006158put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006159 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006160 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006162 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006163 if (file_lock)
6164 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 return status;
6166
6167out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006168 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006169 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170}
6171
6172/*
6173 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006174 * true: locks held by lockowner
6175 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006177static bool
6178check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006180 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006181 int status = false;
6182 struct file *filp = find_any_file(fp);
6183 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006184 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006185
6186 if (!filp) {
6187 /* Any valid lock stateid should have some sort of access */
6188 WARN_ON_ONCE(1);
6189 return status;
6190 }
6191
6192 inode = file_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006193 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006195 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006196 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006197 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6198 if (fl->fl_owner == (fl_owner_t)lowner) {
6199 status = true;
6200 break;
6201 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006202 }
Jeff Layton6109c852015-01-16 15:05:57 -05006203 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006205 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 return status;
6207}
6208
Al Virob37ad282006-10-19 23:28:59 -07006209__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006210nfsd4_release_lockowner(struct svc_rqst *rqstp,
6211 struct nfsd4_compound_state *cstate,
6212 struct nfsd4_release_lockowner *rlockowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213{
6214 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006215 struct nfs4_stateowner *sop;
6216 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006217 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006219 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006220 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006221 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006222 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006223 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
6225 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6226 clid->cl_boot, clid->cl_id);
6227
Jeff Layton4b24ca72014-06-30 11:48:44 -04006228 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006229 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006230 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006231
Trond Myklebustd4f04892014-07-29 21:34:36 -04006232 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006233 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006234 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006235 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006236 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006237
6238 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006239 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006240
Jeff Layton882e9d22014-07-29 21:34:37 -04006241 /* see if there are still any locks associated with it */
6242 lo = lockowner(sop);
6243 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6244 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6245 status = nfserr_locks_held;
6246 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006247 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006248 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006249 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006250
Kinglong Meeb5971af2014-08-22 10:18:43 -04006251 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006252 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006253 }
Chuck Lever88584812016-07-13 16:40:14 -04006254 if (!lo) {
6255 spin_unlock(&clp->cl_lock);
6256 return status;
6257 }
6258
6259 unhash_lockowner_locked(lo);
6260 while (!list_empty(&lo->lo_owner.so_stateids)) {
6261 stp = list_first_entry(&lo->lo_owner.so_stateids,
6262 struct nfs4_ol_stateid,
6263 st_perstateowner);
6264 WARN_ON(!unhash_lock_stateid(stp));
6265 put_ol_stateid_locked(stp, &reaplist);
6266 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006267 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006268 free_ol_stateid_reaplist(&reaplist);
6269 nfs4_put_stateowner(&lo->lo_owner);
6270
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 return status;
6272}
6273
6274static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006275alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276{
NeilBrowna55370a2005-06-23 22:03:52 -07006277 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278}
6279
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006280bool
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006281nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006282{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006283 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006284
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006285 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006286 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006287}
6288
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289/*
6290 * failure => all reset bets are off, nfserr_no_grace...
6291 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006292struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006293nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294{
6295 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006296 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
NeilBrowna55370a2005-06-23 22:03:52 -07006298 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6299 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006300 if (crp) {
6301 strhashval = clientstr_hashval(name);
6302 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006303 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Jeff Layton772a9bb2012-11-12 15:00:54 -05006304 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006305 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006306 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006307 }
6308 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309}
6310
Jeff Layton2a4317c2012-03-21 16:42:43 -04006311void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006312nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006313{
6314 list_del(&crp->cr_strhash);
6315 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006316 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006317}
6318
6319void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006320nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321{
6322 struct nfs4_client_reclaim *crp = NULL;
6323 int i;
6324
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006326 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6327 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006329 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 }
6331 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006332 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333}
6334
6335/*
6336 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006337struct nfs4_client_reclaim *
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006338nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339{
6340 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 struct nfs4_client_reclaim *crp = NULL;
6342
Jeff Layton278c9312012-11-12 15:00:52 -05006343 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
Jeff Layton278c9312012-11-12 15:00:52 -05006345 strhashval = clientstr_hashval(recdir);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006346 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Jeff Layton278c9312012-11-12 15:00:52 -05006347 if (same_name(crp->cr_recdir, recdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 return crp;
6349 }
6350 }
6351 return NULL;
6352}
6353
6354/*
6355* Called from OPEN. Look for clientid in reclaim list.
6356*/
Al Virob37ad282006-10-19 23:28:59 -07006357__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006358nfs4_check_open_reclaim(clientid_t *clid,
6359 struct nfsd4_compound_state *cstate,
6360 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006362 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006363
6364 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006365 status = lookup_clientid(clid, cstate, nn);
6366 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006367 return nfserr_reclaim_bad;
6368
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006369 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6370 return nfserr_no_grace;
6371
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006372 if (nfsd4_client_record_check(cstate->clp))
6373 return nfserr_reclaim_bad;
6374
6375 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376}
6377
Bryan Schumaker65178db2011-11-01 13:35:21 -04006378#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006379static inline void
6380put_client(struct nfs4_client *clp)
6381{
6382 atomic_dec(&clp->cl_refcount);
6383}
6384
Jeff Layton285abde2014-07-30 08:27:24 -04006385static struct nfs4_client *
6386nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6387{
6388 struct nfs4_client *clp;
6389 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6390 nfsd_net_id);
6391
6392 if (!nfsd_netns_ready(nn))
6393 return NULL;
6394
6395 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6396 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6397 return clp;
6398 }
6399 return NULL;
6400}
6401
Jeff Layton7ec0e362014-07-30 08:27:17 -04006402u64
Jeff Layton285abde2014-07-30 08:27:24 -04006403nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006404{
6405 struct nfs4_client *clp;
6406 u64 count = 0;
6407 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6408 nfsd_net_id);
6409 char buf[INET6_ADDRSTRLEN];
6410
6411 if (!nfsd_netns_ready(nn))
6412 return 0;
6413
6414 spin_lock(&nn->client_lock);
6415 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6416 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6417 pr_info("NFS Client: %s\n", buf);
6418 ++count;
6419 }
6420 spin_unlock(&nn->client_lock);
6421
6422 return count;
6423}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006424
Jeff Laytona0926d12014-07-30 08:27:18 -04006425u64
Jeff Layton285abde2014-07-30 08:27:24 -04006426nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006427{
6428 u64 count = 0;
6429 struct nfs4_client *clp;
6430 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6431 nfsd_net_id);
6432
6433 if (!nfsd_netns_ready(nn))
6434 return count;
6435
6436 spin_lock(&nn->client_lock);
6437 clp = nfsd_find_client(addr, addr_size);
6438 if (clp) {
6439 if (mark_client_expired_locked(clp) == nfs_ok)
6440 ++count;
6441 else
6442 clp = NULL;
6443 }
6444 spin_unlock(&nn->client_lock);
6445
6446 if (clp)
6447 expire_client(clp);
6448
6449 return count;
6450}
6451
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006452u64
Jeff Layton285abde2014-07-30 08:27:24 -04006453nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04006454{
6455 u64 count = 0;
6456 struct nfs4_client *clp, *next;
6457 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6458 nfsd_net_id);
6459 LIST_HEAD(reaplist);
6460
6461 if (!nfsd_netns_ready(nn))
6462 return count;
6463
6464 spin_lock(&nn->client_lock);
6465 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6466 if (mark_client_expired_locked(clp) == nfs_ok) {
6467 list_add(&clp->cl_lru, &reaplist);
6468 if (max != 0 && ++count >= max)
6469 break;
6470 }
6471 }
6472 spin_unlock(&nn->client_lock);
6473
6474 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6475 expire_client(clp);
6476
6477 return count;
6478}
6479
Bryan Schumaker184c1842012-11-29 11:40:44 -05006480static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6481 const char *type)
6482{
6483 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05006484 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05006485 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6486}
6487
Jeff Layton016200c2014-07-30 08:27:21 -04006488static void
6489nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6490 struct list_head *collect)
6491{
6492 struct nfs4_client *clp = lst->st_stid.sc_client;
6493 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6494 nfsd_net_id);
6495
6496 if (!collect)
6497 return;
6498
6499 lockdep_assert_held(&nn->client_lock);
6500 atomic_inc(&clp->cl_refcount);
6501 list_add(&lst->st_locks, collect);
6502}
6503
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006504static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04006505 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04006506 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05006507{
6508 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006509 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006510 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006511 u64 count = 0;
6512
Jeff Layton016200c2014-07-30 08:27:21 -04006513 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006514 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006515 list_for_each_entry_safe(stp, st_next,
6516 &oop->oo_owner.so_stateids, st_perstateowner) {
6517 list_for_each_entry_safe(lst, lst_next,
6518 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04006519 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04006520 if (func(lst))
6521 nfsd_inject_add_lock_to_list(lst,
6522 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04006523 }
Jeff Layton016200c2014-07-30 08:27:21 -04006524 ++count;
6525 /*
6526 * Despite the fact that these functions deal
6527 * with 64-bit integers for "count", we must
6528 * ensure that it doesn't blow up the
6529 * clp->cl_refcount. Throw a warning if we
6530 * start to approach INT_MAX here.
6531 */
6532 WARN_ON_ONCE(count == (INT_MAX / 2));
6533 if (count == max)
6534 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05006535 }
6536 }
6537 }
Jeff Layton016200c2014-07-30 08:27:21 -04006538out:
6539 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006540
6541 return count;
6542}
6543
Jeff Layton016200c2014-07-30 08:27:21 -04006544static u64
6545nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6546 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05006547{
Jeff Layton016200c2014-07-30 08:27:21 -04006548 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05006549}
6550
Jeff Layton016200c2014-07-30 08:27:21 -04006551static u64
6552nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006553{
Jeff Layton016200c2014-07-30 08:27:21 -04006554 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006555 nfsd_print_count(clp, count, "locked files");
6556 return count;
6557}
6558
Jeff Layton016200c2014-07-30 08:27:21 -04006559u64
Jeff Layton285abde2014-07-30 08:27:24 -04006560nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04006561{
6562 struct nfs4_client *clp;
6563 u64 count = 0;
6564 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6565 nfsd_net_id);
6566
6567 if (!nfsd_netns_ready(nn))
6568 return 0;
6569
6570 spin_lock(&nn->client_lock);
6571 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6572 count += nfsd_print_client_locks(clp);
6573 spin_unlock(&nn->client_lock);
6574
6575 return count;
6576}
6577
6578static void
6579nfsd_reap_locks(struct list_head *reaplist)
6580{
6581 struct nfs4_client *clp;
6582 struct nfs4_ol_stateid *stp, *next;
6583
6584 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6585 list_del_init(&stp->st_locks);
6586 clp = stp->st_stid.sc_client;
6587 nfs4_put_stid(&stp->st_stid);
6588 put_client(clp);
6589 }
6590}
6591
6592u64
Jeff Layton285abde2014-07-30 08:27:24 -04006593nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04006594{
6595 unsigned int count = 0;
6596 struct nfs4_client *clp;
6597 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6598 nfsd_net_id);
6599 LIST_HEAD(reaplist);
6600
6601 if (!nfsd_netns_ready(nn))
6602 return count;
6603
6604 spin_lock(&nn->client_lock);
6605 clp = nfsd_find_client(addr, addr_size);
6606 if (clp)
6607 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6608 spin_unlock(&nn->client_lock);
6609 nfsd_reap_locks(&reaplist);
6610 return count;
6611}
6612
6613u64
Jeff Layton285abde2014-07-30 08:27:24 -04006614nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04006615{
6616 u64 count = 0;
6617 struct nfs4_client *clp;
6618 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6619 nfsd_net_id);
6620 LIST_HEAD(reaplist);
6621
6622 if (!nfsd_netns_ready(nn))
6623 return count;
6624
6625 spin_lock(&nn->client_lock);
6626 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6627 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6628 if (max != 0 && count >= max)
6629 break;
6630 }
6631 spin_unlock(&nn->client_lock);
6632 nfsd_reap_locks(&reaplist);
6633 return count;
6634}
6635
Jeff Layton82e05ef2014-07-30 08:27:22 -04006636static u64
6637nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6638 struct list_head *collect,
6639 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006640{
6641 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04006642 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6643 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006644 u64 count = 0;
6645
Jeff Layton82e05ef2014-07-30 08:27:22 -04006646 lockdep_assert_held(&nn->client_lock);
6647
6648 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006649 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04006650 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006651 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04006652 if (collect) {
6653 atomic_inc(&clp->cl_refcount);
6654 list_add(&oop->oo_perclient, collect);
6655 }
6656 }
6657 ++count;
6658 /*
6659 * Despite the fact that these functions deal with
6660 * 64-bit integers for "count", we must ensure that
6661 * it doesn't blow up the clp->cl_refcount. Throw a
6662 * warning if we start to approach INT_MAX here.
6663 */
6664 WARN_ON_ONCE(count == (INT_MAX / 2));
6665 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006666 break;
6667 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04006668 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006669
6670 return count;
6671}
6672
Jeff Layton82e05ef2014-07-30 08:27:22 -04006673static u64
6674nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006675{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006676 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6677
6678 nfsd_print_count(clp, count, "openowners");
6679 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05006680}
6681
Jeff Layton82e05ef2014-07-30 08:27:22 -04006682static u64
6683nfsd_collect_client_openowners(struct nfs4_client *clp,
6684 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05006685{
Jeff Layton82e05ef2014-07-30 08:27:22 -04006686 return nfsd_foreach_client_openowner(clp, max, collect,
6687 unhash_openowner_locked);
6688}
6689
6690u64
Jeff Layton285abde2014-07-30 08:27:24 -04006691nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006692{
6693 struct nfs4_client *clp;
6694 u64 count = 0;
6695 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6696 nfsd_net_id);
6697
6698 if (!nfsd_netns_ready(nn))
6699 return 0;
6700
6701 spin_lock(&nn->client_lock);
6702 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6703 count += nfsd_print_client_openowners(clp);
6704 spin_unlock(&nn->client_lock);
6705
6706 return count;
6707}
6708
6709static void
6710nfsd_reap_openowners(struct list_head *reaplist)
6711{
6712 struct nfs4_client *clp;
6713 struct nfs4_openowner *oop, *next;
6714
6715 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6716 list_del_init(&oop->oo_perclient);
6717 clp = oop->oo_owner.so_client;
6718 release_openowner(oop);
6719 put_client(clp);
6720 }
6721}
6722
6723u64
Jeff Layton285abde2014-07-30 08:27:24 -04006724nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6725 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006726{
6727 unsigned int count = 0;
6728 struct nfs4_client *clp;
6729 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6730 nfsd_net_id);
6731 LIST_HEAD(reaplist);
6732
6733 if (!nfsd_netns_ready(nn))
6734 return count;
6735
6736 spin_lock(&nn->client_lock);
6737 clp = nfsd_find_client(addr, addr_size);
6738 if (clp)
6739 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6740 spin_unlock(&nn->client_lock);
6741 nfsd_reap_openowners(&reaplist);
6742 return count;
6743}
6744
6745u64
Jeff Layton285abde2014-07-30 08:27:24 -04006746nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04006747{
6748 u64 count = 0;
6749 struct nfs4_client *clp;
6750 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6751 nfsd_net_id);
6752 LIST_HEAD(reaplist);
6753
6754 if (!nfsd_netns_ready(nn))
6755 return count;
6756
6757 spin_lock(&nn->client_lock);
6758 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6759 count += nfsd_collect_client_openowners(clp, &reaplist,
6760 max - count);
6761 if (max != 0 && count >= max)
6762 break;
6763 }
6764 spin_unlock(&nn->client_lock);
6765 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006766 return count;
6767}
6768
Bryan Schumaker269de302012-11-29 11:40:42 -05006769static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6770 struct list_head *victims)
6771{
6772 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006773 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6774 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05006775 u64 count = 0;
6776
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006777 lockdep_assert_held(&nn->client_lock);
6778
6779 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006780 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04006781 if (victims) {
6782 /*
6783 * It's not safe to mess with delegations that have a
6784 * non-zero dl_time. They might have already been broken
6785 * and could be processed by the laundromat outside of
6786 * the state_lock. Just leave them be.
6787 */
6788 if (dp->dl_time != 0)
6789 continue;
6790
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006791 atomic_inc(&clp->cl_refcount);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006792 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006793 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04006794 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006795 ++count;
6796 /*
6797 * Despite the fact that these functions deal with
6798 * 64-bit integers for "count", we must ensure that
6799 * it doesn't blow up the clp->cl_refcount. Throw a
6800 * warning if we start to approach INT_MAX here.
6801 */
6802 WARN_ON_ONCE(count == (INT_MAX / 2));
6803 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05006804 break;
6805 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006806 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05006807 return count;
6808}
6809
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006810static u64
6811nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05006812{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006813 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05006814
6815 nfsd_print_count(clp, count, "delegations");
6816 return count;
6817}
6818
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006819u64
Jeff Layton285abde2014-07-30 08:27:24 -04006820nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006821{
6822 struct nfs4_client *clp;
6823 u64 count = 0;
6824 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6825 nfsd_net_id);
6826
6827 if (!nfsd_netns_ready(nn))
6828 return 0;
6829
6830 spin_lock(&nn->client_lock);
6831 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6832 count += nfsd_print_client_delegations(clp);
6833 spin_unlock(&nn->client_lock);
6834
6835 return count;
6836}
6837
6838static void
6839nfsd_forget_delegations(struct list_head *reaplist)
6840{
6841 struct nfs4_client *clp;
6842 struct nfs4_delegation *dp, *next;
6843
6844 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6845 list_del_init(&dp->dl_recall_lru);
6846 clp = dp->dl_stid.sc_client;
6847 revoke_delegation(dp);
6848 put_client(clp);
6849 }
6850}
6851
6852u64
Jeff Layton285abde2014-07-30 08:27:24 -04006853nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6854 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006855{
6856 u64 count = 0;
6857 struct nfs4_client *clp;
6858 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6859 nfsd_net_id);
6860 LIST_HEAD(reaplist);
6861
6862 if (!nfsd_netns_ready(nn))
6863 return count;
6864
6865 spin_lock(&nn->client_lock);
6866 clp = nfsd_find_client(addr, addr_size);
6867 if (clp)
6868 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6869 spin_unlock(&nn->client_lock);
6870
6871 nfsd_forget_delegations(&reaplist);
6872 return count;
6873}
6874
6875u64
Jeff Layton285abde2014-07-30 08:27:24 -04006876nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006877{
6878 u64 count = 0;
6879 struct nfs4_client *clp;
6880 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6881 nfsd_net_id);
6882 LIST_HEAD(reaplist);
6883
6884 if (!nfsd_netns_ready(nn))
6885 return count;
6886
6887 spin_lock(&nn->client_lock);
6888 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6889 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6890 if (max != 0 && count >= max)
6891 break;
6892 }
6893 spin_unlock(&nn->client_lock);
6894 nfsd_forget_delegations(&reaplist);
6895 return count;
6896}
6897
6898static void
6899nfsd_recall_delegations(struct list_head *reaplist)
6900{
6901 struct nfs4_client *clp;
6902 struct nfs4_delegation *dp, *next;
6903
6904 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6905 list_del_init(&dp->dl_recall_lru);
6906 clp = dp->dl_stid.sc_client;
6907 /*
6908 * We skipped all entries that had a zero dl_time before,
6909 * so we can now reset the dl_time back to 0. If a delegation
6910 * break comes in now, then it won't make any difference since
6911 * we're recalling it either way.
6912 */
6913 spin_lock(&state_lock);
6914 dp->dl_time = 0;
6915 spin_unlock(&state_lock);
6916 nfsd_break_one_deleg(dp);
6917 put_client(clp);
6918 }
6919}
6920
6921u64
Jeff Layton285abde2014-07-30 08:27:24 -04006922nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006923 size_t addr_size)
6924{
6925 u64 count = 0;
6926 struct nfs4_client *clp;
6927 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6928 nfsd_net_id);
6929 LIST_HEAD(reaplist);
6930
6931 if (!nfsd_netns_ready(nn))
6932 return count;
6933
6934 spin_lock(&nn->client_lock);
6935 clp = nfsd_find_client(addr, addr_size);
6936 if (clp)
6937 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6938 spin_unlock(&nn->client_lock);
6939
6940 nfsd_recall_delegations(&reaplist);
6941 return count;
6942}
6943
6944u64
Jeff Layton285abde2014-07-30 08:27:24 -04006945nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04006946{
6947 u64 count = 0;
6948 struct nfs4_client *clp, *next;
6949 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6950 nfsd_net_id);
6951 LIST_HEAD(reaplist);
6952
6953 if (!nfsd_netns_ready(nn))
6954 return count;
6955
6956 spin_lock(&nn->client_lock);
6957 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6958 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6959 if (max != 0 && ++count >= max)
6960 break;
6961 }
6962 spin_unlock(&nn->client_lock);
6963 nfsd_recall_delegations(&reaplist);
6964 return count;
6965}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006966#endif /* CONFIG_NFSD_FAULT_INJECTION */
6967
Meelap Shahc2f1a552007-07-17 04:04:39 -07006968/*
6969 * Since the lifetime of a delegation isn't limited to that of an open, a
6970 * client may quite reasonably hang on to a delegation as long as it has
6971 * the inode cached. This becomes an obvious problem the first time a
6972 * client's inode cache approaches the size of the server's total memory.
6973 *
6974 * For now we avoid this problem by imposing a hard limit on the number
6975 * of delegations, which varies according to the server's memory size.
6976 */
6977static void
6978set_max_delegations(void)
6979{
6980 /*
6981 * Allow at most 4 delegations per megabyte of RAM. Quick
6982 * estimates suggest that in the worst case (where every delegation
6983 * is for a different inode), a delegation could take about 1.5K,
6984 * giving a worst case usage of about 6% of memory.
6985 */
6986 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6987}
6988
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03006989static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03006990{
6991 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6992 int i;
6993
6994 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6995 CLIENT_HASH_SIZE, GFP_KERNEL);
6996 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03006997 goto err;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03006998 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6999 CLIENT_HASH_SIZE, GFP_KERNEL);
7000 if (!nn->unconf_id_hashtbl)
7001 goto err_unconf_id;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007002 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
7003 SESSION_HASH_SIZE, GFP_KERNEL);
7004 if (!nn->sessionid_hashtbl)
7005 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007006
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007007 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007008 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007009 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007010 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007011 for (i = 0; i < SESSION_HASH_SIZE; i++)
7012 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007013 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007014 nn->unconf_name_tree = RB_ROOT;
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007015 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007016 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007017 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007018 spin_lock_init(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007019
Jeff Layton0cc11a62016-10-20 09:34:31 -04007020 spin_lock_init(&nn->blocked_locks_lock);
7021 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7022
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007023 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007024 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007025
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007026 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007027
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007028err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007029 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007030err_unconf_id:
7031 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007032err:
7033 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007034}
7035
7036static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007037nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007038{
7039 int i;
7040 struct nfs4_client *clp = NULL;
7041 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7042
7043 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7044 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7045 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7046 destroy_client(clp);
7047 }
7048 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007049
Kinglong Mee2b905632014-03-26 22:09:30 +08007050 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7051 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7052 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7053 destroy_client(clp);
7054 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007055 }
7056
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007057 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007058 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007059 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007060 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007061}
7062
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007063int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007064nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007065{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007066 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007067 int ret;
7068
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007069 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007070 if (ret)
7071 return ret;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04007072 nn->boot_time = get_seconds();
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04007073 nn->grace_ended = false;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04007074 nn->nfsd4_manager.block_opens = true;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007075 locks_start_grace(net, &nn->nfsd4_manager);
7076 nfsd4_client_tracking_init(net);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007077 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007078 nn->nfsd4_grace, net);
7079 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007080 return 0;
7081}
7082
7083/* initialization to perform when the nfsd service is started: */
7084
7085int
7086nfs4_state_start(void)
7087{
7088 int ret;
7089
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007090 ret = set_callback_cred();
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007091 if (ret)
Kinglong Meed4bda742017-02-05 09:57:07 +08007092 return ret;
7093
Jeff Layton51a54562015-08-20 07:17:01 -04007094 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007095 if (laundry_wq == NULL) {
7096 ret = -ENOMEM;
Kinglong Meed4bda742017-02-05 09:57:07 +08007097 goto out_cleanup_cred;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007098 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007099 ret = nfsd4_create_callback_queue();
7100 if (ret)
7101 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007102
Meelap Shahc2f1a552007-07-17 04:04:39 -07007103 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007104 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007105
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007106out_free_laundry:
7107 destroy_workqueue(laundry_wq);
Kinglong Meed4bda742017-02-05 09:57:07 +08007108out_cleanup_cred:
7109 cleanup_callback_cred();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111}
7112
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007113void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007114nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007118 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007119 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007121 cancel_delayed_work_sync(&nn->laundromat_work);
7122 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007123
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc97502014-05-30 09:09:30 -04007125 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007126 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007128 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007129 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 }
Benny Halevycdc97502014-05-30 09:09:30 -04007131 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 list_for_each_safe(pos, next, &reaplist) {
7133 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007134 list_del_init(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02007135 put_clnt_odstate(dp->dl_clnt_odstate);
Jeff Laytonafbda402014-08-09 10:22:41 -04007136 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
Trond Myklebust60116952014-07-29 21:34:06 -04007137 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 }
7139
Jeff Layton7919d0a2016-09-16 16:28:25 -04007140 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04007141 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007142 while (!list_empty(&nn->blocked_locks_lru)) {
7143 nbl = list_first_entry(&nn->blocked_locks_lru,
7144 struct nfsd4_blocked_lock, nbl_lru);
7145 list_move(&nbl->nbl_lru, &reaplist);
7146 list_del_init(&nbl->nbl_list);
7147 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04007148 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007149
7150 while (!list_empty(&reaplist)) {
Naofumi Hondaaa1a6cf2017-11-09 10:57:16 -05007151 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04007152 struct nfsd4_blocked_lock, nbl_lru);
7153 list_del_init(&nbl->nbl_lru);
7154 posix_unblock_lock(&nbl->nbl_lock);
7155 free_blocked_lock(nbl);
7156 }
7157
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007158 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007159 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160}
7161
7162void
7163nfs4_state_shutdown(void)
7164{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007165 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007166 nfsd4_destroy_callback_queue();
Kinglong Meed4bda742017-02-05 09:57:07 +08007167 cleanup_callback_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007169
7170static void
7171get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7172{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007173 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7174 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007175}
7176
7177static void
7178put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7179{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007180 if (cstate->minorversion) {
7181 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7182 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7183 }
7184}
7185
7186void
7187clear_current_stateid(struct nfsd4_compound_state *cstate)
7188{
7189 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007190}
7191
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007192/*
7193 * functions to set current state id
7194 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007195void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007196nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7197{
7198 put_stateid(cstate, &odp->od_stateid);
7199}
7200
7201void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007202nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
7203{
7204 put_stateid(cstate, &open->op_stateid);
7205}
7206
7207void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007208nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7209{
7210 put_stateid(cstate, &close->cl_stateid);
7211}
7212
7213void
7214nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
7215{
7216 put_stateid(cstate, &lock->lk_resp_stateid);
7217}
7218
7219/*
7220 * functions to consume current state id
7221 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007222
7223void
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007224nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
7225{
7226 get_stateid(cstate, &odp->od_stateid);
7227}
7228
7229void
7230nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7231{
7232 get_stateid(cstate, &drp->dr_stateid);
7233}
7234
7235void
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007236nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7237{
7238 get_stateid(cstate, &fsp->fr_stateid);
7239}
7240
7241void
7242nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7243{
7244 get_stateid(cstate, &setattr->sa_stateid);
7245}
7246
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007247void
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007248nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7249{
7250 get_stateid(cstate, &close->cl_stateid);
7251}
7252
7253void
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007254nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007255{
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007256 get_stateid(cstate, &locku->lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007257}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007258
7259void
7260nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7261{
7262 get_stateid(cstate, &read->rd_stateid);
7263}
7264
7265void
7266nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7267{
7268 get_stateid(cstate, &write->wr_stateid);
7269}