]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * 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 | ||
aceaf78d | 35 | #include <linux/file.h> |
b89f4321 | 36 | #include <linux/fs.h> |
5a0e3ad6 | 37 | #include <linux/slab.h> |
0964a3d3 | 38 | #include <linux/namei.h> |
c2f1a551 | 39 | #include <linux/swap.h> |
17456804 | 40 | #include <linux/pagemap.h> |
7df302f7 | 41 | #include <linux/ratelimit.h> |
68e76ad0 | 42 | #include <linux/sunrpc/svcauth_gss.h> |
5976687a | 43 | #include <linux/sunrpc/addr.h> |
87545899 | 44 | #include <linux/jhash.h> |
9a74af21 | 45 | #include "xdr4.h" |
06b332a5 | 46 | #include "xdr4cb.h" |
0a3adade | 47 | #include "vfs.h" |
bfa4b365 | 48 | #include "current_stateid.h" |
1da177e4 | 49 | |
5e1533c7 | 50 | #include "netns.h" |
9cf514cc | 51 | #include "pnfs.h" |
5e1533c7 | 52 | |
1da177e4 LT |
53 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
54 | ||
f32f3c2d BF |
55 | #define all_ones {{~0,~0},~0} |
56 | static const stateid_t one_stateid = { | |
57 | .si_generation = ~0, | |
58 | .si_opaque = all_ones, | |
59 | }; | |
60 | static const stateid_t zero_stateid = { | |
61 | /* all fields zero */ | |
62 | }; | |
19ff0f28 TM |
63 | static const stateid_t currentstateid = { |
64 | .si_generation = 1, | |
65 | }; | |
f32f3c2d | 66 | |
ec6b5d7b | 67 | static u64 current_sessionid = 1; |
fd39ca9a | 68 | |
f32f3c2d BF |
69 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
70 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) | |
19ff0f28 | 71 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
1da177e4 | 72 | |
1da177e4 | 73 | /* forward declarations */ |
f9c00c3a | 74 | static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); |
6011695d | 75 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
1da177e4 | 76 | |
8b671b80 BF |
77 | /* Locking: */ |
78 | ||
8b671b80 BF |
79 | /* |
80 | * Currently used for the del_recall_lru and file hash table. In an | |
81 | * effort to decrease the scope of the client_mutex, this spinlock may | |
82 | * eventually cover more: | |
83 | */ | |
cdc97505 | 84 | static DEFINE_SPINLOCK(state_lock); |
8b671b80 | 85 | |
b401be22 JL |
86 | /* |
87 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for | |
88 | * the refcount on the open stateid to drop. | |
89 | */ | |
90 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); | |
91 | ||
abf1135b CH |
92 | static struct kmem_cache *openowner_slab; |
93 | static struct kmem_cache *lockowner_slab; | |
94 | static struct kmem_cache *file_slab; | |
95 | static struct kmem_cache *stateid_slab; | |
96 | static struct kmem_cache *deleg_slab; | |
8287f009 | 97 | static struct kmem_cache *odstate_slab; |
e60d4398 | 98 | |
66b2b9b2 | 99 | static void free_session(struct nfsd4_session *); |
508dc6e1 | 100 | |
c4cb8974 | 101 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
0162ac2b | 102 | |
f0f51f5c | 103 | static bool is_session_dead(struct nfsd4_session *ses) |
66b2b9b2 | 104 | { |
f0f51f5c | 105 | return ses->se_flags & NFS4_SESSION_DEAD; |
66b2b9b2 BF |
106 | } |
107 | ||
f0f51f5c | 108 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
508dc6e1 | 109 | { |
f0f51f5c | 110 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
66b2b9b2 BF |
111 | return nfserr_jukebox; |
112 | ses->se_flags |= NFS4_SESSION_DEAD; | |
113 | return nfs_ok; | |
508dc6e1 BH |
114 | } |
115 | ||
221a6876 BF |
116 | static bool is_client_expired(struct nfs4_client *clp) |
117 | { | |
118 | return clp->cl_time == 0; | |
119 | } | |
120 | ||
221a6876 BF |
121 | static __be32 get_client_locked(struct nfs4_client *clp) |
122 | { | |
0a880a28 TM |
123 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
124 | ||
125 | lockdep_assert_held(&nn->client_lock); | |
126 | ||
221a6876 BF |
127 | if (is_client_expired(clp)) |
128 | return nfserr_expired; | |
129 | atomic_inc(&clp->cl_refcount); | |
130 | return nfs_ok; | |
131 | } | |
132 | ||
133 | /* must be called under the client_lock */ | |
134 | static inline void | |
135 | renew_client_locked(struct nfs4_client *clp) | |
136 | { | |
137 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
138 | ||
139 | if (is_client_expired(clp)) { | |
140 | WARN_ON(1); | |
141 | printk("%s: client (clientid %08x/%08x) already expired\n", | |
142 | __func__, | |
143 | clp->cl_clientid.cl_boot, | |
144 | clp->cl_clientid.cl_id); | |
145 | return; | |
146 | } | |
147 | ||
148 | dprintk("renewing client (clientid %08x/%08x)\n", | |
149 | clp->cl_clientid.cl_boot, | |
150 | clp->cl_clientid.cl_id); | |
151 | list_move_tail(&clp->cl_lru, &nn->client_lru); | |
152 | clp->cl_time = get_seconds(); | |
153 | } | |
154 | ||
ba138435 | 155 | static void put_client_renew_locked(struct nfs4_client *clp) |
221a6876 | 156 | { |
0a880a28 TM |
157 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
158 | ||
159 | lockdep_assert_held(&nn->client_lock); | |
160 | ||
221a6876 BF |
161 | if (!atomic_dec_and_test(&clp->cl_refcount)) |
162 | return; | |
163 | if (!is_client_expired(clp)) | |
164 | renew_client_locked(clp); | |
165 | } | |
166 | ||
4b24ca7d JL |
167 | static void put_client_renew(struct nfs4_client *clp) |
168 | { | |
169 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
170 | ||
d6c249b4 JL |
171 | if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock)) |
172 | return; | |
173 | if (!is_client_expired(clp)) | |
174 | renew_client_locked(clp); | |
4b24ca7d JL |
175 | spin_unlock(&nn->client_lock); |
176 | } | |
177 | ||
d4e19e70 TM |
178 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
179 | { | |
180 | __be32 status; | |
181 | ||
182 | if (is_session_dead(ses)) | |
183 | return nfserr_badsession; | |
184 | status = get_client_locked(ses->se_client); | |
185 | if (status) | |
186 | return status; | |
187 | atomic_inc(&ses->se_ref); | |
188 | return nfs_ok; | |
189 | } | |
190 | ||
191 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) | |
192 | { | |
193 | struct nfs4_client *clp = ses->se_client; | |
0a880a28 TM |
194 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
195 | ||
196 | lockdep_assert_held(&nn->client_lock); | |
d4e19e70 TM |
197 | |
198 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) | |
199 | free_session(ses); | |
200 | put_client_renew_locked(clp); | |
201 | } | |
202 | ||
203 | static void nfsd4_put_session(struct nfsd4_session *ses) | |
204 | { | |
205 | struct nfs4_client *clp = ses->se_client; | |
206 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
207 | ||
208 | spin_lock(&nn->client_lock); | |
209 | nfsd4_put_session_locked(ses); | |
210 | spin_unlock(&nn->client_lock); | |
211 | } | |
212 | ||
b5971afa KM |
213 | static inline struct nfs4_stateowner * |
214 | nfs4_get_stateowner(struct nfs4_stateowner *sop) | |
215 | { | |
216 | atomic_inc(&sop->so_count); | |
217 | return sop; | |
218 | } | |
219 | ||
7ffb5880 | 220 | static int |
d4f0489f | 221 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
7ffb5880 TM |
222 | { |
223 | return (sop->so_owner.len == owner->len) && | |
d4f0489f | 224 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
7ffb5880 TM |
225 | } |
226 | ||
227 | static struct nfs4_openowner * | |
228 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, | |
d4f0489f | 229 | struct nfs4_client *clp) |
7ffb5880 TM |
230 | { |
231 | struct nfs4_stateowner *so; | |
7ffb5880 | 232 | |
d4f0489f | 233 | lockdep_assert_held(&clp->cl_lock); |
7ffb5880 | 234 | |
d4f0489f TM |
235 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
236 | so_strhash) { | |
7ffb5880 TM |
237 | if (!so->so_is_open_owner) |
238 | continue; | |
b5971afa KM |
239 | if (same_owner_str(so, &open->op_owner)) |
240 | return openowner(nfs4_get_stateowner(so)); | |
7ffb5880 TM |
241 | } |
242 | return NULL; | |
243 | } | |
244 | ||
245 | static struct nfs4_openowner * | |
246 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, | |
d4f0489f | 247 | struct nfs4_client *clp) |
7ffb5880 TM |
248 | { |
249 | struct nfs4_openowner *oo; | |
250 | ||
d4f0489f TM |
251 | spin_lock(&clp->cl_lock); |
252 | oo = find_openstateowner_str_locked(hashval, open, clp); | |
253 | spin_unlock(&clp->cl_lock); | |
7ffb5880 TM |
254 | return oo; |
255 | } | |
256 | ||
1da177e4 LT |
257 | static inline u32 |
258 | opaque_hashval(const void *ptr, int nbytes) | |
259 | { | |
260 | unsigned char *cptr = (unsigned char *) ptr; | |
261 | ||
262 | u32 x = 0; | |
263 | while (nbytes--) { | |
264 | x *= 37; | |
265 | x += *cptr++; | |
266 | } | |
267 | return x; | |
268 | } | |
269 | ||
5b095e99 | 270 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
32513b40 | 271 | { |
5b095e99 JL |
272 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
273 | ||
274 | kmem_cache_free(file_slab, fp); | |
32513b40 BF |
275 | } |
276 | ||
e6ba76e1 | 277 | void |
13cd2184 N |
278 | put_nfs4_file(struct nfs4_file *fi) |
279 | { | |
02e1215f JL |
280 | might_lock(&state_lock); |
281 | ||
cdc97505 | 282 | if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) { |
5b095e99 | 283 | hlist_del_rcu(&fi->fi_hash); |
cdc97505 | 284 | spin_unlock(&state_lock); |
8287f009 | 285 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
5b095e99 JL |
286 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
287 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); | |
8b671b80 | 288 | } |
13cd2184 N |
289 | } |
290 | ||
de18643d TM |
291 | static struct file * |
292 | __nfs4_get_fd(struct nfs4_file *f, int oflag) | |
293 | { | |
294 | if (f->fi_fds[oflag]) | |
295 | return get_file(f->fi_fds[oflag]); | |
296 | return NULL; | |
297 | } | |
298 | ||
299 | static struct file * | |
300 | find_writeable_file_locked(struct nfs4_file *f) | |
301 | { | |
302 | struct file *ret; | |
303 | ||
304 | lockdep_assert_held(&f->fi_lock); | |
305 | ||
306 | ret = __nfs4_get_fd(f, O_WRONLY); | |
307 | if (!ret) | |
308 | ret = __nfs4_get_fd(f, O_RDWR); | |
309 | return ret; | |
310 | } | |
311 | ||
312 | static struct file * | |
313 | find_writeable_file(struct nfs4_file *f) | |
314 | { | |
315 | struct file *ret; | |
316 | ||
317 | spin_lock(&f->fi_lock); | |
318 | ret = find_writeable_file_locked(f); | |
319 | spin_unlock(&f->fi_lock); | |
320 | ||
321 | return ret; | |
322 | } | |
323 | ||
324 | static struct file *find_readable_file_locked(struct nfs4_file *f) | |
325 | { | |
326 | struct file *ret; | |
327 | ||
328 | lockdep_assert_held(&f->fi_lock); | |
329 | ||
330 | ret = __nfs4_get_fd(f, O_RDONLY); | |
331 | if (!ret) | |
332 | ret = __nfs4_get_fd(f, O_RDWR); | |
333 | return ret; | |
334 | } | |
335 | ||
336 | static struct file * | |
337 | find_readable_file(struct nfs4_file *f) | |
338 | { | |
339 | struct file *ret; | |
340 | ||
341 | spin_lock(&f->fi_lock); | |
342 | ret = find_readable_file_locked(f); | |
343 | spin_unlock(&f->fi_lock); | |
344 | ||
345 | return ret; | |
346 | } | |
347 | ||
4d227fca | 348 | struct file * |
de18643d TM |
349 | find_any_file(struct nfs4_file *f) |
350 | { | |
351 | struct file *ret; | |
352 | ||
353 | spin_lock(&f->fi_lock); | |
354 | ret = __nfs4_get_fd(f, O_RDWR); | |
355 | if (!ret) { | |
356 | ret = __nfs4_get_fd(f, O_WRONLY); | |
357 | if (!ret) | |
358 | ret = __nfs4_get_fd(f, O_RDONLY); | |
359 | } | |
360 | spin_unlock(&f->fi_lock); | |
361 | return ret; | |
362 | } | |
363 | ||
02a3508d | 364 | static atomic_long_t num_delegations; |
697ce9be | 365 | unsigned long max_delegations; |
ef0f3390 N |
366 | |
367 | /* | |
368 | * Open owner state (share locks) | |
369 | */ | |
370 | ||
16bfdaaf BF |
371 | /* hash tables for lock and open owners */ |
372 | #define OWNER_HASH_BITS 8 | |
373 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) | |
374 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) | |
ef0f3390 | 375 | |
d4f0489f | 376 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
ddc04c41 BF |
377 | { |
378 | unsigned int ret; | |
379 | ||
380 | ret = opaque_hashval(ownername->data, ownername->len); | |
16bfdaaf | 381 | return ret & OWNER_HASH_MASK; |
ddc04c41 | 382 | } |
ef0f3390 | 383 | |
ef0f3390 N |
384 | /* hash table for nfs4_file */ |
385 | #define FILE_HASH_BITS 8 | |
386 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) | |
35079582 | 387 | |
ca943217 | 388 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
ddc04c41 | 389 | { |
ca943217 TM |
390 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
391 | } | |
392 | ||
393 | static unsigned int file_hashval(struct knfsd_fh *fh) | |
394 | { | |
395 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); | |
396 | } | |
397 | ||
89876f8c | 398 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
ef0f3390 | 399 | |
12659651 JL |
400 | static void |
401 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) | |
3477565e | 402 | { |
7214e860 JL |
403 | lockdep_assert_held(&fp->fi_lock); |
404 | ||
12659651 JL |
405 | if (access & NFS4_SHARE_ACCESS_WRITE) |
406 | atomic_inc(&fp->fi_access[O_WRONLY]); | |
407 | if (access & NFS4_SHARE_ACCESS_READ) | |
408 | atomic_inc(&fp->fi_access[O_RDONLY]); | |
3477565e BF |
409 | } |
410 | ||
12659651 JL |
411 | static __be32 |
412 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) | |
998db52c | 413 | { |
7214e860 JL |
414 | lockdep_assert_held(&fp->fi_lock); |
415 | ||
12659651 JL |
416 | /* Does this access mode make sense? */ |
417 | if (access & ~NFS4_SHARE_ACCESS_BOTH) | |
418 | return nfserr_inval; | |
419 | ||
baeb4ff0 JL |
420 | /* Does it conflict with a deny mode already set? */ |
421 | if ((access & fp->fi_share_deny) != 0) | |
422 | return nfserr_share_denied; | |
423 | ||
12659651 JL |
424 | __nfs4_file_get_access(fp, access); |
425 | return nfs_ok; | |
998db52c BF |
426 | } |
427 | ||
baeb4ff0 JL |
428 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
429 | { | |
430 | /* Common case is that there is no deny mode. */ | |
431 | if (deny) { | |
432 | /* Does this deny mode make sense? */ | |
433 | if (deny & ~NFS4_SHARE_DENY_BOTH) | |
434 | return nfserr_inval; | |
435 | ||
436 | if ((deny & NFS4_SHARE_DENY_READ) && | |
437 | atomic_read(&fp->fi_access[O_RDONLY])) | |
438 | return nfserr_share_denied; | |
439 | ||
440 | if ((deny & NFS4_SHARE_DENY_WRITE) && | |
441 | atomic_read(&fp->fi_access[O_WRONLY])) | |
442 | return nfserr_share_denied; | |
443 | } | |
444 | return nfs_ok; | |
445 | } | |
446 | ||
998db52c | 447 | static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
f9d7562f | 448 | { |
de18643d TM |
449 | might_lock(&fp->fi_lock); |
450 | ||
451 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { | |
452 | struct file *f1 = NULL; | |
453 | struct file *f2 = NULL; | |
454 | ||
6d338b51 | 455 | swap(f1, fp->fi_fds[oflag]); |
0c7c3e67 | 456 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
6d338b51 | 457 | swap(f2, fp->fi_fds[O_RDWR]); |
de18643d TM |
458 | spin_unlock(&fp->fi_lock); |
459 | if (f1) | |
460 | fput(f1); | |
461 | if (f2) | |
462 | fput(f2); | |
f9d7562f BF |
463 | } |
464 | } | |
465 | ||
12659651 | 466 | static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) |
998db52c | 467 | { |
12659651 JL |
468 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
469 | ||
470 | if (access & NFS4_SHARE_ACCESS_WRITE) | |
998db52c | 471 | __nfs4_file_put_access(fp, O_WRONLY); |
12659651 JL |
472 | if (access & NFS4_SHARE_ACCESS_READ) |
473 | __nfs4_file_put_access(fp, O_RDONLY); | |
998db52c BF |
474 | } |
475 | ||
8287f009 SB |
476 | /* |
477 | * Allocate a new open/delegation state counter. This is needed for | |
478 | * pNFS for proper return on close semantics. | |
479 | * | |
480 | * Note that we only allocate it for pNFS-enabled exports, otherwise | |
481 | * all pointers to struct nfs4_clnt_odstate are always NULL. | |
482 | */ | |
483 | static struct nfs4_clnt_odstate * | |
484 | alloc_clnt_odstate(struct nfs4_client *clp) | |
485 | { | |
486 | struct nfs4_clnt_odstate *co; | |
487 | ||
488 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); | |
489 | if (co) { | |
490 | co->co_client = clp; | |
491 | atomic_set(&co->co_odcount, 1); | |
492 | } | |
493 | return co; | |
494 | } | |
495 | ||
496 | static void | |
497 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) | |
498 | { | |
499 | struct nfs4_file *fp = co->co_file; | |
500 | ||
501 | lockdep_assert_held(&fp->fi_lock); | |
502 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); | |
503 | } | |
504 | ||
505 | static inline void | |
506 | get_clnt_odstate(struct nfs4_clnt_odstate *co) | |
507 | { | |
508 | if (co) | |
509 | atomic_inc(&co->co_odcount); | |
510 | } | |
511 | ||
512 | static void | |
513 | put_clnt_odstate(struct nfs4_clnt_odstate *co) | |
514 | { | |
515 | struct nfs4_file *fp; | |
516 | ||
517 | if (!co) | |
518 | return; | |
519 | ||
520 | fp = co->co_file; | |
521 | if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { | |
522 | list_del(&co->co_perfile); | |
523 | spin_unlock(&fp->fi_lock); | |
524 | ||
525 | nfsd4_return_all_file_layouts(co->co_client, fp); | |
526 | kmem_cache_free(odstate_slab, co); | |
527 | } | |
528 | } | |
529 | ||
530 | static struct nfs4_clnt_odstate * | |
531 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) | |
532 | { | |
533 | struct nfs4_clnt_odstate *co; | |
534 | struct nfs4_client *cl; | |
535 | ||
536 | if (!new) | |
537 | return NULL; | |
538 | ||
539 | cl = new->co_client; | |
540 | ||
541 | spin_lock(&fp->fi_lock); | |
542 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { | |
543 | if (co->co_client == cl) { | |
544 | get_clnt_odstate(co); | |
545 | goto out; | |
546 | } | |
547 | } | |
548 | co = new; | |
549 | co->co_file = fp; | |
550 | hash_clnt_odstate_locked(new); | |
551 | out: | |
552 | spin_unlock(&fp->fi_lock); | |
553 | return co; | |
554 | } | |
555 | ||
cd61c522 | 556 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, |
6011695d | 557 | struct kmem_cache *slab) |
2a74aba7 | 558 | { |
3abdb607 | 559 | struct nfs4_stid *stid; |
6136d2b4 | 560 | int new_id; |
2a74aba7 | 561 | |
f8338834 | 562 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
3abdb607 BF |
563 | if (!stid) |
564 | return NULL; | |
565 | ||
4770d722 JL |
566 | idr_preload(GFP_KERNEL); |
567 | spin_lock(&cl->cl_lock); | |
568 | new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT); | |
569 | spin_unlock(&cl->cl_lock); | |
570 | idr_preload_end(); | |
ebd6c707 | 571 | if (new_id < 0) |
3abdb607 | 572 | goto out_free; |
2a74aba7 | 573 | stid->sc_client = cl; |
3abdb607 BF |
574 | stid->sc_stateid.si_opaque.so_id = new_id; |
575 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; | |
2a74aba7 | 576 | /* Will be incremented before return to client: */ |
72c0b0fb | 577 | atomic_set(&stid->sc_count, 1); |
9767feb2 | 578 | spin_lock_init(&stid->sc_lock); |
996e0938 | 579 | |
996e0938 | 580 | /* |
3abdb607 BF |
581 | * It shouldn't be a problem to reuse an opaque stateid value. |
582 | * I don't think it is for 4.1. But with 4.0 I worry that, for | |
583 | * example, a stray write retransmission could be accepted by | |
584 | * the server when it should have been rejected. Therefore, | |
585 | * adopt a trick from the sctp code to attempt to maximize the | |
586 | * amount of time until an id is reused, by ensuring they always | |
587 | * "increase" (mod INT_MAX): | |
996e0938 | 588 | */ |
3abdb607 BF |
589 | return stid; |
590 | out_free: | |
2c44a234 | 591 | kmem_cache_free(slab, stid); |
3abdb607 | 592 | return NULL; |
2a74aba7 BF |
593 | } |
594 | ||
b49e084d | 595 | static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) |
4cdc951b | 596 | { |
6011695d TM |
597 | struct nfs4_stid *stid; |
598 | struct nfs4_ol_stateid *stp; | |
599 | ||
600 | stid = nfs4_alloc_stid(clp, stateid_slab); | |
601 | if (!stid) | |
602 | return NULL; | |
603 | ||
604 | stp = openlockstateid(stid); | |
605 | stp->st_stid.sc_free = nfs4_free_ol_stateid; | |
606 | return stp; | |
607 | } | |
608 | ||
609 | static void nfs4_free_deleg(struct nfs4_stid *stid) | |
610 | { | |
6011695d TM |
611 | kmem_cache_free(deleg_slab, stid); |
612 | atomic_long_dec(&num_delegations); | |
4cdc951b BF |
613 | } |
614 | ||
6282cd56 N |
615 | /* |
616 | * When we recall a delegation, we should be careful not to hand it | |
617 | * out again straight away. | |
618 | * To ensure this we keep a pair of bloom filters ('new' and 'old') | |
619 | * in which the filehandles of recalled delegations are "stored". | |
620 | * If a filehandle appear in either filter, a delegation is blocked. | |
621 | * When a delegation is recalled, the filehandle is stored in the "new" | |
622 | * filter. | |
623 | * Every 30 seconds we swap the filters and clear the "new" one, | |
624 | * unless both are empty of course. | |
625 | * | |
626 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the | |
627 | * low 3 bytes as hash-table indices. | |
628 | * | |
f54fe962 | 629 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
6282cd56 N |
630 | * is used to manage concurrent access. Testing does not need the lock |
631 | * except when swapping the two filters. | |
632 | */ | |
f54fe962 | 633 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
6282cd56 N |
634 | static struct bloom_pair { |
635 | int entries, old_entries; | |
636 | time_t swap_time; | |
637 | int new; /* index into 'set' */ | |
638 | DECLARE_BITMAP(set[2], 256); | |
639 | } blocked_delegations; | |
640 | ||
641 | static int delegation_blocked(struct knfsd_fh *fh) | |
642 | { | |
643 | u32 hash; | |
644 | struct bloom_pair *bd = &blocked_delegations; | |
645 | ||
646 | if (bd->entries == 0) | |
647 | return 0; | |
648 | if (seconds_since_boot() - bd->swap_time > 30) { | |
f54fe962 | 649 | spin_lock(&blocked_delegations_lock); |
6282cd56 N |
650 | if (seconds_since_boot() - bd->swap_time > 30) { |
651 | bd->entries -= bd->old_entries; | |
652 | bd->old_entries = bd->entries; | |
653 | memset(bd->set[bd->new], 0, | |
654 | sizeof(bd->set[0])); | |
655 | bd->new = 1-bd->new; | |
656 | bd->swap_time = seconds_since_boot(); | |
657 | } | |
f54fe962 | 658 | spin_unlock(&blocked_delegations_lock); |
6282cd56 | 659 | } |
87545899 | 660 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
6282cd56 N |
661 | if (test_bit(hash&255, bd->set[0]) && |
662 | test_bit((hash>>8)&255, bd->set[0]) && | |
663 | test_bit((hash>>16)&255, bd->set[0])) | |
664 | return 1; | |
665 | ||
666 | if (test_bit(hash&255, bd->set[1]) && | |
667 | test_bit((hash>>8)&255, bd->set[1]) && | |
668 | test_bit((hash>>16)&255, bd->set[1])) | |
669 | return 1; | |
670 | ||
671 | return 0; | |
672 | } | |
673 | ||
674 | static void block_delegations(struct knfsd_fh *fh) | |
675 | { | |
676 | u32 hash; | |
677 | struct bloom_pair *bd = &blocked_delegations; | |
678 | ||
87545899 | 679 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
6282cd56 | 680 | |
f54fe962 | 681 | spin_lock(&blocked_delegations_lock); |
6282cd56 N |
682 | __set_bit(hash&255, bd->set[bd->new]); |
683 | __set_bit((hash>>8)&255, bd->set[bd->new]); | |
684 | __set_bit((hash>>16)&255, bd->set[bd->new]); | |
685 | if (bd->entries == 0) | |
686 | bd->swap_time = seconds_since_boot(); | |
687 | bd->entries += 1; | |
f54fe962 | 688 | spin_unlock(&blocked_delegations_lock); |
6282cd56 N |
689 | } |
690 | ||
1da177e4 | 691 | static struct nfs4_delegation * |
8287f009 SB |
692 | alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh, |
693 | struct nfs4_clnt_odstate *odstate) | |
1da177e4 LT |
694 | { |
695 | struct nfs4_delegation *dp; | |
02a3508d | 696 | long n; |
1da177e4 LT |
697 | |
698 | dprintk("NFSD alloc_init_deleg\n"); | |
02a3508d TM |
699 | n = atomic_long_inc_return(&num_delegations); |
700 | if (n < 0 || n > max_delegations) | |
701 | goto out_dec; | |
6282cd56 | 702 | if (delegation_blocked(¤t_fh->fh_handle)) |
02a3508d | 703 | goto out_dec; |
996e0938 | 704 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab)); |
5b2d21c1 | 705 | if (dp == NULL) |
02a3508d | 706 | goto out_dec; |
6011695d TM |
707 | |
708 | dp->dl_stid.sc_free = nfs4_free_deleg; | |
2a74aba7 BF |
709 | /* |
710 | * delegation seqid's are never incremented. The 4.1 special | |
6136d2b4 BF |
711 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
712 | * 0 anyway just for consistency and use 1: | |
2a74aba7 BF |
713 | */ |
714 | dp->dl_stid.sc_stateid.si_generation = 1; | |
ea1da636 N |
715 | INIT_LIST_HEAD(&dp->dl_perfile); |
716 | INIT_LIST_HEAD(&dp->dl_perclnt); | |
1da177e4 | 717 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
8287f009 SB |
718 | dp->dl_clnt_odstate = odstate; |
719 | get_clnt_odstate(odstate); | |
99c41515 | 720 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
f0b5de1b CH |
721 | dp->dl_retries = 1; |
722 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, | |
0162ac2b | 723 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
1da177e4 | 724 | return dp; |
02a3508d TM |
725 | out_dec: |
726 | atomic_long_dec(&num_delegations); | |
727 | return NULL; | |
1da177e4 LT |
728 | } |
729 | ||
730 | void | |
6011695d | 731 | nfs4_put_stid(struct nfs4_stid *s) |
1da177e4 | 732 | { |
11b9164a | 733 | struct nfs4_file *fp = s->sc_file; |
6011695d TM |
734 | struct nfs4_client *clp = s->sc_client; |
735 | ||
4770d722 JL |
736 | might_lock(&clp->cl_lock); |
737 | ||
b401be22 JL |
738 | if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
739 | wake_up_all(&close_wq); | |
6011695d | 740 | return; |
b401be22 | 741 | } |
6011695d | 742 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
4770d722 | 743 | spin_unlock(&clp->cl_lock); |
6011695d | 744 | s->sc_free(s); |
11b9164a TM |
745 | if (fp) |
746 | put_nfs4_file(fp); | |
1da177e4 LT |
747 | } |
748 | ||
9767feb2 JL |
749 | void |
750 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) | |
751 | { | |
752 | stateid_t *src = &stid->sc_stateid; | |
753 | ||
754 | spin_lock(&stid->sc_lock); | |
755 | if (unlikely(++src->si_generation == 0)) | |
756 | src->si_generation = 1; | |
757 | memcpy(dst, src, sizeof(*dst)); | |
758 | spin_unlock(&stid->sc_lock); | |
759 | } | |
760 | ||
acfdf5c3 | 761 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
1da177e4 | 762 | { |
6bcc034e | 763 | struct file *filp = NULL; |
417c6629 | 764 | |
6bcc034e | 765 | spin_lock(&fp->fi_lock); |
67db1034 | 766 | if (fp->fi_deleg_file && --fp->fi_delegees == 0) |
6bcc034e | 767 | swap(filp, fp->fi_deleg_file); |
6bcc034e JL |
768 | spin_unlock(&fp->fi_lock); |
769 | ||
770 | if (filp) { | |
2ab99ee1 | 771 | vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp); |
6bcc034e | 772 | fput(filp); |
acfdf5c3 | 773 | } |
1da177e4 LT |
774 | } |
775 | ||
cd61c522 | 776 | void nfs4_unhash_stid(struct nfs4_stid *s) |
6136d2b4 | 777 | { |
3abdb607 | 778 | s->sc_type = 0; |
6136d2b4 BF |
779 | } |
780 | ||
34ed9872 AE |
781 | /** |
782 | * nfs4_get_existing_delegation - Discover if this delegation already exists | |
783 | * @clp: a pointer to the nfs4_client we're granting a delegation to | |
784 | * @fp: a pointer to the nfs4_file we're granting a delegation on | |
785 | * | |
786 | * Return: | |
787 | * On success: NULL if an existing delegation was not found. | |
788 | * | |
789 | * On error: -EAGAIN if one was previously granted to this nfs4_client | |
790 | * for this nfs4_file. | |
791 | * | |
792 | */ | |
793 | ||
794 | static int | |
795 | nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp) | |
796 | { | |
797 | struct nfs4_delegation *searchdp = NULL; | |
798 | struct nfs4_client *searchclp = NULL; | |
799 | ||
800 | lockdep_assert_held(&state_lock); | |
801 | lockdep_assert_held(&fp->fi_lock); | |
802 | ||
803 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { | |
804 | searchclp = searchdp->dl_stid.sc_client; | |
805 | if (clp == searchclp) { | |
806 | return -EAGAIN; | |
807 | } | |
808 | } | |
809 | return 0; | |
810 | } | |
811 | ||
812 | /** | |
813 | * hash_delegation_locked - Add a delegation to the appropriate lists | |
814 | * @dp: a pointer to the nfs4_delegation we are adding. | |
815 | * @fp: a pointer to the nfs4_file we're granting a delegation on | |
816 | * | |
817 | * Return: | |
818 | * On success: NULL if the delegation was successfully hashed. | |
819 | * | |
820 | * On error: -EAGAIN if one was previously granted to this | |
821 | * nfs4_client for this nfs4_file. Delegation is not hashed. | |
822 | * | |
823 | */ | |
824 | ||
825 | static int | |
931ee56c BH |
826 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
827 | { | |
34ed9872 AE |
828 | int status; |
829 | struct nfs4_client *clp = dp->dl_stid.sc_client; | |
830 | ||
cdc97505 | 831 | lockdep_assert_held(&state_lock); |
417c6629 | 832 | lockdep_assert_held(&fp->fi_lock); |
931ee56c | 833 | |
34ed9872 AE |
834 | status = nfs4_get_existing_delegation(clp, fp); |
835 | if (status) | |
836 | return status; | |
837 | ++fp->fi_delegees; | |
67cb1279 | 838 | atomic_inc(&dp->dl_stid.sc_count); |
3fb87d13 | 839 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
931ee56c | 840 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
34ed9872 AE |
841 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
842 | return 0; | |
931ee56c BH |
843 | } |
844 | ||
3fcbbd24 | 845 | static bool |
42690676 | 846 | unhash_delegation_locked(struct nfs4_delegation *dp) |
1da177e4 | 847 | { |
11b9164a | 848 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
02e1215f | 849 | |
42690676 JL |
850 | lockdep_assert_held(&state_lock); |
851 | ||
3fcbbd24 JL |
852 | if (list_empty(&dp->dl_perfile)) |
853 | return false; | |
854 | ||
b0fc29d6 | 855 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
d55a166c JL |
856 | /* Ensure that deleg break won't try to requeue it */ |
857 | ++dp->dl_time; | |
417c6629 | 858 | spin_lock(&fp->fi_lock); |
931ee56c | 859 | list_del_init(&dp->dl_perclnt); |
1da177e4 | 860 | list_del_init(&dp->dl_recall_lru); |
02e1215f JL |
861 | list_del_init(&dp->dl_perfile); |
862 | spin_unlock(&fp->fi_lock); | |
3fcbbd24 | 863 | return true; |
3bd64a5b BF |
864 | } |
865 | ||
3bd64a5b BF |
866 | static void destroy_delegation(struct nfs4_delegation *dp) |
867 | { | |
3fcbbd24 JL |
868 | bool unhashed; |
869 | ||
42690676 | 870 | spin_lock(&state_lock); |
3fcbbd24 | 871 | unhashed = unhash_delegation_locked(dp); |
42690676 | 872 | spin_unlock(&state_lock); |
3fcbbd24 JL |
873 | if (unhashed) { |
874 | put_clnt_odstate(dp->dl_clnt_odstate); | |
875 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); | |
876 | nfs4_put_stid(&dp->dl_stid); | |
877 | } | |
3bd64a5b BF |
878 | } |
879 | ||
880 | static void revoke_delegation(struct nfs4_delegation *dp) | |
881 | { | |
882 | struct nfs4_client *clp = dp->dl_stid.sc_client; | |
883 | ||
2d4a532d JL |
884 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
885 | ||
8287f009 | 886 | put_clnt_odstate(dp->dl_clnt_odstate); |
afbda402 JL |
887 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
888 | ||
3bd64a5b | 889 | if (clp->cl_minorversion == 0) |
6011695d | 890 | nfs4_put_stid(&dp->dl_stid); |
3bd64a5b | 891 | else { |
3bd64a5b | 892 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
2d4a532d JL |
893 | spin_lock(&clp->cl_lock); |
894 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); | |
895 | spin_unlock(&clp->cl_lock); | |
3bd64a5b BF |
896 | } |
897 | } | |
898 | ||
1da177e4 LT |
899 | /* |
900 | * SETCLIENTID state | |
901 | */ | |
902 | ||
ddc04c41 BF |
903 | static unsigned int clientid_hashval(u32 id) |
904 | { | |
905 | return id & CLIENT_HASH_MASK; | |
906 | } | |
907 | ||
908 | static unsigned int clientstr_hashval(const char *name) | |
909 | { | |
910 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; | |
911 | } | |
912 | ||
f9d7562f BF |
913 | /* |
914 | * We store the NONE, READ, WRITE, and BOTH bits separately in the | |
915 | * st_{access,deny}_bmap field of the stateid, in order to track not | |
916 | * only what share bits are currently in force, but also what | |
917 | * combinations of share bits previous opens have used. This allows us | |
918 | * to enforce the recommendation of rfc 3530 14.2.19 that the server | |
919 | * return an error if the client attempt to downgrade to a combination | |
920 | * of share bits not explicable by closing some of its previous opens. | |
921 | * | |
922 | * XXX: This enforcement is actually incomplete, since we don't keep | |
923 | * track of access/deny bit combinations; so, e.g., we allow: | |
924 | * | |
925 | * OPEN allow read, deny write | |
926 | * OPEN allow both, deny none | |
927 | * DOWNGRADE allow read, deny none | |
928 | * | |
929 | * which we should reject. | |
930 | */ | |
5ae037e5 JL |
931 | static unsigned int |
932 | bmap_to_share_mode(unsigned long bmap) { | |
f9d7562f | 933 | int i; |
5ae037e5 | 934 | unsigned int access = 0; |
f9d7562f | 935 | |
f9d7562f BF |
936 | for (i = 1; i < 4; i++) { |
937 | if (test_bit(i, &bmap)) | |
5ae037e5 | 938 | access |= i; |
f9d7562f | 939 | } |
5ae037e5 | 940 | return access; |
f9d7562f BF |
941 | } |
942 | ||
82c5ff1b JL |
943 | /* set share access for a given stateid */ |
944 | static inline void | |
945 | set_access(u32 access, struct nfs4_ol_stateid *stp) | |
946 | { | |
c11c591f JL |
947 | unsigned char mask = 1 << access; |
948 | ||
949 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); | |
950 | stp->st_access_bmap |= mask; | |
82c5ff1b JL |
951 | } |
952 | ||
953 | /* clear share access for a given stateid */ | |
954 | static inline void | |
955 | clear_access(u32 access, struct nfs4_ol_stateid *stp) | |
956 | { | |
c11c591f JL |
957 | unsigned char mask = 1 << access; |
958 | ||
959 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); | |
960 | stp->st_access_bmap &= ~mask; | |
82c5ff1b JL |
961 | } |
962 | ||
963 | /* test whether a given stateid has access */ | |
964 | static inline bool | |
965 | test_access(u32 access, struct nfs4_ol_stateid *stp) | |
966 | { | |
c11c591f JL |
967 | unsigned char mask = 1 << access; |
968 | ||
969 | return (bool)(stp->st_access_bmap & mask); | |
82c5ff1b JL |
970 | } |
971 | ||
ce0fc43c JL |
972 | /* set share deny for a given stateid */ |
973 | static inline void | |
c11c591f | 974 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
ce0fc43c | 975 | { |
c11c591f JL |
976 | unsigned char mask = 1 << deny; |
977 | ||
978 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); | |
979 | stp->st_deny_bmap |= mask; | |
ce0fc43c JL |
980 | } |
981 | ||
982 | /* clear share deny for a given stateid */ | |
983 | static inline void | |
c11c591f | 984 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
ce0fc43c | 985 | { |
c11c591f JL |
986 | unsigned char mask = 1 << deny; |
987 | ||
988 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); | |
989 | stp->st_deny_bmap &= ~mask; | |
ce0fc43c JL |
990 | } |
991 | ||
992 | /* test whether a given stateid is denying specific access */ | |
993 | static inline bool | |
c11c591f | 994 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
ce0fc43c | 995 | { |
c11c591f JL |
996 | unsigned char mask = 1 << deny; |
997 | ||
998 | return (bool)(stp->st_deny_bmap & mask); | |
f9d7562f BF |
999 | } |
1000 | ||
1001 | static int nfs4_access_to_omode(u32 access) | |
1002 | { | |
8f34a430 | 1003 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
f9d7562f BF |
1004 | case NFS4_SHARE_ACCESS_READ: |
1005 | return O_RDONLY; | |
1006 | case NFS4_SHARE_ACCESS_WRITE: | |
1007 | return O_WRONLY; | |
1008 | case NFS4_SHARE_ACCESS_BOTH: | |
1009 | return O_RDWR; | |
1010 | } | |
063b0fb9 BF |
1011 | WARN_ON_ONCE(1); |
1012 | return O_RDONLY; | |
f9d7562f BF |
1013 | } |
1014 | ||
baeb4ff0 JL |
1015 | /* |
1016 | * A stateid that had a deny mode associated with it is being released | |
1017 | * or downgraded. Recalculate the deny mode on the file. | |
1018 | */ | |
1019 | static void | |
1020 | recalculate_deny_mode(struct nfs4_file *fp) | |
1021 | { | |
1022 | struct nfs4_ol_stateid *stp; | |
1023 | ||
1024 | spin_lock(&fp->fi_lock); | |
1025 | fp->fi_share_deny = 0; | |
1026 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) | |
1027 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); | |
1028 | spin_unlock(&fp->fi_lock); | |
1029 | } | |
1030 | ||
1031 | static void | |
1032 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) | |
1033 | { | |
1034 | int i; | |
1035 | bool change = false; | |
1036 | ||
1037 | for (i = 1; i < 4; i++) { | |
1038 | if ((i & deny) != i) { | |
1039 | change = true; | |
1040 | clear_deny(i, stp); | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | /* Recalculate per-file deny mode if there was a change */ | |
1045 | if (change) | |
11b9164a | 1046 | recalculate_deny_mode(stp->st_stid.sc_file); |
baeb4ff0 JL |
1047 | } |
1048 | ||
82c5ff1b JL |
1049 | /* release all access and file references for a given stateid */ |
1050 | static void | |
1051 | release_all_access(struct nfs4_ol_stateid *stp) | |
1052 | { | |
1053 | int i; | |
11b9164a | 1054 | struct nfs4_file *fp = stp->st_stid.sc_file; |
baeb4ff0 JL |
1055 | |
1056 | if (fp && stp->st_deny_bmap != 0) | |
1057 | recalculate_deny_mode(fp); | |
82c5ff1b JL |
1058 | |
1059 | for (i = 1; i < 4; i++) { | |
1060 | if (test_access(i, stp)) | |
11b9164a | 1061 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
82c5ff1b JL |
1062 | clear_access(i, stp); |
1063 | } | |
1064 | } | |
1065 | ||
d50ffded KM |
1066 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
1067 | { | |
1068 | kfree(sop->so_owner.data); | |
1069 | sop->so_ops->so_free(sop); | |
1070 | } | |
1071 | ||
6b180f0b JL |
1072 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
1073 | { | |
a819ecc1 JL |
1074 | struct nfs4_client *clp = sop->so_client; |
1075 | ||
1076 | might_lock(&clp->cl_lock); | |
1077 | ||
1078 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) | |
6b180f0b | 1079 | return; |
8f4b54c5 | 1080 | sop->so_ops->so_unhash(sop); |
a819ecc1 | 1081 | spin_unlock(&clp->cl_lock); |
d50ffded | 1082 | nfs4_free_stateowner(sop); |
6b180f0b JL |
1083 | } |
1084 | ||
e8568739 | 1085 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
529d7b2a | 1086 | { |
11b9164a | 1087 | struct nfs4_file *fp = stp->st_stid.sc_file; |
1d31a253 | 1088 | |
1c755dc1 JL |
1089 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
1090 | ||
e8568739 JL |
1091 | if (list_empty(&stp->st_perfile)) |
1092 | return false; | |
1093 | ||
1d31a253 | 1094 | spin_lock(&fp->fi_lock); |
e8568739 | 1095 | list_del_init(&stp->st_perfile); |
1d31a253 | 1096 | spin_unlock(&fp->fi_lock); |
529d7b2a | 1097 | list_del(&stp->st_perstateowner); |
e8568739 | 1098 | return true; |
529d7b2a BF |
1099 | } |
1100 | ||
6011695d | 1101 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
529d7b2a | 1102 | { |
6011695d | 1103 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
4665e2ba | 1104 | |
8287f009 | 1105 | put_clnt_odstate(stp->st_clnt_odstate); |
6011695d | 1106 | release_all_access(stp); |
d3134b10 JL |
1107 | if (stp->st_stateowner) |
1108 | nfs4_put_stateowner(stp->st_stateowner); | |
6011695d | 1109 | kmem_cache_free(stateid_slab, stid); |
529d7b2a BF |
1110 | } |
1111 | ||
b49e084d | 1112 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
529d7b2a | 1113 | { |
b49e084d JL |
1114 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
1115 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); | |
529d7b2a BF |
1116 | struct file *file; |
1117 | ||
b49e084d JL |
1118 | file = find_any_file(stp->st_stid.sc_file); |
1119 | if (file) | |
1120 | filp_close(file, (fl_owner_t)lo); | |
1121 | nfs4_free_ol_stateid(stid); | |
1122 | } | |
1123 | ||
2c41beb0 JL |
1124 | /* |
1125 | * Put the persistent reference to an already unhashed generic stateid, while | |
1126 | * holding the cl_lock. If it's the last reference, then put it onto the | |
1127 | * reaplist for later destruction. | |
1128 | */ | |
1129 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, | |
1130 | struct list_head *reaplist) | |
1131 | { | |
1132 | struct nfs4_stid *s = &stp->st_stid; | |
1133 | struct nfs4_client *clp = s->sc_client; | |
1134 | ||
1135 | lockdep_assert_held(&clp->cl_lock); | |
1136 | ||
1137 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); | |
1138 | ||
1139 | if (!atomic_dec_and_test(&s->sc_count)) { | |
1140 | wake_up_all(&close_wq); | |
1141 | return; | |
1142 | } | |
1143 | ||
1144 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); | |
1145 | list_add(&stp->st_locks, reaplist); | |
1146 | } | |
1147 | ||
e8568739 | 1148 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
b49e084d | 1149 | { |
1c755dc1 JL |
1150 | struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner); |
1151 | ||
3c1c995c JL |
1152 | lockdep_assert_held(&oo->oo_owner.so_client->cl_lock); |
1153 | ||
1154 | list_del_init(&stp->st_locks); | |
cd61c522 | 1155 | nfs4_unhash_stid(&stp->st_stid); |
e8568739 | 1156 | return unhash_ol_stateid(stp); |
3c1c995c JL |
1157 | } |
1158 | ||
1159 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) | |
1160 | { | |
1161 | struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner); | |
e8568739 | 1162 | bool unhashed; |
3c1c995c JL |
1163 | |
1164 | spin_lock(&oo->oo_owner.so_client->cl_lock); | |
e8568739 | 1165 | unhashed = unhash_lock_stateid(stp); |
1c755dc1 | 1166 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
e8568739 JL |
1167 | if (unhashed) |
1168 | nfs4_put_stid(&stp->st_stid); | |
529d7b2a BF |
1169 | } |
1170 | ||
c58c6610 | 1171 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
8f4b54c5 | 1172 | { |
d4f0489f | 1173 | struct nfs4_client *clp = lo->lo_owner.so_client; |
c58c6610 | 1174 | |
d4f0489f | 1175 | lockdep_assert_held(&clp->cl_lock); |
c58c6610 | 1176 | |
8f4b54c5 JL |
1177 | list_del_init(&lo->lo_owner.so_strhash); |
1178 | } | |
1179 | ||
2c41beb0 JL |
1180 | /* |
1181 | * Free a list of generic stateids that were collected earlier after being | |
1182 | * fully unhashed. | |
1183 | */ | |
1184 | static void | |
1185 | free_ol_stateid_reaplist(struct list_head *reaplist) | |
1186 | { | |
1187 | struct nfs4_ol_stateid *stp; | |
fb94d766 | 1188 | struct nfs4_file *fp; |
2c41beb0 JL |
1189 | |
1190 | might_sleep(); | |
1191 | ||
1192 | while (!list_empty(reaplist)) { | |
1193 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, | |
1194 | st_locks); | |
1195 | list_del(&stp->st_locks); | |
fb94d766 | 1196 | fp = stp->st_stid.sc_file; |
2c41beb0 | 1197 | stp->st_stid.sc_free(&stp->st_stid); |
fb94d766 KM |
1198 | if (fp) |
1199 | put_nfs4_file(fp); | |
2c41beb0 JL |
1200 | } |
1201 | } | |
1202 | ||
d83017f9 JL |
1203 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
1204 | struct list_head *reaplist) | |
3c87b9b7 TM |
1205 | { |
1206 | struct nfs4_ol_stateid *stp; | |
1207 | ||
e8568739 JL |
1208 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
1209 | ||
3c87b9b7 TM |
1210 | while (!list_empty(&open_stp->st_locks)) { |
1211 | stp = list_entry(open_stp->st_locks.next, | |
1212 | struct nfs4_ol_stateid, st_locks); | |
e8568739 | 1213 | WARN_ON(!unhash_lock_stateid(stp)); |
d83017f9 | 1214 | put_ol_stateid_locked(stp, reaplist); |
529d7b2a BF |
1215 | } |
1216 | } | |
1217 | ||
e8568739 | 1218 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
d83017f9 | 1219 | struct list_head *reaplist) |
2283963f | 1220 | { |
e8568739 JL |
1221 | bool unhashed; |
1222 | ||
2c41beb0 JL |
1223 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
1224 | ||
e8568739 | 1225 | unhashed = unhash_ol_stateid(stp); |
d83017f9 | 1226 | release_open_stateid_locks(stp, reaplist); |
e8568739 | 1227 | return unhashed; |
38c387b5 BF |
1228 | } |
1229 | ||
1230 | static void release_open_stateid(struct nfs4_ol_stateid *stp) | |
1231 | { | |
2c41beb0 JL |
1232 | LIST_HEAD(reaplist); |
1233 | ||
1234 | spin_lock(&stp->st_stid.sc_client->cl_lock); | |
e8568739 JL |
1235 | if (unhash_open_stateid(stp, &reaplist)) |
1236 | put_ol_stateid_locked(stp, &reaplist); | |
2c41beb0 JL |
1237 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
1238 | free_ol_stateid_reaplist(&reaplist); | |
2283963f BF |
1239 | } |
1240 | ||
7ffb5880 | 1241 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
f1d110ca | 1242 | { |
d4f0489f | 1243 | struct nfs4_client *clp = oo->oo_owner.so_client; |
7ffb5880 | 1244 | |
d4f0489f | 1245 | lockdep_assert_held(&clp->cl_lock); |
7ffb5880 | 1246 | |
8f4b54c5 JL |
1247 | list_del_init(&oo->oo_owner.so_strhash); |
1248 | list_del_init(&oo->oo_perclient); | |
f1d110ca BF |
1249 | } |
1250 | ||
f7a4d872 BF |
1251 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
1252 | { | |
217526e7 JL |
1253 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
1254 | nfsd_net_id); | |
1255 | struct nfs4_ol_stateid *s; | |
f7a4d872 | 1256 | |
217526e7 JL |
1257 | spin_lock(&nn->client_lock); |
1258 | s = oo->oo_last_closed_stid; | |
f7a4d872 | 1259 | if (s) { |
d3134b10 | 1260 | list_del_init(&oo->oo_close_lru); |
f7a4d872 BF |
1261 | oo->oo_last_closed_stid = NULL; |
1262 | } | |
217526e7 JL |
1263 | spin_unlock(&nn->client_lock); |
1264 | if (s) | |
1265 | nfs4_put_stid(&s->st_stid); | |
f7a4d872 BF |
1266 | } |
1267 | ||
2c41beb0 | 1268 | static void release_openowner(struct nfs4_openowner *oo) |
8f4b54c5 JL |
1269 | { |
1270 | struct nfs4_ol_stateid *stp; | |
d4f0489f | 1271 | struct nfs4_client *clp = oo->oo_owner.so_client; |
2c41beb0 | 1272 | struct list_head reaplist; |
7ffb5880 | 1273 | |
2c41beb0 | 1274 | INIT_LIST_HEAD(&reaplist); |
8f4b54c5 | 1275 | |
2c41beb0 JL |
1276 | spin_lock(&clp->cl_lock); |
1277 | unhash_openowner_locked(oo); | |
8f4b54c5 JL |
1278 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
1279 | stp = list_first_entry(&oo->oo_owner.so_stateids, | |
1280 | struct nfs4_ol_stateid, st_perstateowner); | |
e8568739 JL |
1281 | if (unhash_open_stateid(stp, &reaplist)) |
1282 | put_ol_stateid_locked(stp, &reaplist); | |
8f4b54c5 | 1283 | } |
d4f0489f | 1284 | spin_unlock(&clp->cl_lock); |
2c41beb0 | 1285 | free_ol_stateid_reaplist(&reaplist); |
f7a4d872 | 1286 | release_last_closed_stateid(oo); |
6b180f0b | 1287 | nfs4_put_stateowner(&oo->oo_owner); |
f1d110ca BF |
1288 | } |
1289 | ||
5282fd72 ME |
1290 | static inline int |
1291 | hash_sessionid(struct nfs4_sessionid *sessionid) | |
1292 | { | |
1293 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; | |
1294 | ||
1295 | return sid->sequence % SESSION_HASH_SIZE; | |
1296 | } | |
1297 | ||
135dd002 | 1298 | #ifdef CONFIG_SUNRPC_DEBUG |
5282fd72 ME |
1299 | static inline void |
1300 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
1301 | { | |
1302 | u32 *ptr = (u32 *)(&sessionid->data[0]); | |
1303 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); | |
1304 | } | |
8f199b82 TM |
1305 | #else |
1306 | static inline void | |
1307 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) | |
1308 | { | |
1309 | } | |
1310 | #endif | |
1311 | ||
9411b1d4 BF |
1312 | /* |
1313 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it | |
1314 | * won't be used for replay. | |
1315 | */ | |
1316 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) | |
1317 | { | |
1318 | struct nfs4_stateowner *so = cstate->replay_owner; | |
1319 | ||
1320 | if (nfserr == nfserr_replay_me) | |
1321 | return; | |
1322 | ||
1323 | if (!seqid_mutating_err(ntohl(nfserr))) { | |
58fb12e6 | 1324 | nfsd4_cstate_clear_replay(cstate); |
9411b1d4 BF |
1325 | return; |
1326 | } | |
1327 | if (!so) | |
1328 | return; | |
1329 | if (so->so_is_open_owner) | |
1330 | release_last_closed_stateid(openowner(so)); | |
1331 | so->so_seqid++; | |
1332 | return; | |
1333 | } | |
5282fd72 | 1334 | |
ec6b5d7b AA |
1335 | static void |
1336 | gen_sessionid(struct nfsd4_session *ses) | |
1337 | { | |
1338 | struct nfs4_client *clp = ses->se_client; | |
1339 | struct nfsd4_sessionid *sid; | |
1340 | ||
1341 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; | |
1342 | sid->clientid = clp->cl_clientid; | |
1343 | sid->sequence = current_sessionid++; | |
1344 | sid->reserved = 0; | |
1345 | } | |
1346 | ||
1347 | /* | |
a649637c AA |
1348 | * The protocol defines ca_maxresponssize_cached to include the size of |
1349 | * the rpc header, but all we need to cache is the data starting after | |
1350 | * the end of the initial SEQUENCE operation--the rest we regenerate | |
1351 | * each time. Therefore we can advertise a ca_maxresponssize_cached | |
1352 | * value that is the number of bytes in our cache plus a few additional | |
1353 | * bytes. In order to stay on the safe side, and not promise more than | |
1354 | * we can cache, those additional bytes must be the minimum possible: 24 | |
1355 | * bytes of rpc header (xid through accept state, with AUTH_NULL | |
1356 | * verifier), 12 for the compound header (with zero-length tag), and 44 | |
1357 | * for the SEQUENCE op response: | |
1358 | */ | |
1359 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) | |
1360 | ||
557ce264 AA |
1361 | static void |
1362 | free_session_slots(struct nfsd4_session *ses) | |
1363 | { | |
1364 | int i; | |
1365 | ||
1366 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) | |
1367 | kfree(ses->se_slots[i]); | |
1368 | } | |
1369 | ||
a649637c | 1370 | /* |
efe0cb6d BF |
1371 | * We don't actually need to cache the rpc and session headers, so we |
1372 | * can allocate a little less for each slot: | |
1373 | */ | |
55c760cf | 1374 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
efe0cb6d | 1375 | { |
55c760cf | 1376 | u32 size; |
efe0cb6d | 1377 | |
55c760cf BF |
1378 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
1379 | size = 0; | |
1380 | else | |
1381 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; | |
1382 | return size + sizeof(struct nfsd4_slot); | |
5b6feee9 | 1383 | } |
ec6b5d7b | 1384 | |
5b6feee9 BF |
1385 | /* |
1386 | * XXX: If we run out of reserved DRC memory we could (up to a point) | |
a649637c | 1387 | * re-negotiate active sessions and reduce their slot usage to make |
42b2aa86 | 1388 | * room for new connections. For now we just fail the create session. |
ec6b5d7b | 1389 | */ |
55c760cf | 1390 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca) |
ec6b5d7b | 1391 | { |
55c760cf BF |
1392 | u32 slotsize = slot_bytes(ca); |
1393 | u32 num = ca->maxreqs; | |
5b6feee9 | 1394 | int avail; |
ec6b5d7b | 1395 | |
5b6feee9 | 1396 | spin_lock(&nfsd_drc_lock); |
697ce9be ZY |
1397 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, |
1398 | nfsd_drc_max_mem - nfsd_drc_mem_used); | |
5b6feee9 BF |
1399 | num = min_t(int, num, avail / slotsize); |
1400 | nfsd_drc_mem_used += num * slotsize; | |
1401 | spin_unlock(&nfsd_drc_lock); | |
ec6b5d7b | 1402 | |
5b6feee9 BF |
1403 | return num; |
1404 | } | |
ec6b5d7b | 1405 | |
55c760cf | 1406 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
5b6feee9 | 1407 | { |
55c760cf BF |
1408 | int slotsize = slot_bytes(ca); |
1409 | ||
4bd9b0f4 | 1410 | spin_lock(&nfsd_drc_lock); |
55c760cf | 1411 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
4bd9b0f4 | 1412 | spin_unlock(&nfsd_drc_lock); |
5b6feee9 | 1413 | } |
ec6b5d7b | 1414 | |
60810e54 KM |
1415 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
1416 | struct nfsd4_channel_attrs *battrs) | |
5b6feee9 | 1417 | { |
60810e54 KM |
1418 | int numslots = fattrs->maxreqs; |
1419 | int slotsize = slot_bytes(fattrs); | |
5b6feee9 BF |
1420 | struct nfsd4_session *new; |
1421 | int mem, i; | |
a649637c | 1422 | |
5b6feee9 BF |
1423 | BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *) |
1424 | + sizeof(struct nfsd4_session) > PAGE_SIZE); | |
1425 | mem = numslots * sizeof(struct nfsd4_slot *); | |
ec6b5d7b | 1426 | |
5b6feee9 BF |
1427 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
1428 | if (!new) | |
1429 | return NULL; | |
557ce264 | 1430 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
5b6feee9 | 1431 | for (i = 0; i < numslots; i++) { |
55c760cf | 1432 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
5b6feee9 | 1433 | if (!new->se_slots[i]) |
557ce264 | 1434 | goto out_free; |
557ce264 | 1435 | } |
60810e54 KM |
1436 | |
1437 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); | |
1438 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); | |
1439 | ||
5b6feee9 BF |
1440 | return new; |
1441 | out_free: | |
1442 | while (i--) | |
1443 | kfree(new->se_slots[i]); | |
1444 | kfree(new); | |
1445 | return NULL; | |
ec6b5d7b AA |
1446 | } |
1447 | ||
19cf5c02 BF |
1448 | static void free_conn(struct nfsd4_conn *c) |
1449 | { | |
1450 | svc_xprt_put(c->cn_xprt); | |
1451 | kfree(c); | |
1452 | } | |
ec6b5d7b | 1453 | |
19cf5c02 BF |
1454 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
1455 | { | |
1456 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); | |
1457 | struct nfs4_client *clp = c->cn_session->se_client; | |
ec6b5d7b | 1458 | |
19cf5c02 BF |
1459 | spin_lock(&clp->cl_lock); |
1460 | if (!list_empty(&c->cn_persession)) { | |
1461 | list_del(&c->cn_persession); | |
1462 | free_conn(c); | |
1463 | } | |
eea49806 | 1464 | nfsd4_probe_callback(clp); |
2e4b7239 | 1465 | spin_unlock(&clp->cl_lock); |
19cf5c02 | 1466 | } |
ec6b5d7b | 1467 | |
d29c374c | 1468 | static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) |
c7662518 | 1469 | { |
c7662518 | 1470 | struct nfsd4_conn *conn; |
ec6b5d7b | 1471 | |
c7662518 BF |
1472 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
1473 | if (!conn) | |
db90681d | 1474 | return NULL; |
c7662518 BF |
1475 | svc_xprt_get(rqstp->rq_xprt); |
1476 | conn->cn_xprt = rqstp->rq_xprt; | |
d29c374c | 1477 | conn->cn_flags = flags; |
db90681d BF |
1478 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
1479 | return conn; | |
1480 | } | |
a649637c | 1481 | |
328ead28 BF |
1482 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
1483 | { | |
1484 | conn->cn_session = ses; | |
1485 | list_add(&conn->cn_persession, &ses->se_conns); | |
ec6b5d7b AA |
1486 | } |
1487 | ||
db90681d | 1488 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
557ce264 | 1489 | { |
db90681d | 1490 | struct nfs4_client *clp = ses->se_client; |
557ce264 | 1491 | |
c7662518 | 1492 | spin_lock(&clp->cl_lock); |
328ead28 | 1493 | __nfsd4_hash_conn(conn, ses); |
c7662518 | 1494 | spin_unlock(&clp->cl_lock); |
557ce264 AA |
1495 | } |
1496 | ||
21b75b01 | 1497 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
efe0cb6d | 1498 | { |
19cf5c02 | 1499 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
21b75b01 | 1500 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
efe0cb6d BF |
1501 | } |
1502 | ||
e1ff371f | 1503 | static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) |
ec6b5d7b | 1504 | { |
21b75b01 | 1505 | int ret; |
ec6b5d7b | 1506 | |
db90681d | 1507 | nfsd4_hash_conn(conn, ses); |
21b75b01 BF |
1508 | ret = nfsd4_register_conn(conn); |
1509 | if (ret) | |
1510 | /* oops; xprt is already down: */ | |
1511 | nfsd4_conn_lost(&conn->cn_xpt_user); | |
57a37144 BF |
1512 | /* We may have gained or lost a callback channel: */ |
1513 | nfsd4_probe_callback_sync(ses->se_client); | |
c7662518 | 1514 | } |
ec6b5d7b | 1515 | |
e1ff371f | 1516 | static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) |
1d1bc8f2 BF |
1517 | { |
1518 | u32 dir = NFS4_CDFC4_FORE; | |
1519 | ||
e1ff371f | 1520 | if (cses->flags & SESSION4_BACK_CHAN) |
1d1bc8f2 | 1521 | dir |= NFS4_CDFC4_BACK; |
e1ff371f | 1522 | return alloc_conn(rqstp, dir); |
1d1bc8f2 BF |
1523 | } |
1524 | ||
1525 | /* must be called under client_lock */ | |
19cf5c02 | 1526 | static void nfsd4_del_conns(struct nfsd4_session *s) |
c7662518 | 1527 | { |
19cf5c02 BF |
1528 | struct nfs4_client *clp = s->se_client; |
1529 | struct nfsd4_conn *c; | |
ec6b5d7b | 1530 | |
19cf5c02 BF |
1531 | spin_lock(&clp->cl_lock); |
1532 | while (!list_empty(&s->se_conns)) { | |
1533 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); | |
1534 | list_del_init(&c->cn_persession); | |
1535 | spin_unlock(&clp->cl_lock); | |
557ce264 | 1536 | |
19cf5c02 BF |
1537 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
1538 | free_conn(c); | |
ec6b5d7b | 1539 | |
19cf5c02 BF |
1540 | spin_lock(&clp->cl_lock); |
1541 | } | |
1542 | spin_unlock(&clp->cl_lock); | |
c7662518 | 1543 | } |
ec6b5d7b | 1544 | |
1377b69e BF |
1545 | static void __free_session(struct nfsd4_session *ses) |
1546 | { | |
1377b69e BF |
1547 | free_session_slots(ses); |
1548 | kfree(ses); | |
1549 | } | |
1550 | ||
66b2b9b2 | 1551 | static void free_session(struct nfsd4_session *ses) |
c7662518 | 1552 | { |
19cf5c02 | 1553 | nfsd4_del_conns(ses); |
55c760cf | 1554 | nfsd4_put_drc_mem(&ses->se_fchannel); |
1377b69e | 1555 | __free_session(ses); |
c7662518 BF |
1556 | } |
1557 | ||
135ae827 | 1558 | static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) |
a827bcb2 | 1559 | { |
a827bcb2 | 1560 | int idx; |
1872de0e | 1561 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
a827bcb2 | 1562 | |
ec6b5d7b AA |
1563 | new->se_client = clp; |
1564 | gen_sessionid(new); | |
ec6b5d7b | 1565 | |
c7662518 BF |
1566 | INIT_LIST_HEAD(&new->se_conns); |
1567 | ||
ac7c46f2 | 1568 | new->se_cb_seq_nr = 1; |
ec6b5d7b | 1569 | new->se_flags = cses->flags; |
8b5ce5cd | 1570 | new->se_cb_prog = cses->callback_prog; |
c6bb3ca2 | 1571 | new->se_cb_sec = cses->cb_sec; |
66b2b9b2 | 1572 | atomic_set(&new->se_ref, 0); |
5b6feee9 | 1573 | idx = hash_sessionid(&new->se_sessionid); |
1872de0e | 1574 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
4c649378 | 1575 | spin_lock(&clp->cl_lock); |
ec6b5d7b | 1576 | list_add(&new->se_perclnt, &clp->cl_sessions); |
4c649378 | 1577 | spin_unlock(&clp->cl_lock); |
60810e54 | 1578 | |
b0d2e42c | 1579 | { |
edd76786 | 1580 | struct sockaddr *sa = svc_addr(rqstp); |
dcbeaa68 BF |
1581 | /* |
1582 | * This is a little silly; with sessions there's no real | |
1583 | * use for the callback address. Use the peer address | |
1584 | * as a reasonable default for now, but consider fixing | |
1585 | * the rpc client not to require an address in the | |
1586 | * future: | |
1587 | */ | |
edd76786 BF |
1588 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
1589 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); | |
edd76786 | 1590 | } |
ec6b5d7b AA |
1591 | } |
1592 | ||
9089f1b4 | 1593 | /* caller must hold client_lock */ |
5282fd72 | 1594 | static struct nfsd4_session * |
d4e19e70 | 1595 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
5282fd72 ME |
1596 | { |
1597 | struct nfsd4_session *elem; | |
1598 | int idx; | |
1872de0e | 1599 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
5282fd72 | 1600 | |
0a880a28 TM |
1601 | lockdep_assert_held(&nn->client_lock); |
1602 | ||
5282fd72 ME |
1603 | dump_sessionid(__func__, sessionid); |
1604 | idx = hash_sessionid(sessionid); | |
5282fd72 | 1605 | /* Search in the appropriate list */ |
1872de0e | 1606 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
5282fd72 ME |
1607 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
1608 | NFS4_MAX_SESSIONID_LEN)) { | |
1609 | return elem; | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | dprintk("%s: session not found\n", __func__); | |
1614 | return NULL; | |
1615 | } | |
1616 | ||
d4e19e70 TM |
1617 | static struct nfsd4_session * |
1618 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, | |
1619 | __be32 *ret) | |
1620 | { | |
1621 | struct nfsd4_session *session; | |
1622 | __be32 status = nfserr_badsession; | |
1623 | ||
1624 | session = __find_in_sessionid_hashtbl(sessionid, net); | |
1625 | if (!session) | |
1626 | goto out; | |
1627 | status = nfsd4_get_session_locked(session); | |
1628 | if (status) | |
1629 | session = NULL; | |
1630 | out: | |
1631 | *ret = status; | |
1632 | return session; | |
1633 | } | |
1634 | ||
9089f1b4 | 1635 | /* caller must hold client_lock */ |
7116ed6b | 1636 | static void |
5282fd72 | 1637 | unhash_session(struct nfsd4_session *ses) |
7116ed6b | 1638 | { |
0a880a28 TM |
1639 | struct nfs4_client *clp = ses->se_client; |
1640 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
1641 | ||
1642 | lockdep_assert_held(&nn->client_lock); | |
1643 | ||
7116ed6b | 1644 | list_del(&ses->se_hash); |
4c649378 | 1645 | spin_lock(&ses->se_client->cl_lock); |
7116ed6b | 1646 | list_del(&ses->se_perclnt); |
4c649378 | 1647 | spin_unlock(&ses->se_client->cl_lock); |
5282fd72 ME |
1648 | } |
1649 | ||
1da177e4 LT |
1650 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
1651 | static int | |
2c142baa | 1652 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
1da177e4 | 1653 | { |
bbc7f33a BF |
1654 | /* |
1655 | * We're assuming the clid was not given out from a boot | |
1656 | * precisely 2^32 (about 136 years) before this one. That seems | |
1657 | * a safe assumption: | |
1658 | */ | |
1659 | if (clid->cl_boot == (u32)nn->boot_time) | |
1da177e4 | 1660 | return 0; |
60adfc50 | 1661 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
2c142baa | 1662 | clid->cl_boot, clid->cl_id, nn->boot_time); |
1da177e4 LT |
1663 | return 1; |
1664 | } | |
1665 | ||
1666 | /* | |
1667 | * XXX Should we use a slab cache ? | |
1668 | * This type of memory management is somewhat inefficient, but we use it | |
1669 | * anyway since SETCLIENTID is not a common operation. | |
1670 | */ | |
35bba9a3 | 1671 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
1da177e4 LT |
1672 | { |
1673 | struct nfs4_client *clp; | |
d4f0489f | 1674 | int i; |
1da177e4 | 1675 | |
35bba9a3 BF |
1676 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
1677 | if (clp == NULL) | |
1678 | return NULL; | |
67114fe6 | 1679 | clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL); |
d4f0489f TM |
1680 | if (clp->cl_name.data == NULL) |
1681 | goto err_no_name; | |
1682 | clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) * | |
1683 | OWNER_HASH_SIZE, GFP_KERNEL); | |
1684 | if (!clp->cl_ownerstr_hashtbl) | |
1685 | goto err_no_hashtbl; | |
1686 | for (i = 0; i < OWNER_HASH_SIZE; i++) | |
1687 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); | |
35bba9a3 | 1688 | clp->cl_name.len = name.len; |
5694c93e TM |
1689 | INIT_LIST_HEAD(&clp->cl_sessions); |
1690 | idr_init(&clp->cl_stateids); | |
1691 | atomic_set(&clp->cl_refcount, 0); | |
1692 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; | |
1693 | INIT_LIST_HEAD(&clp->cl_idhash); | |
1694 | INIT_LIST_HEAD(&clp->cl_openowners); | |
1695 | INIT_LIST_HEAD(&clp->cl_delegations); | |
1696 | INIT_LIST_HEAD(&clp->cl_lru); | |
5694c93e | 1697 | INIT_LIST_HEAD(&clp->cl_revoked); |
9cf514cc CH |
1698 | #ifdef CONFIG_NFSD_PNFS |
1699 | INIT_LIST_HEAD(&clp->cl_lo_states); | |
1700 | #endif | |
5694c93e TM |
1701 | spin_lock_init(&clp->cl_lock); |
1702 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); | |
1da177e4 | 1703 | return clp; |
d4f0489f TM |
1704 | err_no_hashtbl: |
1705 | kfree(clp->cl_name.data); | |
1706 | err_no_name: | |
1707 | kfree(clp); | |
1708 | return NULL; | |
1da177e4 LT |
1709 | } |
1710 | ||
4dd86e15 | 1711 | static void |
1da177e4 LT |
1712 | free_client(struct nfs4_client *clp) |
1713 | { | |
792c95dd BF |
1714 | while (!list_empty(&clp->cl_sessions)) { |
1715 | struct nfsd4_session *ses; | |
1716 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, | |
1717 | se_perclnt); | |
1718 | list_del(&ses->se_perclnt); | |
66b2b9b2 BF |
1719 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
1720 | free_session(ses); | |
792c95dd | 1721 | } |
4cb57e30 | 1722 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
03a4e1f6 | 1723 | free_svc_cred(&clp->cl_cred); |
d4f0489f | 1724 | kfree(clp->cl_ownerstr_hashtbl); |
1da177e4 | 1725 | kfree(clp->cl_name.data); |
2d32b29a | 1726 | idr_destroy(&clp->cl_stateids); |
1da177e4 LT |
1727 | kfree(clp); |
1728 | } | |
1729 | ||
84d38ac9 | 1730 | /* must be called under the client_lock */ |
4beb345b | 1731 | static void |
84d38ac9 BH |
1732 | unhash_client_locked(struct nfs4_client *clp) |
1733 | { | |
4beb345b | 1734 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
792c95dd BF |
1735 | struct nfsd4_session *ses; |
1736 | ||
0a880a28 TM |
1737 | lockdep_assert_held(&nn->client_lock); |
1738 | ||
4beb345b TM |
1739 | /* Mark the client as expired! */ |
1740 | clp->cl_time = 0; | |
1741 | /* Make it invisible */ | |
1742 | if (!list_empty(&clp->cl_idhash)) { | |
1743 | list_del_init(&clp->cl_idhash); | |
1744 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) | |
1745 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); | |
1746 | else | |
1747 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); | |
1748 | } | |
1749 | list_del_init(&clp->cl_lru); | |
4c649378 | 1750 | spin_lock(&clp->cl_lock); |
792c95dd BF |
1751 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
1752 | list_del_init(&ses->se_hash); | |
4c649378 | 1753 | spin_unlock(&clp->cl_lock); |
84d38ac9 BH |
1754 | } |
1755 | ||
1da177e4 | 1756 | static void |
4beb345b TM |
1757 | unhash_client(struct nfs4_client *clp) |
1758 | { | |
1759 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); | |
1760 | ||
1761 | spin_lock(&nn->client_lock); | |
1762 | unhash_client_locked(clp); | |
1763 | spin_unlock(&nn->client_lock); | |
1764 | } | |
1765 | ||
97403d95 JL |
1766 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
1767 | { | |
1768 | if (atomic_read(&clp->cl_refcount)) | |
1769 | return nfserr_jukebox; | |
1770 | unhash_client_locked(clp); | |
1771 | return nfs_ok; | |
1772 | } | |
1773 | ||
4beb345b TM |
1774 | static void |
1775 | __destroy_client(struct nfs4_client *clp) | |
1da177e4 | 1776 | { |
fe0750e5 | 1777 | struct nfs4_openowner *oo; |
1da177e4 | 1778 | struct nfs4_delegation *dp; |
1da177e4 LT |
1779 | struct list_head reaplist; |
1780 | ||
1da177e4 | 1781 | INIT_LIST_HEAD(&reaplist); |
cdc97505 | 1782 | spin_lock(&state_lock); |
ea1da636 N |
1783 | while (!list_empty(&clp->cl_delegations)) { |
1784 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); | |
3fcbbd24 | 1785 | WARN_ON(!unhash_delegation_locked(dp)); |
42690676 | 1786 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 1787 | } |
cdc97505 | 1788 | spin_unlock(&state_lock); |
1da177e4 LT |
1789 | while (!list_empty(&reaplist)) { |
1790 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); | |
42690676 | 1791 | list_del_init(&dp->dl_recall_lru); |
8287f009 | 1792 | put_clnt_odstate(dp->dl_clnt_odstate); |
afbda402 | 1793 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
6011695d | 1794 | nfs4_put_stid(&dp->dl_stid); |
1da177e4 | 1795 | } |
2d4a532d | 1796 | while (!list_empty(&clp->cl_revoked)) { |
c876486b | 1797 | dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); |
2d4a532d | 1798 | list_del_init(&dp->dl_recall_lru); |
6011695d | 1799 | nfs4_put_stid(&dp->dl_stid); |
956c4fee | 1800 | } |
ea1da636 | 1801 | while (!list_empty(&clp->cl_openowners)) { |
fe0750e5 | 1802 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
b5971afa | 1803 | nfs4_get_stateowner(&oo->oo_owner); |
fe0750e5 | 1804 | release_openowner(oo); |
1da177e4 | 1805 | } |
9cf514cc | 1806 | nfsd4_return_all_client_layouts(clp); |
6ff8da08 | 1807 | nfsd4_shutdown_callback(clp); |
84d38ac9 BH |
1808 | if (clp->cl_cb_conn.cb_xprt) |
1809 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); | |
221a6876 | 1810 | free_client(clp); |
1da177e4 LT |
1811 | } |
1812 | ||
4beb345b TM |
1813 | static void |
1814 | destroy_client(struct nfs4_client *clp) | |
1815 | { | |
1816 | unhash_client(clp); | |
1817 | __destroy_client(clp); | |
1818 | } | |
1819 | ||
0d22f68f BF |
1820 | static void expire_client(struct nfs4_client *clp) |
1821 | { | |
4beb345b | 1822 | unhash_client(clp); |
0d22f68f | 1823 | nfsd4_client_record_remove(clp); |
4beb345b | 1824 | __destroy_client(clp); |
0d22f68f BF |
1825 | } |
1826 | ||
35bba9a3 BF |
1827 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
1828 | { | |
1829 | memcpy(target->cl_verifier.data, source->data, | |
1830 | sizeof(target->cl_verifier.data)); | |
1da177e4 LT |
1831 | } |
1832 | ||
35bba9a3 BF |
1833 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
1834 | { | |
1da177e4 LT |
1835 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
1836 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; | |
1837 | } | |
1838 | ||
50043859 | 1839 | int strdup_if_nonnull(char **target, char *source) |
35bba9a3 | 1840 | { |
50043859 BF |
1841 | if (source) { |
1842 | *target = kstrdup(source, GFP_KERNEL); | |
1843 | if (!*target) | |
03a4e1f6 BF |
1844 | return -ENOMEM; |
1845 | } else | |
50043859 BF |
1846 | *target = NULL; |
1847 | return 0; | |
1848 | } | |
1849 | ||
1850 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) | |
1851 | { | |
1852 | int ret; | |
1853 | ||
1854 | ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal); | |
414ca017 BF |
1855 | if (ret) |
1856 | return ret; | |
1857 | ret = strdup_if_nonnull(&target->cr_raw_principal, | |
1858 | source->cr_raw_principal); | |
50043859 BF |
1859 | if (ret) |
1860 | return ret; | |
d5497fc6 | 1861 | target->cr_flavor = source->cr_flavor; |
1da177e4 LT |
1862 | target->cr_uid = source->cr_uid; |
1863 | target->cr_gid = source->cr_gid; | |
1864 | target->cr_group_info = source->cr_group_info; | |
1865 | get_group_info(target->cr_group_info); | |
0dc1531a BF |
1866 | target->cr_gss_mech = source->cr_gss_mech; |
1867 | if (source->cr_gss_mech) | |
1868 | gss_mech_get(source->cr_gss_mech); | |
03a4e1f6 | 1869 | return 0; |
1da177e4 LT |
1870 | } |
1871 | ||
ef17af2a | 1872 | static int |
ac55fdc4 JL |
1873 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
1874 | { | |
ef17af2a RV |
1875 | if (o1->len < o2->len) |
1876 | return -1; | |
1877 | if (o1->len > o2->len) | |
1878 | return 1; | |
1879 | return memcmp(o1->data, o2->data, o1->len); | |
ac55fdc4 JL |
1880 | } |
1881 | ||
35bba9a3 | 1882 | static int same_name(const char *n1, const char *n2) |
599e0a22 | 1883 | { |
a55370a3 | 1884 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
1da177e4 LT |
1885 | } |
1886 | ||
1887 | static int | |
599e0a22 BF |
1888 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
1889 | { | |
1890 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); | |
1da177e4 LT |
1891 | } |
1892 | ||
1893 | static int | |
599e0a22 BF |
1894 | same_clid(clientid_t *cl1, clientid_t *cl2) |
1895 | { | |
1896 | return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); | |
1da177e4 LT |
1897 | } |
1898 | ||
8fbba96e BF |
1899 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
1900 | { | |
1901 | int i; | |
1902 | ||
1903 | if (g1->ngroups != g2->ngroups) | |
1904 | return false; | |
1905 | for (i=0; i<g1->ngroups; i++) | |
6fab8779 | 1906 | if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i))) |
8fbba96e BF |
1907 | return false; |
1908 | return true; | |
1909 | } | |
1910 | ||
68eb3508 BF |
1911 | /* |
1912 | * RFC 3530 language requires clid_inuse be returned when the | |
1913 | * "principal" associated with a requests differs from that previously | |
1914 | * used. We use uid, gid's, and gss principal string as our best | |
1915 | * approximation. We also don't want to allow non-gss use of a client | |
1916 | * established using gss: in theory cr_principal should catch that | |
1917 | * change, but in practice cr_principal can be null even in the gss case | |
1918 | * since gssd doesn't always pass down a principal string. | |
1919 | */ | |
1920 | static bool is_gss_cred(struct svc_cred *cr) | |
1921 | { | |
1922 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ | |
1923 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); | |
1924 | } | |
1925 | ||
1926 | ||
5559b50a | 1927 | static bool |
599e0a22 BF |
1928 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
1929 | { | |
68eb3508 | 1930 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
6fab8779 EB |
1931 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
1932 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) | |
8fbba96e BF |
1933 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
1934 | return false; | |
1935 | if (cr1->cr_principal == cr2->cr_principal) | |
1936 | return true; | |
1937 | if (!cr1->cr_principal || !cr2->cr_principal) | |
1938 | return false; | |
5559b50a | 1939 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
1da177e4 LT |
1940 | } |
1941 | ||
57266a6e BF |
1942 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
1943 | { | |
1944 | struct svc_cred *cr = &rqstp->rq_cred; | |
1945 | u32 service; | |
1946 | ||
c4720591 BF |
1947 | if (!cr->cr_gss_mech) |
1948 | return false; | |
57266a6e BF |
1949 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
1950 | return service == RPC_GSS_SVC_INTEGRITY || | |
1951 | service == RPC_GSS_SVC_PRIVACY; | |
1952 | } | |
1953 | ||
dedeb13f | 1954 | bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
57266a6e BF |
1955 | { |
1956 | struct svc_cred *cr = &rqstp->rq_cred; | |
1957 | ||
1958 | if (!cl->cl_mach_cred) | |
1959 | return true; | |
1960 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) | |
1961 | return false; | |
1962 | if (!svc_rqst_integrity_protected(rqstp)) | |
1963 | return false; | |
414ca017 BF |
1964 | if (cl->cl_cred.cr_raw_principal) |
1965 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, | |
1966 | cr->cr_raw_principal); | |
57266a6e BF |
1967 | if (!cr->cr_principal) |
1968 | return false; | |
1969 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); | |
1970 | } | |
1971 | ||
294ac32e | 1972 | static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) |
deda2faa | 1973 | { |
ab4684d1 | 1974 | __be32 verf[2]; |
1da177e4 | 1975 | |
f419992c JL |
1976 | /* |
1977 | * This is opaque to client, so no need to byte-swap. Use | |
1978 | * __force to keep sparse happy | |
1979 | */ | |
1980 | verf[0] = (__force __be32)get_seconds(); | |
19311aa8 | 1981 | verf[1] = (__force __be32)nn->clverifier_counter++; |
ab4684d1 | 1982 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
1da177e4 LT |
1983 | } |
1984 | ||
294ac32e JL |
1985 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
1986 | { | |
1987 | clp->cl_clientid.cl_boot = nn->boot_time; | |
1988 | clp->cl_clientid.cl_id = nn->clientid_counter++; | |
1989 | gen_confirm(clp, nn); | |
1990 | } | |
1991 | ||
4770d722 JL |
1992 | static struct nfs4_stid * |
1993 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) | |
4581d140 | 1994 | { |
3abdb607 BF |
1995 | struct nfs4_stid *ret; |
1996 | ||
1997 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); | |
1998 | if (!ret || !ret->sc_type) | |
1999 | return NULL; | |
2000 | return ret; | |
4d71ab87 BF |
2001 | } |
2002 | ||
4770d722 JL |
2003 | static struct nfs4_stid * |
2004 | find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask) | |
f459e453 BF |
2005 | { |
2006 | struct nfs4_stid *s; | |
4d71ab87 | 2007 | |
4770d722 JL |
2008 | spin_lock(&cl->cl_lock); |
2009 | s = find_stateid_locked(cl, t); | |
2d3f9668 TM |
2010 | if (s != NULL) { |
2011 | if (typemask & s->sc_type) | |
2012 | atomic_inc(&s->sc_count); | |
2013 | else | |
2014 | s = NULL; | |
2015 | } | |
4770d722 JL |
2016 | spin_unlock(&cl->cl_lock); |
2017 | return s; | |
4581d140 BF |
2018 | } |
2019 | ||
2216d449 | 2020 | static struct nfs4_client *create_client(struct xdr_netobj name, |
b09333c4 RL |
2021 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
2022 | { | |
2023 | struct nfs4_client *clp; | |
2024 | struct sockaddr *sa = svc_addr(rqstp); | |
03a4e1f6 | 2025 | int ret; |
c212cecf | 2026 | struct net *net = SVC_NET(rqstp); |
b09333c4 RL |
2027 | |
2028 | clp = alloc_client(name); | |
2029 | if (clp == NULL) | |
2030 | return NULL; | |
2031 | ||
03a4e1f6 BF |
2032 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
2033 | if (ret) { | |
03a4e1f6 | 2034 | free_client(clp); |
03a4e1f6 | 2035 | return NULL; |
b09333c4 | 2036 | } |
0162ac2b | 2037 | nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); |
07cd4909 | 2038 | clp->cl_time = get_seconds(); |
b09333c4 | 2039 | clear_bit(0, &clp->cl_cb_slot_busy); |
b09333c4 RL |
2040 | copy_verf(clp, verf); |
2041 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); | |
edd76786 | 2042 | clp->cl_cb_session = NULL; |
c212cecf | 2043 | clp->net = net; |
b09333c4 RL |
2044 | return clp; |
2045 | } | |
2046 | ||
fd39ca9a | 2047 | static void |
ac55fdc4 JL |
2048 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
2049 | { | |
2050 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
2051 | struct nfs4_client *clp; | |
2052 | ||
2053 | while (*new) { | |
2054 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); | |
2055 | parent = *new; | |
2056 | ||
2057 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) | |
2058 | new = &((*new)->rb_left); | |
2059 | else | |
2060 | new = &((*new)->rb_right); | |
2061 | } | |
2062 | ||
2063 | rb_link_node(&new_clp->cl_namenode, parent, new); | |
2064 | rb_insert_color(&new_clp->cl_namenode, root); | |
2065 | } | |
2066 | ||
2067 | static struct nfs4_client * | |
2068 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) | |
2069 | { | |
ef17af2a | 2070 | int cmp; |
ac55fdc4 JL |
2071 | struct rb_node *node = root->rb_node; |
2072 | struct nfs4_client *clp; | |
2073 | ||
2074 | while (node) { | |
2075 | clp = rb_entry(node, struct nfs4_client, cl_namenode); | |
2076 | cmp = compare_blob(&clp->cl_name, name); | |
2077 | if (cmp > 0) | |
2078 | node = node->rb_left; | |
2079 | else if (cmp < 0) | |
2080 | node = node->rb_right; | |
2081 | else | |
2082 | return clp; | |
2083 | } | |
2084 | return NULL; | |
2085 | } | |
2086 | ||
2087 | static void | |
2088 | add_to_unconfirmed(struct nfs4_client *clp) | |
1da177e4 LT |
2089 | { |
2090 | unsigned int idhashval; | |
0a7ec377 | 2091 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 | 2092 | |
0a880a28 TM |
2093 | lockdep_assert_held(&nn->client_lock); |
2094 | ||
ac55fdc4 | 2095 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
a99454aa | 2096 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
1da177e4 | 2097 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
0a7ec377 | 2098 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
3dbacee6 | 2099 | renew_client_locked(clp); |
1da177e4 LT |
2100 | } |
2101 | ||
fd39ca9a | 2102 | static void |
1da177e4 LT |
2103 | move_to_confirmed(struct nfs4_client *clp) |
2104 | { | |
2105 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); | |
8daae4dc | 2106 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
1da177e4 | 2107 | |
0a880a28 TM |
2108 | lockdep_assert_held(&nn->client_lock); |
2109 | ||
1da177e4 | 2110 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
8daae4dc | 2111 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
a99454aa | 2112 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
382a62e7 | 2113 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
ac55fdc4 | 2114 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
3dbacee6 | 2115 | renew_client_locked(clp); |
1da177e4 LT |
2116 | } |
2117 | ||
2118 | static struct nfs4_client * | |
bfa85e83 | 2119 | find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) |
1da177e4 LT |
2120 | { |
2121 | struct nfs4_client *clp; | |
2122 | unsigned int idhashval = clientid_hashval(clid->cl_id); | |
2123 | ||
bfa85e83 | 2124 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
a50d2ad1 | 2125 | if (same_clid(&clp->cl_clientid, clid)) { |
d15c077e BF |
2126 | if ((bool)clp->cl_minorversion != sessions) |
2127 | return NULL; | |
3dbacee6 | 2128 | renew_client_locked(clp); |
1da177e4 | 2129 | return clp; |
a50d2ad1 | 2130 | } |
1da177e4 LT |
2131 | } |
2132 | return NULL; | |
2133 | } | |
2134 | ||
bfa85e83 BF |
2135 | static struct nfs4_client * |
2136 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) | |
2137 | { | |
2138 | struct list_head *tbl = nn->conf_id_hashtbl; | |
2139 | ||
0a880a28 | 2140 | lockdep_assert_held(&nn->client_lock); |
bfa85e83 BF |
2141 | return find_client_in_id_table(tbl, clid, sessions); |
2142 | } | |
2143 | ||
1da177e4 | 2144 | static struct nfs4_client * |
0a7ec377 | 2145 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
1da177e4 | 2146 | { |
bfa85e83 | 2147 | struct list_head *tbl = nn->unconf_id_hashtbl; |
1da177e4 | 2148 | |
0a880a28 | 2149 | lockdep_assert_held(&nn->client_lock); |
bfa85e83 | 2150 | return find_client_in_id_table(tbl, clid, sessions); |
1da177e4 LT |
2151 | } |
2152 | ||
6e5f15c9 | 2153 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
a1bcecd2 | 2154 | { |
6e5f15c9 | 2155 | return clp->cl_exchange_flags != 0; |
e203d506 | 2156 | } |
a1bcecd2 | 2157 | |
28ce6054 | 2158 | static struct nfs4_client * |
382a62e7 | 2159 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
28ce6054 | 2160 | { |
0a880a28 | 2161 | lockdep_assert_held(&nn->client_lock); |
382a62e7 | 2162 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
28ce6054 N |
2163 | } |
2164 | ||
2165 | static struct nfs4_client * | |
a99454aa | 2166 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
28ce6054 | 2167 | { |
0a880a28 | 2168 | lockdep_assert_held(&nn->client_lock); |
a99454aa | 2169 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
28ce6054 N |
2170 | } |
2171 | ||
fd39ca9a | 2172 | static void |
6f3d772f | 2173 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) |
1da177e4 | 2174 | { |
07263f1e | 2175 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
6f3d772f TU |
2176 | struct sockaddr *sa = svc_addr(rqstp); |
2177 | u32 scopeid = rpc_get_scope_id(sa); | |
7077ecba JL |
2178 | unsigned short expected_family; |
2179 | ||
2180 | /* Currently, we only support tcp and tcp6 for the callback channel */ | |
2181 | if (se->se_callback_netid_len == 3 && | |
2182 | !memcmp(se->se_callback_netid_val, "tcp", 3)) | |
2183 | expected_family = AF_INET; | |
2184 | else if (se->se_callback_netid_len == 4 && | |
2185 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) | |
2186 | expected_family = AF_INET6; | |
2187 | else | |
1da177e4 LT |
2188 | goto out_err; |
2189 | ||
c212cecf | 2190 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
aa9a4ec7 | 2191 | se->se_callback_addr_len, |
07263f1e BF |
2192 | (struct sockaddr *)&conn->cb_addr, |
2193 | sizeof(conn->cb_addr)); | |
aa9a4ec7 | 2194 | |
07263f1e | 2195 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
1da177e4 | 2196 | goto out_err; |
aa9a4ec7 | 2197 | |
07263f1e BF |
2198 | if (conn->cb_addr.ss_family == AF_INET6) |
2199 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; | |
fbf4665f | 2200 | |
07263f1e BF |
2201 | conn->cb_prog = se->se_callback_prog; |
2202 | conn->cb_ident = se->se_callback_ident; | |
849a1cf1 | 2203 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
1da177e4 LT |
2204 | return; |
2205 | out_err: | |
07263f1e BF |
2206 | conn->cb_addr.ss_family = AF_UNSPEC; |
2207 | conn->cb_addrlen = 0; | |
849823c5 | 2208 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
1da177e4 LT |
2209 | "will not receive delegations\n", |
2210 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); | |
2211 | ||
1da177e4 LT |
2212 | return; |
2213 | } | |
2214 | ||
074fe897 | 2215 | /* |
067e1ace | 2216 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
074fe897 | 2217 | */ |
b607664e | 2218 | static void |
074fe897 | 2219 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
074fe897 | 2220 | { |
f5236013 | 2221 | struct xdr_buf *buf = resp->xdr.buf; |
557ce264 AA |
2222 | struct nfsd4_slot *slot = resp->cstate.slot; |
2223 | unsigned int base; | |
074fe897 | 2224 | |
557ce264 | 2225 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 2226 | |
557ce264 AA |
2227 | slot->sl_opcnt = resp->opcnt; |
2228 | slot->sl_status = resp->cstate.status; | |
074fe897 | 2229 | |
bf5c43c8 | 2230 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
bf864a31 | 2231 | if (nfsd4_not_cached(resp)) { |
557ce264 | 2232 | slot->sl_datalen = 0; |
bf864a31 | 2233 | return; |
074fe897 | 2234 | } |
f5236013 BF |
2235 | base = resp->cstate.data_offset; |
2236 | slot->sl_datalen = buf->len - base; | |
2237 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) | |
d3f03403 DC |
2238 | WARN(1, "%s: sessions DRC could not cache compound\n", |
2239 | __func__); | |
557ce264 | 2240 | return; |
074fe897 AA |
2241 | } |
2242 | ||
2243 | /* | |
abfabf8c AA |
2244 | * Encode the replay sequence operation from the slot values. |
2245 | * If cachethis is FALSE encode the uncached rep error on the next | |
2246 | * operation which sets resp->p and increments resp->opcnt for | |
2247 | * nfs4svc_encode_compoundres. | |
074fe897 | 2248 | * |
074fe897 | 2249 | */ |
abfabf8c AA |
2250 | static __be32 |
2251 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, | |
2252 | struct nfsd4_compoundres *resp) | |
074fe897 | 2253 | { |
abfabf8c AA |
2254 | struct nfsd4_op *op; |
2255 | struct nfsd4_slot *slot = resp->cstate.slot; | |
bf864a31 | 2256 | |
abfabf8c AA |
2257 | /* Encode the replayed sequence operation */ |
2258 | op = &args->ops[resp->opcnt - 1]; | |
2259 | nfsd4_encode_operation(resp, op); | |
bf864a31 | 2260 | |
abfabf8c | 2261 | /* Return nfserr_retry_uncached_rep in next operation. */ |
73e79482 | 2262 | if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) { |
abfabf8c AA |
2263 | op = &args->ops[resp->opcnt++]; |
2264 | op->status = nfserr_retry_uncached_rep; | |
2265 | nfsd4_encode_operation(resp, op); | |
074fe897 | 2266 | } |
abfabf8c | 2267 | return op->status; |
074fe897 AA |
2268 | } |
2269 | ||
2270 | /* | |
557ce264 AA |
2271 | * The sequence operation is not cached because we can use the slot and |
2272 | * session values. | |
074fe897 | 2273 | */ |
3ca2eb98 | 2274 | static __be32 |
bf864a31 AA |
2275 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
2276 | struct nfsd4_sequence *seq) | |
074fe897 | 2277 | { |
557ce264 | 2278 | struct nfsd4_slot *slot = resp->cstate.slot; |
f5236013 BF |
2279 | struct xdr_stream *xdr = &resp->xdr; |
2280 | __be32 *p; | |
074fe897 AA |
2281 | __be32 status; |
2282 | ||
557ce264 | 2283 | dprintk("--> %s slot %p\n", __func__, slot); |
074fe897 | 2284 | |
abfabf8c | 2285 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
0da7b19c | 2286 | if (status) |
abfabf8c | 2287 | return status; |
074fe897 | 2288 | |
f5236013 BF |
2289 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
2290 | if (!p) { | |
2291 | WARN_ON_ONCE(1); | |
2292 | return nfserr_serverfault; | |
2293 | } | |
2294 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); | |
2295 | xdr_commit_encode(xdr); | |
074fe897 | 2296 | |
557ce264 | 2297 | resp->opcnt = slot->sl_opcnt; |
f5236013 | 2298 | return slot->sl_status; |
074fe897 AA |
2299 | } |
2300 | ||
0733d213 AA |
2301 | /* |
2302 | * Set the exchange_id flags returned by the server. | |
2303 | */ | |
2304 | static void | |
2305 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) | |
2306 | { | |
9cf514cc CH |
2307 | #ifdef CONFIG_NFSD_PNFS |
2308 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; | |
2309 | #else | |
0733d213 | 2310 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
9cf514cc | 2311 | #endif |
0733d213 AA |
2312 | |
2313 | /* Referrals are supported, Migration is not. */ | |
2314 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; | |
2315 | ||
2316 | /* set the wire flags to return to client. */ | |
2317 | clid->flags = new->cl_exchange_flags; | |
2318 | } | |
2319 | ||
4eaea134 BF |
2320 | static bool client_has_openowners(struct nfs4_client *clp) |
2321 | { | |
2322 | struct nfs4_openowner *oo; | |
2323 | ||
2324 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { | |
2325 | if (!list_empty(&oo->oo_owner.so_stateids)) | |
2326 | return true; | |
2327 | } | |
2328 | return false; | |
2329 | } | |
2330 | ||
631fc9ea BF |
2331 | static bool client_has_state(struct nfs4_client *clp) |
2332 | { | |
4eaea134 | 2333 | return client_has_openowners(clp) |
47e970be KM |
2334 | #ifdef CONFIG_NFSD_PNFS |
2335 | || !list_empty(&clp->cl_lo_states) | |
2336 | #endif | |
6eccece9 BF |
2337 | || !list_empty(&clp->cl_delegations) |
2338 | || !list_empty(&clp->cl_sessions); | |
631fc9ea BF |
2339 | } |
2340 | ||
069b6ad4 AA |
2341 | __be32 |
2342 | nfsd4_exchange_id(struct svc_rqst *rqstp, | |
2343 | struct nfsd4_compound_state *cstate, | |
2344 | struct nfsd4_exchange_id *exid) | |
2345 | { | |
3dbacee6 TM |
2346 | struct nfs4_client *conf, *new; |
2347 | struct nfs4_client *unconf = NULL; | |
57b7b43b | 2348 | __be32 status; |
363168b4 | 2349 | char addr_str[INET6_ADDRSTRLEN]; |
0733d213 | 2350 | nfs4_verifier verf = exid->verifier; |
363168b4 | 2351 | struct sockaddr *sa = svc_addr(rqstp); |
83e08fd4 | 2352 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
c212cecf | 2353 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
0733d213 | 2354 | |
363168b4 | 2355 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
0733d213 | 2356 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
363168b4 | 2357 | "ip_addr=%s flags %x, spa_how %d\n", |
0733d213 | 2358 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
363168b4 | 2359 | addr_str, exid->flags, exid->spa_how); |
0733d213 | 2360 | |
a084daf5 | 2361 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
0733d213 AA |
2362 | return nfserr_inval; |
2363 | ||
50c7b948 BF |
2364 | new = create_client(exid->clname, rqstp, &verf); |
2365 | if (new == NULL) | |
2366 | return nfserr_jukebox; | |
2367 | ||
0733d213 | 2368 | switch (exid->spa_how) { |
57266a6e | 2369 | case SP4_MACH_CRED: |
ed941643 AE |
2370 | exid->spo_must_enforce[0] = 0; |
2371 | exid->spo_must_enforce[1] = ( | |
2372 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
2373 | 1 << (OP_EXCHANGE_ID - 32) | | |
2374 | 1 << (OP_CREATE_SESSION - 32) | | |
2375 | 1 << (OP_DESTROY_SESSION - 32) | | |
2376 | 1 << (OP_DESTROY_CLIENTID - 32)); | |
2377 | ||
2378 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | | |
2379 | 1 << (OP_OPEN_DOWNGRADE) | | |
2380 | 1 << (OP_LOCKU) | | |
2381 | 1 << (OP_DELEGRETURN)); | |
2382 | ||
2383 | exid->spo_must_allow[1] &= ( | |
2384 | 1 << (OP_TEST_STATEID - 32) | | |
2385 | 1 << (OP_FREE_STATEID - 32)); | |
50c7b948 BF |
2386 | if (!svc_rqst_integrity_protected(rqstp)) { |
2387 | status = nfserr_inval; | |
2388 | goto out_nolock; | |
920dd9bb BF |
2389 | } |
2390 | /* | |
2391 | * Sometimes userspace doesn't give us a principal. | |
2392 | * Which is a bug, really. Anyway, we can't enforce | |
2393 | * MACH_CRED in that case, better to give up now: | |
2394 | */ | |
414ca017 BF |
2395 | if (!new->cl_cred.cr_principal && |
2396 | !new->cl_cred.cr_raw_principal) { | |
920dd9bb BF |
2397 | status = nfserr_serverfault; |
2398 | goto out_nolock; | |
50c7b948 BF |
2399 | } |
2400 | new->cl_mach_cred = true; | |
0733d213 AA |
2401 | case SP4_NONE: |
2402 | break; | |
063b0fb9 BF |
2403 | default: /* checked by xdr code */ |
2404 | WARN_ON_ONCE(1); | |
0733d213 | 2405 | case SP4_SSV: |
8edf4b02 KM |
2406 | status = nfserr_encr_alg_unsupp; |
2407 | goto out_nolock; | |
0733d213 AA |
2408 | } |
2409 | ||
2dbb269d | 2410 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
3dbacee6 | 2411 | spin_lock(&nn->client_lock); |
382a62e7 | 2412 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
0733d213 | 2413 | if (conf) { |
83e08fd4 BF |
2414 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
2415 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); | |
2416 | ||
136e658d BF |
2417 | if (update) { |
2418 | if (!clp_used_exchangeid(conf)) { /* buggy client */ | |
2dbb269d | 2419 | status = nfserr_inval; |
1a308118 BF |
2420 | goto out; |
2421 | } | |
dedeb13f | 2422 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
57266a6e BF |
2423 | status = nfserr_wrong_cred; |
2424 | goto out; | |
2425 | } | |
136e658d | 2426 | if (!creds_match) { /* case 9 */ |
ea236d07 | 2427 | status = nfserr_perm; |
136e658d BF |
2428 | goto out; |
2429 | } | |
2430 | if (!verfs_match) { /* case 8 */ | |
0733d213 AA |
2431 | status = nfserr_not_same; |
2432 | goto out; | |
2433 | } | |
136e658d BF |
2434 | /* case 6 */ |
2435 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; | |
136e658d | 2436 | goto out_copy; |
0733d213 | 2437 | } |
136e658d | 2438 | if (!creds_match) { /* case 3 */ |
631fc9ea BF |
2439 | if (client_has_state(conf)) { |
2440 | status = nfserr_clid_inuse; | |
0733d213 AA |
2441 | goto out; |
2442 | } | |
0733d213 AA |
2443 | goto out_new; |
2444 | } | |
136e658d | 2445 | if (verfs_match) { /* case 2 */ |
0f1ba0ef | 2446 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
136e658d BF |
2447 | goto out_copy; |
2448 | } | |
2449 | /* case 5, client reboot */ | |
3dbacee6 | 2450 | conf = NULL; |
136e658d | 2451 | goto out_new; |
6ddbbbfe MS |
2452 | } |
2453 | ||
2dbb269d | 2454 | if (update) { /* case 7 */ |
6ddbbbfe MS |
2455 | status = nfserr_noent; |
2456 | goto out; | |
0733d213 AA |
2457 | } |
2458 | ||
a99454aa | 2459 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
2dbb269d | 2460 | if (unconf) /* case 4, possible retry or client restart */ |
3dbacee6 | 2461 | unhash_client_locked(unconf); |
0733d213 | 2462 | |
2dbb269d | 2463 | /* case 1 (normal case) */ |
0733d213 | 2464 | out_new: |
fd699b8a JL |
2465 | if (conf) { |
2466 | status = mark_client_expired_locked(conf); | |
2467 | if (status) | |
2468 | goto out; | |
2469 | } | |
4f540e29 | 2470 | new->cl_minorversion = cstate->minorversion; |
ed941643 AE |
2471 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
2472 | new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; | |
0733d213 | 2473 | |
c212cecf | 2474 | gen_clid(new, nn); |
ac55fdc4 | 2475 | add_to_unconfirmed(new); |
3dbacee6 | 2476 | swap(new, conf); |
0733d213 | 2477 | out_copy: |
5cc40fd7 TM |
2478 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
2479 | exid->clientid.cl_id = conf->cl_clientid.cl_id; | |
0733d213 | 2480 | |
5cc40fd7 TM |
2481 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
2482 | nfsd4_set_ex_flags(conf, exid); | |
0733d213 AA |
2483 | |
2484 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", | |
5cc40fd7 | 2485 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
0733d213 AA |
2486 | status = nfs_ok; |
2487 | ||
2488 | out: | |
3dbacee6 | 2489 | spin_unlock(&nn->client_lock); |
50c7b948 | 2490 | out_nolock: |
5cc40fd7 | 2491 | if (new) |
3dbacee6 TM |
2492 | expire_client(new); |
2493 | if (unconf) | |
2494 | expire_client(unconf); | |
0733d213 | 2495 | return status; |
069b6ad4 AA |
2496 | } |
2497 | ||
57b7b43b | 2498 | static __be32 |
88e588d5 | 2499 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
b85d4c01 | 2500 | { |
88e588d5 AA |
2501 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
2502 | slot_seqid); | |
b85d4c01 BH |
2503 | |
2504 | /* The slot is in use, and no response has been sent. */ | |
88e588d5 AA |
2505 | if (slot_inuse) { |
2506 | if (seqid == slot_seqid) | |
b85d4c01 BH |
2507 | return nfserr_jukebox; |
2508 | else | |
2509 | return nfserr_seq_misordered; | |
2510 | } | |
f6d82485 | 2511 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
88e588d5 | 2512 | if (likely(seqid == slot_seqid + 1)) |
b85d4c01 | 2513 | return nfs_ok; |
88e588d5 | 2514 | if (seqid == slot_seqid) |
b85d4c01 | 2515 | return nfserr_replay_cache; |
b85d4c01 BH |
2516 | return nfserr_seq_misordered; |
2517 | } | |
2518 | ||
49557cc7 AA |
2519 | /* |
2520 | * Cache the create session result into the create session single DRC | |
2521 | * slot cache by saving the xdr structure. sl_seqid has been set. | |
2522 | * Do this for solo or embedded create session operations. | |
2523 | */ | |
2524 | static void | |
2525 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, | |
57b7b43b | 2526 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
49557cc7 AA |
2527 | { |
2528 | slot->sl_status = nfserr; | |
2529 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); | |
2530 | } | |
2531 | ||
2532 | static __be32 | |
2533 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, | |
2534 | struct nfsd4_clid_slot *slot) | |
2535 | { | |
2536 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); | |
2537 | return slot->sl_status; | |
2538 | } | |
2539 | ||
1b74c25b MJ |
2540 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
2541 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ | |
2542 | 1 + /* MIN tag is length with zero, only length */ \ | |
2543 | 3 + /* version, opcount, opcode */ \ | |
2544 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ | |
2545 | /* seqid, slotID, slotID, cache */ \ | |
2546 | 4 ) * sizeof(__be32)) | |
2547 | ||
2548 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ | |
2549 | 2 + /* verifier: AUTH_NULL, length 0 */\ | |
2550 | 1 + /* status */ \ | |
2551 | 1 + /* MIN tag is length with zero, only length */ \ | |
2552 | 3 + /* opcount, opcode, opstatus*/ \ | |
2553 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ | |
2554 | /* seqid, slotID, slotID, slotID, status */ \ | |
2555 | 5 ) * sizeof(__be32)) | |
2556 | ||
55c760cf | 2557 | static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
1b74c25b | 2558 | { |
55c760cf BF |
2559 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
2560 | ||
373cd409 BF |
2561 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
2562 | return nfserr_toosmall; | |
2563 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) | |
2564 | return nfserr_toosmall; | |
55c760cf BF |
2565 | ca->headerpadsz = 0; |
2566 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); | |
2567 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); | |
2568 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); | |
2569 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, | |
2570 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); | |
2571 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); | |
2572 | /* | |
2573 | * Note decreasing slot size below client's request may make it | |
2574 | * difficult for client to function correctly, whereas | |
2575 | * decreasing the number of slots will (just?) affect | |
2576 | * performance. When short on memory we therefore prefer to | |
2577 | * decrease number of slots instead of their size. Clients that | |
2578 | * request larger slots than they need will get poor results: | |
2579 | */ | |
2580 | ca->maxreqs = nfsd4_get_drc_mem(ca); | |
2581 | if (!ca->maxreqs) | |
2582 | return nfserr_jukebox; | |
2583 | ||
373cd409 | 2584 | return nfs_ok; |
1b74c25b MJ |
2585 | } |
2586 | ||
4500632f CL |
2587 | /* |
2588 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. | |
2589 | * These are based on similar macros in linux/sunrpc/msg_prot.h . | |
2590 | */ | |
2591 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ | |
2592 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) | |
2593 | ||
2594 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ | |
2595 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) | |
2596 | ||
8a891633 | 2597 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
4500632f | 2598 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
8a891633 | 2599 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
4500632f CL |
2600 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
2601 | sizeof(__be32)) | |
8a891633 | 2602 | |
06b332a5 | 2603 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
1b74c25b | 2604 | { |
06b332a5 BF |
2605 | ca->headerpadsz = 0; |
2606 | ||
8a891633 | 2607 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
06b332a5 | 2608 | return nfserr_toosmall; |
8a891633 | 2609 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
06b332a5 BF |
2610 | return nfserr_toosmall; |
2611 | ca->maxresp_cached = 0; | |
2612 | if (ca->maxops < 2) | |
2613 | return nfserr_toosmall; | |
2614 | ||
2615 | return nfs_ok; | |
1b74c25b MJ |
2616 | } |
2617 | ||
b78724b7 BF |
2618 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
2619 | { | |
2620 | switch (cbs->flavor) { | |
2621 | case RPC_AUTH_NULL: | |
2622 | case RPC_AUTH_UNIX: | |
2623 | return nfs_ok; | |
2624 | default: | |
2625 | /* | |
2626 | * GSS case: the spec doesn't allow us to return this | |
2627 | * error. But it also doesn't allow us not to support | |
2628 | * GSS. | |
2629 | * I'd rather this fail hard than return some error the | |
2630 | * client might think it can already handle: | |
2631 | */ | |
2632 | return nfserr_encr_alg_unsupp; | |
2633 | } | |
2634 | } | |
2635 | ||
069b6ad4 AA |
2636 | __be32 |
2637 | nfsd4_create_session(struct svc_rqst *rqstp, | |
2638 | struct nfsd4_compound_state *cstate, | |
2639 | struct nfsd4_create_session *cr_ses) | |
2640 | { | |
363168b4 | 2641 | struct sockaddr *sa = svc_addr(rqstp); |
ec6b5d7b | 2642 | struct nfs4_client *conf, *unconf; |
d20c11d8 | 2643 | struct nfs4_client *old = NULL; |
ac7c46f2 | 2644 | struct nfsd4_session *new; |
81f0b2a4 | 2645 | struct nfsd4_conn *conn; |
49557cc7 | 2646 | struct nfsd4_clid_slot *cs_slot = NULL; |
57b7b43b | 2647 | __be32 status = 0; |
8daae4dc | 2648 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
ec6b5d7b | 2649 | |
a62573dc MJ |
2650 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
2651 | return nfserr_inval; | |
b78724b7 BF |
2652 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
2653 | if (status) | |
2654 | return status; | |
55c760cf | 2655 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
06b332a5 BF |
2656 | if (status) |
2657 | return status; | |
2658 | status = check_backchannel_attrs(&cr_ses->back_channel); | |
373cd409 | 2659 | if (status) |
f403e450 | 2660 | goto out_release_drc_mem; |
81f0b2a4 | 2661 | status = nfserr_jukebox; |
60810e54 | 2662 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
55c760cf BF |
2663 | if (!new) |
2664 | goto out_release_drc_mem; | |
81f0b2a4 BF |
2665 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
2666 | if (!conn) | |
2667 | goto out_free_session; | |
a62573dc | 2668 | |
d20c11d8 | 2669 | spin_lock(&nn->client_lock); |
0a7ec377 | 2670 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
8daae4dc | 2671 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
78389046 | 2672 | WARN_ON_ONCE(conf && unconf); |
ec6b5d7b AA |
2673 | |
2674 | if (conf) { | |
57266a6e | 2675 | status = nfserr_wrong_cred; |
dedeb13f | 2676 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
57266a6e | 2677 | goto out_free_conn; |
49557cc7 AA |
2678 | cs_slot = &conf->cl_cs_slot; |
2679 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
f5e22bb6 KM |
2680 | if (status) { |
2681 | if (status == nfserr_replay_cache) | |
2682 | status = nfsd4_replay_create_session(cr_ses, cs_slot); | |
81f0b2a4 | 2683 | goto out_free_conn; |
ec6b5d7b | 2684 | } |
ec6b5d7b AA |
2685 | } else if (unconf) { |
2686 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || | |
363168b4 | 2687 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
ec6b5d7b | 2688 | status = nfserr_clid_inuse; |
81f0b2a4 | 2689 | goto out_free_conn; |
ec6b5d7b | 2690 | } |
57266a6e | 2691 | status = nfserr_wrong_cred; |
dedeb13f | 2692 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
57266a6e | 2693 | goto out_free_conn; |
49557cc7 AA |
2694 | cs_slot = &unconf->cl_cs_slot; |
2695 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); | |
38eb76a5 AA |
2696 | if (status) { |
2697 | /* an unconfirmed replay returns misordered */ | |
ec6b5d7b | 2698 | status = nfserr_seq_misordered; |
81f0b2a4 | 2699 | goto out_free_conn; |
ec6b5d7b | 2700 | } |
382a62e7 | 2701 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
221a6876 | 2702 | if (old) { |
d20c11d8 | 2703 | status = mark_client_expired_locked(old); |
7abea1e8 JL |
2704 | if (status) { |
2705 | old = NULL; | |
221a6876 | 2706 | goto out_free_conn; |
7abea1e8 | 2707 | } |
221a6876 | 2708 | } |
8f9d3d3b | 2709 | move_to_confirmed(unconf); |
ec6b5d7b AA |
2710 | conf = unconf; |
2711 | } else { | |
2712 | status = nfserr_stale_clientid; | |
81f0b2a4 | 2713 | goto out_free_conn; |
ec6b5d7b | 2714 | } |
81f0b2a4 | 2715 | status = nfs_ok; |
4ce85c8c | 2716 | /* Persistent sessions are not supported */ |
408b79bc | 2717 | cr_ses->flags &= ~SESSION4_PERSIST; |
4ce85c8c | 2718 | /* Upshifting from TCP to RDMA is not supported */ |
408b79bc BF |
2719 | cr_ses->flags &= ~SESSION4_RDMA; |
2720 | ||
81f0b2a4 | 2721 | init_session(rqstp, new, conf, cr_ses); |
d20c11d8 | 2722 | nfsd4_get_session_locked(new); |
81f0b2a4 | 2723 | |
ac7c46f2 | 2724 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
ec6b5d7b | 2725 | NFS4_MAX_SESSIONID_LEN); |
86c3e16c | 2726 | cs_slot->sl_seqid++; |
49557cc7 | 2727 | cr_ses->seqid = cs_slot->sl_seqid; |
ec6b5d7b | 2728 | |
d20c11d8 | 2729 | /* cache solo and embedded create sessions under the client_lock */ |
49557cc7 | 2730 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
d20c11d8 JL |
2731 | spin_unlock(&nn->client_lock); |
2732 | /* init connection and backchannel */ | |
2733 | nfsd4_init_conn(rqstp, conn, new); | |
2734 | nfsd4_put_session(new); | |
d20c11d8 JL |
2735 | if (old) |
2736 | expire_client(old); | |
ec6b5d7b | 2737 | return status; |
81f0b2a4 | 2738 | out_free_conn: |
d20c11d8 | 2739 | spin_unlock(&nn->client_lock); |
81f0b2a4 | 2740 | free_conn(conn); |
d20c11d8 JL |
2741 | if (old) |
2742 | expire_client(old); | |
81f0b2a4 BF |
2743 | out_free_session: |
2744 | __free_session(new); | |
55c760cf BF |
2745 | out_release_drc_mem: |
2746 | nfsd4_put_drc_mem(&cr_ses->fore_channel); | |
1ca50792 | 2747 | return status; |
069b6ad4 AA |
2748 | } |
2749 | ||
1d1bc8f2 BF |
2750 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
2751 | { | |
2752 | switch (*dir) { | |
2753 | case NFS4_CDFC4_FORE: | |
2754 | case NFS4_CDFC4_BACK: | |
2755 | return nfs_ok; | |
2756 | case NFS4_CDFC4_FORE_OR_BOTH: | |
2757 | case NFS4_CDFC4_BACK_OR_BOTH: | |
2758 | *dir = NFS4_CDFC4_BOTH; | |
2759 | return nfs_ok; | |
2760 | }; | |
2761 | return nfserr_inval; | |
2762 | } | |
2763 | ||
cb73a9f4 BF |
2764 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc) |
2765 | { | |
2766 | struct nfsd4_session *session = cstate->session; | |
c9a49628 | 2767 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
b78724b7 | 2768 | __be32 status; |
cb73a9f4 | 2769 | |
b78724b7 BF |
2770 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
2771 | if (status) | |
2772 | return status; | |
c9a49628 | 2773 | spin_lock(&nn->client_lock); |
cb73a9f4 BF |
2774 | session->se_cb_prog = bc->bc_cb_program; |
2775 | session->se_cb_sec = bc->bc_cb_sec; | |
c9a49628 | 2776 | spin_unlock(&nn->client_lock); |
cb73a9f4 BF |
2777 | |
2778 | nfsd4_probe_callback(session->se_client); | |
2779 | ||
2780 | return nfs_ok; | |
2781 | } | |
2782 | ||
1d1bc8f2 BF |
2783 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
2784 | struct nfsd4_compound_state *cstate, | |
2785 | struct nfsd4_bind_conn_to_session *bcts) | |
2786 | { | |
2787 | __be32 status; | |
3ba63671 | 2788 | struct nfsd4_conn *conn; |
4f6e6c17 | 2789 | struct nfsd4_session *session; |
d4e19e70 TM |
2790 | struct net *net = SVC_NET(rqstp); |
2791 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1d1bc8f2 BF |
2792 | |
2793 | if (!nfsd4_last_compound_op(rqstp)) | |
2794 | return nfserr_not_only_op; | |
c9a49628 | 2795 | spin_lock(&nn->client_lock); |
d4e19e70 | 2796 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
c9a49628 | 2797 | spin_unlock(&nn->client_lock); |
4f6e6c17 | 2798 | if (!session) |
d4e19e70 | 2799 | goto out_no_session; |
57266a6e | 2800 | status = nfserr_wrong_cred; |
dedeb13f | 2801 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
57266a6e | 2802 | goto out; |
1d1bc8f2 | 2803 | status = nfsd4_map_bcts_dir(&bcts->dir); |
3ba63671 | 2804 | if (status) |
4f6e6c17 | 2805 | goto out; |
3ba63671 | 2806 | conn = alloc_conn(rqstp, bcts->dir); |
4f6e6c17 | 2807 | status = nfserr_jukebox; |
3ba63671 | 2808 | if (!conn) |
4f6e6c17 BF |
2809 | goto out; |
2810 | nfsd4_init_conn(rqstp, conn, session); | |
2811 | status = nfs_ok; | |
2812 | out: | |
d4e19e70 TM |
2813 | nfsd4_put_session(session); |
2814 | out_no_session: | |
4f6e6c17 | 2815 | return status; |
1d1bc8f2 BF |
2816 | } |
2817 | ||
5d4cec2f BF |
2818 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
2819 | { | |
2820 | if (!session) | |
2821 | return 0; | |
2822 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); | |
2823 | } | |
2824 | ||
069b6ad4 AA |
2825 | __be32 |
2826 | nfsd4_destroy_session(struct svc_rqst *r, | |
2827 | struct nfsd4_compound_state *cstate, | |
2828 | struct nfsd4_destroy_session *sessionid) | |
2829 | { | |
e10e0cfc | 2830 | struct nfsd4_session *ses; |
abcdff09 | 2831 | __be32 status; |
f0f51f5c | 2832 | int ref_held_by_me = 0; |
d4e19e70 TM |
2833 | struct net *net = SVC_NET(r); |
2834 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
e10e0cfc | 2835 | |
abcdff09 | 2836 | status = nfserr_not_only_op; |
5d4cec2f | 2837 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
57716355 | 2838 | if (!nfsd4_last_compound_op(r)) |
abcdff09 | 2839 | goto out; |
f0f51f5c | 2840 | ref_held_by_me++; |
57716355 | 2841 | } |
e10e0cfc | 2842 | dump_sessionid(__func__, &sessionid->sessionid); |
c9a49628 | 2843 | spin_lock(&nn->client_lock); |
d4e19e70 | 2844 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status); |
abcdff09 BF |
2845 | if (!ses) |
2846 | goto out_client_lock; | |
57266a6e | 2847 | status = nfserr_wrong_cred; |
dedeb13f | 2848 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
d4e19e70 | 2849 | goto out_put_session; |
f0f51f5c | 2850 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
66b2b9b2 | 2851 | if (status) |
f0f51f5c | 2852 | goto out_put_session; |
e10e0cfc | 2853 | unhash_session(ses); |
c9a49628 | 2854 | spin_unlock(&nn->client_lock); |
e10e0cfc | 2855 | |
84f5f7cc | 2856 | nfsd4_probe_callback_sync(ses->se_client); |
19cf5c02 | 2857 | |
c9a49628 | 2858 | spin_lock(&nn->client_lock); |
e10e0cfc | 2859 | status = nfs_ok; |
f0f51f5c | 2860 | out_put_session: |
d4e19e70 | 2861 | nfsd4_put_session_locked(ses); |
abcdff09 BF |
2862 | out_client_lock: |
2863 | spin_unlock(&nn->client_lock); | |
e10e0cfc | 2864 | out: |
e10e0cfc | 2865 | return status; |
069b6ad4 AA |
2866 | } |
2867 | ||
a663bdd8 | 2868 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
328ead28 BF |
2869 | { |
2870 | struct nfsd4_conn *c; | |
2871 | ||
2872 | list_for_each_entry(c, &s->se_conns, cn_persession) { | |
a663bdd8 | 2873 | if (c->cn_xprt == xpt) { |
328ead28 BF |
2874 | return c; |
2875 | } | |
2876 | } | |
2877 | return NULL; | |
2878 | } | |
2879 | ||
57266a6e | 2880 | static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) |
328ead28 BF |
2881 | { |
2882 | struct nfs4_client *clp = ses->se_client; | |
a663bdd8 | 2883 | struct nfsd4_conn *c; |
57266a6e | 2884 | __be32 status = nfs_ok; |
21b75b01 | 2885 | int ret; |
328ead28 BF |
2886 | |
2887 | spin_lock(&clp->cl_lock); | |
a663bdd8 | 2888 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
57266a6e BF |
2889 | if (c) |
2890 | goto out_free; | |
2891 | status = nfserr_conn_not_bound_to_session; | |
2892 | if (clp->cl_mach_cred) | |
2893 | goto out_free; | |
328ead28 BF |
2894 | __nfsd4_hash_conn(new, ses); |
2895 | spin_unlock(&clp->cl_lock); | |
21b75b01 BF |
2896 | ret = nfsd4_register_conn(new); |
2897 | if (ret) | |
2898 | /* oops; xprt is already down: */ | |
2899 | nfsd4_conn_lost(&new->cn_xpt_user); | |
57266a6e BF |
2900 | return nfs_ok; |
2901 | out_free: | |
2902 | spin_unlock(&clp->cl_lock); | |
2903 | free_conn(new); | |
2904 | return status; | |
328ead28 BF |
2905 | } |
2906 | ||
868b89c3 MJ |
2907 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
2908 | { | |
2909 | struct nfsd4_compoundargs *args = rqstp->rq_argp; | |
2910 | ||
2911 | return args->opcnt > session->se_fchannel.maxops; | |
2912 | } | |
2913 | ||
ae82a8d0 MJ |
2914 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
2915 | struct nfsd4_session *session) | |
2916 | { | |
2917 | struct xdr_buf *xb = &rqstp->rq_arg; | |
2918 | ||
2919 | return xb->len > session->se_fchannel.maxreq_sz; | |
2920 | } | |
2921 | ||
069b6ad4 | 2922 | __be32 |
b85d4c01 | 2923 | nfsd4_sequence(struct svc_rqst *rqstp, |
069b6ad4 AA |
2924 | struct nfsd4_compound_state *cstate, |
2925 | struct nfsd4_sequence *seq) | |
2926 | { | |
f9bb94c4 | 2927 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
47ee5298 | 2928 | struct xdr_stream *xdr = &resp->xdr; |
b85d4c01 | 2929 | struct nfsd4_session *session; |
221a6876 | 2930 | struct nfs4_client *clp; |
b85d4c01 | 2931 | struct nfsd4_slot *slot; |
a663bdd8 | 2932 | struct nfsd4_conn *conn; |
57b7b43b | 2933 | __be32 status; |
47ee5298 | 2934 | int buflen; |
d4e19e70 TM |
2935 | struct net *net = SVC_NET(rqstp); |
2936 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
b85d4c01 | 2937 | |
f9bb94c4 AA |
2938 | if (resp->opcnt != 1) |
2939 | return nfserr_sequence_pos; | |
2940 | ||
a663bdd8 BF |
2941 | /* |
2942 | * Will be either used or freed by nfsd4_sequence_check_conn | |
2943 | * below. | |
2944 | */ | |
2945 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); | |
2946 | if (!conn) | |
2947 | return nfserr_jukebox; | |
2948 | ||
c9a49628 | 2949 | spin_lock(&nn->client_lock); |
d4e19e70 | 2950 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
b85d4c01 | 2951 | if (!session) |
221a6876 BF |
2952 | goto out_no_session; |
2953 | clp = session->se_client; | |
b85d4c01 | 2954 | |
868b89c3 MJ |
2955 | status = nfserr_too_many_ops; |
2956 | if (nfsd4_session_too_many_ops(rqstp, session)) | |
66b2b9b2 | 2957 | goto out_put_session; |
868b89c3 | 2958 | |
ae82a8d0 MJ |
2959 | status = nfserr_req_too_big; |
2960 | if (nfsd4_request_too_big(rqstp, session)) | |
66b2b9b2 | 2961 | goto out_put_session; |
ae82a8d0 | 2962 | |
b85d4c01 | 2963 | status = nfserr_badslot; |
6c18ba9f | 2964 | if (seq->slotid >= session->se_fchannel.maxreqs) |
66b2b9b2 | 2965 | goto out_put_session; |
b85d4c01 | 2966 | |
557ce264 | 2967 | slot = session->se_slots[seq->slotid]; |
b85d4c01 BH |
2968 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
2969 | ||
a8dfdaeb AA |
2970 | /* We do not negotiate the number of slots yet, so set the |
2971 | * maxslots to the session maxreqs which is used to encode | |
2972 | * sr_highest_slotid and the sr_target_slot id to maxslots */ | |
2973 | seq->maxslots = session->se_fchannel.maxreqs; | |
2974 | ||
73e79482 BF |
2975 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
2976 | slot->sl_flags & NFSD4_SLOT_INUSE); | |
b85d4c01 | 2977 | if (status == nfserr_replay_cache) { |
bf5c43c8 BF |
2978 | status = nfserr_seq_misordered; |
2979 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) | |
66b2b9b2 | 2980 | goto out_put_session; |
b85d4c01 BH |
2981 | cstate->slot = slot; |
2982 | cstate->session = session; | |
4b24ca7d | 2983 | cstate->clp = clp; |
da3846a2 | 2984 | /* Return the cached reply status and set cstate->status |
557ce264 | 2985 | * for nfsd4_proc_compound processing */ |
bf864a31 | 2986 | status = nfsd4_replay_cache_entry(resp, seq); |
da3846a2 | 2987 | cstate->status = nfserr_replay_cache; |
aaf84eb9 | 2988 | goto out; |
b85d4c01 BH |
2989 | } |
2990 | if (status) | |
66b2b9b2 | 2991 | goto out_put_session; |
b85d4c01 | 2992 | |
57266a6e | 2993 | status = nfsd4_sequence_check_conn(conn, session); |
a663bdd8 | 2994 | conn = NULL; |
57266a6e BF |
2995 | if (status) |
2996 | goto out_put_session; | |
328ead28 | 2997 | |
47ee5298 BF |
2998 | buflen = (seq->cachethis) ? |
2999 | session->se_fchannel.maxresp_cached : | |
3000 | session->se_fchannel.maxresp_sz; | |
3001 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : | |
3002 | nfserr_rep_too_big; | |
a5cddc88 | 3003 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
47ee5298 | 3004 | goto out_put_session; |
32aaa62e | 3005 | svc_reserve(rqstp, buflen); |
47ee5298 BF |
3006 | |
3007 | status = nfs_ok; | |
b85d4c01 | 3008 | /* Success! bump slot seqid */ |
b85d4c01 | 3009 | slot->sl_seqid = seq->seqid; |
bf5c43c8 | 3010 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
73e79482 BF |
3011 | if (seq->cachethis) |
3012 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; | |
bf5c43c8 BF |
3013 | else |
3014 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; | |
b85d4c01 BH |
3015 | |
3016 | cstate->slot = slot; | |
3017 | cstate->session = session; | |
4b24ca7d | 3018 | cstate->clp = clp; |
b85d4c01 | 3019 | |
b85d4c01 | 3020 | out: |
221a6876 BF |
3021 | switch (clp->cl_cb_state) { |
3022 | case NFSD4_CB_DOWN: | |
3023 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; | |
3024 | break; | |
3025 | case NFSD4_CB_FAULT: | |
3026 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; | |
3027 | break; | |
3028 | default: | |
3029 | seq->status_flags = 0; | |
aaf84eb9 | 3030 | } |
3bd64a5b BF |
3031 | if (!list_empty(&clp->cl_revoked)) |
3032 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; | |
221a6876 | 3033 | out_no_session: |
3f42d2c4 KM |
3034 | if (conn) |
3035 | free_conn(conn); | |
c9a49628 | 3036 | spin_unlock(&nn->client_lock); |
b85d4c01 | 3037 | return status; |
66b2b9b2 | 3038 | out_put_session: |
d4e19e70 | 3039 | nfsd4_put_session_locked(session); |
221a6876 | 3040 | goto out_no_session; |
069b6ad4 AA |
3041 | } |
3042 | ||
b607664e TM |
3043 | void |
3044 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) | |
3045 | { | |
3046 | struct nfsd4_compound_state *cs = &resp->cstate; | |
3047 | ||
3048 | if (nfsd4_has_session(cs)) { | |
b607664e TM |
3049 | if (cs->status != nfserr_replay_cache) { |
3050 | nfsd4_store_cache_entry(resp); | |
3051 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; | |
3052 | } | |
d4e19e70 | 3053 | /* Drop session reference that was taken in nfsd4_sequence() */ |
b607664e | 3054 | nfsd4_put_session(cs->session); |
4b24ca7d JL |
3055 | } else if (cs->clp) |
3056 | put_client_renew(cs->clp); | |
b607664e TM |
3057 | } |
3058 | ||
345c2842 MJ |
3059 | __be32 |
3060 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc) | |
3061 | { | |
6b10ad19 TM |
3062 | struct nfs4_client *conf, *unconf; |
3063 | struct nfs4_client *clp = NULL; | |
57b7b43b | 3064 | __be32 status = 0; |
8daae4dc | 3065 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
345c2842 | 3066 | |
6b10ad19 | 3067 | spin_lock(&nn->client_lock); |
0a7ec377 | 3068 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
8daae4dc | 3069 | conf = find_confirmed_client(&dc->clientid, true, nn); |
78389046 | 3070 | WARN_ON_ONCE(conf && unconf); |
345c2842 MJ |
3071 | |
3072 | if (conf) { | |
c0293b01 | 3073 | if (client_has_state(conf)) { |
345c2842 MJ |
3074 | status = nfserr_clientid_busy; |
3075 | goto out; | |
3076 | } | |
fd699b8a JL |
3077 | status = mark_client_expired_locked(conf); |
3078 | if (status) | |
3079 | goto out; | |
6b10ad19 | 3080 | clp = conf; |
345c2842 MJ |
3081 | } else if (unconf) |
3082 | clp = unconf; | |
3083 | else { | |
3084 | status = nfserr_stale_clientid; | |
3085 | goto out; | |
3086 | } | |
dedeb13f | 3087 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
6b10ad19 | 3088 | clp = NULL; |
57266a6e BF |
3089 | status = nfserr_wrong_cred; |
3090 | goto out; | |
3091 | } | |
6b10ad19 | 3092 | unhash_client_locked(clp); |
345c2842 | 3093 | out: |
6b10ad19 | 3094 | spin_unlock(&nn->client_lock); |
6b10ad19 TM |
3095 | if (clp) |
3096 | expire_client(clp); | |
345c2842 MJ |
3097 | return status; |
3098 | } | |
3099 | ||
4dc6ec00 BF |
3100 | __be32 |
3101 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) | |
3102 | { | |
57b7b43b | 3103 | __be32 status = 0; |
bcecf1cc | 3104 | |
4dc6ec00 BF |
3105 | if (rc->rca_one_fs) { |
3106 | if (!cstate->current_fh.fh_dentry) | |
3107 | return nfserr_nofilehandle; | |
3108 | /* | |
3109 | * We don't take advantage of the rca_one_fs case. | |
3110 | * That's OK, it's optional, we can safely ignore it. | |
3111 | */ | |
d28c442f | 3112 | return nfs_ok; |
4dc6ec00 | 3113 | } |
bcecf1cc | 3114 | |
bcecf1cc | 3115 | status = nfserr_complete_already; |
a52d726b JL |
3116 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
3117 | &cstate->session->se_client->cl_flags)) | |
bcecf1cc MJ |
3118 | goto out; |
3119 | ||
3120 | status = nfserr_stale_clientid; | |
3121 | if (is_client_expired(cstate->session->se_client)) | |
4dc6ec00 BF |
3122 | /* |
3123 | * The following error isn't really legal. | |
3124 | * But we only get here if the client just explicitly | |
3125 | * destroyed the client. Surely it no longer cares what | |
3126 | * error it gets back on an operation for the dead | |
3127 | * client. | |
3128 | */ | |
bcecf1cc MJ |
3129 | goto out; |
3130 | ||
3131 | status = nfs_ok; | |
2a4317c5 | 3132 | nfsd4_client_record_create(cstate->session->se_client); |
bcecf1cc | 3133 | out: |
bcecf1cc | 3134 | return status; |
4dc6ec00 BF |
3135 | } |
3136 | ||
b37ad28b | 3137 | __be32 |
b591480b BF |
3138 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
3139 | struct nfsd4_setclientid *setclid) | |
1da177e4 | 3140 | { |
a084daf5 | 3141 | struct xdr_netobj clname = setclid->se_name; |
1da177e4 | 3142 | nfs4_verifier clverifier = setclid->se_verf; |
3dbacee6 TM |
3143 | struct nfs4_client *conf, *new; |
3144 | struct nfs4_client *unconf = NULL; | |
b37ad28b | 3145 | __be32 status; |
c212cecf SK |
3146 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
3147 | ||
5cc40fd7 TM |
3148 | new = create_client(clname, rqstp, &clverifier); |
3149 | if (new == NULL) | |
3150 | return nfserr_jukebox; | |
63db4632 | 3151 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
3dbacee6 | 3152 | spin_lock(&nn->client_lock); |
382a62e7 | 3153 | conf = find_confirmed_client_by_name(&clname, nn); |
2b634821 | 3154 | if (conf && client_has_state(conf)) { |
63db4632 | 3155 | /* case 0: */ |
1da177e4 | 3156 | status = nfserr_clid_inuse; |
e203d506 BF |
3157 | if (clp_used_exchangeid(conf)) |
3158 | goto out; | |
026722c2 | 3159 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
363168b4 JL |
3160 | char addr_str[INET6_ADDRSTRLEN]; |
3161 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, | |
3162 | sizeof(addr_str)); | |
3163 | dprintk("NFSD: setclientid: string in use by client " | |
3164 | "at %s\n", addr_str); | |
1da177e4 LT |
3165 | goto out; |
3166 | } | |
1da177e4 | 3167 | } |
a99454aa | 3168 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
8f930711 | 3169 | if (unconf) |
3dbacee6 | 3170 | unhash_client_locked(unconf); |
41eb1670 | 3171 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
63db4632 | 3172 | /* case 1: probable callback update */ |
1da177e4 | 3173 | copy_clid(new, conf); |
41eb1670 KM |
3174 | gen_confirm(new, nn); |
3175 | } else /* case 4 (new client) or cases 2, 3 (client reboot): */ | |
c212cecf | 3176 | gen_clid(new, nn); |
8323c3b2 | 3177 | new->cl_minorversion = 0; |
6f3d772f | 3178 | gen_callback(new, setclid, rqstp); |
ac55fdc4 | 3179 | add_to_unconfirmed(new); |
1da177e4 LT |
3180 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
3181 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; | |
3182 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); | |
5cc40fd7 | 3183 | new = NULL; |
1da177e4 LT |
3184 | status = nfs_ok; |
3185 | out: | |
3dbacee6 | 3186 | spin_unlock(&nn->client_lock); |
5cc40fd7 TM |
3187 | if (new) |
3188 | free_client(new); | |
3dbacee6 TM |
3189 | if (unconf) |
3190 | expire_client(unconf); | |
1da177e4 LT |
3191 | return status; |
3192 | } | |
3193 | ||
3194 | ||
b37ad28b | 3195 | __be32 |
b591480b BF |
3196 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
3197 | struct nfsd4_compound_state *cstate, | |
3198 | struct nfsd4_setclientid_confirm *setclientid_confirm) | |
1da177e4 | 3199 | { |
21ab45a4 | 3200 | struct nfs4_client *conf, *unconf; |
d20c11d8 | 3201 | struct nfs4_client *old = NULL; |
1da177e4 LT |
3202 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
3203 | clientid_t * clid = &setclientid_confirm->sc_clientid; | |
b37ad28b | 3204 | __be32 status; |
7f2210fa | 3205 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 3206 | |
2c142baa | 3207 | if (STALE_CLIENTID(clid, nn)) |
1da177e4 | 3208 | return nfserr_stale_clientid; |
21ab45a4 | 3209 | |
d20c11d8 | 3210 | spin_lock(&nn->client_lock); |
8daae4dc | 3211 | conf = find_confirmed_client(clid, false, nn); |
0a7ec377 | 3212 | unconf = find_unconfirmed_client(clid, false, nn); |
a186e767 | 3213 | /* |
8695b90a BF |
3214 | * We try hard to give out unique clientid's, so if we get an |
3215 | * attempt to confirm the same clientid with a different cred, | |
f984a7ce BF |
3216 | * the client may be buggy; this should never happen. |
3217 | * | |
3218 | * Nevertheless, RFC 7530 recommends INUSE for this case: | |
a186e767 | 3219 | */ |
f984a7ce | 3220 | status = nfserr_clid_inuse; |
8695b90a BF |
3221 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
3222 | goto out; | |
3223 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) | |
3224 | goto out; | |
63db4632 | 3225 | /* cases below refer to rfc 3530 section 14.2.34: */ |
90d700b7 BF |
3226 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
3227 | if (conf && !unconf) /* case 2: probable retransmit */ | |
1da177e4 | 3228 | status = nfs_ok; |
90d700b7 BF |
3229 | else /* case 4: client hasn't noticed we rebooted yet? */ |
3230 | status = nfserr_stale_clientid; | |
3231 | goto out; | |
3232 | } | |
3233 | status = nfs_ok; | |
3234 | if (conf) { /* case 1: callback update */ | |
d20c11d8 JL |
3235 | old = unconf; |
3236 | unhash_client_locked(old); | |
8695b90a | 3237 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
90d700b7 | 3238 | } else { /* case 3: normal case; new or rebooted client */ |
d20c11d8 JL |
3239 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
3240 | if (old) { | |
2b634821 BF |
3241 | status = nfserr_clid_inuse; |
3242 | if (client_has_state(old) | |
3243 | && !same_creds(&unconf->cl_cred, | |
3244 | &old->cl_cred)) | |
3245 | goto out; | |
d20c11d8 | 3246 | status = mark_client_expired_locked(old); |
7abea1e8 JL |
3247 | if (status) { |
3248 | old = NULL; | |
221a6876 | 3249 | goto out; |
7abea1e8 | 3250 | } |
221a6876 | 3251 | } |
8695b90a | 3252 | move_to_confirmed(unconf); |
d20c11d8 | 3253 | conf = unconf; |
08e8987c | 3254 | } |
d20c11d8 JL |
3255 | get_client_locked(conf); |
3256 | spin_unlock(&nn->client_lock); | |
3257 | nfsd4_probe_callback(conf); | |
3258 | spin_lock(&nn->client_lock); | |
3259 | put_client_renew_locked(conf); | |
1da177e4 | 3260 | out: |
d20c11d8 JL |
3261 | spin_unlock(&nn->client_lock); |
3262 | if (old) | |
3263 | expire_client(old); | |
1da177e4 LT |
3264 | return status; |
3265 | } | |
3266 | ||
32513b40 BF |
3267 | static struct nfs4_file *nfsd4_alloc_file(void) |
3268 | { | |
3269 | return kmem_cache_alloc(file_slab, GFP_KERNEL); | |
3270 | } | |
3271 | ||
1da177e4 | 3272 | /* OPEN Share state helper functions */ |
5b095e99 JL |
3273 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
3274 | struct nfs4_file *fp) | |
1da177e4 | 3275 | { |
950e0118 TM |
3276 | lockdep_assert_held(&state_lock); |
3277 | ||
32513b40 | 3278 | atomic_set(&fp->fi_ref, 1); |
1d31a253 | 3279 | spin_lock_init(&fp->fi_lock); |
32513b40 BF |
3280 | INIT_LIST_HEAD(&fp->fi_stateids); |
3281 | INIT_LIST_HEAD(&fp->fi_delegations); | |
8287f009 | 3282 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
e2cf80d7 | 3283 | fh_copy_shallow(&fp->fi_fhandle, fh); |
0c637be8 | 3284 | fp->fi_deleg_file = NULL; |
32513b40 | 3285 | fp->fi_had_conflict = false; |
baeb4ff0 | 3286 | fp->fi_share_deny = 0; |
32513b40 BF |
3287 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
3288 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); | |
9cf514cc CH |
3289 | #ifdef CONFIG_NFSD_PNFS |
3290 | INIT_LIST_HEAD(&fp->fi_lo_states); | |
c5c707f9 | 3291 | atomic_set(&fp->fi_lo_recalls, 0); |
9cf514cc | 3292 | #endif |
5b095e99 | 3293 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
1da177e4 LT |
3294 | } |
3295 | ||
e8ff2a84 | 3296 | void |
1da177e4 LT |
3297 | nfsd4_free_slabs(void) |
3298 | { | |
8287f009 | 3299 | kmem_cache_destroy(odstate_slab); |
abf1135b CH |
3300 | kmem_cache_destroy(openowner_slab); |
3301 | kmem_cache_destroy(lockowner_slab); | |
3302 | kmem_cache_destroy(file_slab); | |
3303 | kmem_cache_destroy(stateid_slab); | |
3304 | kmem_cache_destroy(deleg_slab); | |
e60d4398 | 3305 | } |
1da177e4 | 3306 | |
72083396 | 3307 | int |
e60d4398 N |
3308 | nfsd4_init_slabs(void) |
3309 | { | |
fe0750e5 BF |
3310 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
3311 | sizeof(struct nfs4_openowner), 0, 0, NULL); | |
3312 | if (openowner_slab == NULL) | |
abf1135b | 3313 | goto out; |
fe0750e5 | 3314 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
3c40794b | 3315 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
fe0750e5 | 3316 | if (lockowner_slab == NULL) |
abf1135b | 3317 | goto out_free_openowner_slab; |
e60d4398 | 3318 | file_slab = kmem_cache_create("nfsd4_files", |
20c2df83 | 3319 | sizeof(struct nfs4_file), 0, 0, NULL); |
e60d4398 | 3320 | if (file_slab == NULL) |
abf1135b | 3321 | goto out_free_lockowner_slab; |
5ac049ac | 3322 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
dcef0413 | 3323 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
5ac049ac | 3324 | if (stateid_slab == NULL) |
abf1135b | 3325 | goto out_free_file_slab; |
5b2d21c1 | 3326 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
20c2df83 | 3327 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
5b2d21c1 | 3328 | if (deleg_slab == NULL) |
abf1135b | 3329 | goto out_free_stateid_slab; |
8287f009 SB |
3330 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
3331 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); | |
3332 | if (odstate_slab == NULL) | |
3333 | goto out_free_deleg_slab; | |
e60d4398 | 3334 | return 0; |
abf1135b | 3335 | |
8287f009 SB |
3336 | out_free_deleg_slab: |
3337 | kmem_cache_destroy(deleg_slab); | |
abf1135b CH |
3338 | out_free_stateid_slab: |
3339 | kmem_cache_destroy(stateid_slab); | |
3340 | out_free_file_slab: | |
3341 | kmem_cache_destroy(file_slab); | |
3342 | out_free_lockowner_slab: | |
3343 | kmem_cache_destroy(lockowner_slab); | |
3344 | out_free_openowner_slab: | |
3345 | kmem_cache_destroy(openowner_slab); | |
3346 | out: | |
e60d4398 N |
3347 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
3348 | return -ENOMEM; | |
1da177e4 LT |
3349 | } |
3350 | ||
ff194bd9 | 3351 | static void init_nfs4_replay(struct nfs4_replay *rp) |
1da177e4 | 3352 | { |
ff194bd9 BF |
3353 | rp->rp_status = nfserr_serverfault; |
3354 | rp->rp_buflen = 0; | |
3355 | rp->rp_buf = rp->rp_ibuf; | |
58fb12e6 JL |
3356 | mutex_init(&rp->rp_mutex); |
3357 | } | |
3358 | ||
3359 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, | |
3360 | struct nfs4_stateowner *so) | |
3361 | { | |
3362 | if (!nfsd4_has_session(cstate)) { | |
3363 | mutex_lock(&so->so_replay.rp_mutex); | |
b5971afa | 3364 | cstate->replay_owner = nfs4_get_stateowner(so); |
58fb12e6 JL |
3365 | } |
3366 | } | |
3367 | ||
3368 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) | |
3369 | { | |
3370 | struct nfs4_stateowner *so = cstate->replay_owner; | |
3371 | ||
3372 | if (so != NULL) { | |
3373 | cstate->replay_owner = NULL; | |
3374 | mutex_unlock(&so->so_replay.rp_mutex); | |
3375 | nfs4_put_stateowner(so); | |
3376 | } | |
1da177e4 LT |
3377 | } |
3378 | ||
fe0750e5 | 3379 | static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) |
ff194bd9 | 3380 | { |
1da177e4 | 3381 | struct nfs4_stateowner *sop; |
1da177e4 | 3382 | |
fe0750e5 | 3383 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
ff194bd9 BF |
3384 | if (!sop) |
3385 | return NULL; | |
3386 | ||
3387 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); | |
3388 | if (!sop->so_owner.data) { | |
fe0750e5 | 3389 | kmem_cache_free(slab, sop); |
1da177e4 | 3390 | return NULL; |
ff194bd9 BF |
3391 | } |
3392 | sop->so_owner.len = owner->len; | |
3393 | ||
ea1da636 | 3394 | INIT_LIST_HEAD(&sop->so_stateids); |
ff194bd9 BF |
3395 | sop->so_client = clp; |
3396 | init_nfs4_replay(&sop->so_replay); | |
6b180f0b | 3397 | atomic_set(&sop->so_count, 1); |
ff194bd9 BF |
3398 | return sop; |
3399 | } | |
3400 | ||
fe0750e5 | 3401 | static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) |
ff194bd9 | 3402 | { |
d4f0489f | 3403 | lockdep_assert_held(&clp->cl_lock); |
9b531137 | 3404 | |
d4f0489f TM |
3405 | list_add(&oo->oo_owner.so_strhash, |
3406 | &clp->cl_ownerstr_hashtbl[strhashval]); | |
fe0750e5 | 3407 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
ff194bd9 BF |
3408 | } |
3409 | ||
8f4b54c5 JL |
3410 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
3411 | { | |
d4f0489f | 3412 | unhash_openowner_locked(openowner(so)); |
8f4b54c5 JL |
3413 | } |
3414 | ||
6b180f0b JL |
3415 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
3416 | { | |
3417 | struct nfs4_openowner *oo = openowner(so); | |
3418 | ||
3419 | kmem_cache_free(openowner_slab, oo); | |
3420 | } | |
3421 | ||
3422 | static const struct nfs4_stateowner_operations openowner_ops = { | |
8f4b54c5 JL |
3423 | .so_unhash = nfs4_unhash_openowner, |
3424 | .so_free = nfs4_free_openowner, | |
6b180f0b JL |
3425 | }; |
3426 | ||
7fc0564e AE |
3427 | static struct nfs4_ol_stateid * |
3428 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) | |
3429 | { | |
3430 | struct nfs4_ol_stateid *local, *ret = NULL; | |
3431 | struct nfs4_openowner *oo = open->op_openowner; | |
3432 | ||
3433 | lockdep_assert_held(&fp->fi_lock); | |
3434 | ||
3435 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { | |
3436 | /* ignore lock owners */ | |
3437 | if (local->st_stateowner->so_is_open_owner == 0) | |
3438 | continue; | |
3439 | if (local->st_stateowner == &oo->oo_owner) { | |
3440 | ret = local; | |
3441 | atomic_inc(&ret->st_stid.sc_count); | |
3442 | break; | |
3443 | } | |
3444 | } | |
3445 | return ret; | |
3446 | } | |
3447 | ||
fe0750e5 | 3448 | static struct nfs4_openowner * |
13d6f66b | 3449 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
db24b3b4 JL |
3450 | struct nfsd4_compound_state *cstate) |
3451 | { | |
13d6f66b | 3452 | struct nfs4_client *clp = cstate->clp; |
7ffb5880 | 3453 | struct nfs4_openowner *oo, *ret; |
ff194bd9 | 3454 | |
fe0750e5 BF |
3455 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
3456 | if (!oo) | |
ff194bd9 | 3457 | return NULL; |
6b180f0b | 3458 | oo->oo_owner.so_ops = &openowner_ops; |
fe0750e5 BF |
3459 | oo->oo_owner.so_is_open_owner = 1; |
3460 | oo->oo_owner.so_seqid = open->op_seqid; | |
d3134b10 | 3461 | oo->oo_flags = 0; |
db24b3b4 JL |
3462 | if (nfsd4_has_session(cstate)) |
3463 | oo->oo_flags |= NFS4_OO_CONFIRMED; | |
fe0750e5 | 3464 | oo->oo_time = 0; |
38c387b5 | 3465 | oo->oo_last_closed_stid = NULL; |
fe0750e5 | 3466 | INIT_LIST_HEAD(&oo->oo_close_lru); |
d4f0489f TM |
3467 | spin_lock(&clp->cl_lock); |
3468 | ret = find_openstateowner_str_locked(strhashval, open, clp); | |
7ffb5880 TM |
3469 | if (ret == NULL) { |
3470 | hash_openowner(oo, clp, strhashval); | |
3471 | ret = oo; | |
3472 | } else | |
d50ffded KM |
3473 | nfs4_free_stateowner(&oo->oo_owner); |
3474 | ||
d4f0489f | 3475 | spin_unlock(&clp->cl_lock); |
c5952338 | 3476 | return ret; |
1da177e4 LT |
3477 | } |
3478 | ||
7fc0564e | 3479 | static struct nfs4_ol_stateid * |
8c7245ab | 3480 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
7fc0564e AE |
3481 | { |
3482 | ||
fe0750e5 | 3483 | struct nfs4_openowner *oo = open->op_openowner; |
7fc0564e | 3484 | struct nfs4_ol_stateid *retstp = NULL; |
8c7245ab | 3485 | struct nfs4_ol_stateid *stp; |
1da177e4 | 3486 | |
8c7245ab | 3487 | stp = open->op_stp; |
5cc1fb2a OD |
3488 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
3489 | mutex_init(&stp->st_mutex); | |
3490 | mutex_lock(&stp->st_mutex); | |
3491 | ||
7fc0564e AE |
3492 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
3493 | spin_lock(&fp->fi_lock); | |
3494 | ||
3495 | retstp = nfsd4_find_existing_open(fp, open); | |
3496 | if (retstp) | |
3497 | goto out_unlock; | |
8c7245ab OD |
3498 | |
3499 | open->op_stp = NULL; | |
d6f2bc5d | 3500 | atomic_inc(&stp->st_stid.sc_count); |
3abdb607 | 3501 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
3c87b9b7 | 3502 | INIT_LIST_HEAD(&stp->st_locks); |
b5971afa | 3503 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
13cd2184 | 3504 | get_nfs4_file(fp); |
11b9164a | 3505 | stp->st_stid.sc_file = fp; |
1da177e4 LT |
3506 | stp->st_access_bmap = 0; |
3507 | stp->st_deny_bmap = 0; | |
4c4cd222 | 3508 | stp->st_openstp = NULL; |
1c755dc1 | 3509 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
1d31a253 | 3510 | list_add(&stp->st_perfile, &fp->fi_stateids); |
7fc0564e AE |
3511 | |
3512 | out_unlock: | |
1d31a253 | 3513 | spin_unlock(&fp->fi_lock); |
1c755dc1 | 3514 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
5cc1fb2a OD |
3515 | if (retstp) { |
3516 | mutex_lock(&retstp->st_mutex); | |
8c7245ab | 3517 | /* To keep mutex tracking happy */ |
5cc1fb2a | 3518 | mutex_unlock(&stp->st_mutex); |
8c7245ab | 3519 | stp = retstp; |
5cc1fb2a | 3520 | } |
8c7245ab | 3521 | return stp; |
1da177e4 LT |
3522 | } |
3523 | ||
d3134b10 JL |
3524 | /* |
3525 | * In the 4.0 case we need to keep the owners around a little while to handle | |
3526 | * CLOSE replay. We still do need to release any file access that is held by | |
3527 | * them before returning however. | |
3528 | */ | |
fd39ca9a | 3529 | static void |
d3134b10 | 3530 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
1da177e4 | 3531 | { |
217526e7 | 3532 | struct nfs4_ol_stateid *last; |
d3134b10 JL |
3533 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
3534 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, | |
3535 | nfsd_net_id); | |
73758fed | 3536 | |
fe0750e5 | 3537 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
1da177e4 | 3538 | |
b401be22 JL |
3539 | /* |
3540 | * We know that we hold one reference via nfsd4_close, and another | |
3541 | * "persistent" reference for the client. If the refcount is higher | |
3542 | * than 2, then there are still calls in progress that are using this | |
3543 | * stateid. We can't put the sc_file reference until they are finished. | |
3544 | * Wait for the refcount to drop to 2. Since it has been unhashed, | |
3545 | * there should be no danger of the refcount going back up again at | |
3546 | * this point. | |
3547 | */ | |
3548 | wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2); | |
3549 | ||
d3134b10 JL |
3550 | release_all_access(s); |
3551 | if (s->st_stid.sc_file) { | |
3552 | put_nfs4_file(s->st_stid.sc_file); | |
3553 | s->st_stid.sc_file = NULL; | |
3554 | } | |
217526e7 JL |
3555 | |
3556 | spin_lock(&nn->client_lock); | |
3557 | last = oo->oo_last_closed_stid; | |
d3134b10 | 3558 | oo->oo_last_closed_stid = s; |
73758fed | 3559 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
fe0750e5 | 3560 | oo->oo_time = get_seconds(); |
217526e7 JL |
3561 | spin_unlock(&nn->client_lock); |
3562 | if (last) | |
3563 | nfs4_put_stid(&last->st_stid); | |
1da177e4 LT |
3564 | } |
3565 | ||
1da177e4 LT |
3566 | /* search file_hashtbl[] for file */ |
3567 | static struct nfs4_file * | |
5b095e99 | 3568 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
1da177e4 | 3569 | { |
1da177e4 LT |
3570 | struct nfs4_file *fp; |
3571 | ||
5b095e99 | 3572 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) { |
4d94c2ef | 3573 | if (fh_match(&fp->fi_fhandle, fh)) { |
5b095e99 JL |
3574 | if (atomic_inc_not_zero(&fp->fi_ref)) |
3575 | return fp; | |
13cd2184 | 3576 | } |
1da177e4 LT |
3577 | } |
3578 | return NULL; | |
3579 | } | |
3580 | ||
e6ba76e1 | 3581 | struct nfs4_file * |
ca943217 | 3582 | find_file(struct knfsd_fh *fh) |
950e0118 TM |
3583 | { |
3584 | struct nfs4_file *fp; | |
5b095e99 | 3585 | unsigned int hashval = file_hashval(fh); |
950e0118 | 3586 | |
5b095e99 JL |
3587 | rcu_read_lock(); |
3588 | fp = find_file_locked(fh, hashval); | |
3589 | rcu_read_unlock(); | |
950e0118 TM |
3590 | return fp; |
3591 | } | |
3592 | ||
3593 | static struct nfs4_file * | |
f9c00c3a | 3594 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
950e0118 TM |
3595 | { |
3596 | struct nfs4_file *fp; | |
5b095e99 JL |
3597 | unsigned int hashval = file_hashval(fh); |
3598 | ||
3599 | rcu_read_lock(); | |
3600 | fp = find_file_locked(fh, hashval); | |
3601 | rcu_read_unlock(); | |
3602 | if (fp) | |
3603 | return fp; | |
950e0118 TM |
3604 | |
3605 | spin_lock(&state_lock); | |
5b095e99 JL |
3606 | fp = find_file_locked(fh, hashval); |
3607 | if (likely(fp == NULL)) { | |
3608 | nfsd4_init_file(fh, hashval, new); | |
950e0118 TM |
3609 | fp = new; |
3610 | } | |
3611 | spin_unlock(&state_lock); | |
3612 | ||
3613 | return fp; | |
3614 | } | |
3615 | ||
1da177e4 LT |
3616 | /* |
3617 | * Called to check deny when READ with all zero stateid or | |
3618 | * WRITE with all zero or all one stateid | |
3619 | */ | |
b37ad28b | 3620 | static __be32 |
1da177e4 LT |
3621 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
3622 | { | |
1da177e4 | 3623 | struct nfs4_file *fp; |
baeb4ff0 | 3624 | __be32 ret = nfs_ok; |
1da177e4 | 3625 | |
ca943217 | 3626 | fp = find_file(¤t_fh->fh_handle); |
13cd2184 | 3627 | if (!fp) |
baeb4ff0 JL |
3628 | return ret; |
3629 | /* Check for conflicting share reservations */ | |
1d31a253 | 3630 | spin_lock(&fp->fi_lock); |
baeb4ff0 JL |
3631 | if (fp->fi_share_deny & deny_type) |
3632 | ret = nfserr_locked; | |
1d31a253 | 3633 | spin_unlock(&fp->fi_lock); |
13cd2184 N |
3634 | put_nfs4_file(fp); |
3635 | return ret; | |
1da177e4 LT |
3636 | } |
3637 | ||
0162ac2b | 3638 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
1da177e4 | 3639 | { |
0162ac2b | 3640 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
11b9164a TM |
3641 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
3642 | nfsd_net_id); | |
e8c69d17 | 3643 | |
11b9164a | 3644 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
f54fe962 | 3645 | |
dff1399f | 3646 | /* |
f54fe962 JL |
3647 | * We can't do this in nfsd_break_deleg_cb because it is |
3648 | * already holding inode->i_lock. | |
3649 | * | |
dff1399f JL |
3650 | * If the dl_time != 0, then we know that it has already been |
3651 | * queued for a lease break. Don't queue it again. | |
3652 | */ | |
f54fe962 | 3653 | spin_lock(&state_lock); |
dff1399f | 3654 | if (dp->dl_time == 0) { |
dff1399f | 3655 | dp->dl_time = get_seconds(); |
02e1215f | 3656 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
dff1399f | 3657 | } |
02e1215f JL |
3658 | spin_unlock(&state_lock); |
3659 | } | |
1da177e4 | 3660 | |
0162ac2b CH |
3661 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
3662 | struct rpc_task *task) | |
3663 | { | |
3664 | struct nfs4_delegation *dp = cb_to_delegation(cb); | |
3665 | ||
a457974f AE |
3666 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) |
3667 | return 1; | |
3668 | ||
0162ac2b CH |
3669 | switch (task->tk_status) { |
3670 | case 0: | |
3671 | return 1; | |
3672 | case -EBADHANDLE: | |
3673 | case -NFS4ERR_BAD_STATEID: | |
3674 | /* | |
3675 | * Race: client probably got cb_recall before open reply | |
3676 | * granting delegation. | |
3677 | */ | |
3678 | if (dp->dl_retries--) { | |
3679 | rpc_delay(task, 2 * HZ); | |
3680 | return 0; | |
3681 | } | |
3682 | /*FALLTHRU*/ | |
3683 | default: | |
3684 | return -1; | |
3685 | } | |
3686 | } | |
3687 | ||
3688 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) | |
3689 | { | |
3690 | struct nfs4_delegation *dp = cb_to_delegation(cb); | |
3691 | ||
3692 | nfs4_put_stid(&dp->dl_stid); | |
3693 | } | |
3694 | ||
c4cb8974 | 3695 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
0162ac2b CH |
3696 | .prepare = nfsd4_cb_recall_prepare, |
3697 | .done = nfsd4_cb_recall_done, | |
3698 | .release = nfsd4_cb_recall_release, | |
3699 | }; | |
3700 | ||
02e1215f JL |
3701 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
3702 | { | |
3703 | /* | |
3704 | * We're assuming the state code never drops its reference | |
3705 | * without first removing the lease. Since we're in this lease | |
3706 | * callback (and since the lease code is serialized by the kernel | |
3707 | * lock) we know the server hasn't removed the lease yet, we know | |
3708 | * it's safe to take a reference. | |
3709 | */ | |
72c0b0fb | 3710 | atomic_inc(&dp->dl_stid.sc_count); |
f0b5de1b | 3711 | nfsd4_run_cb(&dp->dl_recall); |
6b57d9c8 BF |
3712 | } |
3713 | ||
1c8c601a | 3714 | /* Called from break_lease() with i_lock held. */ |
4d01b7f5 JL |
3715 | static bool |
3716 | nfsd_break_deleg_cb(struct file_lock *fl) | |
6b57d9c8 | 3717 | { |
4d01b7f5 | 3718 | bool ret = false; |
acfdf5c3 BF |
3719 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
3720 | struct nfs4_delegation *dp; | |
6b57d9c8 | 3721 | |
7fa10cd1 BF |
3722 | if (!fp) { |
3723 | WARN(1, "(%p)->fl_owner NULL\n", fl); | |
4d01b7f5 | 3724 | return ret; |
7fa10cd1 BF |
3725 | } |
3726 | if (fp->fi_had_conflict) { | |
3727 | WARN(1, "duplicate break on %p\n", fp); | |
4d01b7f5 | 3728 | return ret; |
7fa10cd1 | 3729 | } |
0272e1fd BF |
3730 | /* |
3731 | * We don't want the locks code to timeout the lease for us; | |
acfdf5c3 | 3732 | * we'll remove it ourself if a delegation isn't returned |
6b57d9c8 | 3733 | * in time: |
0272e1fd BF |
3734 | */ |
3735 | fl->fl_break_time = 0; | |
1da177e4 | 3736 | |
02e1215f | 3737 | spin_lock(&fp->fi_lock); |
417c6629 JL |
3738 | fp->fi_had_conflict = true; |
3739 | /* | |
4d01b7f5 JL |
3740 | * If there are no delegations on the list, then return true |
3741 | * so that the lease code will go ahead and delete it. | |
417c6629 JL |
3742 | */ |
3743 | if (list_empty(&fp->fi_delegations)) | |
4d01b7f5 | 3744 | ret = true; |
417c6629 JL |
3745 | else |
3746 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) | |
3747 | nfsd_break_one_deleg(dp); | |
02e1215f | 3748 | spin_unlock(&fp->fi_lock); |
4d01b7f5 | 3749 | return ret; |
1da177e4 LT |
3750 | } |
3751 | ||
c45198ed | 3752 | static int |
7448cc37 JL |
3753 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
3754 | struct list_head *dispose) | |
1da177e4 LT |
3755 | { |
3756 | if (arg & F_UNLCK) | |
c45198ed | 3757 | return lease_modify(onlist, arg, dispose); |
1da177e4 LT |
3758 | else |
3759 | return -EAGAIN; | |
3760 | } | |
3761 | ||
7b021967 | 3762 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
8fb47a4f BF |
3763 | .lm_break = nfsd_break_deleg_cb, |
3764 | .lm_change = nfsd_change_deleg_cb, | |
1da177e4 LT |
3765 | }; |
3766 | ||
7a8711c9 BF |
3767 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
3768 | { | |
3769 | if (nfsd4_has_session(cstate)) | |
3770 | return nfs_ok; | |
3771 | if (seqid == so->so_seqid - 1) | |
3772 | return nfserr_replay_me; | |
3773 | if (seqid == so->so_seqid) | |
3774 | return nfs_ok; | |
3775 | return nfserr_bad_seqid; | |
3776 | } | |
1da177e4 | 3777 | |
4b24ca7d JL |
3778 | static __be32 lookup_clientid(clientid_t *clid, |
3779 | struct nfsd4_compound_state *cstate, | |
3780 | struct nfsd_net *nn) | |
3781 | { | |
3782 | struct nfs4_client *found; | |
3783 | ||
3784 | if (cstate->clp) { | |
3785 | found = cstate->clp; | |
3786 | if (!same_clid(&found->cl_clientid, clid)) | |
3787 | return nfserr_stale_clientid; | |
3788 | return nfs_ok; | |
3789 | } | |
3790 | ||
3791 | if (STALE_CLIENTID(clid, nn)) | |
3792 | return nfserr_stale_clientid; | |
3793 | ||
3794 | /* | |
3795 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one | |
3796 | * cached already then we know this is for is for v4.0 and "sessions" | |
3797 | * will be false. | |
3798 | */ | |
3799 | WARN_ON_ONCE(cstate->session); | |
3e339f96 | 3800 | spin_lock(&nn->client_lock); |
4b24ca7d | 3801 | found = find_confirmed_client(clid, false, nn); |
3e339f96 TM |
3802 | if (!found) { |
3803 | spin_unlock(&nn->client_lock); | |
4b24ca7d | 3804 | return nfserr_expired; |
3e339f96 TM |
3805 | } |
3806 | atomic_inc(&found->cl_refcount); | |
3807 | spin_unlock(&nn->client_lock); | |
4b24ca7d JL |
3808 | |
3809 | /* Cache the nfs4_client in cstate! */ | |
3810 | cstate->clp = found; | |
4b24ca7d JL |
3811 | return nfs_ok; |
3812 | } | |
3813 | ||
b37ad28b | 3814 | __be32 |
6668958f | 3815 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
3320fef1 | 3816 | struct nfsd4_open *open, struct nfsd_net *nn) |
1da177e4 | 3817 | { |
1da177e4 LT |
3818 | clientid_t *clientid = &open->op_clientid; |
3819 | struct nfs4_client *clp = NULL; | |
3820 | unsigned int strhashval; | |
fe0750e5 | 3821 | struct nfs4_openowner *oo = NULL; |
4cdc951b | 3822 | __be32 status; |
1da177e4 | 3823 | |
2c142baa | 3824 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
1da177e4 | 3825 | return nfserr_stale_clientid; |
32513b40 BF |
3826 | /* |
3827 | * In case we need it later, after we've already created the | |
3828 | * file and don't want to risk a further failure: | |
3829 | */ | |
3830 | open->op_file = nfsd4_alloc_file(); | |
3831 | if (open->op_file == NULL) | |
3832 | return nfserr_jukebox; | |
1da177e4 | 3833 | |
2d91e895 TM |
3834 | status = lookup_clientid(clientid, cstate, nn); |
3835 | if (status) | |
3836 | return status; | |
3837 | clp = cstate->clp; | |
3838 | ||
d4f0489f TM |
3839 | strhashval = ownerstr_hashval(&open->op_owner); |
3840 | oo = find_openstateowner_str(strhashval, open, clp); | |
fe0750e5 BF |
3841 | open->op_openowner = oo; |
3842 | if (!oo) { | |
bcf130f9 | 3843 | goto new_owner; |
1da177e4 | 3844 | } |
dad1c067 | 3845 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
0f442aa2 | 3846 | /* Replace unconfirmed owners without checking for replay. */ |
fe0750e5 BF |
3847 | release_openowner(oo); |
3848 | open->op_openowner = NULL; | |
bcf130f9 | 3849 | goto new_owner; |
0f442aa2 | 3850 | } |
4cdc951b BF |
3851 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
3852 | if (status) | |
3853 | return status; | |
4cdc951b | 3854 | goto alloc_stateid; |
bcf130f9 | 3855 | new_owner: |
13d6f66b | 3856 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
bcf130f9 BF |
3857 | if (oo == NULL) |
3858 | return nfserr_jukebox; | |
3859 | open->op_openowner = oo; | |
4cdc951b | 3860 | alloc_stateid: |
b49e084d | 3861 | open->op_stp = nfs4_alloc_open_stateid(clp); |
4cdc951b BF |
3862 | if (!open->op_stp) |
3863 | return nfserr_jukebox; | |
8287f009 SB |
3864 | |
3865 | if (nfsd4_has_session(cstate) && | |
3866 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { | |
3867 | open->op_odstate = alloc_clnt_odstate(clp); | |
3868 | if (!open->op_odstate) | |
3869 | return nfserr_jukebox; | |
3870 | } | |
3871 | ||
0f442aa2 | 3872 | return nfs_ok; |
1da177e4 LT |
3873 | } |
3874 | ||
b37ad28b | 3875 | static inline __be32 |
4a6e43e6 N |
3876 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
3877 | { | |
3878 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) | |
3879 | return nfserr_openmode; | |
3880 | else | |
3881 | return nfs_ok; | |
3882 | } | |
3883 | ||
f459e453 | 3884 | static int share_access_to_flags(u32 share_access) |
52f4fb43 | 3885 | { |
f459e453 | 3886 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
52f4fb43 N |
3887 | } |
3888 | ||
38c2f4b1 | 3889 | static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s) |
24a0111e | 3890 | { |
f459e453 | 3891 | struct nfs4_stid *ret; |
24a0111e | 3892 | |
38c2f4b1 | 3893 | ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID); |
f459e453 BF |
3894 | if (!ret) |
3895 | return NULL; | |
3896 | return delegstateid(ret); | |
24a0111e BF |
3897 | } |
3898 | ||
8b289b2c BF |
3899 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
3900 | { | |
3901 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || | |
3902 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; | |
3903 | } | |
3904 | ||
b37ad28b | 3905 | static __be32 |
41d22663 | 3906 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
567d9829 N |
3907 | struct nfs4_delegation **dp) |
3908 | { | |
3909 | int flags; | |
b37ad28b | 3910 | __be32 status = nfserr_bad_stateid; |
dcd94cc2 | 3911 | struct nfs4_delegation *deleg; |
567d9829 | 3912 | |
dcd94cc2 TM |
3913 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
3914 | if (deleg == NULL) | |
c44c5eeb | 3915 | goto out; |
24a0111e | 3916 | flags = share_access_to_flags(open->op_share_access); |
dcd94cc2 TM |
3917 | status = nfs4_check_delegmode(deleg, flags); |
3918 | if (status) { | |
3919 | nfs4_put_stid(&deleg->dl_stid); | |
3920 | goto out; | |
3921 | } | |
3922 | *dp = deleg; | |
c44c5eeb | 3923 | out: |
8b289b2c | 3924 | if (!nfsd4_is_deleg_cur(open)) |
c44c5eeb N |
3925 | return nfs_ok; |
3926 | if (status) | |
3927 | return status; | |
dad1c067 | 3928 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
c44c5eeb | 3929 | return nfs_ok; |
567d9829 N |
3930 | } |
3931 | ||
21fb4016 BF |
3932 | static inline int nfs4_access_to_access(u32 nfs4_access) |
3933 | { | |
3934 | int flags = 0; | |
3935 | ||
3936 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) | |
3937 | flags |= NFSD_MAY_READ; | |
3938 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) | |
3939 | flags |= NFSD_MAY_WRITE; | |
3940 | return flags; | |
3941 | } | |
3942 | ||
7e6a72e5 CH |
3943 | static inline __be32 |
3944 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, | |
3945 | struct nfsd4_open *open) | |
3946 | { | |
3947 | struct iattr iattr = { | |
3948 | .ia_valid = ATTR_SIZE, | |
3949 | .ia_size = 0, | |
3950 | }; | |
3951 | if (!open->op_truncate) | |
3952 | return 0; | |
3953 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) | |
3954 | return nfserr_inval; | |
3955 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); | |
3956 | } | |
3957 | ||
0c12eaff | 3958 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
6eb3a1d0 JL |
3959 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
3960 | struct nfsd4_open *open) | |
f9d7562f | 3961 | { |
de18643d | 3962 | struct file *filp = NULL; |
f9d7562f | 3963 | __be32 status; |
0c12eaff CB |
3964 | int oflag = nfs4_access_to_omode(open->op_share_access); |
3965 | int access = nfs4_access_to_access(open->op_share_access); | |
baeb4ff0 | 3966 | unsigned char old_access_bmap, old_deny_bmap; |
0c12eaff | 3967 | |
de18643d | 3968 | spin_lock(&fp->fi_lock); |
baeb4ff0 JL |
3969 | |
3970 | /* | |
3971 | * Are we trying to set a deny mode that would conflict with | |
3972 | * current access? | |
3973 | */ | |
3974 | status = nfs4_file_check_deny(fp, open->op_share_deny); | |
3975 | if (status != nfs_ok) { | |
3976 | spin_unlock(&fp->fi_lock); | |
3977 | goto out; | |
3978 | } | |
3979 | ||
3980 | /* set access to the file */ | |
3981 | status = nfs4_file_get_access(fp, open->op_share_access); | |
3982 | if (status != nfs_ok) { | |
3983 | spin_unlock(&fp->fi_lock); | |
3984 | goto out; | |
3985 | } | |
3986 | ||
3987 | /* Set access bits in stateid */ | |
3988 | old_access_bmap = stp->st_access_bmap; | |
3989 | set_access(open->op_share_access, stp); | |
3990 | ||
3991 | /* Set new deny mask */ | |
3992 | old_deny_bmap = stp->st_deny_bmap; | |
3993 | set_deny(open->op_share_deny, stp); | |
3994 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); | |
3995 | ||
f9d7562f | 3996 | if (!fp->fi_fds[oflag]) { |
de18643d TM |
3997 | spin_unlock(&fp->fi_lock); |
3998 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp); | |
f9d7562f | 3999 | if (status) |
baeb4ff0 | 4000 | goto out_put_access; |
de18643d TM |
4001 | spin_lock(&fp->fi_lock); |
4002 | if (!fp->fi_fds[oflag]) { | |
4003 | fp->fi_fds[oflag] = filp; | |
4004 | filp = NULL; | |
4005 | } | |
f9d7562f | 4006 | } |
de18643d TM |
4007 | spin_unlock(&fp->fi_lock); |
4008 | if (filp) | |
4009 | fput(filp); | |
f9d7562f | 4010 | |
7e6a72e5 CH |
4011 | status = nfsd4_truncate(rqstp, cur_fh, open); |
4012 | if (status) | |
4013 | goto out_put_access; | |
7e6a72e5 CH |
4014 | out: |
4015 | return status; | |
baeb4ff0 JL |
4016 | out_put_access: |
4017 | stp->st_access_bmap = old_access_bmap; | |
4018 | nfs4_file_put_access(fp, open->op_share_access); | |
4019 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); | |
4020 | goto out; | |
1da177e4 LT |
4021 | } |
4022 | ||
b37ad28b | 4023 | static __be32 |
dcef0413 | 4024 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open) |
1da177e4 | 4025 | { |
b37ad28b | 4026 | __be32 status; |
6ac75368 | 4027 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
1da177e4 | 4028 | |
6eb3a1d0 | 4029 | if (!test_access(open->op_share_access, stp)) |
baeb4ff0 | 4030 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
7e6a72e5 | 4031 | |
baeb4ff0 JL |
4032 | /* test and set deny mode */ |
4033 | spin_lock(&fp->fi_lock); | |
4034 | status = nfs4_file_check_deny(fp, open->op_share_deny); | |
4035 | if (status == nfs_ok) { | |
baeb4ff0 JL |
4036 | set_deny(open->op_share_deny, stp); |
4037 | fp->fi_share_deny |= | |
4038 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); | |
4039 | } | |
4040 | spin_unlock(&fp->fi_lock); | |
4041 | ||
4042 | if (status != nfs_ok) | |
1da177e4 | 4043 | return status; |
1da177e4 | 4044 | |
baeb4ff0 JL |
4045 | status = nfsd4_truncate(rqstp, cur_fh, open); |
4046 | if (status != nfs_ok) | |
4047 | reset_union_bmap_deny(old_deny_bmap, stp); | |
4048 | return status; | |
4049 | } | |
1da177e4 | 4050 | |
14a24e99 BF |
4051 | /* Should we give out recallable state?: */ |
4052 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) | |
4053 | { | |
4054 | if (clp->cl_cb_state == NFSD4_CB_UP) | |
4055 | return true; | |
4056 | /* | |
4057 | * In the sessions case, since we don't have to establish a | |
4058 | * separate connection for callbacks, we assume it's OK | |
4059 | * until we hear otherwise: | |
4060 | */ | |
4061 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; | |
4062 | } | |
4063 | ||
d564fbec | 4064 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag) |
22d38c4c BF |
4065 | { |
4066 | struct file_lock *fl; | |
4067 | ||
4068 | fl = locks_alloc_lock(); | |
4069 | if (!fl) | |
4070 | return NULL; | |
22d38c4c | 4071 | fl->fl_lmops = &nfsd_lease_mng_ops; |
617588d5 | 4072 | fl->fl_flags = FL_DELEG; |
22d38c4c BF |
4073 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
4074 | fl->fl_end = OFFSET_MAX; | |
d564fbec | 4075 | fl->fl_owner = (fl_owner_t)fp; |
22d38c4c | 4076 | fl->fl_pid = current->tgid; |
22d38c4c BF |
4077 | return fl; |
4078 | } | |
4079 | ||
34ed9872 AE |
4080 | /** |
4081 | * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer | |
4082 | * @dp: a pointer to the nfs4_delegation we're adding. | |
4083 | * | |
4084 | * Return: | |
4085 | * On success: Return code will be 0 on success. | |
4086 | * | |
4087 | * On error: -EAGAIN if there was an existing delegation. | |
4088 | * nonzero if there is an error in other cases. | |
4089 | * | |
4090 | */ | |
4091 | ||
99c41515 | 4092 | static int nfs4_setlease(struct nfs4_delegation *dp) |
edab9782 | 4093 | { |
11b9164a | 4094 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
efde6b4d | 4095 | struct file_lock *fl; |
417c6629 JL |
4096 | struct file *filp; |
4097 | int status = 0; | |
edab9782 | 4098 | |
d564fbec | 4099 | fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ); |
edab9782 BF |
4100 | if (!fl) |
4101 | return -ENOMEM; | |
417c6629 JL |
4102 | filp = find_readable_file(fp); |
4103 | if (!filp) { | |
4104 | /* We should always have a readable file here */ | |
4105 | WARN_ON_ONCE(1); | |
af9dbaf4 | 4106 | locks_free_lock(fl); |
417c6629 JL |
4107 | return -EBADF; |
4108 | } | |
4109 | fl->fl_file = filp; | |
e6f5c789 | 4110 | status = vfs_setlease(filp, fl->fl_type, &fl, NULL); |
1c7dd2ff | 4111 | if (fl) |
417c6629 | 4112 | locks_free_lock(fl); |
1c7dd2ff | 4113 | if (status) |
417c6629 | 4114 | goto out_fput; |
417c6629 JL |
4115 | spin_lock(&state_lock); |
4116 | spin_lock(&fp->fi_lock); | |
4117 | /* Did the lease get broken before we took the lock? */ | |
4118 | status = -EAGAIN; | |
4119 | if (fp->fi_had_conflict) | |
4120 | goto out_unlock; | |
4121 | /* Race breaker */ | |
0c637be8 | 4122 | if (fp->fi_deleg_file) { |
34ed9872 | 4123 | status = hash_delegation_locked(dp, fp); |
417c6629 JL |
4124 | goto out_unlock; |
4125 | } | |
417c6629 | 4126 | fp->fi_deleg_file = filp; |
34ed9872 AE |
4127 | fp->fi_delegees = 0; |
4128 | status = hash_delegation_locked(dp, fp); | |
417c6629 | 4129 | spin_unlock(&fp->fi_lock); |
cdc97505 | 4130 | spin_unlock(&state_lock); |
34ed9872 AE |
4131 | if (status) { |
4132 | /* Should never happen, this is a new fi_deleg_file */ | |
4133 | WARN_ON_ONCE(1); | |
4134 | goto out_fput; | |
4135 | } | |
acfdf5c3 | 4136 | return 0; |
417c6629 JL |
4137 | out_unlock: |
4138 | spin_unlock(&fp->fi_lock); | |
4139 | spin_unlock(&state_lock); | |
4140 | out_fput: | |
4141 | fput(filp); | |
e873088f | 4142 | return status; |
acfdf5c3 BF |
4143 | } |
4144 | ||
0b26693c JL |
4145 | static struct nfs4_delegation * |
4146 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, | |
8287f009 | 4147 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
acfdf5c3 | 4148 | { |
0b26693c JL |
4149 | int status; |
4150 | struct nfs4_delegation *dp; | |
417c6629 | 4151 | |
bf7bd3e9 | 4152 | if (fp->fi_had_conflict) |
0b26693c JL |
4153 | return ERR_PTR(-EAGAIN); |
4154 | ||
34ed9872 AE |
4155 | spin_lock(&state_lock); |
4156 | spin_lock(&fp->fi_lock); | |
4157 | status = nfs4_get_existing_delegation(clp, fp); | |
4158 | spin_unlock(&fp->fi_lock); | |
4159 | spin_unlock(&state_lock); | |
4160 | ||
4161 | if (status) | |
4162 | return ERR_PTR(status); | |
4163 | ||
8287f009 | 4164 | dp = alloc_init_deleg(clp, fh, odstate); |
0b26693c JL |
4165 | if (!dp) |
4166 | return ERR_PTR(-ENOMEM); | |
4167 | ||
bf7bd3e9 | 4168 | get_nfs4_file(fp); |
417c6629 JL |
4169 | spin_lock(&state_lock); |
4170 | spin_lock(&fp->fi_lock); | |
11b9164a | 4171 | dp->dl_stid.sc_file = fp; |
0c637be8 | 4172 | if (!fp->fi_deleg_file) { |
417c6629 JL |
4173 | spin_unlock(&fp->fi_lock); |
4174 | spin_unlock(&state_lock); | |
0b26693c JL |
4175 | status = nfs4_setlease(dp); |
4176 | goto out; | |
417c6629 | 4177 | } |
acfdf5c3 | 4178 | if (fp->fi_had_conflict) { |
417c6629 JL |
4179 | status = -EAGAIN; |
4180 | goto out_unlock; | |
acfdf5c3 | 4181 | } |
34ed9872 | 4182 | status = hash_delegation_locked(dp, fp); |
417c6629 JL |
4183 | out_unlock: |
4184 | spin_unlock(&fp->fi_lock); | |
cdc97505 | 4185 | spin_unlock(&state_lock); |
0b26693c JL |
4186 | out: |
4187 | if (status) { | |
8287f009 | 4188 | put_clnt_odstate(dp->dl_clnt_odstate); |
6011695d | 4189 | nfs4_put_stid(&dp->dl_stid); |
0b26693c JL |
4190 | return ERR_PTR(status); |
4191 | } | |
4192 | return dp; | |
edab9782 BF |
4193 | } |
4194 | ||
4aa8913c BH |
4195 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
4196 | { | |
4197 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
4198 | if (status == -EAGAIN) | |
4199 | open->op_why_no_deleg = WND4_CONTENTION; | |
4200 | else { | |
4201 | open->op_why_no_deleg = WND4_RESOURCE; | |
4202 | switch (open->op_deleg_want) { | |
4203 | case NFS4_SHARE_WANT_READ_DELEG: | |
4204 | case NFS4_SHARE_WANT_WRITE_DELEG: | |
4205 | case NFS4_SHARE_WANT_ANY_DELEG: | |
4206 | break; | |
4207 | case NFS4_SHARE_WANT_CANCEL: | |
4208 | open->op_why_no_deleg = WND4_CANCELLED; | |
4209 | break; | |
4210 | case NFS4_SHARE_WANT_NO_DELEG: | |
063b0fb9 | 4211 | WARN_ON_ONCE(1); |
4aa8913c BH |
4212 | } |
4213 | } | |
4214 | } | |
4215 | ||
1da177e4 LT |
4216 | /* |
4217 | * Attempt to hand out a delegation. | |
99c41515 BF |
4218 | * |
4219 | * Note we don't support write delegations, and won't until the vfs has | |
4220 | * proper support for them. | |
1da177e4 LT |
4221 | */ |
4222 | static void | |
4cf59221 JL |
4223 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
4224 | struct nfs4_ol_stateid *stp) | |
1da177e4 LT |
4225 | { |
4226 | struct nfs4_delegation *dp; | |
4cf59221 JL |
4227 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
4228 | struct nfs4_client *clp = stp->st_stid.sc_client; | |
14a24e99 | 4229 | int cb_up; |
99c41515 | 4230 | int status = 0; |
1da177e4 | 4231 | |
fe0750e5 | 4232 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
7b190fec N |
4233 | open->op_recall = 0; |
4234 | switch (open->op_claim_type) { | |
4235 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
2bf23875 | 4236 | if (!cb_up) |
7b190fec | 4237 | open->op_recall = 1; |
99c41515 BF |
4238 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
4239 | goto out_no_deleg; | |
7b190fec N |
4240 | break; |
4241 | case NFS4_OPEN_CLAIM_NULL: | |
ed47b062 | 4242 | case NFS4_OPEN_CLAIM_FH: |
99c41515 BF |
4243 | /* |
4244 | * Let's not give out any delegations till everyone's | |
c87fb4a3 BF |
4245 | * had the chance to reclaim theirs, *and* until |
4246 | * NLM locks have all been reclaimed: | |
99c41515 | 4247 | */ |
4cf59221 | 4248 | if (locks_in_grace(clp->net)) |
99c41515 | 4249 | goto out_no_deleg; |
dad1c067 | 4250 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
99c41515 | 4251 | goto out_no_deleg; |
9a0590ae SD |
4252 | /* |
4253 | * Also, if the file was opened for write or | |
4254 | * create, there's a good chance the client's | |
4255 | * about to write to it, resulting in an | |
4256 | * immediate recall (since we don't support | |
4257 | * write delegations): | |
4258 | */ | |
7b190fec | 4259 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
99c41515 BF |
4260 | goto out_no_deleg; |
4261 | if (open->op_create == NFS4_OPEN_CREATE) | |
4262 | goto out_no_deleg; | |
7b190fec N |
4263 | break; |
4264 | default: | |
99c41515 | 4265 | goto out_no_deleg; |
7b190fec | 4266 | } |
8287f009 | 4267 | dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate); |
0b26693c | 4268 | if (IS_ERR(dp)) |
dd239cc0 | 4269 | goto out_no_deleg; |
1da177e4 | 4270 | |
d5477a8d | 4271 | memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); |
1da177e4 | 4272 | |
8c10cbdb | 4273 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
d5477a8d | 4274 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
99c41515 | 4275 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
67cb1279 | 4276 | nfs4_put_stid(&dp->dl_stid); |
dd239cc0 | 4277 | return; |
dd239cc0 | 4278 | out_no_deleg: |
99c41515 BF |
4279 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
4280 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && | |
d08d32e6 | 4281 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
99c41515 | 4282 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
d08d32e6 BF |
4283 | open->op_recall = 1; |
4284 | } | |
99c41515 BF |
4285 | |
4286 | /* 4.1 client asking for a delegation? */ | |
4287 | if (open->op_deleg_want) | |
4288 | nfsd4_open_deleg_none_ext(open, status); | |
4289 | return; | |
1da177e4 LT |
4290 | } |
4291 | ||
e27f49c3 BH |
4292 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
4293 | struct nfs4_delegation *dp) | |
4294 | { | |
4295 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && | |
4296 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { | |
4297 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
4298 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; | |
4299 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && | |
4300 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { | |
4301 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
4302 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; | |
4303 | } | |
4304 | /* Otherwise the client must be confused wanting a delegation | |
4305 | * it already has, therefore we don't return | |
4306 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. | |
4307 | */ | |
4308 | } | |
4309 | ||
b37ad28b | 4310 | __be32 |
1da177e4 LT |
4311 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
4312 | { | |
6668958f | 4313 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
38c2f4b1 | 4314 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
1da177e4 | 4315 | struct nfs4_file *fp = NULL; |
dcef0413 | 4316 | struct nfs4_ol_stateid *stp = NULL; |
567d9829 | 4317 | struct nfs4_delegation *dp = NULL; |
b37ad28b | 4318 | __be32 status; |
1da177e4 | 4319 | |
1da177e4 LT |
4320 | /* |
4321 | * Lookup file; if found, lookup stateid and check open request, | |
4322 | * and check for delegations in the process of being recalled. | |
4323 | * If not found, create the nfs4_file struct | |
4324 | */ | |
f9c00c3a | 4325 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
950e0118 | 4326 | if (fp != open->op_file) { |
41d22663 | 4327 | status = nfs4_check_deleg(cl, open, &dp); |
c44c5eeb N |
4328 | if (status) |
4329 | goto out; | |
7fc0564e | 4330 | spin_lock(&fp->fi_lock); |
a46cb7f2 | 4331 | stp = nfsd4_find_existing_open(fp, open); |
7fc0564e | 4332 | spin_unlock(&fp->fi_lock); |
1da177e4 | 4333 | } else { |
950e0118 | 4334 | open->op_file = NULL; |
c44c5eeb | 4335 | status = nfserr_bad_stateid; |
8b289b2c | 4336 | if (nfsd4_is_deleg_cur(open)) |
c44c5eeb | 4337 | goto out; |
1da177e4 LT |
4338 | } |
4339 | ||
4340 | /* | |
4341 | * OPEN the file, or upgrade an existing OPEN. | |
4342 | * If truncate fails, the OPEN fails. | |
4343 | */ | |
4344 | if (stp) { | |
4345 | /* Stateid was found, this is an OPEN upgrade */ | |
feb9dad5 | 4346 | mutex_lock(&stp->st_mutex); |
f9d7562f | 4347 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
35a92fe8 | 4348 | if (status) { |
feb9dad5 | 4349 | mutex_unlock(&stp->st_mutex); |
1da177e4 | 4350 | goto out; |
35a92fe8 | 4351 | } |
1da177e4 | 4352 | } else { |
8c7245ab OD |
4353 | /* stp is returned locked. */ |
4354 | stp = init_open_stateid(fp, open); | |
4355 | /* See if we lost the race to some other thread */ | |
4356 | if (stp->st_access_bmap != 0) { | |
7fc0564e AE |
4357 | status = nfs4_upgrade_open(rqstp, fp, current_fh, |
4358 | stp, open); | |
4359 | if (status) { | |
feb9dad5 | 4360 | mutex_unlock(&stp->st_mutex); |
7fc0564e AE |
4361 | goto out; |
4362 | } | |
4363 | goto upgrade_out; | |
4364 | } | |
6eb3a1d0 JL |
4365 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
4366 | if (status) { | |
feb9dad5 | 4367 | mutex_unlock(&stp->st_mutex); |
6eb3a1d0 JL |
4368 | release_open_stateid(stp); |
4369 | goto out; | |
4370 | } | |
8287f009 SB |
4371 | |
4372 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, | |
4373 | open->op_odstate); | |
4374 | if (stp->st_clnt_odstate == open->op_odstate) | |
4375 | open->op_odstate = NULL; | |
1da177e4 | 4376 | } |
7fc0564e | 4377 | upgrade_out: |
9767feb2 | 4378 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
feb9dad5 | 4379 | mutex_unlock(&stp->st_mutex); |
1da177e4 | 4380 | |
d24433cd | 4381 | if (nfsd4_has_session(&resp->cstate)) { |
d24433cd BH |
4382 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
4383 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; | |
4384 | open->op_why_no_deleg = WND4_NOT_WANTED; | |
4385 | goto nodeleg; | |
4386 | } | |
4387 | } | |
4388 | ||
1da177e4 LT |
4389 | /* |
4390 | * Attempt to hand out a delegation. No error return, because the | |
4391 | * OPEN succeeds even if we fail. | |
4392 | */ | |
4cf59221 | 4393 | nfs4_open_delegation(current_fh, open, stp); |
d24433cd | 4394 | nodeleg: |
1da177e4 LT |
4395 | status = nfs_ok; |
4396 | ||
8c10cbdb | 4397 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
dcef0413 | 4398 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
1da177e4 | 4399 | out: |
d24433cd BH |
4400 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
4401 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && | |
e27f49c3 BH |
4402 | open->op_deleg_want) |
4403 | nfsd4_deleg_xgrade_none_ext(open, dp); | |
d24433cd | 4404 | |
13cd2184 N |
4405 | if (fp) |
4406 | put_nfs4_file(fp); | |
37515177 | 4407 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
87186022 | 4408 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
1da177e4 LT |
4409 | /* |
4410 | * To finish the open response, we just need to set the rflags. | |
4411 | */ | |
4412 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; | |
dad1c067 | 4413 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
6668958f | 4414 | !nfsd4_has_session(&resp->cstate)) |
1da177e4 | 4415 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
dcd94cc2 TM |
4416 | if (dp) |
4417 | nfs4_put_stid(&dp->dl_stid); | |
d6f2bc5d TM |
4418 | if (stp) |
4419 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 LT |
4420 | |
4421 | return status; | |
4422 | } | |
4423 | ||
58fb12e6 | 4424 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
42297899 | 4425 | struct nfsd4_open *open) |
d29b20cd BF |
4426 | { |
4427 | if (open->op_openowner) { | |
d3134b10 JL |
4428 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
4429 | ||
4430 | nfsd4_cstate_assign_replay(cstate, so); | |
4431 | nfs4_put_stateowner(so); | |
d29b20cd | 4432 | } |
32513b40 | 4433 | if (open->op_file) |
5b095e99 | 4434 | kmem_cache_free(file_slab, open->op_file); |
4cdc951b | 4435 | if (open->op_stp) |
6011695d | 4436 | nfs4_put_stid(&open->op_stp->st_stid); |
8287f009 SB |
4437 | if (open->op_odstate) |
4438 | kmem_cache_free(odstate_slab, open->op_odstate); | |
d29b20cd BF |
4439 | } |
4440 | ||
b37ad28b | 4441 | __be32 |
b591480b BF |
4442 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
4443 | clientid_t *clid) | |
1da177e4 LT |
4444 | { |
4445 | struct nfs4_client *clp; | |
b37ad28b | 4446 | __be32 status; |
7f2210fa | 4447 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 4448 | |
1da177e4 LT |
4449 | dprintk("process_renew(%08x/%08x): starting\n", |
4450 | clid->cl_boot, clid->cl_id); | |
4b24ca7d | 4451 | status = lookup_clientid(clid, cstate, nn); |
9b2ef62b | 4452 | if (status) |
1da177e4 | 4453 | goto out; |
4b24ca7d | 4454 | clp = cstate->clp; |
1da177e4 | 4455 | status = nfserr_cb_path_down; |
ea1da636 | 4456 | if (!list_empty(&clp->cl_delegations) |
77a3569d | 4457 | && clp->cl_cb_state != NFSD4_CB_UP) |
1da177e4 LT |
4458 | goto out; |
4459 | status = nfs_ok; | |
4460 | out: | |
1da177e4 LT |
4461 | return status; |
4462 | } | |
4463 | ||
7f5ef2e9 | 4464 | void |
12760c66 | 4465 | nfsd4_end_grace(struct nfsd_net *nn) |
a76b4319 | 4466 | { |
33dcc481 | 4467 | /* do nothing if grace period already ended */ |
a51c84ed | 4468 | if (nn->grace_ended) |
33dcc481 JL |
4469 | return; |
4470 | ||
a76b4319 | 4471 | dprintk("NFSD: end of grace period\n"); |
a51c84ed | 4472 | nn->grace_ended = true; |
70b28235 BF |
4473 | /* |
4474 | * If the server goes down again right now, an NFSv4 | |
4475 | * client will still be allowed to reclaim after it comes back up, | |
4476 | * even if it hasn't yet had a chance to reclaim state this time. | |
4477 | * | |
4478 | */ | |
919b8049 | 4479 | nfsd4_record_grace_done(nn); |
70b28235 BF |
4480 | /* |
4481 | * At this point, NFSv4 clients can still reclaim. But if the | |
4482 | * server crashes, any that have not yet reclaimed will be out | |
4483 | * of luck on the next boot. | |
4484 | * | |
4485 | * (NFSv4.1+ clients are considered to have reclaimed once they | |
4486 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to | |
4487 | * have reclaimed after their first OPEN.) | |
4488 | */ | |
5e1533c7 | 4489 | locks_end_grace(&nn->nfsd4_manager); |
70b28235 BF |
4490 | /* |
4491 | * At this point, and once lockd and/or any other containers | |
4492 | * exit their grace period, further reclaims will fail and | |
4493 | * regular locking can resume. | |
4494 | */ | |
a76b4319 N |
4495 | } |
4496 | ||
fd39ca9a | 4497 | static time_t |
09121281 | 4498 | nfs4_laundromat(struct nfsd_net *nn) |
1da177e4 LT |
4499 | { |
4500 | struct nfs4_client *clp; | |
fe0750e5 | 4501 | struct nfs4_openowner *oo; |
1da177e4 | 4502 | struct nfs4_delegation *dp; |
217526e7 | 4503 | struct nfs4_ol_stateid *stp; |
1da177e4 | 4504 | struct list_head *pos, *next, reaplist; |
3d733711 | 4505 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
a832e7ae | 4506 | time_t t, new_timeo = nn->nfsd4_lease; |
1da177e4 | 4507 | |
1da177e4 | 4508 | dprintk("NFSD: laundromat service - starting\n"); |
12760c66 | 4509 | nfsd4_end_grace(nn); |
36acb66b | 4510 | INIT_LIST_HEAD(&reaplist); |
c9a49628 | 4511 | spin_lock(&nn->client_lock); |
5ed58bb2 | 4512 | list_for_each_safe(pos, next, &nn->client_lru) { |
1da177e4 LT |
4513 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
4514 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { | |
4515 | t = clp->cl_time - cutoff; | |
a832e7ae | 4516 | new_timeo = min(new_timeo, t); |
1da177e4 LT |
4517 | break; |
4518 | } | |
221a6876 | 4519 | if (mark_client_expired_locked(clp)) { |
d7682988 BH |
4520 | dprintk("NFSD: client in use (clientid %08x)\n", |
4521 | clp->cl_clientid.cl_id); | |
4522 | continue; | |
4523 | } | |
4864af97 | 4524 | list_add(&clp->cl_lru, &reaplist); |
36acb66b | 4525 | } |
c9a49628 | 4526 | spin_unlock(&nn->client_lock); |
36acb66b BH |
4527 | list_for_each_safe(pos, next, &reaplist) { |
4528 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
1da177e4 LT |
4529 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
4530 | clp->cl_clientid.cl_id); | |
4864af97 | 4531 | list_del_init(&clp->cl_lru); |
1da177e4 LT |
4532 | expire_client(clp); |
4533 | } | |
cdc97505 | 4534 | spin_lock(&state_lock); |
e8c69d17 | 4535 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
1da177e4 LT |
4536 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
4537 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { | |
a832e7ae JL |
4538 | t = dp->dl_time - cutoff; |
4539 | new_timeo = min(new_timeo, t); | |
1da177e4 LT |
4540 | break; |
4541 | } | |
3fcbbd24 | 4542 | WARN_ON(!unhash_delegation_locked(dp)); |
42690676 | 4543 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 4544 | } |
cdc97505 | 4545 | spin_unlock(&state_lock); |
2d4a532d JL |
4546 | while (!list_empty(&reaplist)) { |
4547 | dp = list_first_entry(&reaplist, struct nfs4_delegation, | |
4548 | dl_recall_lru); | |
4549 | list_del_init(&dp->dl_recall_lru); | |
3bd64a5b | 4550 | revoke_delegation(dp); |
1da177e4 | 4551 | } |
217526e7 JL |
4552 | |
4553 | spin_lock(&nn->client_lock); | |
4554 | while (!list_empty(&nn->close_lru)) { | |
4555 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, | |
4556 | oo_close_lru); | |
4557 | if (time_after((unsigned long)oo->oo_time, | |
4558 | (unsigned long)cutoff)) { | |
a832e7ae JL |
4559 | t = oo->oo_time - cutoff; |
4560 | new_timeo = min(new_timeo, t); | |
1da177e4 LT |
4561 | break; |
4562 | } | |
217526e7 JL |
4563 | list_del_init(&oo->oo_close_lru); |
4564 | stp = oo->oo_last_closed_stid; | |
4565 | oo->oo_last_closed_stid = NULL; | |
4566 | spin_unlock(&nn->client_lock); | |
4567 | nfs4_put_stid(&stp->st_stid); | |
4568 | spin_lock(&nn->client_lock); | |
1da177e4 | 4569 | } |
217526e7 JL |
4570 | spin_unlock(&nn->client_lock); |
4571 | ||
a832e7ae | 4572 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
a832e7ae | 4573 | return new_timeo; |
1da177e4 LT |
4574 | } |
4575 | ||
a254b246 HH |
4576 | static struct workqueue_struct *laundry_wq; |
4577 | static void laundromat_main(struct work_struct *); | |
a254b246 HH |
4578 | |
4579 | static void | |
09121281 | 4580 | laundromat_main(struct work_struct *laundry) |
1da177e4 LT |
4581 | { |
4582 | time_t t; | |
2e55f3ab | 4583 | struct delayed_work *dwork = to_delayed_work(laundry); |
09121281 SK |
4584 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
4585 | laundromat_work); | |
1da177e4 | 4586 | |
09121281 | 4587 | t = nfs4_laundromat(nn); |
1da177e4 | 4588 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
09121281 | 4589 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
1da177e4 LT |
4590 | } |
4591 | ||
8fcd461d | 4592 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) |
1da177e4 | 4593 | { |
8fcd461d | 4594 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
f7a4d872 BF |
4595 | return nfserr_bad_stateid; |
4596 | return nfs_ok; | |
1da177e4 LT |
4597 | } |
4598 | ||
1da177e4 | 4599 | static inline int |
82c5ff1b | 4600 | access_permit_read(struct nfs4_ol_stateid *stp) |
1da177e4 | 4601 | { |
82c5ff1b JL |
4602 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
4603 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || | |
4604 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); | |
1da177e4 LT |
4605 | } |
4606 | ||
4607 | static inline int | |
82c5ff1b | 4608 | access_permit_write(struct nfs4_ol_stateid *stp) |
1da177e4 | 4609 | { |
82c5ff1b JL |
4610 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
4611 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); | |
1da177e4 LT |
4612 | } |
4613 | ||
4614 | static | |
dcef0413 | 4615 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
1da177e4 | 4616 | { |
b37ad28b | 4617 | __be32 status = nfserr_openmode; |
1da177e4 | 4618 | |
02921914 BF |
4619 | /* For lock stateid's, we test the parent open, not the lock: */ |
4620 | if (stp->st_openstp) | |
4621 | stp = stp->st_openstp; | |
82c5ff1b | 4622 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
1da177e4 | 4623 | goto out; |
82c5ff1b | 4624 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
1da177e4 LT |
4625 | goto out; |
4626 | status = nfs_ok; | |
4627 | out: | |
4628 | return status; | |
4629 | } | |
4630 | ||
b37ad28b | 4631 | static inline __be32 |
5ccb0066 | 4632 | check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) |
1da177e4 | 4633 | { |
203a8c8e | 4634 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
1da177e4 | 4635 | return nfs_ok; |
c87fb4a3 | 4636 | else if (opens_in_grace(net)) { |
25985edc | 4637 | /* Answer in remaining cases depends on existence of |
1da177e4 LT |
4638 | * conflicting state; so we must wait out the grace period. */ |
4639 | return nfserr_grace; | |
4640 | } else if (flags & WR_STATE) | |
4641 | return nfs4_share_conflict(current_fh, | |
4642 | NFS4_SHARE_DENY_WRITE); | |
4643 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ | |
4644 | return nfs4_share_conflict(current_fh, | |
4645 | NFS4_SHARE_DENY_READ); | |
4646 | } | |
4647 | ||
4648 | /* | |
4649 | * Allow READ/WRITE during grace period on recovered state only for files | |
4650 | * that are not able to provide mandatory locking. | |
4651 | */ | |
4652 | static inline int | |
5ccb0066 | 4653 | grace_disallows_io(struct net *net, struct inode *inode) |
1da177e4 | 4654 | { |
c87fb4a3 | 4655 | return opens_in_grace(net) && mandatory_lock(inode); |
1da177e4 LT |
4656 | } |
4657 | ||
57b7b43b | 4658 | static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) |
0836f587 | 4659 | { |
6668958f AA |
4660 | /* |
4661 | * When sessions are used the stateid generation number is ignored | |
4662 | * when it is zero. | |
4663 | */ | |
28dde241 | 4664 | if (has_session && in->si_generation == 0) |
81b82965 BF |
4665 | return nfs_ok; |
4666 | ||
4667 | if (in->si_generation == ref->si_generation) | |
4668 | return nfs_ok; | |
6668958f | 4669 | |
0836f587 | 4670 | /* If the client sends us a stateid from the future, it's buggy: */ |
14b7f4a1 | 4671 | if (nfsd4_stateid_generation_after(in, ref)) |
0836f587 BF |
4672 | return nfserr_bad_stateid; |
4673 | /* | |
81b82965 BF |
4674 | * However, we could see a stateid from the past, even from a |
4675 | * non-buggy client. For example, if the client sends a lock | |
4676 | * while some IO is outstanding, the lock may bump si_generation | |
4677 | * while the IO is still in flight. The client could avoid that | |
4678 | * situation by waiting for responses on all the IO requests, | |
4679 | * but better performance may result in retrying IO that | |
4680 | * receives an old_stateid error if requests are rarely | |
4681 | * reordered in flight: | |
0836f587 | 4682 | */ |
81b82965 | 4683 | return nfserr_old_stateid; |
0836f587 BF |
4684 | } |
4685 | ||
ebe9cb3b CH |
4686 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
4687 | { | |
4688 | if (ols->st_stateowner->so_is_open_owner && | |
4689 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) | |
4690 | return nfserr_bad_stateid; | |
4691 | return nfs_ok; | |
4692 | } | |
4693 | ||
7df302f7 | 4694 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
17456804 | 4695 | { |
97b7e3b6 | 4696 | struct nfs4_stid *s; |
1af71cc8 | 4697 | __be32 status = nfserr_bad_stateid; |
17456804 | 4698 | |
7df302f7 | 4699 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
1af71cc8 | 4700 | return status; |
7df302f7 CL |
4701 | /* Client debugging aid. */ |
4702 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { | |
4703 | char addr_str[INET6_ADDRSTRLEN]; | |
4704 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, | |
4705 | sizeof(addr_str)); | |
4706 | pr_warn_ratelimited("NFSD: client %s testing state ID " | |
4707 | "with incorrect client ID\n", addr_str); | |
1af71cc8 | 4708 | return status; |
7df302f7 | 4709 | } |
1af71cc8 JL |
4710 | spin_lock(&cl->cl_lock); |
4711 | s = find_stateid_locked(cl, stateid); | |
97b7e3b6 | 4712 | if (!s) |
1af71cc8 | 4713 | goto out_unlock; |
36279ac1 | 4714 | status = check_stateid_generation(stateid, &s->sc_stateid, 1); |
17456804 | 4715 | if (status) |
1af71cc8 | 4716 | goto out_unlock; |
23340032 BF |
4717 | switch (s->sc_type) { |
4718 | case NFS4_DELEG_STID: | |
1af71cc8 JL |
4719 | status = nfs_ok; |
4720 | break; | |
3bd64a5b | 4721 | case NFS4_REVOKED_DELEG_STID: |
1af71cc8 JL |
4722 | status = nfserr_deleg_revoked; |
4723 | break; | |
23340032 BF |
4724 | case NFS4_OPEN_STID: |
4725 | case NFS4_LOCK_STID: | |
ebe9cb3b | 4726 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
1af71cc8 | 4727 | break; |
23340032 BF |
4728 | default: |
4729 | printk("unknown stateid type %x\n", s->sc_type); | |
b0fc29d6 | 4730 | /* Fallthrough */ |
23340032 | 4731 | case NFS4_CLOSED_STID: |
b0fc29d6 | 4732 | case NFS4_CLOSED_DELEG_STID: |
1af71cc8 | 4733 | status = nfserr_bad_stateid; |
23340032 | 4734 | } |
1af71cc8 JL |
4735 | out_unlock: |
4736 | spin_unlock(&cl->cl_lock); | |
4737 | return status; | |
17456804 BS |
4738 | } |
4739 | ||
cd61c522 | 4740 | __be32 |
2dd6e458 TM |
4741 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
4742 | stateid_t *stateid, unsigned char typemask, | |
4743 | struct nfs4_stid **s, struct nfsd_net *nn) | |
38c2f4b1 | 4744 | { |
0eb6f20a | 4745 | __be32 status; |
38c2f4b1 BF |
4746 | |
4747 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) | |
4748 | return nfserr_bad_stateid; | |
4b24ca7d | 4749 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
a8a7c677 | 4750 | if (status == nfserr_stale_clientid) { |
4b24ca7d | 4751 | if (cstate->session) |
a8a7c677 | 4752 | return nfserr_bad_stateid; |
38c2f4b1 | 4753 | return nfserr_stale_stateid; |
a8a7c677 | 4754 | } |
0eb6f20a BF |
4755 | if (status) |
4756 | return status; | |
4b24ca7d | 4757 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
38c2f4b1 BF |
4758 | if (!*s) |
4759 | return nfserr_bad_stateid; | |
4760 | return nfs_ok; | |
38c2f4b1 BF |
4761 | } |
4762 | ||
a0649b2d CH |
4763 | static struct file * |
4764 | nfs4_find_file(struct nfs4_stid *s, int flags) | |
4765 | { | |
af90f707 CH |
4766 | if (!s) |
4767 | return NULL; | |
4768 | ||
a0649b2d CH |
4769 | switch (s->sc_type) { |
4770 | case NFS4_DELEG_STID: | |
4771 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) | |
4772 | return NULL; | |
4773 | return get_file(s->sc_file->fi_deleg_file); | |
4774 | case NFS4_OPEN_STID: | |
4775 | case NFS4_LOCK_STID: | |
4776 | if (flags & RD_STATE) | |
4777 | return find_readable_file(s->sc_file); | |
4778 | else | |
4779 | return find_writeable_file(s->sc_file); | |
4780 | break; | |
4781 | } | |
4782 | ||
4783 | return NULL; | |
4784 | } | |
4785 | ||
4786 | static __be32 | |
4787 | nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags) | |
4788 | { | |
4789 | __be32 status; | |
4790 | ||
a0649b2d CH |
4791 | status = nfsd4_check_openowner_confirmed(ols); |
4792 | if (status) | |
4793 | return status; | |
4794 | return nfs4_check_openmode(ols, flags); | |
4795 | } | |
4796 | ||
af90f707 CH |
4797 | static __be32 |
4798 | nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, | |
4799 | struct file **filpp, bool *tmp_file, int flags) | |
4800 | { | |
4801 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; | |
4802 | struct file *file; | |
4803 | __be32 status; | |
4804 | ||
4805 | file = nfs4_find_file(s, flags); | |
4806 | if (file) { | |
4807 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, | |
4808 | acc | NFSD_MAY_OWNER_OVERRIDE); | |
4809 | if (status) { | |
4810 | fput(file); | |
4811 | return status; | |
4812 | } | |
4813 | ||
4814 | *filpp = file; | |
4815 | } else { | |
4816 | status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp); | |
4817 | if (status) | |
4818 | return status; | |
4819 | ||
4820 | if (tmp_file) | |
4821 | *tmp_file = true; | |
4822 | } | |
4823 | ||
4824 | return 0; | |
4825 | } | |
4826 | ||
1da177e4 | 4827 | /* |
a0649b2d CH |
4828 | * Checks for stateid operations |
4829 | */ | |
b37ad28b | 4830 | __be32 |
af90f707 | 4831 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
aa0d6aed AS |
4832 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
4833 | stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file) | |
1da177e4 | 4834 | { |
a0649b2d | 4835 | struct inode *ino = d_inode(fhp->fh_dentry); |
af90f707 | 4836 | struct net *net = SVC_NET(rqstp); |
3320fef1 | 4837 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
af90f707 | 4838 | struct nfs4_stid *s = NULL; |
b37ad28b | 4839 | __be32 status; |
1da177e4 | 4840 | |
1da177e4 LT |
4841 | if (filpp) |
4842 | *filpp = NULL; | |
af90f707 CH |
4843 | if (tmp_file) |
4844 | *tmp_file = false; | |
1da177e4 | 4845 | |
5ccb0066 | 4846 | if (grace_disallows_io(net, ino)) |
1da177e4 LT |
4847 | return nfserr_grace; |
4848 | ||
af90f707 CH |
4849 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
4850 | status = check_special_stateids(net, fhp, stateid, flags); | |
4851 | goto done; | |
4852 | } | |
1da177e4 | 4853 | |
2dd6e458 | 4854 | status = nfsd4_lookup_stateid(cstate, stateid, |
db24b3b4 | 4855 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
2dd6e458 | 4856 | &s, nn); |
38c2f4b1 | 4857 | if (status) |
c2d1d6a8 | 4858 | return status; |
a0649b2d CH |
4859 | status = check_stateid_generation(stateid, &s->sc_stateid, |
4860 | nfsd4_has_session(cstate)); | |
69064a27 BF |
4861 | if (status) |
4862 | goto out; | |
a0649b2d | 4863 | |
f7a4d872 BF |
4864 | switch (s->sc_type) { |
4865 | case NFS4_DELEG_STID: | |
a0649b2d | 4866 | status = nfs4_check_delegmode(delegstateid(s), flags); |
f7a4d872 BF |
4867 | break; |
4868 | case NFS4_OPEN_STID: | |
4869 | case NFS4_LOCK_STID: | |
a0649b2d | 4870 | status = nfs4_check_olstateid(fhp, openlockstateid(s), flags); |
f7a4d872 BF |
4871 | break; |
4872 | default: | |
14bcab1a | 4873 | status = nfserr_bad_stateid; |
a0649b2d CH |
4874 | break; |
4875 | } | |
8fcd461d JL |
4876 | if (status) |
4877 | goto out; | |
4878 | status = nfs4_check_fh(fhp, s); | |
a0649b2d | 4879 | |
af90f707 CH |
4880 | done: |
4881 | if (!status && filpp) | |
4882 | status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags); | |
1da177e4 | 4883 | out: |
af90f707 CH |
4884 | if (s) |
4885 | nfs4_put_stid(s); | |
1da177e4 LT |
4886 | return status; |
4887 | } | |
4888 | ||
17456804 BS |
4889 | /* |
4890 | * Test if the stateid is valid | |
4891 | */ | |
4892 | __be32 | |
4893 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, | |
4894 | struct nfsd4_test_stateid *test_stateid) | |
4895 | { | |
03cfb420 BS |
4896 | struct nfsd4_test_stateid_id *stateid; |
4897 | struct nfs4_client *cl = cstate->session->se_client; | |
4898 | ||
03cfb420 | 4899 | list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) |
7df302f7 CL |
4900 | stateid->ts_id_status = |
4901 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); | |
03cfb420 | 4902 | |
17456804 BS |
4903 | return nfs_ok; |
4904 | } | |
4905 | ||
42691398 CL |
4906 | static __be32 |
4907 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) | |
4908 | { | |
4909 | struct nfs4_ol_stateid *stp = openlockstateid(s); | |
4910 | __be32 ret; | |
4911 | ||
4912 | mutex_lock(&stp->st_mutex); | |
4913 | ||
4914 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); | |
4915 | if (ret) | |
4916 | goto out; | |
4917 | ||
4918 | ret = nfserr_locks_held; | |
4919 | if (check_for_locks(stp->st_stid.sc_file, | |
4920 | lockowner(stp->st_stateowner))) | |
4921 | goto out; | |
4922 | ||
4923 | release_lock_stateid(stp); | |
4924 | ret = nfs_ok; | |
4925 | ||
4926 | out: | |
4927 | mutex_unlock(&stp->st_mutex); | |
4928 | nfs4_put_stid(s); | |
4929 | return ret; | |
4930 | } | |
4931 | ||
e1ca12df BS |
4932 | __be32 |
4933 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, | |
4934 | struct nfsd4_free_stateid *free_stateid) | |
4935 | { | |
4936 | stateid_t *stateid = &free_stateid->fr_stateid; | |
2da1cec7 | 4937 | struct nfs4_stid *s; |
3bd64a5b | 4938 | struct nfs4_delegation *dp; |
38c2f4b1 | 4939 | struct nfs4_client *cl = cstate->session->se_client; |
2da1cec7 | 4940 | __be32 ret = nfserr_bad_stateid; |
e1ca12df | 4941 | |
1af71cc8 JL |
4942 | spin_lock(&cl->cl_lock); |
4943 | s = find_stateid_locked(cl, stateid); | |
2da1cec7 | 4944 | if (!s) |
1af71cc8 | 4945 | goto out_unlock; |
2da1cec7 BF |
4946 | switch (s->sc_type) { |
4947 | case NFS4_DELEG_STID: | |
e1ca12df | 4948 | ret = nfserr_locks_held; |
1af71cc8 | 4949 | break; |
2da1cec7 | 4950 | case NFS4_OPEN_STID: |
2da1cec7 BF |
4951 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
4952 | if (ret) | |
1af71cc8 JL |
4953 | break; |
4954 | ret = nfserr_locks_held; | |
f7a4d872 | 4955 | break; |
1af71cc8 | 4956 | case NFS4_LOCK_STID: |
42691398 | 4957 | atomic_inc(&s->sc_count); |
1af71cc8 | 4958 | spin_unlock(&cl->cl_lock); |
42691398 | 4959 | ret = nfsd4_free_lock_stateid(stateid, s); |
1af71cc8 | 4960 | goto out; |
3bd64a5b BF |
4961 | case NFS4_REVOKED_DELEG_STID: |
4962 | dp = delegstateid(s); | |
2d4a532d JL |
4963 | list_del_init(&dp->dl_recall_lru); |
4964 | spin_unlock(&cl->cl_lock); | |
6011695d | 4965 | nfs4_put_stid(s); |
3bd64a5b | 4966 | ret = nfs_ok; |
1af71cc8 JL |
4967 | goto out; |
4968 | /* Default falls through and returns nfserr_bad_stateid */ | |
e1ca12df | 4969 | } |
1af71cc8 JL |
4970 | out_unlock: |
4971 | spin_unlock(&cl->cl_lock); | |
e1ca12df | 4972 | out: |
e1ca12df BS |
4973 | return ret; |
4974 | } | |
4975 | ||
4c4cd222 N |
4976 | static inline int |
4977 | setlkflg (int type) | |
4978 | { | |
4979 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? | |
4980 | RD_STATE : WR_STATE; | |
4981 | } | |
1da177e4 | 4982 | |
dcef0413 | 4983 | static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) |
c0a5d93e BF |
4984 | { |
4985 | struct svc_fh *current_fh = &cstate->current_fh; | |
4986 | struct nfs4_stateowner *sop = stp->st_stateowner; | |
4987 | __be32 status; | |
4988 | ||
c0a5d93e BF |
4989 | status = nfsd4_check_seqid(cstate, sop, seqid); |
4990 | if (status) | |
4991 | return status; | |
3bd64a5b BF |
4992 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID |
4993 | || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID) | |
f7a4d872 BF |
4994 | /* |
4995 | * "Closed" stateid's exist *only* to return | |
3bd64a5b BF |
4996 | * nfserr_replay_me from the previous step, and |
4997 | * revoked delegations are kept only for free_stateid. | |
f7a4d872 BF |
4998 | */ |
4999 | return nfserr_bad_stateid; | |
feb9dad5 | 5000 | mutex_lock(&stp->st_mutex); |
f7a4d872 | 5001 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
35a92fe8 JL |
5002 | if (status == nfs_ok) |
5003 | status = nfs4_check_fh(current_fh, &stp->st_stid); | |
5004 | if (status != nfs_ok) | |
feb9dad5 | 5005 | mutex_unlock(&stp->st_mutex); |
35a92fe8 | 5006 | return status; |
c0a5d93e BF |
5007 | } |
5008 | ||
1da177e4 LT |
5009 | /* |
5010 | * Checks for sequence id mutating operations. | |
5011 | */ | |
b37ad28b | 5012 | static __be32 |
dd453dfd | 5013 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
2288d0e3 | 5014 | stateid_t *stateid, char typemask, |
3320fef1 SK |
5015 | struct nfs4_ol_stateid **stpp, |
5016 | struct nfsd_net *nn) | |
1da177e4 | 5017 | { |
0836f587 | 5018 | __be32 status; |
38c2f4b1 | 5019 | struct nfs4_stid *s; |
e17f99b7 | 5020 | struct nfs4_ol_stateid *stp = NULL; |
1da177e4 | 5021 | |
8c10cbdb BH |
5022 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
5023 | seqid, STATEID_VAL(stateid)); | |
3a4f98bb | 5024 | |
1da177e4 | 5025 | *stpp = NULL; |
2dd6e458 | 5026 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
c0a5d93e BF |
5027 | if (status) |
5028 | return status; | |
e17f99b7 | 5029 | stp = openlockstateid(s); |
58fb12e6 | 5030 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
1da177e4 | 5031 | |
e17f99b7 | 5032 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
fd911011 | 5033 | if (!status) |
e17f99b7 | 5034 | *stpp = stp; |
fd911011 TM |
5035 | else |
5036 | nfs4_put_stid(&stp->st_stid); | |
e17f99b7 | 5037 | return status; |
c0a5d93e | 5038 | } |
39325bd0 | 5039 | |
3320fef1 SK |
5040 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
5041 | stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) | |
c0a5d93e BF |
5042 | { |
5043 | __be32 status; | |
5044 | struct nfs4_openowner *oo; | |
4cbfc9f7 | 5045 | struct nfs4_ol_stateid *stp; |
1da177e4 | 5046 | |
c0a5d93e | 5047 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
4cbfc9f7 | 5048 | NFS4_OPEN_STID, &stp, nn); |
7a8711c9 BF |
5049 | if (status) |
5050 | return status; | |
4cbfc9f7 TM |
5051 | oo = openowner(stp->st_stateowner); |
5052 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { | |
feb9dad5 | 5053 | mutex_unlock(&stp->st_mutex); |
4cbfc9f7 | 5054 | nfs4_put_stid(&stp->st_stid); |
3a4f98bb | 5055 | return nfserr_bad_stateid; |
4cbfc9f7 TM |
5056 | } |
5057 | *stpp = stp; | |
3a4f98bb | 5058 | return nfs_ok; |
1da177e4 LT |
5059 | } |
5060 | ||
b37ad28b | 5061 | __be32 |
ca364317 | 5062 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 5063 | struct nfsd4_open_confirm *oc) |
1da177e4 | 5064 | { |
b37ad28b | 5065 | __be32 status; |
fe0750e5 | 5066 | struct nfs4_openowner *oo; |
dcef0413 | 5067 | struct nfs4_ol_stateid *stp; |
3320fef1 | 5068 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 5069 | |
a6a9f18f AV |
5070 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
5071 | cstate->current_fh.fh_dentry); | |
1da177e4 | 5072 | |
ca364317 | 5073 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
a8cddc5d BF |
5074 | if (status) |
5075 | return status; | |
1da177e4 | 5076 | |
9072d5c6 | 5077 | status = nfs4_preprocess_seqid_op(cstate, |
ca364317 | 5078 | oc->oc_seqid, &oc->oc_req_stateid, |
3320fef1 | 5079 | NFS4_OPEN_STID, &stp, nn); |
9072d5c6 | 5080 | if (status) |
68b66e82 | 5081 | goto out; |
fe0750e5 | 5082 | oo = openowner(stp->st_stateowner); |
68b66e82 | 5083 | status = nfserr_bad_stateid; |
35a92fe8 | 5084 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
feb9dad5 | 5085 | mutex_unlock(&stp->st_mutex); |
2585fc79 | 5086 | goto put_stateid; |
35a92fe8 | 5087 | } |
dad1c067 | 5088 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
9767feb2 | 5089 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
feb9dad5 | 5090 | mutex_unlock(&stp->st_mutex); |
8c10cbdb | 5091 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
dcef0413 | 5092 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
c7b9a459 | 5093 | |
2a4317c5 | 5094 | nfsd4_client_record_create(oo->oo_owner.so_client); |
68b66e82 | 5095 | status = nfs_ok; |
2585fc79 TM |
5096 | put_stateid: |
5097 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 | 5098 | out: |
9411b1d4 | 5099 | nfsd4_bump_seqid(cstate, status); |
1da177e4 LT |
5100 | return status; |
5101 | } | |
5102 | ||
6409a5a6 | 5103 | static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) |
1da177e4 | 5104 | { |
82c5ff1b | 5105 | if (!test_access(access, stp)) |
6409a5a6 | 5106 | return; |
11b9164a | 5107 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
82c5ff1b | 5108 | clear_access(access, stp); |
6409a5a6 | 5109 | } |
f197c271 | 5110 | |
6409a5a6 BF |
5111 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
5112 | { | |
5113 | switch (to_access) { | |
5114 | case NFS4_SHARE_ACCESS_READ: | |
5115 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); | |
5116 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); | |
5117 | break; | |
5118 | case NFS4_SHARE_ACCESS_WRITE: | |
5119 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); | |
5120 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); | |
5121 | break; | |
5122 | case NFS4_SHARE_ACCESS_BOTH: | |
5123 | break; | |
5124 | default: | |
063b0fb9 | 5125 | WARN_ON_ONCE(1); |
1da177e4 LT |
5126 | } |
5127 | } | |
5128 | ||
b37ad28b | 5129 | __be32 |
ca364317 BF |
5130 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
5131 | struct nfsd4_compound_state *cstate, | |
a4f1706a | 5132 | struct nfsd4_open_downgrade *od) |
1da177e4 | 5133 | { |
b37ad28b | 5134 | __be32 status; |
dcef0413 | 5135 | struct nfs4_ol_stateid *stp; |
3320fef1 | 5136 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 5137 | |
a6a9f18f AV |
5138 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
5139 | cstate->current_fh.fh_dentry); | |
1da177e4 | 5140 | |
c30e92df | 5141 | /* We don't yet support WANT bits: */ |
2c8bd7e0 BH |
5142 | if (od->od_deleg_want) |
5143 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, | |
5144 | od->od_deleg_want); | |
1da177e4 | 5145 | |
c0a5d93e | 5146 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
3320fef1 | 5147 | &od->od_stateid, &stp, nn); |
9072d5c6 | 5148 | if (status) |
1da177e4 | 5149 | goto out; |
1da177e4 | 5150 | status = nfserr_inval; |
82c5ff1b | 5151 | if (!test_access(od->od_share_access, stp)) { |
c11c591f | 5152 | dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", |
1da177e4 | 5153 | stp->st_access_bmap, od->od_share_access); |
0667b1e9 | 5154 | goto put_stateid; |
1da177e4 | 5155 | } |
ce0fc43c | 5156 | if (!test_deny(od->od_share_deny, stp)) { |
c11c591f | 5157 | dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", |
1da177e4 | 5158 | stp->st_deny_bmap, od->od_share_deny); |
0667b1e9 | 5159 | goto put_stateid; |
1da177e4 | 5160 | } |
6409a5a6 | 5161 | nfs4_stateid_downgrade(stp, od->od_share_access); |
ce0fc43c | 5162 | reset_union_bmap_deny(od->od_share_deny, stp); |
9767feb2 | 5163 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
1da177e4 | 5164 | status = nfs_ok; |
0667b1e9 | 5165 | put_stateid: |
feb9dad5 | 5166 | mutex_unlock(&stp->st_mutex); |
0667b1e9 | 5167 | nfs4_put_stid(&stp->st_stid); |
1da177e4 | 5168 | out: |
9411b1d4 | 5169 | nfsd4_bump_seqid(cstate, status); |
1da177e4 LT |
5170 | return status; |
5171 | } | |
5172 | ||
f7a4d872 BF |
5173 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
5174 | { | |
acf9295b | 5175 | struct nfs4_client *clp = s->st_stid.sc_client; |
e8568739 | 5176 | bool unhashed; |
d83017f9 | 5177 | LIST_HEAD(reaplist); |
acf9295b | 5178 | |
f7a4d872 | 5179 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
2c41beb0 | 5180 | spin_lock(&clp->cl_lock); |
e8568739 | 5181 | unhashed = unhash_open_stateid(s, &reaplist); |
acf9295b | 5182 | |
d83017f9 | 5183 | if (clp->cl_minorversion) { |
e8568739 JL |
5184 | if (unhashed) |
5185 | put_ol_stateid_locked(s, &reaplist); | |
d83017f9 JL |
5186 | spin_unlock(&clp->cl_lock); |
5187 | free_ol_stateid_reaplist(&reaplist); | |
5188 | } else { | |
5189 | spin_unlock(&clp->cl_lock); | |
5190 | free_ol_stateid_reaplist(&reaplist); | |
e8568739 JL |
5191 | if (unhashed) |
5192 | move_to_close_lru(s, clp->net); | |
d83017f9 | 5193 | } |
38c387b5 BF |
5194 | } |
5195 | ||
1da177e4 LT |
5196 | /* |
5197 | * nfs4_unlock_state() called after encode | |
5198 | */ | |
b37ad28b | 5199 | __be32 |
ca364317 | 5200 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 5201 | struct nfsd4_close *close) |
1da177e4 | 5202 | { |
b37ad28b | 5203 | __be32 status; |
dcef0413 | 5204 | struct nfs4_ol_stateid *stp; |
3320fef1 SK |
5205 | struct net *net = SVC_NET(rqstp); |
5206 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1da177e4 | 5207 | |
a6a9f18f AV |
5208 | dprintk("NFSD: nfsd4_close on file %pd\n", |
5209 | cstate->current_fh.fh_dentry); | |
1da177e4 | 5210 | |
f7a4d872 BF |
5211 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
5212 | &close->cl_stateid, | |
5213 | NFS4_OPEN_STID|NFS4_CLOSED_STID, | |
3320fef1 | 5214 | &stp, nn); |
9411b1d4 | 5215 | nfsd4_bump_seqid(cstate, status); |
9072d5c6 | 5216 | if (status) |
1da177e4 | 5217 | goto out; |
9767feb2 | 5218 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
feb9dad5 | 5219 | mutex_unlock(&stp->st_mutex); |
1da177e4 | 5220 | |
f7a4d872 | 5221 | nfsd4_close_open_stateid(stp); |
8a0b589d TM |
5222 | |
5223 | /* put reference from nfs4_preprocess_seqid_op */ | |
5224 | nfs4_put_stid(&stp->st_stid); | |
1da177e4 | 5225 | out: |
1da177e4 LT |
5226 | return status; |
5227 | } | |
5228 | ||
b37ad28b | 5229 | __be32 |
ca364317 BF |
5230 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
5231 | struct nfsd4_delegreturn *dr) | |
1da177e4 | 5232 | { |
203a8c8e BF |
5233 | struct nfs4_delegation *dp; |
5234 | stateid_t *stateid = &dr->dr_stateid; | |
38c2f4b1 | 5235 | struct nfs4_stid *s; |
b37ad28b | 5236 | __be32 status; |
3320fef1 | 5237 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 5238 | |
ca364317 | 5239 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
203a8c8e | 5240 | return status; |
1da177e4 | 5241 | |
2dd6e458 | 5242 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
38c2f4b1 | 5243 | if (status) |
203a8c8e | 5244 | goto out; |
38c2f4b1 | 5245 | dp = delegstateid(s); |
d5477a8d | 5246 | status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate)); |
203a8c8e | 5247 | if (status) |
fd911011 | 5248 | goto put_stateid; |
203a8c8e | 5249 | |
3bd64a5b | 5250 | destroy_delegation(dp); |
fd911011 TM |
5251 | put_stateid: |
5252 | nfs4_put_stid(&dp->dl_stid); | |
1da177e4 LT |
5253 | out: |
5254 | return status; | |
5255 | } | |
5256 | ||
87df4de8 BH |
5257 | static inline u64 |
5258 | end_offset(u64 start, u64 len) | |
5259 | { | |
5260 | u64 end; | |
5261 | ||
5262 | end = start + len; | |
5263 | return end >= start ? end: NFS4_MAX_UINT64; | |
5264 | } | |
5265 | ||
5266 | /* last octet in a range */ | |
5267 | static inline u64 | |
5268 | last_byte_offset(u64 start, u64 len) | |
5269 | { | |
5270 | u64 end; | |
5271 | ||
063b0fb9 | 5272 | WARN_ON_ONCE(!len); |
87df4de8 BH |
5273 | end = start + len; |
5274 | return end > start ? end - 1: NFS4_MAX_UINT64; | |
5275 | } | |
5276 | ||
1da177e4 LT |
5277 | /* |
5278 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that | |
5279 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th | |
5280 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit | |
5281 | * locking, this prevents us from being completely protocol-compliant. The | |
5282 | * real solution to this problem is to start using unsigned file offsets in | |
5283 | * the VFS, but this is a very deep change! | |
5284 | */ | |
5285 | static inline void | |
5286 | nfs4_transform_lock_offset(struct file_lock *lock) | |
5287 | { | |
5288 | if (lock->fl_start < 0) | |
5289 | lock->fl_start = OFFSET_MAX; | |
5290 | if (lock->fl_end < 0) | |
5291 | lock->fl_end = OFFSET_MAX; | |
5292 | } | |
5293 | ||
cae80b30 JL |
5294 | static fl_owner_t |
5295 | nfsd4_fl_get_owner(fl_owner_t owner) | |
aef9583b | 5296 | { |
cae80b30 JL |
5297 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
5298 | ||
5299 | nfs4_get_stateowner(&lo->lo_owner); | |
5300 | return owner; | |
aef9583b KM |
5301 | } |
5302 | ||
cae80b30 JL |
5303 | static void |
5304 | nfsd4_fl_put_owner(fl_owner_t owner) | |
aef9583b | 5305 | { |
cae80b30 | 5306 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
aef9583b | 5307 | |
cae80b30 | 5308 | if (lo) |
aef9583b | 5309 | nfs4_put_stateowner(&lo->lo_owner); |
aef9583b KM |
5310 | } |
5311 | ||
7b021967 | 5312 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
aef9583b KM |
5313 | .lm_get_owner = nfsd4_fl_get_owner, |
5314 | .lm_put_owner = nfsd4_fl_put_owner, | |
d5b9026a | 5315 | }; |
1da177e4 LT |
5316 | |
5317 | static inline void | |
5318 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) | |
5319 | { | |
fe0750e5 | 5320 | struct nfs4_lockowner *lo; |
1da177e4 | 5321 | |
d5b9026a | 5322 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
fe0750e5 BF |
5323 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
5324 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, | |
5325 | lo->lo_owner.so_owner.len, GFP_KERNEL); | |
7c13f344 BF |
5326 | if (!deny->ld_owner.data) |
5327 | /* We just don't care that much */ | |
5328 | goto nevermind; | |
fe0750e5 BF |
5329 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
5330 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; | |
d5b9026a | 5331 | } else { |
7c13f344 BF |
5332 | nevermind: |
5333 | deny->ld_owner.len = 0; | |
5334 | deny->ld_owner.data = NULL; | |
d5b9026a N |
5335 | deny->ld_clientid.cl_boot = 0; |
5336 | deny->ld_clientid.cl_id = 0; | |
1da177e4 LT |
5337 | } |
5338 | deny->ld_start = fl->fl_start; | |
87df4de8 BH |
5339 | deny->ld_length = NFS4_MAX_UINT64; |
5340 | if (fl->fl_end != NFS4_MAX_UINT64) | |
1da177e4 LT |
5341 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
5342 | deny->ld_type = NFS4_READ_LT; | |
5343 | if (fl->fl_type != F_RDLCK) | |
5344 | deny->ld_type = NFS4_WRITE_LT; | |
5345 | } | |
5346 | ||
fe0750e5 | 5347 | static struct nfs4_lockowner * |
c8623999 | 5348 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
1da177e4 | 5349 | { |
d4f0489f | 5350 | unsigned int strhashval = ownerstr_hashval(owner); |
b3c32bcd | 5351 | struct nfs4_stateowner *so; |
1da177e4 | 5352 | |
0a880a28 TM |
5353 | lockdep_assert_held(&clp->cl_lock); |
5354 | ||
d4f0489f TM |
5355 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
5356 | so_strhash) { | |
b3c32bcd TM |
5357 | if (so->so_is_open_owner) |
5358 | continue; | |
b5971afa KM |
5359 | if (same_owner_str(so, owner)) |
5360 | return lockowner(nfs4_get_stateowner(so)); | |
1da177e4 LT |
5361 | } |
5362 | return NULL; | |
5363 | } | |
5364 | ||
c58c6610 | 5365 | static struct nfs4_lockowner * |
c8623999 | 5366 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
c58c6610 TM |
5367 | { |
5368 | struct nfs4_lockowner *lo; | |
5369 | ||
d4f0489f | 5370 | spin_lock(&clp->cl_lock); |
c8623999 | 5371 | lo = find_lockowner_str_locked(clp, owner); |
d4f0489f | 5372 | spin_unlock(&clp->cl_lock); |
c58c6610 TM |
5373 | return lo; |
5374 | } | |
5375 | ||
8f4b54c5 JL |
5376 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
5377 | { | |
c58c6610 | 5378 | unhash_lockowner_locked(lockowner(sop)); |
8f4b54c5 JL |
5379 | } |
5380 | ||
6b180f0b JL |
5381 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
5382 | { | |
5383 | struct nfs4_lockowner *lo = lockowner(sop); | |
5384 | ||
5385 | kmem_cache_free(lockowner_slab, lo); | |
5386 | } | |
5387 | ||
5388 | static const struct nfs4_stateowner_operations lockowner_ops = { | |
8f4b54c5 JL |
5389 | .so_unhash = nfs4_unhash_lockowner, |
5390 | .so_free = nfs4_free_lockowner, | |
6b180f0b JL |
5391 | }; |
5392 | ||
1da177e4 LT |
5393 | /* |
5394 | * Alloc a lock owner structure. | |
5395 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has | |
25985edc | 5396 | * occurred. |
1da177e4 | 5397 | * |
16bfdaaf | 5398 | * strhashval = ownerstr_hashval |
1da177e4 | 5399 | */ |
fe0750e5 | 5400 | static struct nfs4_lockowner * |
c58c6610 TM |
5401 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
5402 | struct nfs4_ol_stateid *open_stp, | |
5403 | struct nfsd4_lock *lock) | |
5404 | { | |
c58c6610 | 5405 | struct nfs4_lockowner *lo, *ret; |
1da177e4 | 5406 | |
fe0750e5 BF |
5407 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
5408 | if (!lo) | |
1da177e4 | 5409 | return NULL; |
fe0750e5 BF |
5410 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
5411 | lo->lo_owner.so_is_open_owner = 0; | |
5db1c03f | 5412 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
6b180f0b | 5413 | lo->lo_owner.so_ops = &lockowner_ops; |
d4f0489f | 5414 | spin_lock(&clp->cl_lock); |
c8623999 | 5415 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
c58c6610 TM |
5416 | if (ret == NULL) { |
5417 | list_add(&lo->lo_owner.so_strhash, | |
d4f0489f | 5418 | &clp->cl_ownerstr_hashtbl[strhashval]); |
c58c6610 TM |
5419 | ret = lo; |
5420 | } else | |
d50ffded KM |
5421 | nfs4_free_stateowner(&lo->lo_owner); |
5422 | ||
d4f0489f | 5423 | spin_unlock(&clp->cl_lock); |
340f0ba1 | 5424 | return ret; |
1da177e4 LT |
5425 | } |
5426 | ||
356a95ec JL |
5427 | static void |
5428 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, | |
5429 | struct nfs4_file *fp, struct inode *inode, | |
5430 | struct nfs4_ol_stateid *open_stp) | |
1da177e4 | 5431 | { |
d3b313a4 | 5432 | struct nfs4_client *clp = lo->lo_owner.so_client; |
1da177e4 | 5433 | |
356a95ec JL |
5434 | lockdep_assert_held(&clp->cl_lock); |
5435 | ||
3d0fabd5 | 5436 | atomic_inc(&stp->st_stid.sc_count); |
3abdb607 | 5437 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
b5971afa | 5438 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
13cd2184 | 5439 | get_nfs4_file(fp); |
11b9164a | 5440 | stp->st_stid.sc_file = fp; |
b49e084d | 5441 | stp->st_stid.sc_free = nfs4_free_lock_stateid; |
0997b173 | 5442 | stp->st_access_bmap = 0; |
1da177e4 | 5443 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
4c4cd222 | 5444 | stp->st_openstp = open_stp; |
feb9dad5 | 5445 | mutex_init(&stp->st_mutex); |
3c87b9b7 | 5446 | list_add(&stp->st_locks, &open_stp->st_locks); |
1c755dc1 | 5447 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
1d31a253 TM |
5448 | spin_lock(&fp->fi_lock); |
5449 | list_add(&stp->st_perfile, &fp->fi_stateids); | |
5450 | spin_unlock(&fp->fi_lock); | |
1da177e4 LT |
5451 | } |
5452 | ||
c53530da JL |
5453 | static struct nfs4_ol_stateid * |
5454 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) | |
5455 | { | |
5456 | struct nfs4_ol_stateid *lst; | |
356a95ec JL |
5457 | struct nfs4_client *clp = lo->lo_owner.so_client; |
5458 | ||
5459 | lockdep_assert_held(&clp->cl_lock); | |
c53530da JL |
5460 | |
5461 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { | |
3d0fabd5 TM |
5462 | if (lst->st_stid.sc_file == fp) { |
5463 | atomic_inc(&lst->st_stid.sc_count); | |
c53530da | 5464 | return lst; |
3d0fabd5 | 5465 | } |
c53530da JL |
5466 | } |
5467 | return NULL; | |
5468 | } | |
5469 | ||
356a95ec JL |
5470 | static struct nfs4_ol_stateid * |
5471 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, | |
5472 | struct inode *inode, struct nfs4_ol_stateid *ost, | |
5473 | bool *new) | |
5474 | { | |
5475 | struct nfs4_stid *ns = NULL; | |
5476 | struct nfs4_ol_stateid *lst; | |
5477 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); | |
5478 | struct nfs4_client *clp = oo->oo_owner.so_client; | |
5479 | ||
5480 | spin_lock(&clp->cl_lock); | |
5481 | lst = find_lock_stateid(lo, fi); | |
5482 | if (lst == NULL) { | |
5483 | spin_unlock(&clp->cl_lock); | |
5484 | ns = nfs4_alloc_stid(clp, stateid_slab); | |
5485 | if (ns == NULL) | |
5486 | return NULL; | |
5487 | ||
5488 | spin_lock(&clp->cl_lock); | |
5489 | lst = find_lock_stateid(lo, fi); | |
5490 | if (likely(!lst)) { | |
5491 | lst = openlockstateid(ns); | |
5492 | init_lock_stateid(lst, lo, fi, inode, ost); | |
5493 | ns = NULL; | |
5494 | *new = true; | |
5495 | } | |
5496 | } | |
5497 | spin_unlock(&clp->cl_lock); | |
5498 | if (ns) | |
5499 | nfs4_put_stid(ns); | |
5500 | return lst; | |
5501 | } | |
c53530da | 5502 | |
fd39ca9a | 5503 | static int |
1da177e4 LT |
5504 | check_lock_length(u64 offset, u64 length) |
5505 | { | |
e7969315 KM |
5506 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
5507 | (length > ~offset))); | |
1da177e4 LT |
5508 | } |
5509 | ||
dcef0413 | 5510 | static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) |
0997b173 | 5511 | { |
11b9164a | 5512 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
0997b173 | 5513 | |
7214e860 JL |
5514 | lockdep_assert_held(&fp->fi_lock); |
5515 | ||
82c5ff1b | 5516 | if (test_access(access, lock_stp)) |
0997b173 | 5517 | return; |
12659651 | 5518 | __nfs4_file_get_access(fp, access); |
82c5ff1b | 5519 | set_access(access, lock_stp); |
0997b173 BF |
5520 | } |
5521 | ||
356a95ec JL |
5522 | static __be32 |
5523 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, | |
5524 | struct nfs4_ol_stateid *ost, | |
5525 | struct nfsd4_lock *lock, | |
dd257933 | 5526 | struct nfs4_ol_stateid **plst, bool *new) |
64a284d0 | 5527 | { |
5db1c03f | 5528 | __be32 status; |
11b9164a | 5529 | struct nfs4_file *fi = ost->st_stid.sc_file; |
64a284d0 BF |
5530 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
5531 | struct nfs4_client *cl = oo->oo_owner.so_client; | |
2b0143b5 | 5532 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
64a284d0 | 5533 | struct nfs4_lockowner *lo; |
dd257933 | 5534 | struct nfs4_ol_stateid *lst; |
64a284d0 | 5535 | unsigned int strhashval; |
dd257933 | 5536 | bool hashed; |
64a284d0 | 5537 | |
c8623999 | 5538 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
c53530da | 5539 | if (!lo) { |
76f6c9e1 | 5540 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
c53530da JL |
5541 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
5542 | if (lo == NULL) | |
5543 | return nfserr_jukebox; | |
5544 | } else { | |
5545 | /* with an existing lockowner, seqids must be the same */ | |
5db1c03f | 5546 | status = nfserr_bad_seqid; |
c53530da JL |
5547 | if (!cstate->minorversion && |
5548 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) | |
5db1c03f | 5549 | goto out; |
64a284d0 | 5550 | } |
c53530da | 5551 | |
dd257933 JL |
5552 | retry: |
5553 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); | |
5554 | if (lst == NULL) { | |
5db1c03f JL |
5555 | status = nfserr_jukebox; |
5556 | goto out; | |
64a284d0 | 5557 | } |
dd257933 JL |
5558 | |
5559 | mutex_lock(&lst->st_mutex); | |
5560 | ||
5561 | /* See if it's still hashed to avoid race with FREE_STATEID */ | |
5562 | spin_lock(&cl->cl_lock); | |
5563 | hashed = !list_empty(&lst->st_perfile); | |
5564 | spin_unlock(&cl->cl_lock); | |
5565 | ||
5566 | if (!hashed) { | |
5567 | mutex_unlock(&lst->st_mutex); | |
5568 | nfs4_put_stid(&lst->st_stid); | |
5569 | goto retry; | |
5570 | } | |
5db1c03f | 5571 | status = nfs_ok; |
dd257933 | 5572 | *plst = lst; |
5db1c03f JL |
5573 | out: |
5574 | nfs4_put_stateowner(&lo->lo_owner); | |
5575 | return status; | |
64a284d0 BF |
5576 | } |
5577 | ||
1da177e4 LT |
5578 | /* |
5579 | * LOCK operation | |
5580 | */ | |
b37ad28b | 5581 | __be32 |
ca364317 | 5582 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 5583 | struct nfsd4_lock *lock) |
1da177e4 | 5584 | { |
fe0750e5 BF |
5585 | struct nfs4_openowner *open_sop = NULL; |
5586 | struct nfs4_lockowner *lock_sop = NULL; | |
3d0fabd5 | 5587 | struct nfs4_ol_stateid *lock_stp = NULL; |
0667b1e9 | 5588 | struct nfs4_ol_stateid *open_stp = NULL; |
7214e860 | 5589 | struct nfs4_file *fp; |
7d947842 | 5590 | struct file *filp = NULL; |
21179d81 JL |
5591 | struct file_lock *file_lock = NULL; |
5592 | struct file_lock *conflock = NULL; | |
b37ad28b | 5593 | __be32 status = 0; |
b34f27aa | 5594 | int lkflg; |
b8dd7b9a | 5595 | int err; |
5db1c03f | 5596 | bool new = false; |
3320fef1 SK |
5597 | struct net *net = SVC_NET(rqstp); |
5598 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
1da177e4 LT |
5599 | |
5600 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", | |
5601 | (long long) lock->lk_offset, | |
5602 | (long long) lock->lk_length); | |
5603 | ||
1da177e4 LT |
5604 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
5605 | return nfserr_inval; | |
5606 | ||
ca364317 | 5607 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
8837abca | 5608 | S_IFREG, NFSD_MAY_LOCK))) { |
a6f6ef2f AA |
5609 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
5610 | return status; | |
5611 | } | |
5612 | ||
1da177e4 | 5613 | if (lock->lk_is_new) { |
684e5638 BF |
5614 | if (nfsd4_has_session(cstate)) |
5615 | /* See rfc 5661 18.10.3: given clientid is ignored: */ | |
76f6c9e1 | 5616 | memcpy(&lock->lk_new_clientid, |
684e5638 BF |
5617 | &cstate->session->se_client->cl_clientid, |
5618 | sizeof(clientid_t)); | |
5619 | ||
1da177e4 | 5620 | status = nfserr_stale_clientid; |
2c142baa | 5621 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
1da177e4 | 5622 | goto out; |
1da177e4 | 5623 | |
1da177e4 | 5624 | /* validate and update open stateid and open seqid */ |
c0a5d93e | 5625 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
1da177e4 LT |
5626 | lock->lk_new_open_seqid, |
5627 | &lock->lk_new_open_stateid, | |
3320fef1 | 5628 | &open_stp, nn); |
37515177 | 5629 | if (status) |
1da177e4 | 5630 | goto out; |
feb9dad5 | 5631 | mutex_unlock(&open_stp->st_mutex); |
fe0750e5 | 5632 | open_sop = openowner(open_stp->st_stateowner); |
b34f27aa | 5633 | status = nfserr_bad_stateid; |
684e5638 | 5634 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
76f6c9e1 | 5635 | &lock->lk_new_clientid)) |
b34f27aa | 5636 | goto out; |
64a284d0 | 5637 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
5db1c03f | 5638 | &lock_stp, &new); |
3d0fabd5 | 5639 | } else { |
dd453dfd | 5640 | status = nfs4_preprocess_seqid_op(cstate, |
fe0750e5 BF |
5641 | lock->lk_old_lock_seqid, |
5642 | &lock->lk_old_lock_stateid, | |
3320fef1 | 5643 | NFS4_LOCK_STID, &lock_stp, nn); |
3d0fabd5 | 5644 | } |
e1aaa891 BF |
5645 | if (status) |
5646 | goto out; | |
64a284d0 | 5647 | lock_sop = lockowner(lock_stp->st_stateowner); |
1da177e4 | 5648 | |
b34f27aa BF |
5649 | lkflg = setlkflg(lock->lk_type); |
5650 | status = nfs4_check_openmode(lock_stp, lkflg); | |
5651 | if (status) | |
5652 | goto out; | |
5653 | ||
0dd395dc | 5654 | status = nfserr_grace; |
3320fef1 | 5655 | if (locks_in_grace(net) && !lock->lk_reclaim) |
0dd395dc N |
5656 | goto out; |
5657 | status = nfserr_no_grace; | |
3320fef1 | 5658 | if (!locks_in_grace(net) && lock->lk_reclaim) |
0dd395dc N |
5659 | goto out; |
5660 | ||
21179d81 JL |
5661 | file_lock = locks_alloc_lock(); |
5662 | if (!file_lock) { | |
5663 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
5664 | status = nfserr_jukebox; | |
5665 | goto out; | |
5666 | } | |
5667 | ||
11b9164a | 5668 | fp = lock_stp->st_stid.sc_file; |
1da177e4 LT |
5669 | switch (lock->lk_type) { |
5670 | case NFS4_READ_LT: | |
5671 | case NFS4_READW_LT: | |
7214e860 JL |
5672 | spin_lock(&fp->fi_lock); |
5673 | filp = find_readable_file_locked(fp); | |
0997b173 BF |
5674 | if (filp) |
5675 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); | |
7214e860 | 5676 | spin_unlock(&fp->fi_lock); |
21179d81 | 5677 | file_lock->fl_type = F_RDLCK; |
529d7b2a | 5678 | break; |
1da177e4 LT |
5679 | case NFS4_WRITE_LT: |
5680 | case NFS4_WRITEW_LT: | |
7214e860 JL |
5681 | spin_lock(&fp->fi_lock); |
5682 | filp = find_writeable_file_locked(fp); | |
0997b173 BF |
5683 | if (filp) |
5684 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); | |
7214e860 | 5685 | spin_unlock(&fp->fi_lock); |
21179d81 | 5686 | file_lock->fl_type = F_WRLCK; |
529d7b2a | 5687 | break; |
1da177e4 LT |
5688 | default: |
5689 | status = nfserr_inval; | |
5690 | goto out; | |
5691 | } | |
f9d7562f BF |
5692 | if (!filp) { |
5693 | status = nfserr_openmode; | |
5694 | goto out; | |
5695 | } | |
aef9583b KM |
5696 | |
5697 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); | |
21179d81 JL |
5698 | file_lock->fl_pid = current->tgid; |
5699 | file_lock->fl_file = filp; | |
5700 | file_lock->fl_flags = FL_POSIX; | |
5701 | file_lock->fl_lmops = &nfsd_posix_mng_ops; | |
5702 | file_lock->fl_start = lock->lk_offset; | |
5703 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); | |
5704 | nfs4_transform_lock_offset(file_lock); | |
5705 | ||
5706 | conflock = locks_alloc_lock(); | |
5707 | if (!conflock) { | |
5708 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
5709 | status = nfserr_jukebox; | |
5710 | goto out; | |
5711 | } | |
1da177e4 | 5712 | |
21179d81 | 5713 | err = vfs_lock_file(filp, F_SETLK, file_lock, conflock); |
b8dd7b9a | 5714 | switch (-err) { |
1da177e4 | 5715 | case 0: /* success! */ |
9767feb2 | 5716 | nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); |
b8dd7b9a | 5717 | status = 0; |
eb76b3fd AA |
5718 | break; |
5719 | case (EAGAIN): /* conflock holds conflicting lock */ | |
5720 | status = nfserr_denied; | |
5721 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); | |
21179d81 | 5722 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
eb76b3fd | 5723 | break; |
1da177e4 LT |
5724 | case (EDEADLK): |
5725 | status = nfserr_deadlock; | |
eb76b3fd | 5726 | break; |
3e772463 | 5727 | default: |
fd85b817 | 5728 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
3e772463 | 5729 | status = nfserrno(err); |
eb76b3fd | 5730 | break; |
1da177e4 | 5731 | } |
1da177e4 | 5732 | out: |
de18643d TM |
5733 | if (filp) |
5734 | fput(filp); | |
5db1c03f JL |
5735 | if (lock_stp) { |
5736 | /* Bump seqid manually if the 4.0 replay owner is openowner */ | |
5737 | if (cstate->replay_owner && | |
5738 | cstate->replay_owner != &lock_sop->lo_owner && | |
5739 | seqid_mutating_err(ntohl(status))) | |
5740 | lock_sop->lo_owner.so_seqid++; | |
5741 | ||
feb9dad5 | 5742 | mutex_unlock(&lock_stp->st_mutex); |
35a92fe8 | 5743 | |
5db1c03f JL |
5744 | /* |
5745 | * If this is a new, never-before-used stateid, and we are | |
5746 | * returning an error, then just go ahead and release it. | |
5747 | */ | |
5748 | if (status && new) | |
5749 | release_lock_stateid(lock_stp); | |
5750 | ||
3d0fabd5 | 5751 | nfs4_put_stid(&lock_stp->st_stid); |
5db1c03f | 5752 | } |
0667b1e9 TM |
5753 | if (open_stp) |
5754 | nfs4_put_stid(&open_stp->st_stid); | |
9411b1d4 | 5755 | nfsd4_bump_seqid(cstate, status); |
21179d81 JL |
5756 | if (file_lock) |
5757 | locks_free_lock(file_lock); | |
5758 | if (conflock) | |
5759 | locks_free_lock(conflock); | |
1da177e4 LT |
5760 | return status; |
5761 | } | |
5762 | ||
55ef1274 BF |
5763 | /* |
5764 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, | |
5765 | * so we do a temporary open here just to get an open file to pass to | |
5766 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an | |
5767 | * inode operation.) | |
5768 | */ | |
04da6e9d | 5769 | static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) |
55ef1274 BF |
5770 | { |
5771 | struct file *file; | |
04da6e9d AV |
5772 | __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
5773 | if (!err) { | |
5774 | err = nfserrno(vfs_test_lock(file, lock)); | |
fd891454 | 5775 | fput(file); |
04da6e9d | 5776 | } |
55ef1274 BF |
5777 | return err; |
5778 | } | |
5779 | ||
1da177e4 LT |
5780 | /* |
5781 | * LOCKT operation | |
5782 | */ | |
b37ad28b | 5783 | __be32 |
ca364317 BF |
5784 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
5785 | struct nfsd4_lockt *lockt) | |
1da177e4 | 5786 | { |
21179d81 | 5787 | struct file_lock *file_lock = NULL; |
5db1c03f | 5788 | struct nfs4_lockowner *lo = NULL; |
b37ad28b | 5789 | __be32 status; |
7f2210fa | 5790 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
1da177e4 | 5791 | |
5ccb0066 | 5792 | if (locks_in_grace(SVC_NET(rqstp))) |
1da177e4 LT |
5793 | return nfserr_grace; |
5794 | ||
5795 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) | |
5796 | return nfserr_inval; | |
5797 | ||
9b2ef62b | 5798 | if (!nfsd4_has_session(cstate)) { |
4b24ca7d | 5799 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
9b2ef62b BF |
5800 | if (status) |
5801 | goto out; | |
5802 | } | |
1da177e4 | 5803 | |
75c096f7 | 5804 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
1da177e4 | 5805 | goto out; |
1da177e4 | 5806 | |
21179d81 JL |
5807 | file_lock = locks_alloc_lock(); |
5808 | if (!file_lock) { | |
5809 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
5810 | status = nfserr_jukebox; | |
5811 | goto out; | |
5812 | } | |
6cd90662 | 5813 | |
1da177e4 LT |
5814 | switch (lockt->lt_type) { |
5815 | case NFS4_READ_LT: | |
5816 | case NFS4_READW_LT: | |
21179d81 | 5817 | file_lock->fl_type = F_RDLCK; |
1da177e4 LT |
5818 | break; |
5819 | case NFS4_WRITE_LT: | |
5820 | case NFS4_WRITEW_LT: | |
21179d81 | 5821 | file_lock->fl_type = F_WRLCK; |
1da177e4 LT |
5822 | break; |
5823 | default: | |
2fdada03 | 5824 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
1da177e4 LT |
5825 | status = nfserr_inval; |
5826 | goto out; | |
5827 | } | |
5828 | ||
c8623999 | 5829 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
fe0750e5 | 5830 | if (lo) |
21179d81 JL |
5831 | file_lock->fl_owner = (fl_owner_t)lo; |
5832 | file_lock->fl_pid = current->tgid; | |
5833 | file_lock->fl_flags = FL_POSIX; | |
1da177e4 | 5834 | |
21179d81 JL |
5835 | file_lock->fl_start = lockt->lt_offset; |
5836 | file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); | |
1da177e4 | 5837 | |
21179d81 | 5838 | nfs4_transform_lock_offset(file_lock); |
1da177e4 | 5839 | |
21179d81 | 5840 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
04da6e9d | 5841 | if (status) |
fd85b817 | 5842 | goto out; |
04da6e9d | 5843 | |
21179d81 | 5844 | if (file_lock->fl_type != F_UNLCK) { |
1da177e4 | 5845 | status = nfserr_denied; |
21179d81 | 5846 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
1da177e4 LT |
5847 | } |
5848 | out: | |
5db1c03f JL |
5849 | if (lo) |
5850 | nfs4_put_stateowner(&lo->lo_owner); | |
21179d81 JL |
5851 | if (file_lock) |
5852 | locks_free_lock(file_lock); | |
1da177e4 LT |
5853 | return status; |
5854 | } | |
5855 | ||
b37ad28b | 5856 | __be32 |
ca364317 | 5857 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
a4f1706a | 5858 | struct nfsd4_locku *locku) |
1da177e4 | 5859 | { |
dcef0413 | 5860 | struct nfs4_ol_stateid *stp; |
1da177e4 | 5861 | struct file *filp = NULL; |
21179d81 | 5862 | struct file_lock *file_lock = NULL; |
b37ad28b | 5863 | __be32 status; |
b8dd7b9a | 5864 | int err; |
3320fef1 SK |
5865 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
5866 | ||
1da177e4 LT |
5867 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
5868 | (long long) locku->lu_offset, | |
5869 | (long long) locku->lu_length); | |
5870 | ||
5871 | if (check_lock_length(locku->lu_offset, locku->lu_length)) | |
5872 | return nfserr_inval; | |
5873 | ||
9072d5c6 | 5874 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
3320fef1 SK |
5875 | &locku->lu_stateid, NFS4_LOCK_STID, |
5876 | &stp, nn); | |
9072d5c6 | 5877 | if (status) |
1da177e4 | 5878 | goto out; |
11b9164a | 5879 | filp = find_any_file(stp->st_stid.sc_file); |
f9d7562f BF |
5880 | if (!filp) { |
5881 | status = nfserr_lock_range; | |
858cc573 | 5882 | goto put_stateid; |
f9d7562f | 5883 | } |
21179d81 JL |
5884 | file_lock = locks_alloc_lock(); |
5885 | if (!file_lock) { | |
5886 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); | |
5887 | status = nfserr_jukebox; | |
de18643d | 5888 | goto fput; |
21179d81 | 5889 | } |
6cd90662 | 5890 | |
21179d81 | 5891 | file_lock->fl_type = F_UNLCK; |
aef9583b | 5892 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); |
21179d81 JL |
5893 | file_lock->fl_pid = current->tgid; |
5894 | file_lock->fl_file = filp; | |
5895 | file_lock->fl_flags = FL_POSIX; | |
5896 | file_lock->fl_lmops = &nfsd_posix_mng_ops; | |
5897 | file_lock->fl_start = locku->lu_offset; | |
5898 | ||
5899 | file_lock->fl_end = last_byte_offset(locku->lu_offset, | |
5900 | locku->lu_length); | |
5901 | nfs4_transform_lock_offset(file_lock); | |
1da177e4 | 5902 | |
21179d81 | 5903 | err = vfs_lock_file(filp, F_SETLK, file_lock, NULL); |
b8dd7b9a | 5904 | if (err) { |
fd85b817 | 5905 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
1da177e4 LT |
5906 | goto out_nfserr; |
5907 | } | |
9767feb2 | 5908 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
de18643d TM |
5909 | fput: |
5910 | fput(filp); | |
858cc573 | 5911 | put_stateid: |
feb9dad5 | 5912 | mutex_unlock(&stp->st_mutex); |
858cc573 | 5913 | nfs4_put_stid(&stp->st_stid); |
1da177e4 | 5914 | out: |
9411b1d4 | 5915 | nfsd4_bump_seqid(cstate, status); |
21179d81 JL |
5916 | if (file_lock) |
5917 | locks_free_lock(file_lock); | |
1da177e4 LT |
5918 | return status; |
5919 | ||
5920 | out_nfserr: | |
b8dd7b9a | 5921 | status = nfserrno(err); |
de18643d | 5922 | goto fput; |
1da177e4 LT |
5923 | } |
5924 | ||
5925 | /* | |
5926 | * returns | |
f9c00c3a JL |
5927 | * true: locks held by lockowner |
5928 | * false: no locks held by lockowner | |
1da177e4 | 5929 | */ |
f9c00c3a JL |
5930 | static bool |
5931 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) | |
1da177e4 | 5932 | { |
bd61e0a9 | 5933 | struct file_lock *fl; |
f9c00c3a JL |
5934 | int status = false; |
5935 | struct file *filp = find_any_file(fp); | |
5936 | struct inode *inode; | |
bd61e0a9 | 5937 | struct file_lock_context *flctx; |
f9c00c3a JL |
5938 | |
5939 | if (!filp) { | |
5940 | /* Any valid lock stateid should have some sort of access */ | |
5941 | WARN_ON_ONCE(1); | |
5942 | return status; | |
5943 | } | |
5944 | ||
5945 | inode = file_inode(filp); | |
bd61e0a9 JL |
5946 | flctx = inode->i_flctx; |
5947 | ||
5948 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { | |
6109c850 | 5949 | spin_lock(&flctx->flc_lock); |
bd61e0a9 JL |
5950 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
5951 | if (fl->fl_owner == (fl_owner_t)lowner) { | |
5952 | status = true; | |
5953 | break; | |
5954 | } | |
796dadfd | 5955 | } |
6109c850 | 5956 | spin_unlock(&flctx->flc_lock); |
1da177e4 | 5957 | } |
f9c00c3a | 5958 | fput(filp); |
1da177e4 LT |
5959 | return status; |
5960 | } | |
5961 | ||
b37ad28b | 5962 | __be32 |
b591480b BF |
5963 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
5964 | struct nfsd4_compound_state *cstate, | |
5965 | struct nfsd4_release_lockowner *rlockowner) | |
1da177e4 LT |
5966 | { |
5967 | clientid_t *clid = &rlockowner->rl_clientid; | |
882e9d25 JL |
5968 | struct nfs4_stateowner *sop; |
5969 | struct nfs4_lockowner *lo = NULL; | |
dcef0413 | 5970 | struct nfs4_ol_stateid *stp; |
1da177e4 | 5971 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
d4f0489f | 5972 | unsigned int hashval = ownerstr_hashval(owner); |
b37ad28b | 5973 | __be32 status; |
7f2210fa | 5974 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
c58c6610 | 5975 | struct nfs4_client *clp; |
88584818 | 5976 | LIST_HEAD (reaplist); |
1da177e4 LT |
5977 | |
5978 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", | |
5979 | clid->cl_boot, clid->cl_id); | |
5980 | ||
4b24ca7d | 5981 | status = lookup_clientid(clid, cstate, nn); |
9b2ef62b | 5982 | if (status) |
51f5e783 | 5983 | return status; |
9b2ef62b | 5984 | |
d4f0489f | 5985 | clp = cstate->clp; |
fd44907c | 5986 | /* Find the matching lock stateowner */ |
d4f0489f | 5987 | spin_lock(&clp->cl_lock); |
882e9d25 | 5988 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
d4f0489f | 5989 | so_strhash) { |
fd44907c | 5990 | |
882e9d25 JL |
5991 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
5992 | continue; | |
fd44907c | 5993 | |
882e9d25 JL |
5994 | /* see if there are still any locks associated with it */ |
5995 | lo = lockowner(sop); | |
5996 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { | |
5997 | if (check_for_locks(stp->st_stid.sc_file, lo)) { | |
5998 | status = nfserr_locks_held; | |
5999 | spin_unlock(&clp->cl_lock); | |
51f5e783 | 6000 | return status; |
882e9d25 | 6001 | } |
5adfd885 | 6002 | } |
882e9d25 | 6003 | |
b5971afa | 6004 | nfs4_get_stateowner(sop); |
882e9d25 | 6005 | break; |
1da177e4 | 6006 | } |
88584818 CL |
6007 | if (!lo) { |
6008 | spin_unlock(&clp->cl_lock); | |
6009 | return status; | |
6010 | } | |
6011 | ||
6012 | unhash_lockowner_locked(lo); | |
6013 | while (!list_empty(&lo->lo_owner.so_stateids)) { | |
6014 | stp = list_first_entry(&lo->lo_owner.so_stateids, | |
6015 | struct nfs4_ol_stateid, | |
6016 | st_perstateowner); | |
6017 | WARN_ON(!unhash_lock_stateid(stp)); | |
6018 | put_ol_stateid_locked(stp, &reaplist); | |
6019 | } | |
c58c6610 | 6020 | spin_unlock(&clp->cl_lock); |
88584818 CL |
6021 | free_ol_stateid_reaplist(&reaplist); |
6022 | nfs4_put_stateowner(&lo->lo_owner); | |
6023 | ||
1da177e4 LT |
6024 | return status; |
6025 | } | |
6026 | ||
6027 | static inline struct nfs4_client_reclaim * | |
a55370a3 | 6028 | alloc_reclaim(void) |
1da177e4 | 6029 | { |
a55370a3 | 6030 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
1da177e4 LT |
6031 | } |
6032 | ||
0ce0c2b5 | 6033 | bool |
52e19c09 | 6034 | nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn) |
c7b9a459 | 6035 | { |
0ce0c2b5 | 6036 | struct nfs4_client_reclaim *crp; |
c7b9a459 | 6037 | |
52e19c09 | 6038 | crp = nfsd4_find_reclaim_client(name, nn); |
0ce0c2b5 | 6039 | return (crp && crp->cr_clp); |
c7b9a459 N |
6040 | } |
6041 | ||
1da177e4 LT |
6042 | /* |
6043 | * failure => all reset bets are off, nfserr_no_grace... | |
6044 | */ | |
772a9bbb | 6045 | struct nfs4_client_reclaim * |
52e19c09 | 6046 | nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn) |
1da177e4 LT |
6047 | { |
6048 | unsigned int strhashval; | |
772a9bbb | 6049 | struct nfs4_client_reclaim *crp; |
1da177e4 | 6050 | |
a55370a3 N |
6051 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
6052 | crp = alloc_reclaim(); | |
772a9bbb JL |
6053 | if (crp) { |
6054 | strhashval = clientstr_hashval(name); | |
6055 | INIT_LIST_HEAD(&crp->cr_strhash); | |
52e19c09 | 6056 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
772a9bbb | 6057 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
0ce0c2b5 | 6058 | crp->cr_clp = NULL; |
52e19c09 | 6059 | nn->reclaim_str_hashtbl_size++; |
772a9bbb JL |
6060 | } |
6061 | return crp; | |
1da177e4 LT |
6062 | } |
6063 | ||
ce30e539 | 6064 | void |
52e19c09 | 6065 | nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) |
ce30e539 JL |
6066 | { |
6067 | list_del(&crp->cr_strhash); | |
6068 | kfree(crp); | |
52e19c09 | 6069 | nn->reclaim_str_hashtbl_size--; |
ce30e539 JL |
6070 | } |
6071 | ||
2a4317c5 | 6072 | void |
52e19c09 | 6073 | nfs4_release_reclaim(struct nfsd_net *nn) |
1da177e4 LT |
6074 | { |
6075 | struct nfs4_client_reclaim *crp = NULL; | |
6076 | int i; | |
6077 | ||
1da177e4 | 6078 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
52e19c09 SK |
6079 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
6080 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, | |
1da177e4 | 6081 | struct nfs4_client_reclaim, cr_strhash); |
52e19c09 | 6082 | nfs4_remove_reclaim_record(crp, nn); |
1da177e4 LT |
6083 | } |
6084 | } | |
063b0fb9 | 6085 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
1da177e4 LT |
6086 | } |
6087 | ||
6088 | /* | |
6089 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ | |
2a4317c5 | 6090 | struct nfs4_client_reclaim * |
52e19c09 | 6091 | nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn) |
1da177e4 LT |
6092 | { |
6093 | unsigned int strhashval; | |
1da177e4 LT |
6094 | struct nfs4_client_reclaim *crp = NULL; |
6095 | ||
278c931c | 6096 | dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir); |
1da177e4 | 6097 | |
278c931c | 6098 | strhashval = clientstr_hashval(recdir); |
52e19c09 | 6099 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
278c931c | 6100 | if (same_name(crp->cr_recdir, recdir)) { |
1da177e4 LT |
6101 | return crp; |
6102 | } | |
6103 | } | |
6104 | return NULL; | |
6105 | } | |
6106 | ||
6107 | /* | |
6108 | * Called from OPEN. Look for clientid in reclaim list. | |
6109 | */ | |
b37ad28b | 6110 | __be32 |
0fe492db TM |
6111 | nfs4_check_open_reclaim(clientid_t *clid, |
6112 | struct nfsd4_compound_state *cstate, | |
6113 | struct nfsd_net *nn) | |
1da177e4 | 6114 | { |
0fe492db | 6115 | __be32 status; |
a52d726b JL |
6116 | |
6117 | /* find clientid in conf_id_hashtbl */ | |
0fe492db TM |
6118 | status = lookup_clientid(clid, cstate, nn); |
6119 | if (status) | |
a52d726b JL |
6120 | return nfserr_reclaim_bad; |
6121 | ||
3b3e7b72 JL |
6122 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
6123 | return nfserr_no_grace; | |
6124 | ||
0fe492db TM |
6125 | if (nfsd4_client_record_check(cstate->clp)) |
6126 | return nfserr_reclaim_bad; | |
6127 | ||
6128 | return nfs_ok; | |
1da177e4 LT |
6129 | } |
6130 | ||
65178db4 | 6131 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
016200c3 JL |
6132 | static inline void |
6133 | put_client(struct nfs4_client *clp) | |
6134 | { | |
6135 | atomic_dec(&clp->cl_refcount); | |
6136 | } | |
6137 | ||
285abdee JL |
6138 | static struct nfs4_client * |
6139 | nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) | |
6140 | { | |
6141 | struct nfs4_client *clp; | |
6142 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6143 | nfsd_net_id); | |
6144 | ||
6145 | if (!nfsd_netns_ready(nn)) | |
6146 | return NULL; | |
6147 | ||
6148 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { | |
6149 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) | |
6150 | return clp; | |
6151 | } | |
6152 | return NULL; | |
6153 | } | |
6154 | ||
7ec0e36f | 6155 | u64 |
285abdee | 6156 | nfsd_inject_print_clients(void) |
7ec0e36f JL |
6157 | { |
6158 | struct nfs4_client *clp; | |
6159 | u64 count = 0; | |
6160 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6161 | nfsd_net_id); | |
6162 | char buf[INET6_ADDRSTRLEN]; | |
6163 | ||
6164 | if (!nfsd_netns_ready(nn)) | |
6165 | return 0; | |
6166 | ||
6167 | spin_lock(&nn->client_lock); | |
6168 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { | |
6169 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); | |
6170 | pr_info("NFS Client: %s\n", buf); | |
6171 | ++count; | |
6172 | } | |
6173 | spin_unlock(&nn->client_lock); | |
6174 | ||
6175 | return count; | |
6176 | } | |
65178db4 | 6177 | |
a0926d15 | 6178 | u64 |
285abdee | 6179 | nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) |
a0926d15 JL |
6180 | { |
6181 | u64 count = 0; | |
6182 | struct nfs4_client *clp; | |
6183 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6184 | nfsd_net_id); | |
6185 | ||
6186 | if (!nfsd_netns_ready(nn)) | |
6187 | return count; | |
6188 | ||
6189 | spin_lock(&nn->client_lock); | |
6190 | clp = nfsd_find_client(addr, addr_size); | |
6191 | if (clp) { | |
6192 | if (mark_client_expired_locked(clp) == nfs_ok) | |
6193 | ++count; | |
6194 | else | |
6195 | clp = NULL; | |
6196 | } | |
6197 | spin_unlock(&nn->client_lock); | |
6198 | ||
6199 | if (clp) | |
6200 | expire_client(clp); | |
6201 | ||
6202 | return count; | |
6203 | } | |
6204 | ||
69fc9edf | 6205 | u64 |
285abdee | 6206 | nfsd_inject_forget_clients(u64 max) |
69fc9edf JL |
6207 | { |
6208 | u64 count = 0; | |
6209 | struct nfs4_client *clp, *next; | |
6210 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6211 | nfsd_net_id); | |
6212 | LIST_HEAD(reaplist); | |
6213 | ||
6214 | if (!nfsd_netns_ready(nn)) | |
6215 | return count; | |
6216 | ||
6217 | spin_lock(&nn->client_lock); | |
6218 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { | |
6219 | if (mark_client_expired_locked(clp) == nfs_ok) { | |
6220 | list_add(&clp->cl_lru, &reaplist); | |
6221 | if (max != 0 && ++count >= max) | |
6222 | break; | |
6223 | } | |
6224 | } | |
6225 | spin_unlock(&nn->client_lock); | |
6226 | ||
6227 | list_for_each_entry_safe(clp, next, &reaplist, cl_lru) | |
6228 | expire_client(clp); | |
6229 | ||
6230 | return count; | |
6231 | } | |
6232 | ||
184c1847 BS |
6233 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
6234 | const char *type) | |
6235 | { | |
6236 | char buf[INET6_ADDRSTRLEN]; | |
0a5c33e2 | 6237 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
184c1847 BS |
6238 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
6239 | } | |
6240 | ||
016200c3 JL |
6241 | static void |
6242 | nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, | |
6243 | struct list_head *collect) | |
6244 | { | |
6245 | struct nfs4_client *clp = lst->st_stid.sc_client; | |
6246 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6247 | nfsd_net_id); | |
6248 | ||
6249 | if (!collect) | |
6250 | return; | |
6251 | ||
6252 | lockdep_assert_held(&nn->client_lock); | |
6253 | atomic_inc(&clp->cl_refcount); | |
6254 | list_add(&lst->st_locks, collect); | |
6255 | } | |
6256 | ||
3c87b9b7 | 6257 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
3738d50e | 6258 | struct list_head *collect, |
e8568739 | 6259 | bool (*func)(struct nfs4_ol_stateid *)) |
fc29171f BS |
6260 | { |
6261 | struct nfs4_openowner *oop; | |
fc29171f | 6262 | struct nfs4_ol_stateid *stp, *st_next; |
3c87b9b7 | 6263 | struct nfs4_ol_stateid *lst, *lst_next; |
fc29171f BS |
6264 | u64 count = 0; |
6265 | ||
016200c3 | 6266 | spin_lock(&clp->cl_lock); |
fc29171f | 6267 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
3c87b9b7 TM |
6268 | list_for_each_entry_safe(stp, st_next, |
6269 | &oop->oo_owner.so_stateids, st_perstateowner) { | |
6270 | list_for_each_entry_safe(lst, lst_next, | |
6271 | &stp->st_locks, st_locks) { | |
3738d50e | 6272 | if (func) { |
e8568739 JL |
6273 | if (func(lst)) |
6274 | nfsd_inject_add_lock_to_list(lst, | |
6275 | collect); | |
3738d50e | 6276 | } |
016200c3 JL |
6277 | ++count; |
6278 | /* | |
6279 | * Despite the fact that these functions deal | |
6280 | * with 64-bit integers for "count", we must | |
6281 | * ensure that it doesn't blow up the | |
6282 | * clp->cl_refcount. Throw a warning if we | |
6283 | * start to approach INT_MAX here. | |
6284 | */ | |
6285 | WARN_ON_ONCE(count == (INT_MAX / 2)); | |
6286 | if (count == max) | |
6287 | goto out; | |
fc29171f BS |
6288 | } |
6289 | } | |
6290 | } | |
016200c3 JL |
6291 | out: |
6292 | spin_unlock(&clp->cl_lock); | |
fc29171f BS |
6293 | |
6294 | return count; | |
6295 | } | |
6296 | ||
016200c3 JL |
6297 | static u64 |
6298 | nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, | |
6299 | u64 max) | |
fc29171f | 6300 | { |
016200c3 | 6301 | return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); |
fc29171f BS |
6302 | } |
6303 | ||
016200c3 JL |
6304 | static u64 |
6305 | nfsd_print_client_locks(struct nfs4_client *clp) | |
184c1847 | 6306 | { |
016200c3 | 6307 | u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); |
184c1847 BS |
6308 | nfsd_print_count(clp, count, "locked files"); |
6309 | return count; | |
6310 | } | |
6311 | ||
016200c3 | 6312 | u64 |
285abdee | 6313 | nfsd_inject_print_locks(void) |
016200c3 JL |
6314 | { |
6315 | struct nfs4_client *clp; | |
6316 | u64 count = 0; | |
6317 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6318 | nfsd_net_id); | |
6319 | ||
6320 | if (!nfsd_netns_ready(nn)) | |
6321 | return 0; | |
6322 | ||
6323 | spin_lock(&nn->client_lock); | |
6324 | list_for_each_entry(clp, &nn->client_lru, cl_lru) | |
6325 | count += nfsd_print_client_locks(clp); | |
6326 | spin_unlock(&nn->client_lock); | |
6327 | ||
6328 | return count; | |
6329 | } | |
6330 | ||
6331 | static void | |
6332 | nfsd_reap_locks(struct list_head *reaplist) | |
6333 | { | |
6334 | struct nfs4_client *clp; | |
6335 | struct nfs4_ol_stateid *stp, *next; | |
6336 | ||
6337 | list_for_each_entry_safe(stp, next, reaplist, st_locks) { | |
6338 | list_del_init(&stp->st_locks); | |
6339 | clp = stp->st_stid.sc_client; | |
6340 | nfs4_put_stid(&stp->st_stid); | |
6341 | put_client(clp); | |
6342 | } | |
6343 | } | |
6344 | ||
6345 | u64 | |
285abdee | 6346 | nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size) |
016200c3 JL |
6347 | { |
6348 | unsigned int count = 0; | |
6349 | struct nfs4_client *clp; | |
6350 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6351 | nfsd_net_id); | |
6352 | LIST_HEAD(reaplist); | |
6353 | ||
6354 | if (!nfsd_netns_ready(nn)) | |
6355 | return count; | |
6356 | ||
6357 | spin_lock(&nn->client_lock); | |
6358 | clp = nfsd_find_client(addr, addr_size); | |
6359 | if (clp) | |
6360 | count = nfsd_collect_client_locks(clp, &reaplist, 0); | |
6361 | spin_unlock(&nn->client_lock); | |
6362 | nfsd_reap_locks(&reaplist); | |
6363 | return count; | |
6364 | } | |
6365 | ||
6366 | u64 | |
285abdee | 6367 | nfsd_inject_forget_locks(u64 max) |
016200c3 JL |
6368 | { |
6369 | u64 count = 0; | |
6370 | struct nfs4_client *clp; | |
6371 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6372 | nfsd_net_id); | |
6373 | LIST_HEAD(reaplist); | |
6374 | ||
6375 | if (!nfsd_netns_ready(nn)) | |
6376 | return count; | |
6377 | ||
6378 | spin_lock(&nn->client_lock); | |
6379 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { | |
6380 | count += nfsd_collect_client_locks(clp, &reaplist, max - count); | |
6381 | if (max != 0 && count >= max) | |
6382 | break; | |
6383 | } | |
6384 | spin_unlock(&nn->client_lock); | |
6385 | nfsd_reap_locks(&reaplist); | |
6386 | return count; | |
6387 | } | |
6388 | ||
82e05efa JL |
6389 | static u64 |
6390 | nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, | |
6391 | struct list_head *collect, | |
6392 | void (*func)(struct nfs4_openowner *)) | |
4dbdbda8 BS |
6393 | { |
6394 | struct nfs4_openowner *oop, *next; | |
82e05efa JL |
6395 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
6396 | nfsd_net_id); | |
4dbdbda8 BS |
6397 | u64 count = 0; |
6398 | ||
82e05efa JL |
6399 | lockdep_assert_held(&nn->client_lock); |
6400 | ||
6401 | spin_lock(&clp->cl_lock); | |
4dbdbda8 | 6402 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
82e05efa | 6403 | if (func) { |
4dbdbda8 | 6404 | func(oop); |
82e05efa JL |
6405 | if (collect) { |
6406 | atomic_inc(&clp->cl_refcount); | |
6407 | list_add(&oop->oo_perclient, collect); | |
6408 | } | |
6409 | } | |
6410 | ++count; | |
6411 | /* | |
6412 | * Despite the fact that these functions deal with | |
6413 | * 64-bit integers for "count", we must ensure that | |
6414 | * it doesn't blow up the clp->cl_refcount. Throw a | |
6415 | * warning if we start to approach INT_MAX here. | |
6416 | */ | |
6417 | WARN_ON_ONCE(count == (INT_MAX / 2)); | |
6418 | if (count == max) | |
4dbdbda8 BS |
6419 | break; |
6420 | } | |
82e05efa JL |
6421 | spin_unlock(&clp->cl_lock); |
6422 | ||
6423 | return count; | |
6424 | } | |
6425 | ||
6426 | static u64 | |
6427 | nfsd_print_client_openowners(struct nfs4_client *clp) | |
6428 | { | |
6429 | u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); | |
6430 | ||
6431 | nfsd_print_count(clp, count, "openowners"); | |
6432 | return count; | |
6433 | } | |
6434 | ||
6435 | static u64 | |
6436 | nfsd_collect_client_openowners(struct nfs4_client *clp, | |
6437 | struct list_head *collect, u64 max) | |
6438 | { | |
6439 | return nfsd_foreach_client_openowner(clp, max, collect, | |
6440 | unhash_openowner_locked); | |
6441 | } | |
6442 | ||
6443 | u64 | |
285abdee | 6444 | nfsd_inject_print_openowners(void) |
82e05efa JL |
6445 | { |
6446 | struct nfs4_client *clp; | |
6447 | u64 count = 0; | |
6448 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6449 | nfsd_net_id); | |
6450 | ||
6451 | if (!nfsd_netns_ready(nn)) | |
6452 | return 0; | |
6453 | ||
6454 | spin_lock(&nn->client_lock); | |
6455 | list_for_each_entry(clp, &nn->client_lru, cl_lru) | |
6456 | count += nfsd_print_client_openowners(clp); | |
6457 | spin_unlock(&nn->client_lock); | |
4dbdbda8 BS |
6458 | |
6459 | return count; | |
6460 | } | |
6461 | ||
82e05efa JL |
6462 | static void |
6463 | nfsd_reap_openowners(struct list_head *reaplist) | |
6464 | { | |
6465 | struct nfs4_client *clp; | |
6466 | struct nfs4_openowner *oop, *next; | |
6467 | ||
6468 | list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { | |
6469 | list_del_init(&oop->oo_perclient); | |
6470 | clp = oop->oo_owner.so_client; | |
6471 | release_openowner(oop); | |
6472 | put_client(clp); | |
6473 | } | |
6474 | } | |
6475 | ||
6476 | u64 | |
285abdee JL |
6477 | nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, |
6478 | size_t addr_size) | |
4dbdbda8 | 6479 | { |
82e05efa JL |
6480 | unsigned int count = 0; |
6481 | struct nfs4_client *clp; | |
6482 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6483 | nfsd_net_id); | |
6484 | LIST_HEAD(reaplist); | |
6485 | ||
6486 | if (!nfsd_netns_ready(nn)) | |
6487 | return count; | |
6488 | ||
6489 | spin_lock(&nn->client_lock); | |
6490 | clp = nfsd_find_client(addr, addr_size); | |
6491 | if (clp) | |
6492 | count = nfsd_collect_client_openowners(clp, &reaplist, 0); | |
6493 | spin_unlock(&nn->client_lock); | |
6494 | nfsd_reap_openowners(&reaplist); | |
6495 | return count; | |
4dbdbda8 BS |
6496 | } |
6497 | ||
82e05efa | 6498 | u64 |
285abdee | 6499 | nfsd_inject_forget_openowners(u64 max) |
184c1847 | 6500 | { |
82e05efa JL |
6501 | u64 count = 0; |
6502 | struct nfs4_client *clp; | |
6503 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6504 | nfsd_net_id); | |
6505 | LIST_HEAD(reaplist); | |
6506 | ||
6507 | if (!nfsd_netns_ready(nn)) | |
6508 | return count; | |
6509 | ||
6510 | spin_lock(&nn->client_lock); | |
6511 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { | |
6512 | count += nfsd_collect_client_openowners(clp, &reaplist, | |
6513 | max - count); | |
6514 | if (max != 0 && count >= max) | |
6515 | break; | |
6516 | } | |
6517 | spin_unlock(&nn->client_lock); | |
6518 | nfsd_reap_openowners(&reaplist); | |
184c1847 BS |
6519 | return count; |
6520 | } | |
6521 | ||
269de30f BS |
6522 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
6523 | struct list_head *victims) | |
6524 | { | |
6525 | struct nfs4_delegation *dp, *next; | |
98d5c7c5 JL |
6526 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
6527 | nfsd_net_id); | |
269de30f BS |
6528 | u64 count = 0; |
6529 | ||
98d5c7c5 JL |
6530 | lockdep_assert_held(&nn->client_lock); |
6531 | ||
6532 | spin_lock(&state_lock); | |
269de30f | 6533 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
dff1399f JL |
6534 | if (victims) { |
6535 | /* | |
6536 | * It's not safe to mess with delegations that have a | |
6537 | * non-zero dl_time. They might have already been broken | |
6538 | * and could be processed by the laundromat outside of | |
6539 | * the state_lock. Just leave them be. | |
6540 | */ | |
6541 | if (dp->dl_time != 0) | |
6542 | continue; | |
6543 | ||
98d5c7c5 | 6544 | atomic_inc(&clp->cl_refcount); |
3fcbbd24 | 6545 | WARN_ON(!unhash_delegation_locked(dp)); |
42690676 | 6546 | list_add(&dp->dl_recall_lru, victims); |
dff1399f | 6547 | } |
98d5c7c5 JL |
6548 | ++count; |
6549 | /* | |
6550 | * Despite the fact that these functions deal with | |
6551 | * 64-bit integers for "count", we must ensure that | |
6552 | * it doesn't blow up the clp->cl_refcount. Throw a | |
6553 | * warning if we start to approach INT_MAX here. | |
6554 | */ | |
6555 | WARN_ON_ONCE(count == (INT_MAX / 2)); | |
6556 | if (count == max) | |
269de30f BS |
6557 | break; |
6558 | } | |
98d5c7c5 | 6559 | spin_unlock(&state_lock); |
269de30f BS |
6560 | return count; |
6561 | } | |
6562 | ||
98d5c7c5 JL |
6563 | static u64 |
6564 | nfsd_print_client_delegations(struct nfs4_client *clp) | |
269de30f | 6565 | { |
98d5c7c5 | 6566 | u64 count = nfsd_find_all_delegations(clp, 0, NULL); |
269de30f | 6567 | |
98d5c7c5 JL |
6568 | nfsd_print_count(clp, count, "delegations"); |
6569 | return count; | |
6570 | } | |
6571 | ||
6572 | u64 | |
285abdee | 6573 | nfsd_inject_print_delegations(void) |
98d5c7c5 JL |
6574 | { |
6575 | struct nfs4_client *clp; | |
6576 | u64 count = 0; | |
6577 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6578 | nfsd_net_id); | |
6579 | ||
6580 | if (!nfsd_netns_ready(nn)) | |
6581 | return 0; | |
269de30f | 6582 | |
98d5c7c5 JL |
6583 | spin_lock(&nn->client_lock); |
6584 | list_for_each_entry(clp, &nn->client_lru, cl_lru) | |
6585 | count += nfsd_print_client_delegations(clp); | |
6586 | spin_unlock(&nn->client_lock); | |
6587 | ||
6588 | return count; | |
6589 | } | |
6590 | ||
6591 | static void | |
6592 | nfsd_forget_delegations(struct list_head *reaplist) | |
6593 | { | |
6594 | struct nfs4_client *clp; | |
6595 | struct nfs4_delegation *dp, *next; | |
6596 | ||
6597 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { | |
2d4a532d | 6598 | list_del_init(&dp->dl_recall_lru); |
98d5c7c5 | 6599 | clp = dp->dl_stid.sc_client; |
3bd64a5b | 6600 | revoke_delegation(dp); |
98d5c7c5 | 6601 | put_client(clp); |
2d4a532d | 6602 | } |
98d5c7c5 JL |
6603 | } |
6604 | ||
6605 | u64 | |
285abdee JL |
6606 | nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, |
6607 | size_t addr_size) | |
98d5c7c5 JL |
6608 | { |
6609 | u64 count = 0; | |
6610 | struct nfs4_client *clp; | |
6611 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6612 | nfsd_net_id); | |
6613 | LIST_HEAD(reaplist); | |
6614 | ||
6615 | if (!nfsd_netns_ready(nn)) | |
6616 | return count; | |
6617 | ||
6618 | spin_lock(&nn->client_lock); | |
6619 | clp = nfsd_find_client(addr, addr_size); | |
6620 | if (clp) | |
6621 | count = nfsd_find_all_delegations(clp, 0, &reaplist); | |
6622 | spin_unlock(&nn->client_lock); | |
6623 | ||
6624 | nfsd_forget_delegations(&reaplist); | |
6625 | return count; | |
6626 | } | |
269de30f | 6627 | |
98d5c7c5 | 6628 | u64 |
285abdee | 6629 | nfsd_inject_forget_delegations(u64 max) |
98d5c7c5 JL |
6630 | { |
6631 | u64 count = 0; | |
6632 | struct nfs4_client *clp; | |
6633 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6634 | nfsd_net_id); | |
6635 | LIST_HEAD(reaplist); | |
6636 | ||
6637 | if (!nfsd_netns_ready(nn)) | |
6638 | return count; | |
6639 | ||
6640 | spin_lock(&nn->client_lock); | |
6641 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { | |
6642 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); | |
6643 | if (max != 0 && count >= max) | |
6644 | break; | |
6645 | } | |
6646 | spin_unlock(&nn->client_lock); | |
6647 | nfsd_forget_delegations(&reaplist); | |
269de30f BS |
6648 | return count; |
6649 | } | |
6650 | ||
98d5c7c5 JL |
6651 | static void |
6652 | nfsd_recall_delegations(struct list_head *reaplist) | |
269de30f | 6653 | { |
98d5c7c5 JL |
6654 | struct nfs4_client *clp; |
6655 | struct nfs4_delegation *dp, *next; | |
269de30f | 6656 | |
98d5c7c5 | 6657 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
dff1399f | 6658 | list_del_init(&dp->dl_recall_lru); |
98d5c7c5 JL |
6659 | clp = dp->dl_stid.sc_client; |
6660 | /* | |
6661 | * We skipped all entries that had a zero dl_time before, | |
6662 | * so we can now reset the dl_time back to 0. If a delegation | |
6663 | * break comes in now, then it won't make any difference since | |
6664 | * we're recalling it either way. | |
6665 | */ | |
6666 | spin_lock(&state_lock); | |
dff1399f | 6667 | dp->dl_time = 0; |
98d5c7c5 | 6668 | spin_unlock(&state_lock); |
269de30f | 6669 | nfsd_break_one_deleg(dp); |
98d5c7c5 | 6670 | put_client(clp); |
dff1399f | 6671 | } |
98d5c7c5 | 6672 | } |
269de30f | 6673 | |
98d5c7c5 | 6674 | u64 |
285abdee | 6675 | nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, |
98d5c7c5 JL |
6676 | size_t addr_size) |
6677 | { | |
6678 | u64 count = 0; | |
6679 | struct nfs4_client *clp; | |
6680 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6681 | nfsd_net_id); | |
6682 | LIST_HEAD(reaplist); | |
6683 | ||
6684 | if (!nfsd_netns_ready(nn)) | |
6685 | return count; | |
6686 | ||
6687 | spin_lock(&nn->client_lock); | |
6688 | clp = nfsd_find_client(addr, addr_size); | |
6689 | if (clp) | |
6690 | count = nfsd_find_all_delegations(clp, 0, &reaplist); | |
6691 | spin_unlock(&nn->client_lock); | |
6692 | ||
6693 | nfsd_recall_delegations(&reaplist); | |
269de30f BS |
6694 | return count; |
6695 | } | |
6696 | ||
98d5c7c5 | 6697 | u64 |
285abdee | 6698 | nfsd_inject_recall_delegations(u64 max) |
184c1847 BS |
6699 | { |
6700 | u64 count = 0; | |
98d5c7c5 JL |
6701 | struct nfs4_client *clp, *next; |
6702 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, | |
6703 | nfsd_net_id); | |
6704 | LIST_HEAD(reaplist); | |
184c1847 | 6705 | |
98d5c7c5 JL |
6706 | if (!nfsd_netns_ready(nn)) |
6707 | return count; | |
184c1847 | 6708 | |
98d5c7c5 JL |
6709 | spin_lock(&nn->client_lock); |
6710 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { | |
6711 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); | |
6712 | if (max != 0 && ++count >= max) | |
6713 | break; | |
6714 | } | |
6715 | spin_unlock(&nn->client_lock); | |
6716 | nfsd_recall_delegations(&reaplist); | |
184c1847 BS |
6717 | return count; |
6718 | } | |
65178db4 BS |
6719 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
6720 | ||
c2f1a551 MS |
6721 | /* |
6722 | * Since the lifetime of a delegation isn't limited to that of an open, a | |
6723 | * client may quite reasonably hang on to a delegation as long as it has | |
6724 | * the inode cached. This becomes an obvious problem the first time a | |
6725 | * client's inode cache approaches the size of the server's total memory. | |
6726 | * | |
6727 | * For now we avoid this problem by imposing a hard limit on the number | |
6728 | * of delegations, which varies according to the server's memory size. | |
6729 | */ | |
6730 | static void | |
6731 | set_max_delegations(void) | |
6732 | { | |
6733 | /* | |
6734 | * Allow at most 4 delegations per megabyte of RAM. Quick | |
6735 | * estimates suggest that in the worst case (where every delegation | |
6736 | * is for a different inode), a delegation could take about 1.5K, | |
6737 | * giving a worst case usage of about 6% of memory. | |
6738 | */ | |
6739 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); | |
6740 | } | |
6741 | ||
d85ed443 | 6742 | static int nfs4_state_create_net(struct net *net) |
8daae4dc SK |
6743 | { |
6744 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
6745 | int i; | |
6746 | ||
6747 | nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) * | |
6748 | CLIENT_HASH_SIZE, GFP_KERNEL); | |
6749 | if (!nn->conf_id_hashtbl) | |
382a62e7 | 6750 | goto err; |
0a7ec377 SK |
6751 | nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) * |
6752 | CLIENT_HASH_SIZE, GFP_KERNEL); | |
6753 | if (!nn->unconf_id_hashtbl) | |
6754 | goto err_unconf_id; | |
1872de0e SK |
6755 | nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) * |
6756 | SESSION_HASH_SIZE, GFP_KERNEL); | |
6757 | if (!nn->sessionid_hashtbl) | |
6758 | goto err_sessionid; | |
8daae4dc | 6759 | |
382a62e7 | 6760 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
8daae4dc | 6761 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
0a7ec377 | 6762 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
382a62e7 | 6763 | } |
1872de0e SK |
6764 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
6765 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); | |
382a62e7 | 6766 | nn->conf_name_tree = RB_ROOT; |
a99454aa | 6767 | nn->unconf_name_tree = RB_ROOT; |
5ed58bb2 | 6768 | INIT_LIST_HEAD(&nn->client_lru); |
73758fed | 6769 | INIT_LIST_HEAD(&nn->close_lru); |
e8c69d17 | 6770 | INIT_LIST_HEAD(&nn->del_recall_lru); |
c9a49628 | 6771 | spin_lock_init(&nn->client_lock); |
8daae4dc | 6772 | |
09121281 | 6773 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
d85ed443 | 6774 | get_net(net); |
09121281 | 6775 | |
8daae4dc | 6776 | return 0; |
382a62e7 | 6777 | |
1872de0e | 6778 | err_sessionid: |
9b531137 | 6779 | kfree(nn->unconf_id_hashtbl); |
0a7ec377 SK |
6780 | err_unconf_id: |
6781 | kfree(nn->conf_id_hashtbl); | |
382a62e7 SK |
6782 | err: |
6783 | return -ENOMEM; | |
8daae4dc SK |
6784 | } |
6785 | ||
6786 | static void | |
4dce0ac9 | 6787 | nfs4_state_destroy_net(struct net *net) |
8daae4dc SK |
6788 | { |
6789 | int i; | |
6790 | struct nfs4_client *clp = NULL; | |
6791 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); | |
6792 | ||
6793 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { | |
6794 | while (!list_empty(&nn->conf_id_hashtbl[i])) { | |
6795 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
6796 | destroy_client(clp); | |
6797 | } | |
6798 | } | |
a99454aa | 6799 | |
2b905635 KM |
6800 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
6801 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { | |
6802 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
6803 | destroy_client(clp); | |
6804 | } | |
a99454aa SK |
6805 | } |
6806 | ||
1872de0e | 6807 | kfree(nn->sessionid_hashtbl); |
0a7ec377 | 6808 | kfree(nn->unconf_id_hashtbl); |
8daae4dc | 6809 | kfree(nn->conf_id_hashtbl); |
4dce0ac9 | 6810 | put_net(net); |
8daae4dc SK |
6811 | } |
6812 | ||
f252bc68 | 6813 | int |
d85ed443 | 6814 | nfs4_state_start_net(struct net *net) |
ac4d8ff2 | 6815 | { |
5e1533c7 | 6816 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
b5a1a81e BF |
6817 | int ret; |
6818 | ||
d85ed443 | 6819 | ret = nfs4_state_create_net(net); |
8daae4dc SK |
6820 | if (ret) |
6821 | return ret; | |
2c142baa | 6822 | nn->boot_time = get_seconds(); |
a51c84ed | 6823 | nn->grace_ended = false; |
c87fb4a3 | 6824 | nn->nfsd4_manager.block_opens = true; |
d4318acd JL |
6825 | locks_start_grace(net, &nn->nfsd4_manager); |
6826 | nfsd4_client_tracking_init(net); | |
d85ed443 | 6827 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n", |
5284b44e SK |
6828 | nn->nfsd4_grace, net); |
6829 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); | |
d85ed443 SK |
6830 | return 0; |
6831 | } | |
6832 | ||
6833 | /* initialization to perform when the nfsd service is started: */ | |
6834 | ||
6835 | int | |
6836 | nfs4_state_start(void) | |
6837 | { | |
6838 | int ret; | |
6839 | ||
b5a1a81e | 6840 | ret = set_callback_cred(); |
d85ed443 SK |
6841 | if (ret) |
6842 | return -ENOMEM; | |
51a54568 | 6843 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
a6d6b781 JL |
6844 | if (laundry_wq == NULL) { |
6845 | ret = -ENOMEM; | |
6846 | goto out_recovery; | |
6847 | } | |
b5a1a81e BF |
6848 | ret = nfsd4_create_callback_queue(); |
6849 | if (ret) | |
6850 | goto out_free_laundry; | |
09121281 | 6851 | |
c2f1a551 | 6852 | set_max_delegations(); |
d85ed443 | 6853 | |
b5a1a81e | 6854 | return 0; |
d85ed443 | 6855 | |
b5a1a81e BF |
6856 | out_free_laundry: |
6857 | destroy_workqueue(laundry_wq); | |
a6d6b781 | 6858 | out_recovery: |
b5a1a81e | 6859 | return ret; |
1da177e4 LT |
6860 | } |
6861 | ||
f252bc68 | 6862 | void |
4dce0ac9 | 6863 | nfs4_state_shutdown_net(struct net *net) |
1da177e4 | 6864 | { |
1da177e4 | 6865 | struct nfs4_delegation *dp = NULL; |
1da177e4 | 6866 | struct list_head *pos, *next, reaplist; |
4dce0ac9 | 6867 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
1da177e4 | 6868 | |
4dce0ac9 SK |
6869 | cancel_delayed_work_sync(&nn->laundromat_work); |
6870 | locks_end_grace(&nn->nfsd4_manager); | |
ac55fdc4 | 6871 | |
1da177e4 | 6872 | INIT_LIST_HEAD(&reaplist); |
cdc97505 | 6873 | spin_lock(&state_lock); |
e8c69d17 | 6874 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
1da177e4 | 6875 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
3fcbbd24 | 6876 | WARN_ON(!unhash_delegation_locked(dp)); |
42690676 | 6877 | list_add(&dp->dl_recall_lru, &reaplist); |
1da177e4 | 6878 | } |
cdc97505 | 6879 | spin_unlock(&state_lock); |
1da177e4 LT |
6880 | list_for_each_safe(pos, next, &reaplist) { |
6881 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
42690676 | 6882 | list_del_init(&dp->dl_recall_lru); |
8287f009 | 6883 | put_clnt_odstate(dp->dl_clnt_odstate); |
afbda402 | 6884 | nfs4_put_deleg_lease(dp->dl_stid.sc_file); |
6011695d | 6885 | nfs4_put_stid(&dp->dl_stid); |
1da177e4 LT |
6886 | } |
6887 | ||
3320fef1 | 6888 | nfsd4_client_tracking_exit(net); |
4dce0ac9 | 6889 | nfs4_state_destroy_net(net); |
1da177e4 LT |
6890 | } |
6891 | ||
6892 | void | |
6893 | nfs4_state_shutdown(void) | |
6894 | { | |
5e8d5c29 | 6895 | destroy_workqueue(laundry_wq); |
c3935e30 | 6896 | nfsd4_destroy_callback_queue(); |
1da177e4 | 6897 | } |
8b70484c TM |
6898 | |
6899 | static void | |
6900 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) | |
6901 | { | |
37c593c5 TM |
6902 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
6903 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); | |
8b70484c TM |
6904 | } |
6905 | ||
6906 | static void | |
6907 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) | |
6908 | { | |
37c593c5 TM |
6909 | if (cstate->minorversion) { |
6910 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); | |
6911 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); | |
6912 | } | |
6913 | } | |
6914 | ||
6915 | void | |
6916 | clear_current_stateid(struct nfsd4_compound_state *cstate) | |
6917 | { | |
6918 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); | |
8b70484c TM |
6919 | } |
6920 | ||
62cd4a59 TM |
6921 | /* |
6922 | * functions to set current state id | |
6923 | */ | |
9428fe1a TM |
6924 | void |
6925 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) | |
6926 | { | |
6927 | put_stateid(cstate, &odp->od_stateid); | |
6928 | } | |
6929 | ||
8b70484c TM |
6930 | void |
6931 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open) | |
6932 | { | |
6933 | put_stateid(cstate, &open->op_stateid); | |
6934 | } | |
6935 | ||
62cd4a59 TM |
6936 | void |
6937 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) | |
6938 | { | |
6939 | put_stateid(cstate, &close->cl_stateid); | |
6940 | } | |
6941 | ||
6942 | void | |
6943 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock) | |
6944 | { | |
6945 | put_stateid(cstate, &lock->lk_resp_stateid); | |
6946 | } | |
6947 | ||
6948 | /* | |
6949 | * functions to consume current state id | |
6950 | */ | |
1e97b519 | 6951 | |
9428fe1a TM |
6952 | void |
6953 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp) | |
6954 | { | |
6955 | get_stateid(cstate, &odp->od_stateid); | |
6956 | } | |
6957 | ||
6958 | void | |
6959 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp) | |
6960 | { | |
6961 | get_stateid(cstate, &drp->dr_stateid); | |
6962 | } | |
6963 | ||
1e97b519 TM |
6964 | void |
6965 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp) | |
6966 | { | |
6967 | get_stateid(cstate, &fsp->fr_stateid); | |
6968 | } | |
6969 | ||
6970 | void | |
6971 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr) | |
6972 | { | |
6973 | get_stateid(cstate, &setattr->sa_stateid); | |
6974 | } | |
6975 | ||
8b70484c TM |
6976 | void |
6977 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close) | |
6978 | { | |
6979 | get_stateid(cstate, &close->cl_stateid); | |
6980 | } | |
6981 | ||
6982 | void | |
62cd4a59 | 6983 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku) |
8b70484c | 6984 | { |
62cd4a59 | 6985 | get_stateid(cstate, &locku->lu_stateid); |
8b70484c | 6986 | } |
30813e27 TM |
6987 | |
6988 | void | |
6989 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read) | |
6990 | { | |
6991 | get_stateid(cstate, &read->rd_stateid); | |
6992 | } | |
6993 | ||
6994 | void | |
6995 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write) | |
6996 | { | |
6997 | get_stateid(cstate, &write->wr_stateid); | |
6998 | } |