blob: 090a36b07a2231a966eb01d3af19fe59d68b43e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
42#include <linux/smp_lock.h>
43#include <linux/nfs_fs.h>
44#include <linux/nfs_idmap.h>
Trond Myklebust5043e902006-01-03 09:55:23 +010045#include <linux/kthread.h>
46#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/workqueue.h>
48#include <linux/bitops.h>
49
Trond Myklebust4ce79712005-06-22 17:16:21 +000050#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "callback.h"
52#include "delegation.h"
53
54#define OPENOWNER_POOL_SIZE 8
55
Trond Myklebust4ce79712005-06-22 17:16:21 +000056const nfs4_stateid zero_stateid;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static DEFINE_SPINLOCK(state_spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static LIST_HEAD(nfs4_clientid_list);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061void
62init_nfsv4_state(struct nfs_server *server)
63{
64 server->nfs4_state = NULL;
65 INIT_LIST_HEAD(&server->nfs4_siblings);
66}
67
68void
69destroy_nfsv4_state(struct nfs_server *server)
70{
Jesper Juhlf99d49a2005-11-07 01:01:34 -080071 kfree(server->mnt_path);
72 server->mnt_path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (server->nfs4_state) {
74 nfs4_put_client(server->nfs4_state);
75 server->nfs4_state = NULL;
76 }
77}
78
79/*
80 * nfs4_get_client(): returns an empty client structure
81 * nfs4_put_client(): drops reference to client structure
82 *
83 * Since these are allocated/deallocated very rarely, we don't
84 * bother putting them in a slab cache...
85 */
86static struct nfs4_client *
87nfs4_alloc_client(struct in_addr *addr)
88{
89 struct nfs4_client *clp;
90
91 if (nfs_callback_up() < 0)
92 return NULL;
Trond Myklebust286d7d62006-01-03 09:55:26 +010093 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 nfs_callback_down();
95 return NULL;
96 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
98 init_rwsem(&clp->cl_sem);
99 INIT_LIST_HEAD(&clp->cl_delegations);
100 INIT_LIST_HEAD(&clp->cl_state_owners);
101 INIT_LIST_HEAD(&clp->cl_unused);
102 spin_lock_init(&clp->cl_lock);
103 atomic_set(&clp->cl_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
105 INIT_LIST_HEAD(&clp->cl_superblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS4 client");
J. Bruce Fields6a192752005-06-22 17:16:23 +0000107 clp->cl_rpcclient = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 clp->cl_boot_time = CURRENT_TIME;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100109 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return clp;
111}
112
113static void
114nfs4_free_client(struct nfs4_client *clp)
115{
116 struct nfs4_state_owner *sp;
117
118 while (!list_empty(&clp->cl_unused)) {
119 sp = list_entry(clp->cl_unused.next,
120 struct nfs4_state_owner,
121 so_list);
122 list_del(&sp->so_list);
123 kfree(sp);
124 }
125 BUG_ON(!list_empty(&clp->cl_state_owners));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 nfs_idmap_delete(clp);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000127 if (!IS_ERR(clp->cl_rpcclient))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 rpc_shutdown_client(clp->cl_rpcclient);
129 kfree(clp);
130 nfs_callback_down();
131}
132
133static struct nfs4_client *__nfs4_find_client(struct in_addr *addr)
134{
135 struct nfs4_client *clp;
136 list_for_each_entry(clp, &nfs4_clientid_list, cl_servers) {
137 if (memcmp(&clp->cl_addr, addr, sizeof(clp->cl_addr)) == 0) {
138 atomic_inc(&clp->cl_count);
139 return clp;
140 }
141 }
142 return NULL;
143}
144
145struct nfs4_client *nfs4_find_client(struct in_addr *addr)
146{
147 struct nfs4_client *clp;
148 spin_lock(&state_spinlock);
149 clp = __nfs4_find_client(addr);
150 spin_unlock(&state_spinlock);
151 return clp;
152}
153
154struct nfs4_client *
155nfs4_get_client(struct in_addr *addr)
156{
157 struct nfs4_client *clp, *new = NULL;
158
159 spin_lock(&state_spinlock);
160 for (;;) {
161 clp = __nfs4_find_client(addr);
162 if (clp != NULL)
163 break;
164 clp = new;
165 if (clp != NULL) {
166 list_add(&clp->cl_servers, &nfs4_clientid_list);
167 new = NULL;
168 break;
169 }
170 spin_unlock(&state_spinlock);
171 new = nfs4_alloc_client(addr);
172 spin_lock(&state_spinlock);
173 if (new == NULL)
174 break;
175 }
176 spin_unlock(&state_spinlock);
177 if (new)
178 nfs4_free_client(new);
179 return clp;
180}
181
182void
183nfs4_put_client(struct nfs4_client *clp)
184{
185 if (!atomic_dec_and_lock(&clp->cl_count, &state_spinlock))
186 return;
187 list_del(&clp->cl_servers);
188 spin_unlock(&state_spinlock);
189 BUG_ON(!list_empty(&clp->cl_superblocks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 rpc_wake_up(&clp->cl_rpcwaitq);
191 nfs4_kill_renewd(clp);
192 nfs4_free_client(clp);
193}
194
Trond Myklebust286d7d62006-01-03 09:55:26 +0100195static int nfs4_init_client(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Trond Myklebust286d7d62006-01-03 09:55:26 +0100197 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
198 nfs_callback_tcpport, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (status == 0)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100200 status = nfs4_proc_setclientid_confirm(clp, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (status == 0)
202 nfs4_schedule_state_renewal(clp);
203 return status;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206u32
207nfs4_alloc_lockowner_id(struct nfs4_client *clp)
208{
209 return clp->cl_lockowner_id ++;
210}
211
212static struct nfs4_state_owner *
213nfs4_client_grab_unused(struct nfs4_client *clp, struct rpc_cred *cred)
214{
215 struct nfs4_state_owner *sp = NULL;
216
217 if (!list_empty(&clp->cl_unused)) {
218 sp = list_entry(clp->cl_unused.next, struct nfs4_state_owner, so_list);
219 atomic_inc(&sp->so_count);
220 sp->so_cred = cred;
221 list_move(&sp->so_list, &clp->cl_state_owners);
222 clp->cl_nunused--;
223 }
224 return sp;
225}
226
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100227struct rpc_cred *nfs4_get_renew_cred(struct nfs4_client *clp)
228{
229 struct nfs4_state_owner *sp;
230 struct rpc_cred *cred = NULL;
231
232 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
233 if (list_empty(&sp->so_states))
234 continue;
235 cred = get_rpccred(sp->so_cred);
236 break;
237 }
238 return cred;
239}
240
Trond Myklebust286d7d62006-01-03 09:55:26 +0100241struct rpc_cred *nfs4_get_setclientid_cred(struct nfs4_client *clp)
242{
243 struct nfs4_state_owner *sp;
244
245 if (!list_empty(&clp->cl_state_owners)) {
246 sp = list_entry(clp->cl_state_owners.next,
247 struct nfs4_state_owner, so_list);
248 return get_rpccred(sp->so_cred);
249 }
250 return NULL;
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static struct nfs4_state_owner *
254nfs4_find_state_owner(struct nfs4_client *clp, struct rpc_cred *cred)
255{
256 struct nfs4_state_owner *sp, *res = NULL;
257
258 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
259 if (sp->so_cred != cred)
260 continue;
261 atomic_inc(&sp->so_count);
262 /* Move to the head of the list */
263 list_move(&sp->so_list, &clp->cl_state_owners);
264 res = sp;
265 break;
266 }
267 return res;
268}
269
270/*
271 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
272 * create a new state_owner.
273 *
274 */
275static struct nfs4_state_owner *
276nfs4_alloc_state_owner(void)
277{
278 struct nfs4_state_owner *sp;
279
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700280 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (!sp)
282 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700283 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 INIT_LIST_HEAD(&sp->so_states);
285 INIT_LIST_HEAD(&sp->so_delegations);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700286 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
287 sp->so_seqid.sequence = &sp->so_sequence;
288 spin_lock_init(&sp->so_sequence.lock);
289 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 atomic_set(&sp->so_count, 1);
291 return sp;
292}
293
294void
295nfs4_drop_state_owner(struct nfs4_state_owner *sp)
296{
297 struct nfs4_client *clp = sp->so_client;
298 spin_lock(&clp->cl_lock);
299 list_del_init(&sp->so_list);
300 spin_unlock(&clp->cl_lock);
301}
302
303/*
304 * Note: must be called with clp->cl_sem held in order to prevent races
305 * with reboot recovery!
306 */
307struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
308{
309 struct nfs4_client *clp = server->nfs4_state;
310 struct nfs4_state_owner *sp, *new;
311
312 get_rpccred(cred);
313 new = nfs4_alloc_state_owner();
314 spin_lock(&clp->cl_lock);
315 sp = nfs4_find_state_owner(clp, cred);
316 if (sp == NULL)
317 sp = nfs4_client_grab_unused(clp, cred);
318 if (sp == NULL && new != NULL) {
319 list_add(&new->so_list, &clp->cl_state_owners);
320 new->so_client = clp;
321 new->so_id = nfs4_alloc_lockowner_id(clp);
322 new->so_cred = cred;
323 sp = new;
324 new = NULL;
325 }
326 spin_unlock(&clp->cl_lock);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800327 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (sp != NULL)
329 return sp;
330 put_rpccred(cred);
331 return NULL;
332}
333
334/*
335 * Must be called with clp->cl_sem held in order to avoid races
336 * with state recovery...
337 */
338void nfs4_put_state_owner(struct nfs4_state_owner *sp)
339{
340 struct nfs4_client *clp = sp->so_client;
341 struct rpc_cred *cred = sp->so_cred;
342
343 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
344 return;
345 if (clp->cl_nunused >= OPENOWNER_POOL_SIZE)
346 goto out_free;
347 if (list_empty(&sp->so_list))
348 goto out_free;
349 list_move(&sp->so_list, &clp->cl_unused);
350 clp->cl_nunused++;
351 spin_unlock(&clp->cl_lock);
352 put_rpccred(cred);
353 cred = NULL;
354 return;
355out_free:
356 list_del(&sp->so_list);
357 spin_unlock(&clp->cl_lock);
358 put_rpccred(cred);
359 kfree(sp);
360}
361
362static struct nfs4_state *
363nfs4_alloc_open_state(void)
364{
365 struct nfs4_state *state;
366
Trond Myklebuste7616922006-01-03 09:55:13 +0100367 state = kzalloc(sizeof(*state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!state)
369 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 atomic_set(&state->count, 1);
371 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000372 spin_lock_init(&state->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return state;
374}
375
Trond Myklebust4cecb762005-11-04 15:32:58 -0500376void
377nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
378{
379 if (state->state == mode)
380 return;
381 /* NB! List reordering - see the reclaim code for why. */
382 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
383 if (mode & FMODE_WRITE)
384 list_move(&state->open_states, &state->owner->so_states);
385 else
386 list_move_tail(&state->open_states, &state->owner->so_states);
387 }
388 if (mode == 0)
389 list_del_init(&state->inode_states);
390 state->state = mode;
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
395{
396 struct nfs_inode *nfsi = NFS_I(inode);
397 struct nfs4_state *state;
398
399 list_for_each_entry(state, &nfsi->open_states, inode_states) {
400 /* Is this in the process of being freed? */
Trond Myklebust4cecb762005-11-04 15:32:58 -0500401 if (state->state == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 continue;
403 if (state->owner == owner) {
404 atomic_inc(&state->count);
405 return state;
406 }
407 }
408 return NULL;
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static void
412nfs4_free_open_state(struct nfs4_state *state)
413{
414 kfree(state);
415}
416
417struct nfs4_state *
418nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
419{
420 struct nfs4_state *state, *new;
421 struct nfs_inode *nfsi = NFS_I(inode);
422
423 spin_lock(&inode->i_lock);
424 state = __nfs4_find_state_byowner(inode, owner);
425 spin_unlock(&inode->i_lock);
426 if (state)
427 goto out;
428 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700429 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 spin_lock(&inode->i_lock);
431 state = __nfs4_find_state_byowner(inode, owner);
432 if (state == NULL && new != NULL) {
433 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 state->owner = owner;
435 atomic_inc(&owner->so_count);
436 list_add(&state->inode_states, &nfsi->open_states);
437 state->inode = igrab(inode);
438 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700439 /* Note: The reclaim code dictates that we add stateless
440 * and read-only stateids to the end of the list */
441 list_add_tail(&state->open_states, &owner->so_states);
442 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 } else {
444 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700445 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (new)
447 nfs4_free_open_state(new);
448 }
449out:
450 return state;
451}
452
453/*
454 * Beware! Caller must be holding exactly one
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700455 * reference to clp->cl_sem!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457void nfs4_put_open_state(struct nfs4_state *state)
458{
459 struct inode *inode = state->inode;
460 struct nfs4_state_owner *owner = state->owner;
461
Trond Myklebustec073422005-10-20 14:22:47 -0700462 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700464 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!list_empty(&state->inode_states))
466 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700468 spin_unlock(&inode->i_lock);
469 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 nfs4_free_open_state(state);
472 nfs4_put_state_owner(owner);
473}
474
475/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700476 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
478void nfs4_close_state(struct nfs4_state *state, mode_t mode)
479{
480 struct inode *inode = state->inode;
481 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500482 int oldstate, newstate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700486 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100488 switch (mode & (FMODE_READ | FMODE_WRITE)) {
489 case FMODE_READ:
490 state->n_rdonly--;
491 break;
492 case FMODE_WRITE:
493 state->n_wronly--;
494 break;
495 case FMODE_READ|FMODE_WRITE:
496 state->n_rdwr--;
497 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500498 oldstate = newstate = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +0100499 if (state->n_rdwr == 0) {
500 if (state->n_rdonly == 0)
501 newstate &= ~FMODE_READ;
502 if (state->n_wronly == 0)
503 newstate &= ~FMODE_WRITE;
504 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500505 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
506 nfs4_state_set_mode_locked(state, newstate);
507 oldstate = newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700510 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500511
512 if (oldstate != newstate && nfs4_do_close(inode, state) == 0)
513 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 nfs4_put_state_owner(owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/*
519 * Search the state->lock_states for an existing lock_owner
520 * that is compatible with current->files
521 */
522static struct nfs4_lock_state *
523__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
524{
525 struct nfs4_lock_state *pos;
526 list_for_each_entry(pos, &state->lock_states, ls_locks) {
527 if (pos->ls_owner != fl_owner)
528 continue;
529 atomic_inc(&pos->ls_count);
530 return pos;
531 }
532 return NULL;
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
536 * Return a compatible lock_state. If no initialized lock_state structure
537 * exists, return an uninitialized one.
538 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
541{
542 struct nfs4_lock_state *lsp;
543 struct nfs4_client *clp = state->owner->so_client;
544
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700545 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (lsp == NULL)
547 return NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700548 lsp->ls_seqid.sequence = &state->owner->so_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 atomic_set(&lsp->ls_count, 1);
550 lsp->ls_owner = fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 spin_lock(&clp->cl_lock);
552 lsp->ls_id = nfs4_alloc_lockowner_id(clp);
553 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000554 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return lsp;
556}
557
558/*
559 * Return a compatible lock_state. If no initialized lock_state structure
560 * exists, return an uninitialized one.
561 *
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700562 * The caller must be holding clp->cl_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000564static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000566 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000568 for(;;) {
569 spin_lock(&state->state_lock);
570 lsp = __nfs4_find_lock_state(state, owner);
571 if (lsp != NULL)
572 break;
573 if (new != NULL) {
574 new->ls_state = state;
575 list_add(&new->ls_locks, &state->lock_states);
576 set_bit(LK_STATE_IN_USE, &state->flags);
577 lsp = new;
578 new = NULL;
579 break;
580 }
581 spin_unlock(&state->state_lock);
582 new = nfs4_alloc_lock_state(state, owner);
583 if (new == NULL)
584 return NULL;
585 }
586 spin_unlock(&state->state_lock);
587 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return lsp;
589}
590
591/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000592 * Release reference to lock_state, and free it if we see that
593 * it is no longer in use
594 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700595void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000596{
597 struct nfs4_state *state;
598
599 if (lsp == NULL)
600 return;
601 state = lsp->ls_state;
602 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
603 return;
604 list_del(&lsp->ls_locks);
605 if (list_empty(&state->lock_states))
606 clear_bit(LK_STATE_IN_USE, &state->flags);
607 spin_unlock(&state->state_lock);
608 kfree(lsp);
609}
610
611static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
612{
613 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
614
615 dst->fl_u.nfs4_fl.owner = lsp;
616 atomic_inc(&lsp->ls_count);
617}
618
619static void nfs4_fl_release_lock(struct file_lock *fl)
620{
621 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
622}
623
624static struct file_lock_operations nfs4_fl_lock_ops = {
625 .fl_copy_lock = nfs4_fl_copy_lock,
626 .fl_release_private = nfs4_fl_release_lock,
627};
628
629int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
630{
631 struct nfs4_lock_state *lsp;
632
633 if (fl->fl_ops != NULL)
634 return 0;
635 lsp = nfs4_get_lock_state(state, fl->fl_owner);
636 if (lsp == NULL)
637 return -ENOMEM;
638 fl->fl_u.nfs4_fl.owner = lsp;
639 fl->fl_ops = &nfs4_fl_lock_ops;
640 return 0;
641}
642
643/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * Byte-range lock aware utility to initialize the stateid of read/write
645 * requests.
646 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000647void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000649 struct nfs4_lock_state *lsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 memcpy(dst, &state->stateid, sizeof(*dst));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000652 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
653 return;
654
655 spin_lock(&state->state_lock);
656 lsp = __nfs4_find_lock_state(state, fl_owner);
657 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
658 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
659 spin_unlock(&state->state_lock);
660 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700663struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Trond Myklebust36f20c62005-11-25 17:09:57 -0500665 struct rpc_sequence *sequence = counter->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700666 struct nfs_seqid *new;
667
668 new = kmalloc(sizeof(*new), GFP_KERNEL);
669 if (new != NULL) {
670 new->sequence = counter;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500671 spin_lock(&sequence->lock);
672 list_add_tail(&new->list, &sequence->list);
673 spin_unlock(&sequence->lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700674 }
675 return new;
676}
677
678void nfs_free_seqid(struct nfs_seqid *seqid)
679{
680 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700681
Trond Myklebust36f20c62005-11-25 17:09:57 -0500682 spin_lock(&sequence->lock);
683 list_del(&seqid->list);
684 spin_unlock(&sequence->lock);
685 rpc_wake_up(&sequence->wait);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700686 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700690 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
691 * failed with a seqid incrementing error -
692 * see comments nfs_fs.h:seqid_mutating_error()
693 */
694static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700696 switch (status) {
697 case 0:
698 break;
699 case -NFS4ERR_BAD_SEQID:
700 case -NFS4ERR_STALE_CLIENTID:
701 case -NFS4ERR_STALE_STATEID:
702 case -NFS4ERR_BAD_STATEID:
703 case -NFS4ERR_BADXDR:
704 case -NFS4ERR_RESOURCE:
705 case -NFS4ERR_NOFILEHANDLE:
706 /* Non-seqid mutating errors */
707 return;
708 };
709 /*
710 * Note: no locking needed as we are guaranteed to be first
711 * on the sequence list
712 */
713 seqid->sequence->counter++;
714}
715
716void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
717{
718 if (status == -NFS4ERR_BAD_SEQID) {
719 struct nfs4_state_owner *sp = container_of(seqid->sequence,
720 struct nfs4_state_owner, so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 nfs4_drop_state_owner(sp);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700722 }
723 return nfs_increment_seqid(status, seqid);
724}
725
726/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700727 * Increment the seqid if the LOCK/LOCKU succeeded, or
728 * failed with a seqid incrementing error -
729 * see comments nfs_fs.h:seqid_mutating_error()
730 */
731void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
732{
733 return nfs_increment_seqid(status, seqid);
734}
735
736int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
737{
738 struct rpc_sequence *sequence = seqid->sequence->sequence;
739 int status = 0;
740
Trond Myklebust4e513362005-10-20 14:22:41 -0700741 if (sequence->list.next == &seqid->list)
742 goto out;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700743 spin_lock(&sequence->lock);
Trond Myklebust36f20c62005-11-25 17:09:57 -0500744 if (sequence->list.next != &seqid->list) {
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700745 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
746 status = -EAGAIN;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500747 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700748 spin_unlock(&sequence->lock);
Trond Myklebust4e513362005-10-20 14:22:41 -0700749out:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700750 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753static int reclaimer(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Trond Myklebust433fbe42006-01-03 09:55:22 +0100755static inline void nfs4_clear_recover_bit(struct nfs4_client *clp)
756{
757 smp_mb__before_clear_bit();
758 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
759 smp_mb__after_clear_bit();
760 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
761 rpc_wake_up(&clp->cl_rpcwaitq);
762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/*
765 * State recovery routine
766 */
Trond Myklebust5043e902006-01-03 09:55:23 +0100767static void nfs4_recover_state(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Trond Myklebust5043e902006-01-03 09:55:23 +0100769 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Trond Myklebust5043e902006-01-03 09:55:23 +0100771 __module_get(THIS_MODULE);
772 atomic_inc(&clp->cl_count);
773 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
774 NIPQUAD(clp->cl_addr));
775 if (!IS_ERR(task))
776 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100777 nfs4_clear_recover_bit(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100778 nfs4_put_client(clp);
779 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/*
783 * Schedule a state recovery attempt
784 */
Trond Myklebust5043e902006-01-03 09:55:23 +0100785void nfs4_schedule_state_recovery(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 if (!clp)
788 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100789 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Trond Myklebust5043e902006-01-03 09:55:23 +0100790 nfs4_recover_state(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
794{
795 struct inode *inode = state->inode;
796 struct file_lock *fl;
797 int status = 0;
798
799 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -0500800 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 continue;
802 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
803 continue;
804 status = ops->recover_lock(state, fl);
805 if (status >= 0)
806 continue;
807 switch (status) {
808 default:
809 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
810 __FUNCTION__, status);
811 case -NFS4ERR_EXPIRED:
812 case -NFS4ERR_NO_GRACE:
813 case -NFS4ERR_RECLAIM_BAD:
814 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -0500815 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 break;
817 case -NFS4ERR_STALE_CLIENTID:
818 goto out_err;
819 }
820 }
821 return 0;
822out_err:
823 return status;
824}
825
826static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
827{
828 struct nfs4_state *state;
829 struct nfs4_lock_state *lock;
830 int status = 0;
831
832 /* Note: we rely on the sp->so_states list being ordered
833 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
834 * states first.
835 * This is needed to ensure that the server won't give us any
836 * read delegations that we have to return if, say, we are
837 * recovering after a network partition or a reboot from a
838 * server that doesn't support a grace period.
839 */
840 list_for_each_entry(state, &sp->so_states, open_states) {
841 if (state->state == 0)
842 continue;
843 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (status >= 0) {
845 status = nfs4_reclaim_locks(ops, state);
846 if (status < 0)
847 goto out_err;
848 list_for_each_entry(lock, &state->lock_states, ls_locks) {
849 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
850 printk("%s: Lock reclaim failed!\n",
851 __FUNCTION__);
852 }
853 continue;
854 }
855 switch (status) {
856 default:
857 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
858 __FUNCTION__, status);
859 case -ENOENT:
860 case -NFS4ERR_RECLAIM_BAD:
861 case -NFS4ERR_RECLAIM_CONFLICT:
862 /*
863 * Open state on this file cannot be recovered
864 * All we can do is revert to using the zero stateid.
865 */
866 memset(state->stateid.data, 0,
867 sizeof(state->stateid.data));
868 /* Mark the file as being 'closed' */
869 state->state = 0;
870 break;
871 case -NFS4ERR_EXPIRED:
872 case -NFS4ERR_NO_GRACE:
873 case -NFS4ERR_STALE_CLIENTID:
874 goto out_err;
875 }
876 }
877 return 0;
878out_err:
879 return status;
880}
881
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700882static void nfs4_state_mark_reclaim(struct nfs4_client *clp)
883{
884 struct nfs4_state_owner *sp;
885 struct nfs4_state *state;
886 struct nfs4_lock_state *lock;
887
888 /* Reset all sequence ids to zero */
889 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
890 sp->so_seqid.counter = 0;
891 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -0700892 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700893 list_for_each_entry(state, &sp->so_states, open_states) {
894 list_for_each_entry(lock, &state->lock_states, ls_locks) {
895 lock->ls_seqid.counter = 0;
896 lock->ls_seqid.flags = 0;
897 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
898 }
899 }
Trond Myklebustec073422005-10-20 14:22:47 -0700900 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700901 }
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904static int reclaimer(void *ptr)
905{
Trond Myklebust5043e902006-01-03 09:55:23 +0100906 struct nfs4_client *clp = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct nfs4_state_owner *sp;
908 struct nfs4_state_recovery_ops *ops;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100909 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int status = 0;
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 allow_signal(SIGKILL);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* Ensure exclusive access to NFSv4 state */
915 lock_kernel();
916 down_write(&clp->cl_sem);
917 /* Are there any NFS mounts out there? */
918 if (list_empty(&clp->cl_superblocks))
919 goto out;
920restart_loop:
Trond Myklebust286d7d62006-01-03 09:55:26 +0100921 ops = &nfs4_network_partition_recovery_ops;
922 /* Are there any open files on this volume? */
923 cred = nfs4_get_renew_cred(clp);
924 if (cred != NULL) {
925 /* Yes there are: try to renew the old lease */
926 status = nfs4_proc_renew(clp, cred);
927 switch (status) {
928 case 0:
929 case -NFS4ERR_CB_PATH_DOWN:
930 put_rpccred(cred);
931 goto out;
932 case -NFS4ERR_STALE_CLIENTID:
933 case -NFS4ERR_LEASE_MOVED:
934 ops = &nfs4_reboot_recovery_ops;
935 }
936 } else {
937 /* "reboot" to ensure we clear all state on the server */
938 clp->cl_boot_time = CURRENT_TIME;
939 cred = nfs4_get_setclientid_cred(clp);
940 }
941 /* We're going to have to re-establish a clientid */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700942 nfs4_state_mark_reclaim(clp);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100943 status = -ENOENT;
944 if (cred != NULL) {
945 status = nfs4_init_client(clp, cred);
946 put_rpccred(cred);
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (status)
949 goto out_error;
950 /* Mark all delegations for reclaim */
951 nfs_delegation_mark_reclaim(clp);
952 /* Note: list is protected by exclusive lock on cl->cl_sem */
953 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
954 status = nfs4_reclaim_open_state(ops, sp);
955 if (status < 0) {
956 if (status == -NFS4ERR_NO_GRACE) {
957 ops = &nfs4_network_partition_recovery_ops;
958 status = nfs4_reclaim_open_state(ops, sp);
959 }
960 if (status == -NFS4ERR_STALE_CLIENTID)
961 goto restart_loop;
962 if (status == -NFS4ERR_EXPIRED)
963 goto restart_loop;
964 }
965 }
966 nfs_delegation_reap_unclaimed(clp);
967out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 up_write(&clp->cl_sem);
969 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (status == -NFS4ERR_CB_PATH_DOWN)
971 nfs_handle_cb_pathdown(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100972 nfs4_clear_recover_bit(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 nfs4_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100974 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976out_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);
Trond Myklebust51581f32006-03-20 13:44:47 -0500979 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 goto out;
981}
982
983/*
984 * Local variables:
985 * c-basic-offset: 8
986 * End:
987 */