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