upgrade to linux 2.6.9-1.11_FC2
[linux-2.6.git] / fs / lockd / clntlock.c
1 /*
2  * linux/fs/lockd/clntlock.c
3  *
4  * Lock handling for the client side NLM implementation
5  *
6  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7  */
8
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>
17
18 #define NLMDBG_FACILITY         NLMDBG_CLIENT
19
20 /*
21  * Local function prototypes
22  */
23 static int                      reclaimer(void *ptr);
24
25 /*
26  * The following functions handle blocking and granting from the
27  * client perspective.
28  */
29
30 /*
31  * This is the representation of a blocked client lock.
32  */
33 struct nlm_wait {
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 */
40 };
41
42 static struct nlm_wait *        nlm_blocked;
43
44 /*
45  * Block on a lock
46  */
47 int
48 nlmclnt_block(struct nlm_host *host, struct file_lock *fl, u32 *statp)
49 {
50         struct nlm_wait block, **head;
51         int             err;
52         u32             pstate;
53         wait_queue_t __wait;
54
55         block.b_host   = host;
56         block.b_lock   = fl;
57         block.b_status = NLM_LCK_BLOCKED;
58         init_waitqueue_entry(&__wait, current);
59         init_waitqueue_head(&block.b_wait);
60         add_wait_queue(&block.b_wait, &__wait);
61
62         block.b_next   = nlm_blocked;
63         nlm_blocked    = &block;
64
65
66         /* Remember pseudo nsm state */
67         pstate = host->h_state;
68
69         /* Go to sleep waiting for GRANT callback. Some servers seem
70          * to lose callbacks, however, so we're going to poll from
71          * time to time just to make sure.
72          *
73          * For now, the retry frequency is pretty high; normally 
74          * a 1 minute timeout would do. See the comment before
75          * nlmclnt_lock for an explanation.
76          */
77         set_current_state(TASK_UNINTERRUPTIBLE);
78         schedule_timeout(30*HZ);
79
80         for (head = &nlm_blocked; *head; head = &(*head)->b_next) {
81                 if (*head == &block) {
82                         *head = block.b_next;
83                         break;
84                 }
85         }
86         remove_wait_queue(&block.b_wait, &__wait);
87
88         if (!signalled()) {
89                 *statp = block.b_status;
90                 return 0;
91         }
92
93         /* Okay, we were interrupted. Cancel the pending request
94          * unless the server has rebooted.
95          */
96         if (pstate == host->h_state && (err = nlmclnt_cancel(host, fl)) < 0)
97                 printk(KERN_NOTICE
98                         "lockd: CANCEL call failed (errno %d)\n", -err);
99
100         return -ERESTARTSYS;
101 }
102
103 /*
104  * The server lockd has called us back to tell us the lock was granted
105  */
106 u32
107 nlmclnt_grant(struct nlm_lock *lock)
108 {
109         struct nlm_wait *block;
110
111         /*
112          * Look up blocked request based on arguments. 
113          * Warning: must not use cookie to match it!
114          */
115         for (block = nlm_blocked; block; block = block->b_next) {
116                 if (nlm_compare_locks(block->b_lock, &lock->fl))
117                         break;
118         }
119
120         /* Ooops, no blocked request found. */
121         if (block == NULL)
122                 return nlm_lck_denied;
123
124         /* Alright, we found the lock. Set the return status and
125          * wake up the caller.
126          */
127         block->b_status = NLM_LCK_GRANTED;
128         wake_up(&block->b_wait);
129
130         return nlm_granted;
131 }
132
133 /*
134  * The following procedures deal with the recovery of locks after a
135  * server crash.
136  */
137
138 /*
139  * Mark the locks for reclaiming.
140  * FIXME: In 2.5 we don't want to iterate through any global file_lock_list.
141  *        Maintain NLM lock reclaiming lists in the nlm_host instead.
142  */
143 static
144 void nlmclnt_mark_reclaim(struct nlm_host *host)
145 {
146         struct file_lock *fl;
147         struct inode *inode;
148         struct list_head *tmp;
149
150         list_for_each(tmp, &file_lock_list) {
151                 fl = list_entry(tmp, struct file_lock, fl_link);
152
153                 inode = fl->fl_file->f_dentry->d_inode;
154                 if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
155                         continue;
156                 if (fl->fl_u.nfs_fl.owner->host != host)
157                         continue;
158                 if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_GRANTED))
159                         continue;
160                 fl->fl_u.nfs_fl.flags |= NFS_LCK_RECLAIM;
161         }
162 }
163
164 /*
165  * Someone has sent us an SM_NOTIFY. Ensure we bind to the new port number,
166  * that we mark locks for reclaiming, and that we bump the pseudo NSM state.
167  */
168 static inline
169 void nlmclnt_prepare_reclaim(struct nlm_host *host, u32 newstate)
170 {
171         host->h_monitored = 0;
172         host->h_nsmstate = newstate;
173         host->h_state++;
174         host->h_nextrebind = 0;
175         nlm_rebind_host(host);
176         nlmclnt_mark_reclaim(host);
177         dprintk("NLM: reclaiming locks for host %s\n", host->h_name);
178 }
179
180 /*
181  * Reclaim all locks on server host. We do this by spawning a separate
182  * reclaimer thread.
183  */
184 void
185 nlmclnt_recovery(struct nlm_host *host, u32 newstate)
186 {
187         if (host->h_reclaiming++) {
188                 if (host->h_nsmstate == newstate)
189                         return;
190                 nlmclnt_prepare_reclaim(host, newstate);
191         } else {
192                 nlmclnt_prepare_reclaim(host, newstate);
193                 nlm_get_host(host);
194                 __module_get(THIS_MODULE);
195                 if (kernel_thread(reclaimer, host, CLONE_KERNEL) < 0)
196                         module_put(THIS_MODULE);
197         }
198 }
199
200 static int
201 reclaimer(void *ptr)
202 {
203         struct nlm_host   *host = (struct nlm_host *) ptr;
204         struct nlm_wait   *block;
205         struct list_head *tmp;
206         struct file_lock *fl;
207         struct inode *inode;
208
209         daemonize("%s-reclaim", host->h_name);
210         allow_signal(SIGKILL);
211
212         /* This one ensures that our parent doesn't terminate while the
213          * reclaim is in progress */
214         lock_kernel();
215         lockd_up();
216
217         /* First, reclaim all locks that have been marked. */
218 restart:
219         list_for_each(tmp, &file_lock_list) {
220                 fl = list_entry(tmp, struct file_lock, fl_link);
221
222                 inode = fl->fl_file->f_dentry->d_inode;
223                 if (inode->i_sb->s_magic != NFS_SUPER_MAGIC)
224                         continue;
225                 if (fl->fl_u.nfs_fl.owner->host != host)
226                         continue;
227                 if (!(fl->fl_u.nfs_fl.flags & NFS_LCK_RECLAIM))
228                         continue;
229
230                 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_RECLAIM;
231                 nlmclnt_reclaim(host, fl);
232                 if (signalled())
233                         break;
234                 goto restart;
235         }
236
237         host->h_reclaiming = 0;
238
239         /* Now, wake up all processes that sleep on a blocked lock */
240         for (block = nlm_blocked; block; block = block->b_next) {
241                 if (block->b_host == host) {
242                         block->b_status = NLM_LCK_DENIED_GRACE_PERIOD;
243                         wake_up(&block->b_wait);
244                 }
245         }
246
247         /* Release host handle after use */
248         nlm_release_host(host);
249         lockd_down();
250         unlock_kernel();
251         module_put_and_exit(0);
252 }