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