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