blob: 058811e3955505098cf9a242482fa2ac474c4716 [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"
David Howells24c8dbb2006-08-22 20:06:10 -040053#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define OPENOWNER_POOL_SIZE 8
56
Trond Myklebust4ce79712005-06-22 17:16:21 +000057const nfs4_stateid zero_stateid;
58
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{
David Howells7539bba2006-08-22 20:06:09 -040064 server->nfs_client = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 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;
David Howells7539bba2006-08-22 20:06:09 -040073 if (server->nfs_client) {
David Howells24c8dbb2006-08-22 20:06:10 -040074 nfs_put_client(server->nfs_client);
David Howells7539bba2006-08-22 20:06:09 -040075 server->nfs_client = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77}
78
David Howellsadfa6f92006-08-22 20:06:08 -040079static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Trond Myklebust286d7d62006-01-03 09:55:26 +010081 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
82 nfs_callback_tcpport, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (status == 0)
Trond Myklebust286d7d62006-01-03 09:55:26 +010084 status = nfs4_proc_setclientid_confirm(clp, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (status == 0)
86 nfs4_schedule_state_renewal(clp);
87 return status;
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090u32
David Howellsadfa6f92006-08-22 20:06:08 -040091nfs4_alloc_lockowner_id(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 return clp->cl_lockowner_id ++;
94}
95
96static struct nfs4_state_owner *
David Howellsadfa6f92006-08-22 20:06:08 -040097nfs4_client_grab_unused(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct nfs4_state_owner *sp = NULL;
100
101 if (!list_empty(&clp->cl_unused)) {
102 sp = list_entry(clp->cl_unused.next, struct nfs4_state_owner, so_list);
103 atomic_inc(&sp->so_count);
104 sp->so_cred = cred;
105 list_move(&sp->so_list, &clp->cl_state_owners);
106 clp->cl_nunused--;
107 }
108 return sp;
109}
110
David Howellsadfa6f92006-08-22 20:06:08 -0400111struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100112{
113 struct nfs4_state_owner *sp;
114 struct rpc_cred *cred = NULL;
115
116 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
117 if (list_empty(&sp->so_states))
118 continue;
119 cred = get_rpccred(sp->so_cred);
120 break;
121 }
122 return cred;
123}
124
David Howellsadfa6f92006-08-22 20:06:08 -0400125struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100126{
127 struct nfs4_state_owner *sp;
128
129 if (!list_empty(&clp->cl_state_owners)) {
130 sp = list_entry(clp->cl_state_owners.next,
131 struct nfs4_state_owner, so_list);
132 return get_rpccred(sp->so_cred);
133 }
134 return NULL;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct nfs4_state_owner *
David Howellsadfa6f92006-08-22 20:06:08 -0400138nfs4_find_state_owner(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct nfs4_state_owner *sp, *res = NULL;
141
142 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
143 if (sp->so_cred != cred)
144 continue;
145 atomic_inc(&sp->so_count);
146 /* Move to the head of the list */
147 list_move(&sp->so_list, &clp->cl_state_owners);
148 res = sp;
149 break;
150 }
151 return res;
152}
153
154/*
155 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
156 * create a new state_owner.
157 *
158 */
159static struct nfs4_state_owner *
160nfs4_alloc_state_owner(void)
161{
162 struct nfs4_state_owner *sp;
163
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700164 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!sp)
166 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700167 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 INIT_LIST_HEAD(&sp->so_states);
169 INIT_LIST_HEAD(&sp->so_delegations);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700170 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
171 sp->so_seqid.sequence = &sp->so_sequence;
172 spin_lock_init(&sp->so_sequence.lock);
173 INIT_LIST_HEAD(&sp->so_sequence.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 atomic_set(&sp->so_count, 1);
175 return sp;
176}
177
178void
179nfs4_drop_state_owner(struct nfs4_state_owner *sp)
180{
David Howellsadfa6f92006-08-22 20:06:08 -0400181 struct nfs_client *clp = sp->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 spin_lock(&clp->cl_lock);
183 list_del_init(&sp->so_list);
184 spin_unlock(&clp->cl_lock);
185}
186
187/*
188 * Note: must be called with clp->cl_sem held in order to prevent races
189 * with reboot recovery!
190 */
191struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
192{
David Howells7539bba2006-08-22 20:06:09 -0400193 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct nfs4_state_owner *sp, *new;
195
196 get_rpccred(cred);
197 new = nfs4_alloc_state_owner();
198 spin_lock(&clp->cl_lock);
199 sp = nfs4_find_state_owner(clp, cred);
200 if (sp == NULL)
201 sp = nfs4_client_grab_unused(clp, cred);
202 if (sp == NULL && new != NULL) {
203 list_add(&new->so_list, &clp->cl_state_owners);
204 new->so_client = clp;
205 new->so_id = nfs4_alloc_lockowner_id(clp);
206 new->so_cred = cred;
207 sp = new;
208 new = NULL;
209 }
210 spin_unlock(&clp->cl_lock);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800211 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (sp != NULL)
213 return sp;
214 put_rpccred(cred);
215 return NULL;
216}
217
218/*
219 * Must be called with clp->cl_sem held in order to avoid races
220 * with state recovery...
221 */
222void nfs4_put_state_owner(struct nfs4_state_owner *sp)
223{
David Howellsadfa6f92006-08-22 20:06:08 -0400224 struct nfs_client *clp = sp->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 struct rpc_cred *cred = sp->so_cred;
226
227 if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
228 return;
229 if (clp->cl_nunused >= OPENOWNER_POOL_SIZE)
230 goto out_free;
231 if (list_empty(&sp->so_list))
232 goto out_free;
233 list_move(&sp->so_list, &clp->cl_unused);
234 clp->cl_nunused++;
235 spin_unlock(&clp->cl_lock);
236 put_rpccred(cred);
237 cred = NULL;
238 return;
239out_free:
240 list_del(&sp->so_list);
241 spin_unlock(&clp->cl_lock);
242 put_rpccred(cred);
243 kfree(sp);
244}
245
246static struct nfs4_state *
247nfs4_alloc_open_state(void)
248{
249 struct nfs4_state *state;
250
Trond Myklebuste7616922006-01-03 09:55:13 +0100251 state = kzalloc(sizeof(*state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!state)
253 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 atomic_set(&state->count, 1);
255 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000256 spin_lock_init(&state->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return state;
258}
259
Trond Myklebust4cecb762005-11-04 15:32:58 -0500260void
261nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
262{
263 if (state->state == mode)
264 return;
265 /* NB! List reordering - see the reclaim code for why. */
266 if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
267 if (mode & FMODE_WRITE)
268 list_move(&state->open_states, &state->owner->so_states);
269 else
270 list_move_tail(&state->open_states, &state->owner->so_states);
271 }
272 if (mode == 0)
273 list_del_init(&state->inode_states);
274 state->state = mode;
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
279{
280 struct nfs_inode *nfsi = NFS_I(inode);
281 struct nfs4_state *state;
282
283 list_for_each_entry(state, &nfsi->open_states, inode_states) {
284 /* Is this in the process of being freed? */
Trond Myklebust4cecb762005-11-04 15:32:58 -0500285 if (state->state == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 continue;
287 if (state->owner == owner) {
288 atomic_inc(&state->count);
289 return state;
290 }
291 }
292 return NULL;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static void
296nfs4_free_open_state(struct nfs4_state *state)
297{
298 kfree(state);
299}
300
301struct nfs4_state *
302nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
303{
304 struct nfs4_state *state, *new;
305 struct nfs_inode *nfsi = NFS_I(inode);
306
307 spin_lock(&inode->i_lock);
308 state = __nfs4_find_state_byowner(inode, owner);
309 spin_unlock(&inode->i_lock);
310 if (state)
311 goto out;
312 new = nfs4_alloc_open_state();
Trond Myklebustec073422005-10-20 14:22:47 -0700313 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 spin_lock(&inode->i_lock);
315 state = __nfs4_find_state_byowner(inode, owner);
316 if (state == NULL && new != NULL) {
317 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 state->owner = owner;
319 atomic_inc(&owner->so_count);
320 list_add(&state->inode_states, &nfsi->open_states);
321 state->inode = igrab(inode);
322 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700323 /* Note: The reclaim code dictates that we add stateless
324 * and read-only stateids to the end of the list */
325 list_add_tail(&state->open_states, &owner->so_states);
326 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 } else {
328 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700329 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (new)
331 nfs4_free_open_state(new);
332 }
333out:
334 return state;
335}
336
337/*
338 * Beware! Caller must be holding exactly one
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700339 * reference to clp->cl_sem!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341void nfs4_put_open_state(struct nfs4_state *state)
342{
343 struct inode *inode = state->inode;
344 struct nfs4_state_owner *owner = state->owner;
345
Trond Myklebustec073422005-10-20 14:22:47 -0700346 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700348 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!list_empty(&state->inode_states))
350 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700352 spin_unlock(&inode->i_lock);
353 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 nfs4_free_open_state(state);
356 nfs4_put_state_owner(owner);
357}
358
359/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700360 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
362void nfs4_close_state(struct nfs4_state *state, mode_t mode)
363{
364 struct inode *inode = state->inode;
365 struct nfs4_state_owner *owner = state->owner;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500366 int oldstate, newstate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700370 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100372 switch (mode & (FMODE_READ | FMODE_WRITE)) {
373 case FMODE_READ:
374 state->n_rdonly--;
375 break;
376 case FMODE_WRITE:
377 state->n_wronly--;
378 break;
379 case FMODE_READ|FMODE_WRITE:
380 state->n_rdwr--;
381 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500382 oldstate = newstate = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +0100383 if (state->n_rdwr == 0) {
384 if (state->n_rdonly == 0)
385 newstate &= ~FMODE_READ;
386 if (state->n_wronly == 0)
387 newstate &= ~FMODE_WRITE;
388 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500389 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
390 nfs4_state_set_mode_locked(state, newstate);
391 oldstate = newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700394 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500395
396 if (oldstate != newstate && nfs4_do_close(inode, state) == 0)
397 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 nfs4_put_state_owner(owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402/*
403 * Search the state->lock_states for an existing lock_owner
404 * that is compatible with current->files
405 */
406static struct nfs4_lock_state *
407__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
408{
409 struct nfs4_lock_state *pos;
410 list_for_each_entry(pos, &state->lock_states, ls_locks) {
411 if (pos->ls_owner != fl_owner)
412 continue;
413 atomic_inc(&pos->ls_count);
414 return pos;
415 }
416 return NULL;
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*
420 * Return a compatible lock_state. If no initialized lock_state structure
421 * exists, return an uninitialized one.
422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
424static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
425{
426 struct nfs4_lock_state *lsp;
David Howellsadfa6f92006-08-22 20:06:08 -0400427 struct nfs_client *clp = state->owner->so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700429 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (lsp == NULL)
431 return NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700432 lsp->ls_seqid.sequence = &state->owner->so_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 atomic_set(&lsp->ls_count, 1);
434 lsp->ls_owner = fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 spin_lock(&clp->cl_lock);
436 lsp->ls_id = nfs4_alloc_lockowner_id(clp);
437 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000438 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return lsp;
440}
441
442/*
443 * Return a compatible lock_state. If no initialized lock_state structure
444 * exists, return an uninitialized one.
445 *
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700446 * The caller must be holding clp->cl_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000448static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000450 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000452 for(;;) {
453 spin_lock(&state->state_lock);
454 lsp = __nfs4_find_lock_state(state, owner);
455 if (lsp != NULL)
456 break;
457 if (new != NULL) {
458 new->ls_state = state;
459 list_add(&new->ls_locks, &state->lock_states);
460 set_bit(LK_STATE_IN_USE, &state->flags);
461 lsp = new;
462 new = NULL;
463 break;
464 }
465 spin_unlock(&state->state_lock);
466 new = nfs4_alloc_lock_state(state, owner);
467 if (new == NULL)
468 return NULL;
469 }
470 spin_unlock(&state->state_lock);
471 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return lsp;
473}
474
475/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000476 * Release reference to lock_state, and free it if we see that
477 * it is no longer in use
478 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700479void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000480{
481 struct nfs4_state *state;
482
483 if (lsp == NULL)
484 return;
485 state = lsp->ls_state;
486 if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
487 return;
488 list_del(&lsp->ls_locks);
489 if (list_empty(&state->lock_states))
490 clear_bit(LK_STATE_IN_USE, &state->flags);
491 spin_unlock(&state->state_lock);
492 kfree(lsp);
493}
494
495static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
496{
497 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
498
499 dst->fl_u.nfs4_fl.owner = lsp;
500 atomic_inc(&lsp->ls_count);
501}
502
503static void nfs4_fl_release_lock(struct file_lock *fl)
504{
505 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
506}
507
508static struct file_lock_operations nfs4_fl_lock_ops = {
509 .fl_copy_lock = nfs4_fl_copy_lock,
510 .fl_release_private = nfs4_fl_release_lock,
511};
512
513int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
514{
515 struct nfs4_lock_state *lsp;
516
517 if (fl->fl_ops != NULL)
518 return 0;
519 lsp = nfs4_get_lock_state(state, fl->fl_owner);
520 if (lsp == NULL)
521 return -ENOMEM;
522 fl->fl_u.nfs4_fl.owner = lsp;
523 fl->fl_ops = &nfs4_fl_lock_ops;
524 return 0;
525}
526
527/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * Byte-range lock aware utility to initialize the stateid of read/write
529 * requests.
530 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000531void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000533 struct nfs4_lock_state *lsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 memcpy(dst, &state->stateid, sizeof(*dst));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000536 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
537 return;
538
539 spin_lock(&state->state_lock);
540 lsp = __nfs4_find_lock_state(state, fl_owner);
541 if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
542 memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
543 spin_unlock(&state->state_lock);
544 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700547struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Trond Myklebust36f20c62005-11-25 17:09:57 -0500549 struct rpc_sequence *sequence = counter->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700550 struct nfs_seqid *new;
551
552 new = kmalloc(sizeof(*new), GFP_KERNEL);
553 if (new != NULL) {
554 new->sequence = counter;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500555 spin_lock(&sequence->lock);
556 list_add_tail(&new->list, &sequence->list);
557 spin_unlock(&sequence->lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700558 }
559 return new;
560}
561
562void nfs_free_seqid(struct nfs_seqid *seqid)
563{
564 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700565
Trond Myklebust36f20c62005-11-25 17:09:57 -0500566 spin_lock(&sequence->lock);
567 list_del(&seqid->list);
568 spin_unlock(&sequence->lock);
569 rpc_wake_up(&sequence->wait);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700570 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700574 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
575 * failed with a seqid incrementing error -
576 * see comments nfs_fs.h:seqid_mutating_error()
577 */
578static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700580 switch (status) {
581 case 0:
582 break;
583 case -NFS4ERR_BAD_SEQID:
584 case -NFS4ERR_STALE_CLIENTID:
585 case -NFS4ERR_STALE_STATEID:
586 case -NFS4ERR_BAD_STATEID:
587 case -NFS4ERR_BADXDR:
588 case -NFS4ERR_RESOURCE:
589 case -NFS4ERR_NOFILEHANDLE:
590 /* Non-seqid mutating errors */
591 return;
592 };
593 /*
594 * Note: no locking needed as we are guaranteed to be first
595 * on the sequence list
596 */
597 seqid->sequence->counter++;
598}
599
600void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
601{
602 if (status == -NFS4ERR_BAD_SEQID) {
603 struct nfs4_state_owner *sp = container_of(seqid->sequence,
604 struct nfs4_state_owner, so_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 nfs4_drop_state_owner(sp);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700606 }
607 return nfs_increment_seqid(status, seqid);
608}
609
610/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700611 * Increment the seqid if the LOCK/LOCKU succeeded, or
612 * failed with a seqid incrementing error -
613 * see comments nfs_fs.h:seqid_mutating_error()
614 */
615void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
616{
617 return nfs_increment_seqid(status, seqid);
618}
619
620int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
621{
622 struct rpc_sequence *sequence = seqid->sequence->sequence;
623 int status = 0;
624
Trond Myklebust4e513362005-10-20 14:22:41 -0700625 if (sequence->list.next == &seqid->list)
626 goto out;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700627 spin_lock(&sequence->lock);
Trond Myklebust36f20c62005-11-25 17:09:57 -0500628 if (sequence->list.next != &seqid->list) {
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700629 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
630 status = -EAGAIN;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500631 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700632 spin_unlock(&sequence->lock);
Trond Myklebust4e513362005-10-20 14:22:41 -0700633out:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700634 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637static int reclaimer(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David Howellsadfa6f92006-08-22 20:06:08 -0400639static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
Trond Myklebust433fbe42006-01-03 09:55:22 +0100640{
641 smp_mb__before_clear_bit();
642 clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
643 smp_mb__after_clear_bit();
644 wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
645 rpc_wake_up(&clp->cl_rpcwaitq);
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/*
649 * State recovery routine
650 */
David Howellsadfa6f92006-08-22 20:06:08 -0400651static void nfs4_recover_state(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Trond Myklebust5043e902006-01-03 09:55:23 +0100653 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Trond Myklebust5043e902006-01-03 09:55:23 +0100655 __module_get(THIS_MODULE);
656 atomic_inc(&clp->cl_count);
657 task = kthread_run(reclaimer, clp, "%u.%u.%u.%u-reclaim",
David Howells24c8dbb2006-08-22 20:06:10 -0400658 NIPQUAD(clp->cl_addr.sin_addr));
Trond Myklebust5043e902006-01-03 09:55:23 +0100659 if (!IS_ERR(task))
660 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100661 nfs4_clear_recover_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400662 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100663 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666/*
667 * Schedule a state recovery attempt
668 */
David Howellsadfa6f92006-08-22 20:06:08 -0400669void nfs4_schedule_state_recovery(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 if (!clp)
672 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100673 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Trond Myklebust5043e902006-01-03 09:55:23 +0100674 nfs4_recover_state(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
678{
679 struct inode *inode = state->inode;
680 struct file_lock *fl;
681 int status = 0;
682
683 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
Trond Myklebust43b2a332005-11-04 15:35:30 -0500684 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 continue;
686 if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
687 continue;
688 status = ops->recover_lock(state, fl);
689 if (status >= 0)
690 continue;
691 switch (status) {
692 default:
693 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
694 __FUNCTION__, status);
695 case -NFS4ERR_EXPIRED:
696 case -NFS4ERR_NO_GRACE:
697 case -NFS4ERR_RECLAIM_BAD:
698 case -NFS4ERR_RECLAIM_CONFLICT:
Trond Myklebust43b2a332005-11-04 15:35:30 -0500699 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
701 case -NFS4ERR_STALE_CLIENTID:
702 goto out_err;
703 }
704 }
705 return 0;
706out_err:
707 return status;
708}
709
710static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
711{
712 struct nfs4_state *state;
713 struct nfs4_lock_state *lock;
714 int status = 0;
715
716 /* Note: we rely on the sp->so_states list being ordered
717 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
718 * states first.
719 * This is needed to ensure that the server won't give us any
720 * read delegations that we have to return if, say, we are
721 * recovering after a network partition or a reboot from a
722 * server that doesn't support a grace period.
723 */
724 list_for_each_entry(state, &sp->so_states, open_states) {
725 if (state->state == 0)
726 continue;
727 status = ops->recover_open(sp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (status >= 0) {
729 status = nfs4_reclaim_locks(ops, state);
730 if (status < 0)
731 goto out_err;
732 list_for_each_entry(lock, &state->lock_states, ls_locks) {
733 if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
734 printk("%s: Lock reclaim failed!\n",
735 __FUNCTION__);
736 }
737 continue;
738 }
739 switch (status) {
740 default:
741 printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
742 __FUNCTION__, status);
743 case -ENOENT:
744 case -NFS4ERR_RECLAIM_BAD:
745 case -NFS4ERR_RECLAIM_CONFLICT:
746 /*
747 * Open state on this file cannot be recovered
748 * All we can do is revert to using the zero stateid.
749 */
750 memset(state->stateid.data, 0,
751 sizeof(state->stateid.data));
752 /* Mark the file as being 'closed' */
753 state->state = 0;
754 break;
755 case -NFS4ERR_EXPIRED:
756 case -NFS4ERR_NO_GRACE:
757 case -NFS4ERR_STALE_CLIENTID:
758 goto out_err;
759 }
760 }
761 return 0;
762out_err:
763 return status;
764}
765
David Howellsadfa6f92006-08-22 20:06:08 -0400766static void nfs4_state_mark_reclaim(struct nfs_client *clp)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700767{
768 struct nfs4_state_owner *sp;
769 struct nfs4_state *state;
770 struct nfs4_lock_state *lock;
771
772 /* Reset all sequence ids to zero */
773 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
774 sp->so_seqid.counter = 0;
775 sp->so_seqid.flags = 0;
Trond Myklebustec073422005-10-20 14:22:47 -0700776 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700777 list_for_each_entry(state, &sp->so_states, open_states) {
778 list_for_each_entry(lock, &state->lock_states, ls_locks) {
779 lock->ls_seqid.counter = 0;
780 lock->ls_seqid.flags = 0;
781 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
782 }
783 }
Trond Myklebustec073422005-10-20 14:22:47 -0700784 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700785 }
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788static int reclaimer(void *ptr)
789{
David Howellsadfa6f92006-08-22 20:06:08 -0400790 struct nfs_client *clp = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct nfs4_state_owner *sp;
792 struct nfs4_state_recovery_ops *ops;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100793 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int status = 0;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 allow_signal(SIGKILL);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* Ensure exclusive access to NFSv4 state */
799 lock_kernel();
800 down_write(&clp->cl_sem);
801 /* Are there any NFS mounts out there? */
802 if (list_empty(&clp->cl_superblocks))
803 goto out;
804restart_loop:
Trond Myklebust286d7d62006-01-03 09:55:26 +0100805 ops = &nfs4_network_partition_recovery_ops;
806 /* Are there any open files on this volume? */
807 cred = nfs4_get_renew_cred(clp);
808 if (cred != NULL) {
809 /* Yes there are: try to renew the old lease */
810 status = nfs4_proc_renew(clp, cred);
811 switch (status) {
812 case 0:
813 case -NFS4ERR_CB_PATH_DOWN:
814 put_rpccred(cred);
815 goto out;
816 case -NFS4ERR_STALE_CLIENTID:
817 case -NFS4ERR_LEASE_MOVED:
818 ops = &nfs4_reboot_recovery_ops;
819 }
820 } else {
821 /* "reboot" to ensure we clear all state on the server */
822 clp->cl_boot_time = CURRENT_TIME;
823 cred = nfs4_get_setclientid_cred(clp);
824 }
825 /* We're going to have to re-establish a clientid */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700826 nfs4_state_mark_reclaim(clp);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100827 status = -ENOENT;
828 if (cred != NULL) {
829 status = nfs4_init_client(clp, cred);
830 put_rpccred(cred);
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (status)
833 goto out_error;
834 /* Mark all delegations for reclaim */
835 nfs_delegation_mark_reclaim(clp);
836 /* Note: list is protected by exclusive lock on cl->cl_sem */
837 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
838 status = nfs4_reclaim_open_state(ops, sp);
839 if (status < 0) {
840 if (status == -NFS4ERR_NO_GRACE) {
841 ops = &nfs4_network_partition_recovery_ops;
842 status = nfs4_reclaim_open_state(ops, sp);
843 }
844 if (status == -NFS4ERR_STALE_CLIENTID)
845 goto restart_loop;
846 if (status == -NFS4ERR_EXPIRED)
847 goto restart_loop;
848 }
849 }
850 nfs_delegation_reap_unclaimed(clp);
851out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 up_write(&clp->cl_sem);
853 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (status == -NFS4ERR_CB_PATH_DOWN)
855 nfs_handle_cb_pathdown(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100856 nfs4_clear_recover_bit(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400857 nfs_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100858 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 0;
860out_error:
861 printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
David Howells24c8dbb2006-08-22 20:06:10 -0400862 NIPQUAD(clp->cl_addr.sin_addr), -status);
Trond Myklebust51581f32006-03-20 13:44:47 -0500863 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto out;
865}
866
867/*
868 * Local variables:
869 * c-basic-offset: 8
870 * End:
871 */