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