]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/nfs/nfs4state.c | |
3 | * | |
4 | * Client-side XDR for NFSv4. | |
5 | * | |
6 | * Copyright (c) 2002 The Regents of the University of Michigan. | |
7 | * All rights reserved. | |
8 | * | |
9 | * Kendrick Smith <kmsmith@umich.edu> | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or without | |
12 | * modification, are permitted provided that the following conditions | |
13 | * are met: | |
14 | * | |
15 | * 1. Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * 3. Neither the name of the University nor the names of its | |
21 | * contributors may be used to endorse or promote products derived | |
22 | * from this software without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
27 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
31 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
35 | * | |
36 | * Implementation of the NFSv4 state model. For the time being, | |
37 | * this is minimal, but will be made much more complex in a | |
38 | * subsequent patch. | |
39 | */ | |
40 | ||
41 | #include <linux/config.h> | |
42 | #include <linux/slab.h> | |
43 | #include <linux/smp_lock.h> | |
44 | #include <linux/nfs_fs.h> | |
45 | #include <linux/nfs_idmap.h> | |
46 | #include <linux/workqueue.h> | |
47 | #include <linux/bitops.h> | |
48 | ||
4ce79717 | 49 | #include "nfs4_fs.h" |
1da177e4 LT |
50 | #include "callback.h" |
51 | #include "delegation.h" | |
52 | ||
53 | #define OPENOWNER_POOL_SIZE 8 | |
54 | ||
4ce79717 | 55 | const nfs4_stateid zero_stateid; |
1da177e4 | 56 | |
4ce79717 | 57 | static DEFINE_SPINLOCK(state_spinlock); |
1da177e4 LT |
58 | static LIST_HEAD(nfs4_clientid_list); |
59 | ||
60 | static void nfs4_recover_state(void *); | |
1da177e4 LT |
61 | |
62 | void | |
63 | init_nfsv4_state(struct nfs_server *server) | |
64 | { | |
65 | server->nfs4_state = NULL; | |
66 | INIT_LIST_HEAD(&server->nfs4_siblings); | |
67 | } | |
68 | ||
69 | void | |
70 | destroy_nfsv4_state(struct nfs_server *server) | |
71 | { | |
72 | if (server->mnt_path) { | |
73 | kfree(server->mnt_path); | |
74 | server->mnt_path = NULL; | |
75 | } | |
76 | if (server->nfs4_state) { | |
77 | nfs4_put_client(server->nfs4_state); | |
78 | server->nfs4_state = NULL; | |
79 | } | |
80 | } | |
81 | ||
82 | /* | |
83 | * nfs4_get_client(): returns an empty client structure | |
84 | * nfs4_put_client(): drops reference to client structure | |
85 | * | |
86 | * Since these are allocated/deallocated very rarely, we don't | |
87 | * bother putting them in a slab cache... | |
88 | */ | |
89 | static struct nfs4_client * | |
90 | nfs4_alloc_client(struct in_addr *addr) | |
91 | { | |
92 | struct nfs4_client *clp; | |
93 | ||
94 | if (nfs_callback_up() < 0) | |
95 | return NULL; | |
96 | if ((clp = kmalloc(sizeof(*clp), GFP_KERNEL)) == NULL) { | |
97 | nfs_callback_down(); | |
98 | return NULL; | |
99 | } | |
100 | memset(clp, 0, sizeof(*clp)); | |
101 | memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr)); | |
102 | init_rwsem(&clp->cl_sem); | |
103 | INIT_LIST_HEAD(&clp->cl_delegations); | |
104 | INIT_LIST_HEAD(&clp->cl_state_owners); | |
105 | INIT_LIST_HEAD(&clp->cl_unused); | |
106 | spin_lock_init(&clp->cl_lock); | |
107 | atomic_set(&clp->cl_count, 1); | |
108 | INIT_WORK(&clp->cl_recoverd, nfs4_recover_state, clp); | |
109 | INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp); | |
110 | INIT_LIST_HEAD(&clp->cl_superblocks); | |
111 | init_waitqueue_head(&clp->cl_waitq); | |
112 | rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS4 client"); | |
6a19275a | 113 | clp->cl_rpcclient = ERR_PTR(-EINVAL); |
1da177e4 LT |
114 | clp->cl_boot_time = CURRENT_TIME; |
115 | clp->cl_state = 1 << NFS4CLNT_OK; | |
116 | return clp; | |
117 | } | |
118 | ||
119 | static void | |
120 | nfs4_free_client(struct nfs4_client *clp) | |
121 | { | |
122 | struct nfs4_state_owner *sp; | |
123 | ||
124 | while (!list_empty(&clp->cl_unused)) { | |
125 | sp = list_entry(clp->cl_unused.next, | |
126 | struct nfs4_state_owner, | |
127 | so_list); | |
128 | list_del(&sp->so_list); | |
129 | kfree(sp); | |
130 | } | |
131 | BUG_ON(!list_empty(&clp->cl_state_owners)); | |
132 | if (clp->cl_cred) | |
133 | put_rpccred(clp->cl_cred); | |
134 | nfs_idmap_delete(clp); | |
6a19275a | 135 | if (!IS_ERR(clp->cl_rpcclient)) |
1da177e4 LT |
136 | rpc_shutdown_client(clp->cl_rpcclient); |
137 | kfree(clp); | |
138 | nfs_callback_down(); | |
139 | } | |
140 | ||
141 | static struct nfs4_client *__nfs4_find_client(struct in_addr *addr) | |
142 | { | |
143 | struct nfs4_client *clp; | |
144 | list_for_each_entry(clp, &nfs4_clientid_list, cl_servers) { | |
145 | if (memcmp(&clp->cl_addr, addr, sizeof(clp->cl_addr)) == 0) { | |
146 | atomic_inc(&clp->cl_count); | |
147 | return clp; | |
148 | } | |
149 | } | |
150 | return NULL; | |
151 | } | |
152 | ||
153 | struct nfs4_client *nfs4_find_client(struct in_addr *addr) | |
154 | { | |
155 | struct nfs4_client *clp; | |
156 | spin_lock(&state_spinlock); | |
157 | clp = __nfs4_find_client(addr); | |
158 | spin_unlock(&state_spinlock); | |
159 | return clp; | |
160 | } | |
161 | ||
162 | struct nfs4_client * | |
163 | nfs4_get_client(struct in_addr *addr) | |
164 | { | |
165 | struct nfs4_client *clp, *new = NULL; | |
166 | ||
167 | spin_lock(&state_spinlock); | |
168 | for (;;) { | |
169 | clp = __nfs4_find_client(addr); | |
170 | if (clp != NULL) | |
171 | break; | |
172 | clp = new; | |
173 | if (clp != NULL) { | |
174 | list_add(&clp->cl_servers, &nfs4_clientid_list); | |
175 | new = NULL; | |
176 | break; | |
177 | } | |
178 | spin_unlock(&state_spinlock); | |
179 | new = nfs4_alloc_client(addr); | |
180 | spin_lock(&state_spinlock); | |
181 | if (new == NULL) | |
182 | break; | |
183 | } | |
184 | spin_unlock(&state_spinlock); | |
185 | if (new) | |
186 | nfs4_free_client(new); | |
187 | return clp; | |
188 | } | |
189 | ||
190 | void | |
191 | nfs4_put_client(struct nfs4_client *clp) | |
192 | { | |
193 | if (!atomic_dec_and_lock(&clp->cl_count, &state_spinlock)) | |
194 | return; | |
195 | list_del(&clp->cl_servers); | |
196 | spin_unlock(&state_spinlock); | |
197 | BUG_ON(!list_empty(&clp->cl_superblocks)); | |
198 | wake_up_all(&clp->cl_waitq); | |
199 | rpc_wake_up(&clp->cl_rpcwaitq); | |
200 | nfs4_kill_renewd(clp); | |
201 | nfs4_free_client(clp); | |
202 | } | |
203 | ||
204 | static int __nfs4_init_client(struct nfs4_client *clp) | |
205 | { | |
206 | int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, nfs_callback_tcpport); | |
207 | if (status == 0) | |
208 | status = nfs4_proc_setclientid_confirm(clp); | |
209 | if (status == 0) | |
210 | nfs4_schedule_state_renewal(clp); | |
211 | return status; | |
212 | } | |
213 | ||
214 | int nfs4_init_client(struct nfs4_client *clp) | |
215 | { | |
216 | return nfs4_map_errors(__nfs4_init_client(clp)); | |
217 | } | |
218 | ||
219 | u32 | |
220 | nfs4_alloc_lockowner_id(struct nfs4_client *clp) | |
221 | { | |
222 | return clp->cl_lockowner_id ++; | |
223 | } | |
224 | ||
225 | static struct nfs4_state_owner * | |
226 | nfs4_client_grab_unused(struct nfs4_client *clp, struct rpc_cred *cred) | |
227 | { | |
228 | struct nfs4_state_owner *sp = NULL; | |
229 | ||
230 | if (!list_empty(&clp->cl_unused)) { | |
231 | sp = list_entry(clp->cl_unused.next, struct nfs4_state_owner, so_list); | |
232 | atomic_inc(&sp->so_count); | |
233 | sp->so_cred = cred; | |
234 | list_move(&sp->so_list, &clp->cl_state_owners); | |
235 | clp->cl_nunused--; | |
236 | } | |
237 | return sp; | |
238 | } | |
239 | ||
240 | static struct nfs4_state_owner * | |
241 | nfs4_find_state_owner(struct nfs4_client *clp, struct rpc_cred *cred) | |
242 | { | |
243 | struct nfs4_state_owner *sp, *res = NULL; | |
244 | ||
245 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { | |
246 | if (sp->so_cred != cred) | |
247 | continue; | |
248 | atomic_inc(&sp->so_count); | |
249 | /* Move to the head of the list */ | |
250 | list_move(&sp->so_list, &clp->cl_state_owners); | |
251 | res = sp; | |
252 | break; | |
253 | } | |
254 | return res; | |
255 | } | |
256 | ||
257 | /* | |
258 | * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to | |
259 | * create a new state_owner. | |
260 | * | |
261 | */ | |
262 | static struct nfs4_state_owner * | |
263 | nfs4_alloc_state_owner(void) | |
264 | { | |
265 | struct nfs4_state_owner *sp; | |
266 | ||
cee54fc9 | 267 | sp = kzalloc(sizeof(*sp),GFP_KERNEL); |
1da177e4 LT |
268 | if (!sp) |
269 | return NULL; | |
1da177e4 LT |
270 | INIT_LIST_HEAD(&sp->so_states); |
271 | INIT_LIST_HEAD(&sp->so_delegations); | |
cee54fc9 TM |
272 | rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue"); |
273 | sp->so_seqid.sequence = &sp->so_sequence; | |
274 | spin_lock_init(&sp->so_sequence.lock); | |
275 | INIT_LIST_HEAD(&sp->so_sequence.list); | |
1da177e4 LT |
276 | atomic_set(&sp->so_count, 1); |
277 | return sp; | |
278 | } | |
279 | ||
280 | void | |
281 | nfs4_drop_state_owner(struct nfs4_state_owner *sp) | |
282 | { | |
283 | struct nfs4_client *clp = sp->so_client; | |
284 | spin_lock(&clp->cl_lock); | |
285 | list_del_init(&sp->so_list); | |
286 | spin_unlock(&clp->cl_lock); | |
287 | } | |
288 | ||
289 | /* | |
290 | * Note: must be called with clp->cl_sem held in order to prevent races | |
291 | * with reboot recovery! | |
292 | */ | |
293 | struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred) | |
294 | { | |
295 | struct nfs4_client *clp = server->nfs4_state; | |
296 | struct nfs4_state_owner *sp, *new; | |
297 | ||
298 | get_rpccred(cred); | |
299 | new = nfs4_alloc_state_owner(); | |
300 | spin_lock(&clp->cl_lock); | |
301 | sp = nfs4_find_state_owner(clp, cred); | |
302 | if (sp == NULL) | |
303 | sp = nfs4_client_grab_unused(clp, cred); | |
304 | if (sp == NULL && new != NULL) { | |
305 | list_add(&new->so_list, &clp->cl_state_owners); | |
306 | new->so_client = clp; | |
307 | new->so_id = nfs4_alloc_lockowner_id(clp); | |
308 | new->so_cred = cred; | |
309 | sp = new; | |
310 | new = NULL; | |
311 | } | |
312 | spin_unlock(&clp->cl_lock); | |
313 | if (new) | |
314 | kfree(new); | |
315 | if (sp != NULL) | |
316 | return sp; | |
317 | put_rpccred(cred); | |
318 | return NULL; | |
319 | } | |
320 | ||
321 | /* | |
322 | * Must be called with clp->cl_sem held in order to avoid races | |
323 | * with state recovery... | |
324 | */ | |
325 | void nfs4_put_state_owner(struct nfs4_state_owner *sp) | |
326 | { | |
327 | struct nfs4_client *clp = sp->so_client; | |
328 | struct rpc_cred *cred = sp->so_cred; | |
329 | ||
330 | if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock)) | |
331 | return; | |
332 | if (clp->cl_nunused >= OPENOWNER_POOL_SIZE) | |
333 | goto out_free; | |
334 | if (list_empty(&sp->so_list)) | |
335 | goto out_free; | |
336 | list_move(&sp->so_list, &clp->cl_unused); | |
337 | clp->cl_nunused++; | |
338 | spin_unlock(&clp->cl_lock); | |
339 | put_rpccred(cred); | |
340 | cred = NULL; | |
341 | return; | |
342 | out_free: | |
343 | list_del(&sp->so_list); | |
344 | spin_unlock(&clp->cl_lock); | |
345 | put_rpccred(cred); | |
346 | kfree(sp); | |
347 | } | |
348 | ||
349 | static struct nfs4_state * | |
350 | nfs4_alloc_open_state(void) | |
351 | { | |
352 | struct nfs4_state *state; | |
353 | ||
354 | state = kmalloc(sizeof(*state), GFP_KERNEL); | |
355 | if (!state) | |
356 | return NULL; | |
357 | state->state = 0; | |
358 | state->nreaders = 0; | |
359 | state->nwriters = 0; | |
360 | state->flags = 0; | |
361 | memset(state->stateid.data, 0, sizeof(state->stateid.data)); | |
362 | atomic_set(&state->count, 1); | |
363 | INIT_LIST_HEAD(&state->lock_states); | |
8d0a8a9d | 364 | spin_lock_init(&state->state_lock); |
1da177e4 LT |
365 | return state; |
366 | } | |
367 | ||
368 | static struct nfs4_state * | |
369 | __nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode) | |
370 | { | |
371 | struct nfs_inode *nfsi = NFS_I(inode); | |
372 | struct nfs4_state *state; | |
373 | ||
374 | mode &= (FMODE_READ|FMODE_WRITE); | |
375 | list_for_each_entry(state, &nfsi->open_states, inode_states) { | |
376 | if (state->owner->so_cred != cred) | |
377 | continue; | |
378 | if ((mode & FMODE_READ) != 0 && state->nreaders == 0) | |
379 | continue; | |
380 | if ((mode & FMODE_WRITE) != 0 && state->nwriters == 0) | |
381 | continue; | |
382 | if ((state->state & mode) != mode) | |
383 | continue; | |
384 | atomic_inc(&state->count); | |
385 | if (mode & FMODE_READ) | |
386 | state->nreaders++; | |
387 | if (mode & FMODE_WRITE) | |
388 | state->nwriters++; | |
389 | return state; | |
390 | } | |
391 | return NULL; | |
392 | } | |
393 | ||
394 | static struct nfs4_state * | |
395 | __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner) | |
396 | { | |
397 | struct nfs_inode *nfsi = NFS_I(inode); | |
398 | struct nfs4_state *state; | |
399 | ||
400 | list_for_each_entry(state, &nfsi->open_states, inode_states) { | |
401 | /* Is this in the process of being freed? */ | |
402 | if (state->nreaders == 0 && state->nwriters == 0) | |
403 | continue; | |
404 | if (state->owner == owner) { | |
405 | atomic_inc(&state->count); | |
406 | return state; | |
407 | } | |
408 | } | |
409 | return NULL; | |
410 | } | |
411 | ||
412 | struct nfs4_state * | |
413 | nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode) | |
414 | { | |
415 | struct nfs4_state *state; | |
416 | ||
417 | spin_lock(&inode->i_lock); | |
418 | state = __nfs4_find_state(inode, cred, mode); | |
419 | spin_unlock(&inode->i_lock); | |
420 | return state; | |
421 | } | |
422 | ||
423 | static void | |
424 | nfs4_free_open_state(struct nfs4_state *state) | |
425 | { | |
426 | kfree(state); | |
427 | } | |
428 | ||
429 | struct nfs4_state * | |
430 | nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner) | |
431 | { | |
432 | struct nfs4_state *state, *new; | |
433 | struct nfs_inode *nfsi = NFS_I(inode); | |
434 | ||
435 | spin_lock(&inode->i_lock); | |
436 | state = __nfs4_find_state_byowner(inode, owner); | |
437 | spin_unlock(&inode->i_lock); | |
438 | if (state) | |
439 | goto out; | |
440 | new = nfs4_alloc_open_state(); | |
441 | spin_lock(&inode->i_lock); | |
442 | state = __nfs4_find_state_byowner(inode, owner); | |
443 | if (state == NULL && new != NULL) { | |
444 | state = new; | |
1da177e4 LT |
445 | /* Note: The reclaim code dictates that we add stateless |
446 | * and read-only stateids to the end of the list */ | |
447 | list_add_tail(&state->open_states, &owner->so_states); | |
448 | state->owner = owner; | |
449 | atomic_inc(&owner->so_count); | |
450 | list_add(&state->inode_states, &nfsi->open_states); | |
451 | state->inode = igrab(inode); | |
452 | spin_unlock(&inode->i_lock); | |
453 | } else { | |
454 | spin_unlock(&inode->i_lock); | |
455 | if (new) | |
456 | nfs4_free_open_state(new); | |
457 | } | |
458 | out: | |
459 | return state; | |
460 | } | |
461 | ||
462 | /* | |
463 | * Beware! Caller must be holding exactly one | |
e6dfa553 | 464 | * reference to clp->cl_sem! |
1da177e4 LT |
465 | */ |
466 | void nfs4_put_open_state(struct nfs4_state *state) | |
467 | { | |
468 | struct inode *inode = state->inode; | |
469 | struct nfs4_state_owner *owner = state->owner; | |
470 | ||
471 | if (!atomic_dec_and_lock(&state->count, &inode->i_lock)) | |
472 | return; | |
473 | if (!list_empty(&state->inode_states)) | |
474 | list_del(&state->inode_states); | |
475 | spin_unlock(&inode->i_lock); | |
476 | list_del(&state->open_states); | |
477 | iput(inode); | |
478 | BUG_ON (state->state != 0); | |
479 | nfs4_free_open_state(state); | |
480 | nfs4_put_state_owner(owner); | |
481 | } | |
482 | ||
483 | /* | |
83c9d41e | 484 | * Close the current file. |
1da177e4 LT |
485 | */ |
486 | void nfs4_close_state(struct nfs4_state *state, mode_t mode) | |
487 | { | |
488 | struct inode *inode = state->inode; | |
489 | struct nfs4_state_owner *owner = state->owner; | |
1da177e4 LT |
490 | int newstate; |
491 | ||
492 | atomic_inc(&owner->so_count); | |
1da177e4 LT |
493 | /* Protect against nfs4_find_state() */ |
494 | spin_lock(&inode->i_lock); | |
495 | if (mode & FMODE_READ) | |
496 | state->nreaders--; | |
497 | if (mode & FMODE_WRITE) | |
498 | state->nwriters--; | |
499 | if (state->nwriters == 0) { | |
500 | if (state->nreaders == 0) | |
501 | list_del_init(&state->inode_states); | |
502 | /* See reclaim code */ | |
503 | list_move_tail(&state->open_states, &owner->so_states); | |
504 | } | |
505 | spin_unlock(&inode->i_lock); | |
506 | newstate = 0; | |
507 | if (state->state != 0) { | |
508 | if (state->nreaders) | |
509 | newstate |= FMODE_READ; | |
510 | if (state->nwriters) | |
511 | newstate |= FMODE_WRITE; | |
512 | if (state->state == newstate) | |
513 | goto out; | |
9512135d TM |
514 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
515 | state->state = newstate; | |
516 | goto out; | |
517 | } | |
518 | if (nfs4_do_close(inode, state, newstate) == 0) | |
1da177e4 LT |
519 | return; |
520 | } | |
521 | out: | |
522 | nfs4_put_open_state(state); | |
1da177e4 | 523 | nfs4_put_state_owner(owner); |
1da177e4 LT |
524 | } |
525 | ||
526 | /* | |
527 | * Search the state->lock_states for an existing lock_owner | |
528 | * that is compatible with current->files | |
529 | */ | |
530 | static struct nfs4_lock_state * | |
531 | __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner) | |
532 | { | |
533 | struct nfs4_lock_state *pos; | |
534 | list_for_each_entry(pos, &state->lock_states, ls_locks) { | |
535 | if (pos->ls_owner != fl_owner) | |
536 | continue; | |
537 | atomic_inc(&pos->ls_count); | |
538 | return pos; | |
539 | } | |
540 | return NULL; | |
541 | } | |
542 | ||
1da177e4 LT |
543 | /* |
544 | * Return a compatible lock_state. If no initialized lock_state structure | |
545 | * exists, return an uninitialized one. | |
546 | * | |
1da177e4 LT |
547 | */ |
548 | static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner) | |
549 | { | |
550 | struct nfs4_lock_state *lsp; | |
551 | struct nfs4_client *clp = state->owner->so_client; | |
552 | ||
cee54fc9 | 553 | lsp = kzalloc(sizeof(*lsp), GFP_KERNEL); |
1da177e4 LT |
554 | if (lsp == NULL) |
555 | return NULL; | |
cee54fc9 | 556 | lsp->ls_seqid.sequence = &state->owner->so_sequence; |
1da177e4 LT |
557 | atomic_set(&lsp->ls_count, 1); |
558 | lsp->ls_owner = fl_owner; | |
1da177e4 LT |
559 | spin_lock(&clp->cl_lock); |
560 | lsp->ls_id = nfs4_alloc_lockowner_id(clp); | |
561 | spin_unlock(&clp->cl_lock); | |
8d0a8a9d | 562 | INIT_LIST_HEAD(&lsp->ls_locks); |
1da177e4 LT |
563 | return lsp; |
564 | } | |
565 | ||
566 | /* | |
567 | * Return a compatible lock_state. If no initialized lock_state structure | |
568 | * exists, return an uninitialized one. | |
569 | * | |
e6dfa553 | 570 | * The caller must be holding clp->cl_sem |
1da177e4 | 571 | */ |
8d0a8a9d | 572 | static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner) |
1da177e4 | 573 | { |
8d0a8a9d | 574 | struct nfs4_lock_state *lsp, *new = NULL; |
1da177e4 | 575 | |
8d0a8a9d TM |
576 | for(;;) { |
577 | spin_lock(&state->state_lock); | |
578 | lsp = __nfs4_find_lock_state(state, owner); | |
579 | if (lsp != NULL) | |
580 | break; | |
581 | if (new != NULL) { | |
582 | new->ls_state = state; | |
583 | list_add(&new->ls_locks, &state->lock_states); | |
584 | set_bit(LK_STATE_IN_USE, &state->flags); | |
585 | lsp = new; | |
586 | new = NULL; | |
587 | break; | |
588 | } | |
589 | spin_unlock(&state->state_lock); | |
590 | new = nfs4_alloc_lock_state(state, owner); | |
591 | if (new == NULL) | |
592 | return NULL; | |
593 | } | |
594 | spin_unlock(&state->state_lock); | |
595 | kfree(new); | |
1da177e4 LT |
596 | return lsp; |
597 | } | |
598 | ||
599 | /* | |
8d0a8a9d TM |
600 | * Release reference to lock_state, and free it if we see that |
601 | * it is no longer in use | |
1da177e4 | 602 | */ |
faf5f49c | 603 | void nfs4_put_lock_state(struct nfs4_lock_state *lsp) |
1da177e4 | 604 | { |
8d0a8a9d | 605 | struct nfs4_state *state; |
1da177e4 | 606 | |
8d0a8a9d TM |
607 | if (lsp == NULL) |
608 | return; | |
609 | state = lsp->ls_state; | |
610 | if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock)) | |
611 | return; | |
612 | list_del(&lsp->ls_locks); | |
613 | if (list_empty(&state->lock_states)) | |
614 | clear_bit(LK_STATE_IN_USE, &state->flags); | |
615 | spin_unlock(&state->state_lock); | |
616 | kfree(lsp); | |
1da177e4 LT |
617 | } |
618 | ||
8d0a8a9d | 619 | static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src) |
1da177e4 | 620 | { |
8d0a8a9d | 621 | struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner; |
1da177e4 | 622 | |
8d0a8a9d TM |
623 | dst->fl_u.nfs4_fl.owner = lsp; |
624 | atomic_inc(&lsp->ls_count); | |
625 | } | |
1da177e4 | 626 | |
8d0a8a9d | 627 | static void nfs4_fl_release_lock(struct file_lock *fl) |
1da177e4 | 628 | { |
8d0a8a9d | 629 | nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner); |
1da177e4 LT |
630 | } |
631 | ||
8d0a8a9d TM |
632 | static struct file_lock_operations nfs4_fl_lock_ops = { |
633 | .fl_copy_lock = nfs4_fl_copy_lock, | |
634 | .fl_release_private = nfs4_fl_release_lock, | |
635 | }; | |
636 | ||
637 | int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl) | |
1da177e4 | 638 | { |
8d0a8a9d TM |
639 | struct nfs4_lock_state *lsp; |
640 | ||
641 | if (fl->fl_ops != NULL) | |
642 | return 0; | |
643 | lsp = nfs4_get_lock_state(state, fl->fl_owner); | |
644 | if (lsp == NULL) | |
645 | return -ENOMEM; | |
646 | fl->fl_u.nfs4_fl.owner = lsp; | |
647 | fl->fl_ops = &nfs4_fl_lock_ops; | |
648 | return 0; | |
1da177e4 LT |
649 | } |
650 | ||
8d0a8a9d TM |
651 | /* |
652 | * Byte-range lock aware utility to initialize the stateid of read/write | |
653 | * requests. | |
1da177e4 | 654 | */ |
8d0a8a9d | 655 | void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner) |
1da177e4 | 656 | { |
8d0a8a9d | 657 | struct nfs4_lock_state *lsp; |
1da177e4 | 658 | |
8d0a8a9d TM |
659 | memcpy(dst, &state->stateid, sizeof(*dst)); |
660 | if (test_bit(LK_STATE_IN_USE, &state->flags) == 0) | |
661 | return; | |
1da177e4 | 662 | |
8d0a8a9d TM |
663 | spin_lock(&state->state_lock); |
664 | lsp = __nfs4_find_lock_state(state, fl_owner); | |
665 | if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) | |
666 | memcpy(dst, &lsp->ls_stateid, sizeof(*dst)); | |
667 | spin_unlock(&state->state_lock); | |
1da177e4 LT |
668 | nfs4_put_lock_state(lsp); |
669 | } | |
670 | ||
cee54fc9 TM |
671 | struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter) |
672 | { | |
cee54fc9 TM |
673 | struct nfs_seqid *new; |
674 | ||
675 | new = kmalloc(sizeof(*new), GFP_KERNEL); | |
676 | if (new != NULL) { | |
677 | new->sequence = counter; | |
4e51336a | 678 | INIT_LIST_HEAD(&new->list); |
cee54fc9 TM |
679 | } |
680 | return new; | |
681 | } | |
682 | ||
683 | void nfs_free_seqid(struct nfs_seqid *seqid) | |
1da177e4 | 684 | { |
cee54fc9 | 685 | struct rpc_sequence *sequence = seqid->sequence->sequence; |
cee54fc9 | 686 | |
4e51336a TM |
687 | if (!list_empty(&seqid->list)) { |
688 | spin_lock(&sequence->lock); | |
689 | list_del(&seqid->list); | |
690 | spin_unlock(&sequence->lock); | |
691 | } | |
692 | rpc_wake_up_next(&sequence->wait); | |
cee54fc9 | 693 | kfree(seqid); |
1da177e4 LT |
694 | } |
695 | ||
696 | /* | |
cee54fc9 TM |
697 | * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or |
698 | * failed with a seqid incrementing error - | |
699 | * see comments nfs_fs.h:seqid_mutating_error() | |
700 | */ | |
701 | static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid) | |
702 | { | |
703 | switch (status) { | |
704 | case 0: | |
705 | break; | |
706 | case -NFS4ERR_BAD_SEQID: | |
707 | case -NFS4ERR_STALE_CLIENTID: | |
708 | case -NFS4ERR_STALE_STATEID: | |
709 | case -NFS4ERR_BAD_STATEID: | |
710 | case -NFS4ERR_BADXDR: | |
711 | case -NFS4ERR_RESOURCE: | |
712 | case -NFS4ERR_NOFILEHANDLE: | |
713 | /* Non-seqid mutating errors */ | |
714 | return; | |
715 | }; | |
716 | /* | |
717 | * Note: no locking needed as we are guaranteed to be first | |
718 | * on the sequence list | |
719 | */ | |
720 | seqid->sequence->counter++; | |
721 | } | |
722 | ||
723 | void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid) | |
724 | { | |
725 | if (status == -NFS4ERR_BAD_SEQID) { | |
726 | struct nfs4_state_owner *sp = container_of(seqid->sequence, | |
727 | struct nfs4_state_owner, so_seqid); | |
1da177e4 | 728 | nfs4_drop_state_owner(sp); |
cee54fc9 TM |
729 | } |
730 | return nfs_increment_seqid(status, seqid); | |
731 | } | |
732 | ||
733 | /* | |
cee54fc9 TM |
734 | * Increment the seqid if the LOCK/LOCKU succeeded, or |
735 | * failed with a seqid incrementing error - | |
736 | * see comments nfs_fs.h:seqid_mutating_error() | |
737 | */ | |
738 | void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid) | |
739 | { | |
740 | return nfs_increment_seqid(status, seqid); | |
741 | } | |
742 | ||
743 | int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task) | |
744 | { | |
745 | struct rpc_sequence *sequence = seqid->sequence->sequence; | |
746 | int status = 0; | |
747 | ||
4e51336a TM |
748 | if (sequence->list.next == &seqid->list) |
749 | goto out; | |
cee54fc9 | 750 | spin_lock(&sequence->lock); |
4e51336a | 751 | if (!list_empty(&sequence->list)) { |
cee54fc9 TM |
752 | rpc_sleep_on(&sequence->wait, task, NULL, NULL); |
753 | status = -EAGAIN; | |
4e51336a TM |
754 | } else |
755 | list_add(&seqid->list, &sequence->list); | |
cee54fc9 | 756 | spin_unlock(&sequence->lock); |
4e51336a | 757 | out: |
cee54fc9 | 758 | return status; |
1da177e4 LT |
759 | } |
760 | ||
761 | static int reclaimer(void *); | |
762 | struct reclaimer_args { | |
763 | struct nfs4_client *clp; | |
764 | struct completion complete; | |
765 | }; | |
766 | ||
767 | /* | |
768 | * State recovery routine | |
769 | */ | |
770 | void | |
771 | nfs4_recover_state(void *data) | |
772 | { | |
773 | struct nfs4_client *clp = (struct nfs4_client *)data; | |
774 | struct reclaimer_args args = { | |
775 | .clp = clp, | |
776 | }; | |
777 | might_sleep(); | |
778 | ||
779 | init_completion(&args.complete); | |
780 | ||
781 | if (kernel_thread(reclaimer, &args, CLONE_KERNEL) < 0) | |
782 | goto out_failed_clear; | |
783 | wait_for_completion(&args.complete); | |
784 | return; | |
785 | out_failed_clear: | |
786 | set_bit(NFS4CLNT_OK, &clp->cl_state); | |
787 | wake_up_all(&clp->cl_waitq); | |
788 | rpc_wake_up(&clp->cl_rpcwaitq); | |
789 | } | |
790 | ||
791 | /* | |
792 | * Schedule a state recovery attempt | |
793 | */ | |
794 | void | |
795 | nfs4_schedule_state_recovery(struct nfs4_client *clp) | |
796 | { | |
797 | if (!clp) | |
798 | return; | |
799 | if (test_and_clear_bit(NFS4CLNT_OK, &clp->cl_state)) | |
800 | schedule_work(&clp->cl_recoverd); | |
801 | } | |
802 | ||
803 | static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state) | |
804 | { | |
805 | struct inode *inode = state->inode; | |
806 | struct file_lock *fl; | |
807 | int status = 0; | |
808 | ||
809 | for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) { | |
810 | if (!(fl->fl_flags & FL_POSIX)) | |
811 | continue; | |
812 | if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state) | |
813 | continue; | |
814 | status = ops->recover_lock(state, fl); | |
815 | if (status >= 0) | |
816 | continue; | |
817 | switch (status) { | |
818 | default: | |
819 | printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n", | |
820 | __FUNCTION__, status); | |
821 | case -NFS4ERR_EXPIRED: | |
822 | case -NFS4ERR_NO_GRACE: | |
823 | case -NFS4ERR_RECLAIM_BAD: | |
824 | case -NFS4ERR_RECLAIM_CONFLICT: | |
825 | /* kill_proc(fl->fl_owner, SIGLOST, 1); */ | |
826 | break; | |
827 | case -NFS4ERR_STALE_CLIENTID: | |
828 | goto out_err; | |
829 | } | |
830 | } | |
831 | return 0; | |
832 | out_err: | |
833 | return status; | |
834 | } | |
835 | ||
836 | static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp) | |
837 | { | |
838 | struct nfs4_state *state; | |
839 | struct nfs4_lock_state *lock; | |
840 | int status = 0; | |
841 | ||
842 | /* Note: we rely on the sp->so_states list being ordered | |
843 | * so that we always reclaim open(O_RDWR) and/or open(O_WRITE) | |
844 | * states first. | |
845 | * This is needed to ensure that the server won't give us any | |
846 | * read delegations that we have to return if, say, we are | |
847 | * recovering after a network partition or a reboot from a | |
848 | * server that doesn't support a grace period. | |
849 | */ | |
850 | list_for_each_entry(state, &sp->so_states, open_states) { | |
851 | if (state->state == 0) | |
852 | continue; | |
853 | status = ops->recover_open(sp, state); | |
1da177e4 LT |
854 | if (status >= 0) { |
855 | status = nfs4_reclaim_locks(ops, state); | |
856 | if (status < 0) | |
857 | goto out_err; | |
858 | list_for_each_entry(lock, &state->lock_states, ls_locks) { | |
859 | if (!(lock->ls_flags & NFS_LOCK_INITIALIZED)) | |
860 | printk("%s: Lock reclaim failed!\n", | |
861 | __FUNCTION__); | |
862 | } | |
863 | continue; | |
864 | } | |
865 | switch (status) { | |
866 | default: | |
867 | printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n", | |
868 | __FUNCTION__, status); | |
869 | case -ENOENT: | |
870 | case -NFS4ERR_RECLAIM_BAD: | |
871 | case -NFS4ERR_RECLAIM_CONFLICT: | |
872 | /* | |
873 | * Open state on this file cannot be recovered | |
874 | * All we can do is revert to using the zero stateid. | |
875 | */ | |
876 | memset(state->stateid.data, 0, | |
877 | sizeof(state->stateid.data)); | |
878 | /* Mark the file as being 'closed' */ | |
879 | state->state = 0; | |
880 | break; | |
881 | case -NFS4ERR_EXPIRED: | |
882 | case -NFS4ERR_NO_GRACE: | |
883 | case -NFS4ERR_STALE_CLIENTID: | |
884 | goto out_err; | |
885 | } | |
886 | } | |
887 | return 0; | |
888 | out_err: | |
889 | return status; | |
890 | } | |
891 | ||
cee54fc9 TM |
892 | static void nfs4_state_mark_reclaim(struct nfs4_client *clp) |
893 | { | |
894 | struct nfs4_state_owner *sp; | |
895 | struct nfs4_state *state; | |
896 | struct nfs4_lock_state *lock; | |
897 | ||
898 | /* Reset all sequence ids to zero */ | |
899 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { | |
900 | sp->so_seqid.counter = 0; | |
901 | sp->so_seqid.flags = 0; | |
902 | list_for_each_entry(state, &sp->so_states, open_states) { | |
903 | list_for_each_entry(lock, &state->lock_states, ls_locks) { | |
904 | lock->ls_seqid.counter = 0; | |
905 | lock->ls_seqid.flags = 0; | |
906 | lock->ls_flags &= ~NFS_LOCK_INITIALIZED; | |
907 | } | |
908 | } | |
909 | } | |
910 | } | |
911 | ||
1da177e4 LT |
912 | static int reclaimer(void *ptr) |
913 | { | |
914 | struct reclaimer_args *args = (struct reclaimer_args *)ptr; | |
915 | struct nfs4_client *clp = args->clp; | |
916 | struct nfs4_state_owner *sp; | |
917 | struct nfs4_state_recovery_ops *ops; | |
918 | int status = 0; | |
919 | ||
920 | daemonize("%u.%u.%u.%u-reclaim", NIPQUAD(clp->cl_addr)); | |
921 | allow_signal(SIGKILL); | |
922 | ||
923 | atomic_inc(&clp->cl_count); | |
924 | complete(&args->complete); | |
925 | ||
926 | /* Ensure exclusive access to NFSv4 state */ | |
927 | lock_kernel(); | |
928 | down_write(&clp->cl_sem); | |
929 | /* Are there any NFS mounts out there? */ | |
930 | if (list_empty(&clp->cl_superblocks)) | |
931 | goto out; | |
932 | restart_loop: | |
933 | status = nfs4_proc_renew(clp); | |
934 | switch (status) { | |
935 | case 0: | |
936 | case -NFS4ERR_CB_PATH_DOWN: | |
937 | goto out; | |
938 | case -NFS4ERR_STALE_CLIENTID: | |
939 | case -NFS4ERR_LEASE_MOVED: | |
940 | ops = &nfs4_reboot_recovery_ops; | |
941 | break; | |
942 | default: | |
943 | ops = &nfs4_network_partition_recovery_ops; | |
944 | }; | |
cee54fc9 | 945 | nfs4_state_mark_reclaim(clp); |
1da177e4 LT |
946 | status = __nfs4_init_client(clp); |
947 | if (status) | |
948 | goto out_error; | |
949 | /* Mark all delegations for reclaim */ | |
950 | nfs_delegation_mark_reclaim(clp); | |
951 | /* Note: list is protected by exclusive lock on cl->cl_sem */ | |
952 | list_for_each_entry(sp, &clp->cl_state_owners, so_list) { | |
953 | status = nfs4_reclaim_open_state(ops, sp); | |
954 | if (status < 0) { | |
955 | if (status == -NFS4ERR_NO_GRACE) { | |
956 | ops = &nfs4_network_partition_recovery_ops; | |
957 | status = nfs4_reclaim_open_state(ops, sp); | |
958 | } | |
959 | if (status == -NFS4ERR_STALE_CLIENTID) | |
960 | goto restart_loop; | |
961 | if (status == -NFS4ERR_EXPIRED) | |
962 | goto restart_loop; | |
963 | } | |
964 | } | |
965 | nfs_delegation_reap_unclaimed(clp); | |
966 | out: | |
967 | set_bit(NFS4CLNT_OK, &clp->cl_state); | |
968 | up_write(&clp->cl_sem); | |
969 | unlock_kernel(); | |
970 | wake_up_all(&clp->cl_waitq); | |
971 | rpc_wake_up(&clp->cl_rpcwaitq); | |
972 | if (status == -NFS4ERR_CB_PATH_DOWN) | |
973 | nfs_handle_cb_pathdown(clp); | |
974 | nfs4_put_client(clp); | |
975 | return 0; | |
976 | out_error: | |
977 | printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n", | |
978 | NIPQUAD(clp->cl_addr.s_addr), -status); | |
979 | goto out; | |
980 | } | |
981 | ||
982 | /* | |
983 | * Local variables: | |
984 | * c-basic-offset: 8 | |
985 | * End: | |
986 | */ |