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