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