blob: afad0255e7db58ac24118dad07af597ce9d88386 [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
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>
Trond Myklebust5043e902006-01-03 09:55:23 +010046#include <linux/kthread.h>
47#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/workqueue.h>
49#include <linux/bitops.h>
50
Trond Myklebust4ce79712005-06-22 17:16:21 +000051#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "callback.h"
53#include "delegation.h"
54
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 DEFINE_SPINLOCK(state_spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static LIST_HEAD(nfs4_clientid_list);
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062void
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{
Jesper Juhlf99d49a2005-11-07 01:01:34 -080072 kfree(server->mnt_path);
73 server->mnt_path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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;
Trond Myklebust286d7d62006-01-03 09:55:26 +010094 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 nfs_callback_down();
96 return NULL;
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
106 INIT_LIST_HEAD(&clp->cl_superblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS4 client");
J. Bruce Fields6a192752005-06-22 17:16:23 +0000108 clp->cl_rpcclient = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 clp->cl_boot_time = CURRENT_TIME;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100110 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 nfs_idmap_delete(clp);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000128 if (!IS_ERR(clp->cl_rpcclient))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 rpc_wake_up(&clp->cl_rpcwaitq);
192 nfs4_kill_renewd(clp);
193 nfs4_free_client(clp);
194}
195
Trond Myklebust286d7d62006-01-03 09:55:26 +0100196static int nfs4_init_client(struct nfs4_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Trond Myklebust286d7d62006-01-03 09:55:26 +0100198 int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
199 nfs_callback_tcpport, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (status == 0)
Trond Myklebust286d7d62006-01-03 09:55:26 +0100201 status = nfs4_proc_setclientid_confirm(clp, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (status == 0)
203 nfs4_schedule_state_renewal(clp);
204 return status;
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207u32
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
Trond Myklebustb4454fe2006-01-03 09:55:25 +0100228struct 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
Trond Myklebust286d7d62006-01-03 09:55:26 +0100242struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static 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
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700281 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!sp)
283 return NULL;
Trond Myklebustec073422005-10-20 14:22:47 -0700284 spin_lock_init(&sp->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 INIT_LIST_HEAD(&sp->so_states);
286 INIT_LIST_HEAD(&sp->so_delegations);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700287 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 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);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800328 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 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
Trond Myklebuste7616922006-01-03 09:55:13 +0100368 state = kzalloc(sizeof(*state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (!state)
370 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 atomic_set(&state->count, 1);
372 INIT_LIST_HEAD(&state->lock_states);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000373 spin_lock_init(&state->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return state;
375}
376
Trond Myklebust4cecb762005-11-04 15:32:58 -0500377void
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static struct nfs4_state *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395__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? */
Trond Myklebust4cecb762005-11-04 15:32:58 -0500402 if (state->state == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 continue;
404 if (state->owner == owner) {
405 atomic_inc(&state->count);
406 return state;
407 }
408 }
409 return NULL;
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static 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();
Trond Myklebustec073422005-10-20 14:22:47 -0700430 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 spin_lock(&inode->i_lock);
432 state = __nfs4_find_state_byowner(inode, owner);
433 if (state == NULL && new != NULL) {
434 state = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 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);
Trond Myklebustec073422005-10-20 14:22:47 -0700440 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else {
445 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700446 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (new)
448 nfs4_free_open_state(new);
449 }
450out:
451 return state;
452}
453
454/*
455 * Beware! Caller must be holding exactly one
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700456 * reference to clp->cl_sem!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
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
Trond Myklebustec073422005-10-20 14:22:47 -0700463 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return;
Trond Myklebustec073422005-10-20 14:22:47 -0700465 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!list_empty(&state->inode_states))
467 list_del(&state->inode_states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 list_del(&state->open_states);
Trond Myklebustec073422005-10-20 14:22:47 -0700469 spin_unlock(&inode->i_lock);
470 spin_unlock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 nfs4_free_open_state(state);
473 nfs4_put_state_owner(owner);
474}
475
476/*
Trond Myklebust83c9d412005-10-18 14:20:13 -0700477 * Close the current file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
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;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500483 int oldstate, newstate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 atomic_inc(&owner->so_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* Protect against nfs4_find_state() */
Trond Myklebustec073422005-10-20 14:22:47 -0700487 spin_lock(&owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 spin_lock(&inode->i_lock);
Trond Myklebuste7616922006-01-03 09:55:13 +0100489 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 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500499 oldstate = newstate = state->state;
Trond Myklebuste7616922006-01-03 09:55:13 +0100500 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 }
Trond Myklebust4cecb762005-11-04 15:32:58 -0500506 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
507 nfs4_state_set_mode_locked(state, newstate);
508 oldstate = newstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700511 spin_unlock(&owner->so_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500512
513 if (oldstate != newstate && nfs4_do_close(inode, state) == 0)
514 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 nfs4_put_state_owner(owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/*
537 * Return a compatible lock_state. If no initialized lock_state structure
538 * exists, return an uninitialized one.
539 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
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
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700546 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (lsp == NULL)
548 return NULL;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700549 lsp->ls_seqid.sequence = &state->owner->so_sequence;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 atomic_set(&lsp->ls_count, 1);
551 lsp->ls_owner = fl_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 spin_lock(&clp->cl_lock);
553 lsp->ls_id = nfs4_alloc_lockowner_id(clp);
554 spin_unlock(&clp->cl_lock);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000555 INIT_LIST_HEAD(&lsp->ls_locks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return lsp;
557}
558
559/*
560 * Return a compatible lock_state. If no initialized lock_state structure
561 * exists, return an uninitialized one.
562 *
Trond Myklebuste6dfa552005-10-18 14:20:13 -0700563 * The caller must be holding clp->cl_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000565static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000567 struct nfs4_lock_state *lsp, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000569 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return lsp;
590}
591
592/*
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000593 * Release reference to lock_state, and free it if we see that
594 * it is no longer in use
595 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -0700596void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000597{
598 struct nfs4_state *state;
599
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);
610}
611
612static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
613{
614 struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
615
616 dst->fl_u.nfs4_fl.owner = lsp;
617 atomic_inc(&lsp->ls_count);
618}
619
620static void nfs4_fl_release_lock(struct file_lock *fl)
621{
622 nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
623}
624
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)
631{
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;
642}
643
644/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * Byte-range lock aware utility to initialize the stateid of read/write
646 * requests.
647 */
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000648void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000650 struct nfs4_lock_state *lsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 memcpy(dst, &state->stateid, sizeof(*dst));
Trond Myklebust8d0a8a92005-06-22 17:16:32 +0000653 if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
654 return;
655
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);
661 nfs4_put_lock_state(lsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700664struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Trond Myklebust36f20c62005-11-25 17:09:57 -0500666 struct rpc_sequence *sequence = counter->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700667 struct nfs_seqid *new;
668
669 new = kmalloc(sizeof(*new), GFP_KERNEL);
670 if (new != NULL) {
671 new->sequence = counter;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500672 spin_lock(&sequence->lock);
673 list_add_tail(&new->list, &sequence->list);
674 spin_unlock(&sequence->lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700675 }
676 return new;
677}
678
679void nfs_free_seqid(struct nfs_seqid *seqid)
680{
681 struct rpc_sequence *sequence = seqid->sequence->sequence;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700682
Trond Myklebust36f20c62005-11-25 17:09:57 -0500683 spin_lock(&sequence->lock);
684 list_del(&seqid->list);
685 spin_unlock(&sequence->lock);
686 rpc_wake_up(&sequence->wait);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700687 kfree(seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700691 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700697 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 nfs4_drop_state_owner(sp);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700723 }
724 return nfs_increment_seqid(status, seqid);
725}
726
727/*
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700728 * 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
Trond Myklebust4e513362005-10-20 14:22:41 -0700742 if (sequence->list.next == &seqid->list)
743 goto out;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700744 spin_lock(&sequence->lock);
Trond Myklebust36f20c62005-11-25 17:09:57 -0500745 if (sequence->list.next != &seqid->list) {
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700746 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
747 status = -EAGAIN;
Trond Myklebust36f20c62005-11-25 17:09:57 -0500748 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700749 spin_unlock(&sequence->lock);
Trond Myklebust4e513362005-10-20 14:22:41 -0700750out:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700751 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754static int reclaimer(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Trond Myklebust433fbe42006-01-03 09:55:22 +0100756static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/*
766 * State recovery routine
767 */
Trond Myklebust5043e902006-01-03 09:55:23 +0100768static void nfs4_recover_state(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Trond Myklebust5043e902006-01-03 09:55:23 +0100770 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Trond Myklebust5043e902006-01-03 09:55:23 +0100772 __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;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100778 nfs4_clear_recover_bit(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100779 nfs4_put_client(clp);
780 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783/*
784 * Schedule a state recovery attempt
785 */
Trond Myklebust5043e902006-01-03 09:55:23 +0100786void nfs4_schedule_state_recovery(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 if (!clp)
789 return;
Trond Myklebust433fbe42006-01-03 09:55:22 +0100790 if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
Trond Myklebust5043e902006-01-03 09:55:23 +0100791 nfs4_recover_state(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
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) {
Trond Myklebust43b2a332005-11-04 15:35:30 -0500801 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 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:
Trond Myklebust43b2a332005-11-04 15:35:30 -0500816 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 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
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700883static 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;
Trond Myklebustec073422005-10-20 14:22:47 -0700893 spin_lock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700894 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 }
Trond Myklebustec073422005-10-20 14:22:47 -0700901 spin_unlock(&sp->so_lock);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700902 }
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905static int reclaimer(void *ptr)
906{
Trond Myklebust5043e902006-01-03 09:55:23 +0100907 struct nfs4_client *clp = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct nfs4_state_owner *sp;
909 struct nfs4_state_recovery_ops *ops;
Trond Myklebust286d7d62006-01-03 09:55:26 +0100910 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 int status = 0;
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 allow_signal(SIGKILL);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* 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:
Trond Myklebust286d7d62006-01-03 09:55:26 +0100922 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 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700943 nfs4_state_mark_reclaim(clp);
Trond Myklebust286d7d62006-01-03 09:55:26 +0100944 status = -ENOENT;
945 if (cred != NULL) {
946 status = nfs4_init_client(clp, cred);
947 put_rpccred(cred);
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 up_write(&clp->cl_sem);
970 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (status == -NFS4ERR_CB_PATH_DOWN)
972 nfs_handle_cb_pathdown(clp);
Trond Myklebust433fbe42006-01-03 09:55:22 +0100973 nfs4_clear_recover_bit(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 nfs4_put_client(clp);
Trond Myklebust5043e902006-01-03 09:55:23 +0100975 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 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 */