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