nfs: patches backported from 2.6 upstream
[tomato.git] / release / src / linux / linux / fs / lockd / clntlock.c
blob6ef9b0d5282bfb12583196c999c2ac651313f968
1 /*
2 * linux/fs/lockd/clntlock.c
4 * Lock handling for the client side NLM implementation
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
9 #define __KERNEL_SYSCALLS__
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/sched.h>
14 #include <linux/nfs_fs.h>
15 #include <linux/unistd.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/svc.h>
18 #include <linux/lockd/lockd.h>
19 #include <linux/smp_lock.h>
21 #define NLMDBG_FACILITY NLMDBG_CLIENT
24 * Local function prototypes
26 static int reclaimer(void *ptr);
29 * The following functions handle blocking and granting from the
30 * client perspective.
34 * This is the representation of a blocked client lock.
36 struct nlm_wait {
37 struct list_head b_list; /* linked list */
38 wait_queue_head_t b_wait; /* where to wait on */
39 struct nlm_host * b_host;
40 struct file_lock * b_lock; /* local file lock */
41 unsigned short b_reclaim; /* got to reclaim lock */
42 u32 b_status; /* grant callback status */
45 static LIST_HEAD(nlm_blocked);
48 * Block on a lock
50 int
51 nlmclnt_block(struct nlm_host *host, struct file_lock *fl, u32 *statp)
53 struct nlm_wait block;
54 int err;
55 u32 pstate;
57 block.b_host = host;
58 block.b_lock = fl;
59 init_waitqueue_head(&block.b_wait);
60 block.b_status = NLM_LCK_BLOCKED;
61 list_add(&block.b_list, &nlm_blocked);
63 /* Remember pseudo nsm state */
64 pstate = host->h_state;
66 /* Go to sleep waiting for GRANT callback. Some servers seem
67 * to lose callbacks, however, so we're going to poll from
68 * time to time just to make sure.
70 * For now, the retry frequency is pretty high; normally
71 * a 1 minute timeout would do. See the comment before
72 * nlmclnt_lock for an explanation.
74 sleep_on_timeout(&block.b_wait, 30*HZ);
76 list_del(&block.b_list);
78 if (!signalled()) {
79 *statp = block.b_status;
80 return 0;
83 /* Okay, we were interrupted. Cancel the pending request
84 * unless the server has rebooted.
86 if (pstate == host->h_state && (err = nlmclnt_cancel(host, fl)) < 0)
87 printk(KERN_NOTICE
88 "lockd: CANCEL call failed (errno %d)\n", -err);
90 return -ERESTARTSYS;
94 * The server lockd has called us back to tell us the lock was granted
96 u32
97 nlmclnt_grant(struct nlm_lock *lock)
99 struct nlm_wait *block;
102 * Look up blocked request based on arguments.
103 * Warning: must not use cookie to match it!
105 list_for_each_entry(block, &nlm_blocked, b_list) {
106 if (nlm_compare_locks(block->b_lock, &lock->fl))
107 break;
110 /* Ooops, no blocked request found. */
111 if (block == NULL)
112 return nlm_lck_denied;
114 /* Alright, we found the lock. Set the return status and
115 * wake up the caller.
117 block->b_status = NLM_LCK_GRANTED;
118 wake_up(&block->b_wait);
120 return nlm_granted;
124 * The following procedures deal with the recovery of locks after a
125 * server crash.
129 * Mark the locks for reclaiming.
130 * FIXME: In 2.5 we don't want to iterate through any global file_lock_list.
131 * Maintain NLM lock reclaiming lists in the nlm_host instead.
133 static
134 void nlmclnt_mark_reclaim(struct nlm_host *host)
136 struct file_lock *fl;
137 struct inode *inode;
138 struct list_head *tmp;
140 list_for_each(tmp, &file_lock_list) {
141 fl = list_entry(tmp, struct file_lock, fl_link);
143 inode = fl->fl_file->f_dentry->d_inode;
144 if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
145 continue;
146 if (fl->fl_u.nfs_fl.host != host)
147 continue;
148 if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_GRANTED))
149 continue;
150 fl->fl_u.nfs_fl.flags |= NFS_LCK_RECLAIM;
155 * Someone has sent us an SM_NOTIFY. Ensure we bind to the new port number,
156 * that we mark locks for reclaiming, and that we bump the pseudo NSM state.
158 static inline
159 void nlmclnt_prepare_reclaim(struct nlm_host *host, u32 newstate)
161 host->h_monitored = 0;
162 host->h_nsmstate = newstate;
163 host->h_state++;
164 host->h_nextrebind = 0;
165 nlm_rebind_host(host);
166 nlmclnt_mark_reclaim(host);
167 dprintk("NLM: reclaiming locks for host %s", host->h_name);
171 * Reclaim all locks on server host. We do this by spawning a separate
172 * reclaimer thread.
174 void
175 nlmclnt_recovery(struct nlm_host *host, u32 newstate)
177 if (host->h_reclaiming++) {
178 if (host->h_nsmstate == newstate)
179 return;
180 nlmclnt_prepare_reclaim(host, newstate);
181 } else {
182 nlmclnt_prepare_reclaim(host, newstate);
183 nlm_get_host(host);
184 MOD_INC_USE_COUNT;
185 if (kernel_thread(reclaimer, host, CLONE_SIGNAL) < 0)
186 MOD_DEC_USE_COUNT;
190 static int
191 reclaimer(void *ptr)
193 struct nlm_host *host = (struct nlm_host *) ptr;
194 struct nlm_wait *block;
195 struct list_head *tmp;
196 struct file_lock *fl;
197 struct inode *inode;
199 daemonize();
200 reparent_to_init();
201 snprintf(current->comm, sizeof(current->comm),
202 "%s-reclaim",
203 host->h_name);
205 /* This one ensures that our parent doesn't terminate while the
206 * reclaim is in progress */
207 lock_kernel();
208 lockd_up();
210 /* First, reclaim all locks that have been marked. */
211 restart:
212 list_for_each(tmp, &file_lock_list) {
213 fl = list_entry(tmp, struct file_lock, fl_link);
215 inode = fl->fl_file->f_dentry->d_inode;
216 if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
217 continue;
218 if (fl->fl_u.nfs_fl.host != host)
219 continue;
220 if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_RECLAIM))
221 continue;
223 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_RECLAIM;
224 nlmclnt_reclaim(host, fl);
225 if (signalled())
226 break;
227 goto restart;
230 host->h_reclaiming = 0;
231 wake_up(&host->h_gracewait);
233 /* Now, wake up all processes that sleep on a blocked lock */
234 list_for_each_entry(block, &nlm_blocked, b_list) {
235 if (block->b_host == host) {
236 block->b_status = NLM_LCK_DENIED_GRACE_PERIOD;
237 wake_up(&block->b_wait);
241 /* Release host handle after use */
242 nlm_release_host(host);
243 lockd_down();
244 unlock_kernel();
245 MOD_DEC_USE_COUNT;
247 return 0;