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