blob: bacf518c6fa80d891f24e1168a82abf1a48d2d62 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Howells08e0e7c2007-04-26 15:55:03 -07002 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
6 *
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.
10 *
11 * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
12 * David Howells <dhowells@redhat.com>
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
David Howells08e0e7c2007-04-26 15:55:03 -070019#include <linux/circ_buf.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040020#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "internal.h"
David Howells08e0e7c2007-04-26 15:55:03 -070022
23unsigned afs_vnode_update_timeout = 10;
24
25#define afs_breakring_space(server) \
26 CIRC_SPACE((server)->cb_break_head, (server)->cb_break_tail, \
27 ARRAY_SIZE((server)->cb_break))
28
29//static void afs_callback_updater(struct work_struct *);
30
31static struct workqueue_struct *afs_callback_update_worker;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * allow the fileserver to request callback state (re-)initialisation
35 */
David Howells08e0e7c2007-04-26 15:55:03 -070036void afs_init_callback_state(struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
David Howells08e0e7c2007-04-26 15:55:03 -070038 struct afs_vnode *vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
David Howells08e0e7c2007-04-26 15:55:03 -070040 _enter("{%p}", server);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 spin_lock(&server->cb_lock);
43
David Howells08e0e7c2007-04-26 15:55:03 -070044 /* kill all the promises on record from this server */
45 while (!RB_EMPTY_ROOT(&server->cb_promises)) {
46 vnode = rb_entry(server->cb_promises.rb_node,
47 struct afs_vnode, cb_promise);
David Howells416351f2007-05-09 02:33:45 -070048 _debug("UNPROMISE { vid=%x:%u uq=%u}",
David Howells260a9802007-04-26 15:59:35 -070049 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
David Howells08e0e7c2007-04-26 15:55:03 -070050 rb_erase(&vnode->cb_promise, &server->cb_promises);
51 vnode->cb_promised = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
53
54 spin_unlock(&server->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -070055 _leave("");
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
David Howells08e0e7c2007-04-26 15:55:03 -070058/*
59 * handle the data invalidation side of a callback being broken
60 */
61void afs_broken_callback_work(struct work_struct *work)
62{
63 struct afs_vnode *vnode =
64 container_of(work, struct afs_vnode, cb_broken_work);
65
66 _enter("");
67
68 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
69 return;
70
71 /* we're only interested in dealing with a broken callback on *this*
72 * vnode and only if no-one else has dealt with it yet */
David Howells260a9802007-04-26 15:59:35 -070073 if (!mutex_trylock(&vnode->validate_lock))
David Howells08e0e7c2007-04-26 15:55:03 -070074 return; /* someone else is dealing with it */
75
76 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
David Howells00d3b7a2007-04-26 15:57:07 -070077 if (S_ISDIR(vnode->vfs_inode.i_mode))
78 afs_clear_permits(vnode);
79
80 if (afs_vnode_fetch_status(vnode, NULL, NULL) < 0)
David Howells08e0e7c2007-04-26 15:55:03 -070081 goto out;
82
83 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
84 goto out;
85
86 /* if the vnode's data version number changed then its contents
87 * are different */
David Howells416351f2007-05-09 02:33:45 -070088 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
89 afs_zap_data(vnode);
David Howells08e0e7c2007-04-26 15:55:03 -070090 }
91
92out:
David Howells260a9802007-04-26 15:59:35 -070093 mutex_unlock(&vnode->validate_lock);
David Howells08e0e7c2007-04-26 15:55:03 -070094
95 /* avoid the potential race whereby the mutex_trylock() in this
96 * function happens again between the clear_bit() and the
97 * mutex_unlock() */
98 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
99 _debug("requeue");
100 queue_work(afs_callback_update_worker, &vnode->cb_broken_work);
101 }
102 _leave("");
103}
104
105/*
106 * actually break a callback
107 */
108static void afs_break_callback(struct afs_server *server,
109 struct afs_vnode *vnode)
110{
111 _enter("");
112
113 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
114
115 if (vnode->cb_promised) {
116 spin_lock(&vnode->lock);
117
118 _debug("break callback");
119
120 spin_lock(&server->cb_lock);
121 if (vnode->cb_promised) {
122 rb_erase(&vnode->cb_promise, &server->cb_promises);
123 vnode->cb_promised = false;
124 }
125 spin_unlock(&server->cb_lock);
126
127 queue_work(afs_callback_update_worker, &vnode->cb_broken_work);
128 spin_unlock(&vnode->lock);
129 }
130}
131
132/*
133 * allow the fileserver to explicitly break one callback
134 * - happens when
135 * - the backing file is changed
136 * - a lock is released
137 */
138static void afs_break_one_callback(struct afs_server *server,
139 struct afs_fid *fid)
140{
141 struct afs_vnode *vnode;
142 struct rb_node *p;
143
144 _debug("find");
145 spin_lock(&server->fs_lock);
146 p = server->fs_vnodes.rb_node;
147 while (p) {
148 vnode = rb_entry(p, struct afs_vnode, server_rb);
149 if (fid->vid < vnode->fid.vid)
150 p = p->rb_left;
151 else if (fid->vid > vnode->fid.vid)
152 p = p->rb_right;
153 else if (fid->vnode < vnode->fid.vnode)
154 p = p->rb_left;
155 else if (fid->vnode > vnode->fid.vnode)
156 p = p->rb_right;
157 else if (fid->unique < vnode->fid.unique)
158 p = p->rb_left;
159 else if (fid->unique > vnode->fid.unique)
160 p = p->rb_right;
161 else
162 goto found;
163 }
164
165 /* not found so we just ignore it (it may have moved to another
166 * server) */
167not_available:
168 _debug("not avail");
169 spin_unlock(&server->fs_lock);
170 _leave("");
171 return;
172
173found:
174 _debug("found");
175 ASSERTCMP(server, ==, vnode->server);
176
177 if (!igrab(AFS_VNODE_TO_I(vnode)))
178 goto not_available;
179 spin_unlock(&server->fs_lock);
180
181 afs_break_callback(server, vnode);
182 iput(&vnode->vfs_inode);
183 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * allow the fileserver to break callback promises
188 */
David Howells08e0e7c2007-04-26 15:55:03 -0700189void afs_break_callbacks(struct afs_server *server, size_t count,
190 struct afs_callback callbacks[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
David Howells08e0e7c2007-04-26 15:55:03 -0700192 _enter("%p,%zu,", server, count);
193
194 ASSERT(server != NULL);
195 ASSERTCMP(count, <=, AFSCBMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 for (; count > 0; callbacks++, count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }",
199 callbacks->fid.vid,
200 callbacks->fid.vnode,
201 callbacks->fid.unique,
202 callbacks->version,
203 callbacks->expiry,
204 callbacks->type
205 );
David Howells08e0e7c2007-04-26 15:55:03 -0700206 afs_break_one_callback(server, &callbacks->fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
David Howells08e0e7c2007-04-26 15:55:03 -0700209 _leave("");
210 return;
David Howellsec268152007-04-26 15:49:28 -0700211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
David Howells08e0e7c2007-04-26 15:55:03 -0700214 * record the callback for breaking
215 * - the caller must hold server->cb_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
David Howells08e0e7c2007-04-26 15:55:03 -0700217static void afs_do_give_up_callback(struct afs_server *server,
218 struct afs_vnode *vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
David Howells08e0e7c2007-04-26 15:55:03 -0700220 struct afs_callback *cb;
221
222 _enter("%p,%p", server, vnode);
223
224 cb = &server->cb_break[server->cb_break_head];
225 cb->fid = vnode->fid;
226 cb->version = vnode->cb_version;
227 cb->expiry = vnode->cb_expiry;
228 cb->type = vnode->cb_type;
229 smp_wmb();
230 server->cb_break_head =
231 (server->cb_break_head + 1) &
232 (ARRAY_SIZE(server->cb_break) - 1);
233
234 /* defer the breaking of callbacks to try and collect as many as
235 * possible to ship in one operation */
236 switch (atomic_inc_return(&server->cb_break_n)) {
237 case 1 ... AFSCBMAX - 1:
238 queue_delayed_work(afs_callback_update_worker,
239 &server->cb_break_work, HZ * 2);
240 break;
241 case AFSCBMAX:
242 afs_flush_callback_breaks(server);
243 break;
244 default:
245 break;
246 }
247
248 ASSERT(server->cb_promises.rb_node != NULL);
249 rb_erase(&vnode->cb_promise, &server->cb_promises);
250 vnode->cb_promised = false;
251 _leave("");
252}
253
254/*
David Howells260a9802007-04-26 15:59:35 -0700255 * discard the callback on a deleted item
256 */
257void afs_discard_callback_on_delete(struct afs_vnode *vnode)
258{
259 struct afs_server *server = vnode->server;
260
261 _enter("%d", vnode->cb_promised);
262
263 if (!vnode->cb_promised) {
264 _leave(" [not promised]");
265 return;
266 }
267
268 ASSERT(server != NULL);
269
270 spin_lock(&server->cb_lock);
271 if (vnode->cb_promised) {
272 ASSERT(server->cb_promises.rb_node != NULL);
273 rb_erase(&vnode->cb_promise, &server->cb_promises);
274 vnode->cb_promised = false;
275 }
276 spin_unlock(&server->cb_lock);
277 _leave("");
278}
279
280/*
David Howells08e0e7c2007-04-26 15:55:03 -0700281 * give up the callback registered for a vnode on the file server when the
282 * inode is being cleared
283 */
284void afs_give_up_callback(struct afs_vnode *vnode)
285{
286 struct afs_server *server = vnode->server;
287
288 DECLARE_WAITQUEUE(myself, current);
289
290 _enter("%d", vnode->cb_promised);
291
292 _debug("GIVE UP INODE %p", &vnode->vfs_inode);
293
294 if (!vnode->cb_promised) {
295 _leave(" [not promised]");
296 return;
297 }
298
299 ASSERT(server != NULL);
300
301 spin_lock(&server->cb_lock);
302 if (vnode->cb_promised && afs_breakring_space(server) == 0) {
303 add_wait_queue(&server->cb_break_waitq, &myself);
304 for (;;) {
305 set_current_state(TASK_UNINTERRUPTIBLE);
306 if (!vnode->cb_promised ||
307 afs_breakring_space(server) != 0)
308 break;
309 spin_unlock(&server->cb_lock);
310 schedule();
311 spin_lock(&server->cb_lock);
312 }
313 remove_wait_queue(&server->cb_break_waitq, &myself);
314 __set_current_state(TASK_RUNNING);
315 }
316
317 /* of course, it's always possible for the server to break this vnode's
318 * callback first... */
319 if (vnode->cb_promised)
320 afs_do_give_up_callback(server, vnode);
321
322 spin_unlock(&server->cb_lock);
323 _leave("");
324}
325
326/*
327 * dispatch a deferred give up callbacks operation
328 */
329void afs_dispatch_give_up_callbacks(struct work_struct *work)
330{
331 struct afs_server *server =
332 container_of(work, struct afs_server, cb_break_work.work);
333
334 _enter("");
335
336 /* tell the fileserver to discard the callback promises it has
337 * - in the event of ENOMEM or some other error, we just forget that we
338 * had callbacks entirely, and the server will call us later to break
339 * them
340 */
341 afs_fs_give_up_callbacks(server, &afs_async_call);
342}
343
344/*
345 * flush the outstanding callback breaks on a server
346 */
347void afs_flush_callback_breaks(struct afs_server *server)
348{
349 cancel_delayed_work(&server->cb_break_work);
350 queue_delayed_work(afs_callback_update_worker,
351 &server->cb_break_work, 0);
352}
353
354#if 0
355/*
356 * update a bunch of callbacks
357 */
358static void afs_callback_updater(struct work_struct *work)
359{
360 struct afs_server *server;
361 struct afs_vnode *vnode, *xvnode;
362 time_t now;
363 long timeout;
364 int ret;
365
366 server = container_of(work, struct afs_server, updater);
367
368 _enter("");
369
370 now = get_seconds();
371
372 /* find the first vnode to update */
373 spin_lock(&server->cb_lock);
374 for (;;) {
375 if (RB_EMPTY_ROOT(&server->cb_promises)) {
376 spin_unlock(&server->cb_lock);
377 _leave(" [nothing]");
378 return;
379 }
380
381 vnode = rb_entry(rb_first(&server->cb_promises),
382 struct afs_vnode, cb_promise);
383 if (atomic_read(&vnode->usage) > 0)
384 break;
385 rb_erase(&vnode->cb_promise, &server->cb_promises);
386 vnode->cb_promised = false;
387 }
388
389 timeout = vnode->update_at - now;
390 if (timeout > 0) {
391 queue_delayed_work(afs_vnode_update_worker,
392 &afs_vnode_update, timeout * HZ);
393 spin_unlock(&server->cb_lock);
394 _leave(" [nothing]");
395 return;
396 }
397
398 list_del_init(&vnode->update);
399 atomic_inc(&vnode->usage);
400 spin_unlock(&server->cb_lock);
401
402 /* we can now perform the update */
403 _debug("update %s", vnode->vldb.name);
404 vnode->state = AFS_VL_UPDATING;
405 vnode->upd_rej_cnt = 0;
406 vnode->upd_busy_cnt = 0;
407
408 ret = afs_vnode_update_record(vl, &vldb);
409 switch (ret) {
410 case 0:
411 afs_vnode_apply_update(vl, &vldb);
412 vnode->state = AFS_VL_UPDATING;
413 break;
414 case -ENOMEDIUM:
415 vnode->state = AFS_VL_VOLUME_DELETED;
416 break;
417 default:
418 vnode->state = AFS_VL_UNCERTAIN;
419 break;
420 }
421
422 /* and then reschedule */
423 _debug("reschedule");
424 vnode->update_at = get_seconds() + afs_vnode_update_timeout;
425
426 spin_lock(&server->cb_lock);
427
428 if (!list_empty(&server->cb_promises)) {
429 /* next update in 10 minutes, but wait at least 1 second more
430 * than the newest record already queued so that we don't spam
431 * the VL server suddenly with lots of requests
432 */
433 xvnode = list_entry(server->cb_promises.prev,
434 struct afs_vnode, update);
435 if (vnode->update_at <= xvnode->update_at)
436 vnode->update_at = xvnode->update_at + 1;
437 xvnode = list_entry(server->cb_promises.next,
438 struct afs_vnode, update);
439 timeout = xvnode->update_at - now;
440 if (timeout < 0)
441 timeout = 0;
442 } else {
443 timeout = afs_vnode_update_timeout;
444 }
445
446 list_add_tail(&vnode->update, &server->cb_promises);
447
448 _debug("timeout %ld", timeout);
449 queue_delayed_work(afs_vnode_update_worker,
450 &afs_vnode_update, timeout * HZ);
451 spin_unlock(&server->cb_lock);
452 afs_put_vnode(vl);
453}
454#endif
455
456/*
457 * initialise the callback update process
458 */
459int __init afs_callback_update_init(void)
460{
461 afs_callback_update_worker =
462 create_singlethread_workqueue("kafs_callbackd");
463 return afs_callback_update_worker ? 0 : -ENOMEM;
464}
465
466/*
467 * shut down the callback update process
468 */
David Howellsfbb3fcb2007-05-03 03:12:46 -0700469void afs_callback_update_kill(void)
David Howells08e0e7c2007-04-26 15:55:03 -0700470{
471 destroy_workqueue(afs_callback_update_worker);
David Howellsec268152007-04-26 15:49:28 -0700472}