AFS: AFS fixups
[linux-2.6/verdex.git] / fs / afs / callback.c
blobf64e40fefc02841d8c9625e556780e963c0f2090
1 /*
2 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
7 * You should have received a copy of the GNU General Public License
8 * along with this program; if not, write to the Free Software
9 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11 * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
12 * David Howells <dhowells@redhat.com>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/circ_buf.h>
20 #include "internal.h"
22 unsigned afs_vnode_update_timeout = 10;
24 #define afs_breakring_space(server) \
25 CIRC_SPACE((server)->cb_break_head, (server)->cb_break_tail, \
26 ARRAY_SIZE((server)->cb_break))
28 //static void afs_callback_updater(struct work_struct *);
30 static struct workqueue_struct *afs_callback_update_worker;
33 * allow the fileserver to request callback state (re-)initialisation
35 void afs_init_callback_state(struct afs_server *server)
37 struct afs_vnode *vnode;
39 _enter("{%p}", server);
41 spin_lock(&server->cb_lock);
43 /* kill all the promises on record from this server */
44 while (!RB_EMPTY_ROOT(&server->cb_promises)) {
45 vnode = rb_entry(server->cb_promises.rb_node,
46 struct afs_vnode, cb_promise);
47 _debug("UNPROMISE { vid=%x:%u uq=%u}",
48 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
49 rb_erase(&vnode->cb_promise, &server->cb_promises);
50 vnode->cb_promised = false;
53 spin_unlock(&server->cb_lock);
54 _leave("");
58 * handle the data invalidation side of a callback being broken
60 void afs_broken_callback_work(struct work_struct *work)
62 struct afs_vnode *vnode =
63 container_of(work, struct afs_vnode, cb_broken_work);
65 _enter("");
67 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
68 return;
70 /* we're only interested in dealing with a broken callback on *this*
71 * vnode and only if no-one else has dealt with it yet */
72 if (!mutex_trylock(&vnode->validate_lock))
73 return; /* someone else is dealing with it */
75 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
76 if (S_ISDIR(vnode->vfs_inode.i_mode))
77 afs_clear_permits(vnode);
79 if (afs_vnode_fetch_status(vnode, NULL, NULL) < 0)
80 goto out;
82 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
83 goto out;
85 /* if the vnode's data version number changed then its contents
86 * are different */
87 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
88 afs_zap_data(vnode);
91 out:
92 mutex_unlock(&vnode->validate_lock);
94 /* avoid the potential race whereby the mutex_trylock() in this
95 * function happens again between the clear_bit() and the
96 * mutex_unlock() */
97 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
98 _debug("requeue");
99 queue_work(afs_callback_update_worker, &vnode->cb_broken_work);
101 _leave("");
105 * actually break a callback
107 static void afs_break_callback(struct afs_server *server,
108 struct afs_vnode *vnode)
110 _enter("");
112 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
114 if (vnode->cb_promised) {
115 spin_lock(&vnode->lock);
117 _debug("break callback");
119 spin_lock(&server->cb_lock);
120 if (vnode->cb_promised) {
121 rb_erase(&vnode->cb_promise, &server->cb_promises);
122 vnode->cb_promised = false;
124 spin_unlock(&server->cb_lock);
126 queue_work(afs_callback_update_worker, &vnode->cb_broken_work);
127 spin_unlock(&vnode->lock);
132 * allow the fileserver to explicitly break one callback
133 * - happens when
134 * - the backing file is changed
135 * - a lock is released
137 static void afs_break_one_callback(struct afs_server *server,
138 struct afs_fid *fid)
140 struct afs_vnode *vnode;
141 struct rb_node *p;
143 _debug("find");
144 spin_lock(&server->fs_lock);
145 p = server->fs_vnodes.rb_node;
146 while (p) {
147 vnode = rb_entry(p, struct afs_vnode, server_rb);
148 if (fid->vid < vnode->fid.vid)
149 p = p->rb_left;
150 else if (fid->vid > vnode->fid.vid)
151 p = p->rb_right;
152 else if (fid->vnode < vnode->fid.vnode)
153 p = p->rb_left;
154 else if (fid->vnode > vnode->fid.vnode)
155 p = p->rb_right;
156 else if (fid->unique < vnode->fid.unique)
157 p = p->rb_left;
158 else if (fid->unique > vnode->fid.unique)
159 p = p->rb_right;
160 else
161 goto found;
164 /* not found so we just ignore it (it may have moved to another
165 * server) */
166 not_available:
167 _debug("not avail");
168 spin_unlock(&server->fs_lock);
169 _leave("");
170 return;
172 found:
173 _debug("found");
174 ASSERTCMP(server, ==, vnode->server);
176 if (!igrab(AFS_VNODE_TO_I(vnode)))
177 goto not_available;
178 spin_unlock(&server->fs_lock);
180 afs_break_callback(server, vnode);
181 iput(&vnode->vfs_inode);
182 _leave("");
186 * allow the fileserver to break callback promises
188 void afs_break_callbacks(struct afs_server *server, size_t count,
189 struct afs_callback callbacks[])
191 _enter("%p,%zu,", server, count);
193 ASSERT(server != NULL);
194 ASSERTCMP(count, <=, AFSCBMAX);
196 for (; count > 0; callbacks++, count--) {
197 _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }",
198 callbacks->fid.vid,
199 callbacks->fid.vnode,
200 callbacks->fid.unique,
201 callbacks->version,
202 callbacks->expiry,
203 callbacks->type
205 afs_break_one_callback(server, &callbacks->fid);
208 _leave("");
209 return;
213 * record the callback for breaking
214 * - the caller must hold server->cb_lock
216 static void afs_do_give_up_callback(struct afs_server *server,
217 struct afs_vnode *vnode)
219 struct afs_callback *cb;
221 _enter("%p,%p", server, vnode);
223 cb = &server->cb_break[server->cb_break_head];
224 cb->fid = vnode->fid;
225 cb->version = vnode->cb_version;
226 cb->expiry = vnode->cb_expiry;
227 cb->type = vnode->cb_type;
228 smp_wmb();
229 server->cb_break_head =
230 (server->cb_break_head + 1) &
231 (ARRAY_SIZE(server->cb_break) - 1);
233 /* defer the breaking of callbacks to try and collect as many as
234 * possible to ship in one operation */
235 switch (atomic_inc_return(&server->cb_break_n)) {
236 case 1 ... AFSCBMAX - 1:
237 queue_delayed_work(afs_callback_update_worker,
238 &server->cb_break_work, HZ * 2);
239 break;
240 case AFSCBMAX:
241 afs_flush_callback_breaks(server);
242 break;
243 default:
244 break;
247 ASSERT(server->cb_promises.rb_node != NULL);
248 rb_erase(&vnode->cb_promise, &server->cb_promises);
249 vnode->cb_promised = false;
250 _leave("");
254 * discard the callback on a deleted item
256 void afs_discard_callback_on_delete(struct afs_vnode *vnode)
258 struct afs_server *server = vnode->server;
260 _enter("%d", vnode->cb_promised);
262 if (!vnode->cb_promised) {
263 _leave(" [not promised]");
264 return;
267 ASSERT(server != NULL);
269 spin_lock(&server->cb_lock);
270 if (vnode->cb_promised) {
271 ASSERT(server->cb_promises.rb_node != NULL);
272 rb_erase(&vnode->cb_promise, &server->cb_promises);
273 vnode->cb_promised = false;
275 spin_unlock(&server->cb_lock);
276 _leave("");
280 * give up the callback registered for a vnode on the file server when the
281 * inode is being cleared
283 void afs_give_up_callback(struct afs_vnode *vnode)
285 struct afs_server *server = vnode->server;
287 DECLARE_WAITQUEUE(myself, current);
289 _enter("%d", vnode->cb_promised);
291 _debug("GIVE UP INODE %p", &vnode->vfs_inode);
293 if (!vnode->cb_promised) {
294 _leave(" [not promised]");
295 return;
298 ASSERT(server != NULL);
300 spin_lock(&server->cb_lock);
301 if (vnode->cb_promised && afs_breakring_space(server) == 0) {
302 add_wait_queue(&server->cb_break_waitq, &myself);
303 for (;;) {
304 set_current_state(TASK_UNINTERRUPTIBLE);
305 if (!vnode->cb_promised ||
306 afs_breakring_space(server) != 0)
307 break;
308 spin_unlock(&server->cb_lock);
309 schedule();
310 spin_lock(&server->cb_lock);
312 remove_wait_queue(&server->cb_break_waitq, &myself);
313 __set_current_state(TASK_RUNNING);
316 /* of course, it's always possible for the server to break this vnode's
317 * callback first... */
318 if (vnode->cb_promised)
319 afs_do_give_up_callback(server, vnode);
321 spin_unlock(&server->cb_lock);
322 _leave("");
326 * dispatch a deferred give up callbacks operation
328 void afs_dispatch_give_up_callbacks(struct work_struct *work)
330 struct afs_server *server =
331 container_of(work, struct afs_server, cb_break_work.work);
333 _enter("");
335 /* tell the fileserver to discard the callback promises it has
336 * - in the event of ENOMEM or some other error, we just forget that we
337 * had callbacks entirely, and the server will call us later to break
338 * them
340 afs_fs_give_up_callbacks(server, &afs_async_call);
344 * flush the outstanding callback breaks on a server
346 void afs_flush_callback_breaks(struct afs_server *server)
348 cancel_delayed_work(&server->cb_break_work);
349 queue_delayed_work(afs_callback_update_worker,
350 &server->cb_break_work, 0);
353 #if 0
355 * update a bunch of callbacks
357 static void afs_callback_updater(struct work_struct *work)
359 struct afs_server *server;
360 struct afs_vnode *vnode, *xvnode;
361 time_t now;
362 long timeout;
363 int ret;
365 server = container_of(work, struct afs_server, updater);
367 _enter("");
369 now = get_seconds();
371 /* find the first vnode to update */
372 spin_lock(&server->cb_lock);
373 for (;;) {
374 if (RB_EMPTY_ROOT(&server->cb_promises)) {
375 spin_unlock(&server->cb_lock);
376 _leave(" [nothing]");
377 return;
380 vnode = rb_entry(rb_first(&server->cb_promises),
381 struct afs_vnode, cb_promise);
382 if (atomic_read(&vnode->usage) > 0)
383 break;
384 rb_erase(&vnode->cb_promise, &server->cb_promises);
385 vnode->cb_promised = false;
388 timeout = vnode->update_at - now;
389 if (timeout > 0) {
390 queue_delayed_work(afs_vnode_update_worker,
391 &afs_vnode_update, timeout * HZ);
392 spin_unlock(&server->cb_lock);
393 _leave(" [nothing]");
394 return;
397 list_del_init(&vnode->update);
398 atomic_inc(&vnode->usage);
399 spin_unlock(&server->cb_lock);
401 /* we can now perform the update */
402 _debug("update %s", vnode->vldb.name);
403 vnode->state = AFS_VL_UPDATING;
404 vnode->upd_rej_cnt = 0;
405 vnode->upd_busy_cnt = 0;
407 ret = afs_vnode_update_record(vl, &vldb);
408 switch (ret) {
409 case 0:
410 afs_vnode_apply_update(vl, &vldb);
411 vnode->state = AFS_VL_UPDATING;
412 break;
413 case -ENOMEDIUM:
414 vnode->state = AFS_VL_VOLUME_DELETED;
415 break;
416 default:
417 vnode->state = AFS_VL_UNCERTAIN;
418 break;
421 /* and then reschedule */
422 _debug("reschedule");
423 vnode->update_at = get_seconds() + afs_vnode_update_timeout;
425 spin_lock(&server->cb_lock);
427 if (!list_empty(&server->cb_promises)) {
428 /* next update in 10 minutes, but wait at least 1 second more
429 * than the newest record already queued so that we don't spam
430 * the VL server suddenly with lots of requests
432 xvnode = list_entry(server->cb_promises.prev,
433 struct afs_vnode, update);
434 if (vnode->update_at <= xvnode->update_at)
435 vnode->update_at = xvnode->update_at + 1;
436 xvnode = list_entry(server->cb_promises.next,
437 struct afs_vnode, update);
438 timeout = xvnode->update_at - now;
439 if (timeout < 0)
440 timeout = 0;
441 } else {
442 timeout = afs_vnode_update_timeout;
445 list_add_tail(&vnode->update, &server->cb_promises);
447 _debug("timeout %ld", timeout);
448 queue_delayed_work(afs_vnode_update_worker,
449 &afs_vnode_update, timeout * HZ);
450 spin_unlock(&server->cb_lock);
451 afs_put_vnode(vl);
453 #endif
456 * initialise the callback update process
458 int __init afs_callback_update_init(void)
460 afs_callback_update_worker =
461 create_singlethread_workqueue("kafs_callbackd");
462 return afs_callback_update_worker ? 0 : -ENOMEM;
466 * shut down the callback update process
468 void afs_callback_update_kill(void)
470 destroy_workqueue(afs_callback_update_worker);