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>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/time.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/sunrpc/svc.h>
15 #include <linux/lockd/lockd.h>
16 #include <linux/smp_lock.h>
18 #define NLMDBG_FACILITY NLMDBG_CLIENT
21 * Local function prototypes
23 static int reclaimer(void *ptr);
26 * The following functions handle blocking and granting from the
31 * This is the representation of a blocked client lock.
34 struct nlm_wait * b_next; /* linked list */
35 wait_queue_head_t b_wait; /* where to wait on */
36 struct nlm_host * b_host;
37 struct file_lock * b_lock; /* local file lock */
38 unsigned short b_reclaim; /* got to reclaim lock */
39 u32 b_status; /* grant callback status */
42 static struct nlm_wait * nlm_blocked;
48 nlmclnt_block(struct nlm_host *host, struct file_lock *fl, u32 *statp)
50 struct nlm_wait block, **head;
56 init_waitqueue_head(&block.b_wait);
57 block.b_status = NLM_LCK_BLOCKED;
58 block.b_next = nlm_blocked;
61 /* Remember pseudo nsm state */
62 pstate = host->h_state;
64 /* Go to sleep waiting for GRANT callback. Some servers seem
65 * to lose callbacks, however, so we're going to poll from
66 * time to time just to make sure.
68 * For now, the retry frequency is pretty high; normally
69 * a 1 minute timeout would do. See the comment before
70 * nlmclnt_lock for an explanation.
72 sleep_on_timeout(&block.b_wait, 30*HZ);
75 for (head = &nlm_blocked; *head; head = &(*head)->b_next) {
76 if (*head == &block) {
83 *statp = block.b_status;
87 /* Okay, we were interrupted. Cancel the pending request
88 * unless the server has rebooted.
90 if (pstate == host->h_state && (err = nlmclnt_cancel(host, fl)) < 0)
92 "lockd: CANCEL call failed (errno %d)\n", -err);
98 * The server lockd has called us back to tell us the lock was granted
101 nlmclnt_grant(struct nlm_lock *lock)
103 struct nlm_wait *block;
106 * Look up blocked request based on arguments.
107 * Warning: must not use cookie to match it!
109 for (block = nlm_blocked; block; block = block->b_next) {
110 if (nlm_compare_locks(block->b_lock, &lock->fl))
114 /* Ooops, no blocked request found. */
116 return nlm_lck_denied;
118 /* Alright, we found the lock. Set the return status and
119 * wake up the caller.
121 block->b_status = NLM_LCK_GRANTED;
122 wake_up(&block->b_wait);
128 * The following procedures deal with the recovery of locks after a
133 * Mark the locks for reclaiming.
134 * FIXME: In 2.5 we don't want to iterate through any global file_lock_list.
135 * Maintain NLM lock reclaiming lists in the nlm_host instead.
138 void nlmclnt_mark_reclaim(struct nlm_host *host)
140 struct file_lock *fl;
142 struct list_head *tmp;
144 list_for_each(tmp, &file_lock_list) {
145 fl = list_entry(tmp, struct file_lock, fl_link);
147 inode = fl->fl_file->f_dentry->d_inode;
148 if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
150 if (fl->fl_u.nfs_fl.host != host)
152 if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_GRANTED))
154 fl->fl_u.nfs_fl.flags |= NFS_LCK_RECLAIM;
159 * Someone has sent us an SM_NOTIFY. Ensure we bind to the new port number,
160 * that we mark locks for reclaiming, and that we bump the pseudo NSM state.
163 void nlmclnt_prepare_reclaim(struct nlm_host *host, u32 newstate)
165 host->h_monitored = 0;
166 host->h_nsmstate = newstate;
168 host->h_nextrebind = 0;
169 nlm_rebind_host(host);
170 nlmclnt_mark_reclaim(host);
171 dprintk("NLM: reclaiming locks for host %s", host->h_name);
175 * Reclaim all locks on server host. We do this by spawning a separate
179 nlmclnt_recovery(struct nlm_host *host, u32 newstate)
181 if (host->h_reclaiming++) {
182 if (host->h_nsmstate == newstate)
184 nlmclnt_prepare_reclaim(host, newstate);
186 nlmclnt_prepare_reclaim(host, newstate);
188 __module_get(THIS_MODULE);
189 if (kernel_thread(reclaimer, host, CLONE_KERNEL) < 0)
190 module_put(THIS_MODULE);
197 struct nlm_host *host = (struct nlm_host *) ptr;
198 struct nlm_wait *block;
199 struct list_head *tmp;
200 struct file_lock *fl;
203 daemonize("%s-reclaim", host->h_name);
204 allow_signal(SIGKILL);
206 /* This one ensures that our parent doesn't terminate while the
207 * reclaim is in progress */
211 /* First, reclaim all locks that have been marked. */
213 list_for_each(tmp, &file_lock_list) {
214 fl = list_entry(tmp, struct file_lock, fl_link);
216 inode = fl->fl_file->f_dentry->d_inode;
217 if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
219 if (fl->fl_u.nfs_fl.host != host)
221 if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_RECLAIM))
224 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_RECLAIM;
225 nlmclnt_reclaim(host, fl);
231 host->h_reclaiming = 0;
233 /* Now, wake up all processes that sleep on a blocked lock */
234 for (block = nlm_blocked; block; block = block->b_next) {
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);
245 module_put_and_exit(0);