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