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