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