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