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