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