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