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