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