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>
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);
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);
67 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
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)
82 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
85 /* if the vnode's data version number changed then its contents
87 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
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
97 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
99 queue_work(afs_callback_update_worker, &vnode->cb_broken_work);
105 * actually break a callback
107 static void afs_break_callback(struct afs_server *server,
108 struct afs_vnode *vnode)
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
134 * - the backing file is changed
135 * - a lock is released
137 static void afs_break_one_callback(struct afs_server *server,
140 struct afs_vnode *vnode;
144 spin_lock(&server->fs_lock);
145 p = server->fs_vnodes.rb_node;
147 vnode = rb_entry(p, struct afs_vnode, server_rb);
148 if (fid->vid < vnode->fid.vid)
150 else if (fid->vid > vnode->fid.vid)
152 else if (fid->vnode < vnode->fid.vnode)
154 else if (fid->vnode > vnode->fid.vnode)
156 else if (fid->unique < vnode->fid.unique)
158 else if (fid->unique > vnode->fid.unique)
164 /* not found so we just ignore it (it may have moved to another
168 spin_unlock(&server->fs_lock);
174 ASSERTCMP(server, ==, vnode->server);
176 if (!igrab(AFS_VNODE_TO_I(vnode)))
178 spin_unlock(&server->fs_lock);
180 afs_break_callback(server, vnode);
181 iput(&vnode->vfs_inode);
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 }",
199 callbacks->fid.vnode,
200 callbacks->fid.unique,
205 afs_break_one_callback(server, &callbacks->fid);
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;
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);
241 afs_flush_callback_breaks(server);
247 ASSERT(server->cb_promises.rb_node != NULL);
248 rb_erase(&vnode->cb_promise, &server->cb_promises);
249 vnode->cb_promised = false;
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]");
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);
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]");
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);
304 set_current_state(TASK_UNINTERRUPTIBLE);
305 if (!vnode->cb_promised ||
306 afs_breakring_space(server) != 0)
308 spin_unlock(&server->cb_lock);
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);
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);
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
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);
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;
365 server = container_of(work, struct afs_server, updater);
371 /* find the first vnode to update */
372 spin_lock(&server->cb_lock);
374 if (RB_EMPTY_ROOT(&server->cb_promises)) {
375 spin_unlock(&server->cb_lock);
376 _leave(" [nothing]");
380 vnode = rb_entry(rb_first(&server->cb_promises),
381 struct afs_vnode, cb_promise);
382 if (atomic_read(&vnode->usage) > 0)
384 rb_erase(&vnode->cb_promise, &server->cb_promises);
385 vnode->cb_promised = false;
388 timeout = vnode->update_at - now;
390 queue_delayed_work(afs_vnode_update_worker,
391 &afs_vnode_update, timeout * HZ);
392 spin_unlock(&server->cb_lock);
393 _leave(" [nothing]");
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);
410 afs_vnode_apply_update(vl, &vldb);
411 vnode->state = AFS_VL_UPDATING;
414 vnode->state = AFS_VL_VOLUME_DELETED;
417 vnode->state = AFS_VL_UNCERTAIN;
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;
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);
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);