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