blob: f64e40fefc02841d8c9625e556780e963c0f2090 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
David Howells08e0e7c2007-04-26 15:55:03 -070021
22unsigned afs_vnode_update_timeout = 10;
23
24#define afs_breakring_space(server) \
25 CIRC_SPACE((server)->cb_break_head, (server)->cb_break_tail, \
26 ARRAY_SIZE((server)->cb_break))
27
28//static void afs_callback_updater(struct work_struct *);
29
30static struct workqueue_struct *afs_callback_update_worker;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * allow the fileserver to request callback state (re-)initialisation
34 */
David Howells08e0e7c2007-04-26 15:55:03 -070035void afs_init_callback_state(struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
David Howells08e0e7c2007-04-26 15:55:03 -070037 struct afs_vnode *vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
David Howells08e0e7c2007-04-26 15:55:03 -070039 _enter("{%p}", server);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 spin_lock(&server->cb_lock);
42
David Howells08e0e7c2007-04-26 15:55:03 -070043 /* 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);
David Howells416351f2007-05-09 02:33:45 -070047 _debug("UNPROMISE { vid=%x:%u uq=%u}",
David Howells260a9802007-04-26 15:59:35 -070048 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
David Howells08e0e7c2007-04-26 15:55:03 -070049 rb_erase(&vnode->cb_promise, &server->cb_promises);
50 vnode->cb_promised = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52
53 spin_unlock(&server->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -070054 _leave("");
55}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David Howells08e0e7c2007-04-26 15:55:03 -070057/*
58 * handle the data invalidation side of a callback being broken
59 */
60void afs_broken_callback_work(struct work_struct *work)
61{
62 struct afs_vnode *vnode =
63 container_of(work, struct afs_vnode, cb_broken_work);
64
65 _enter("");
66
67 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
68 return;
69
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 */
David Howells260a9802007-04-26 15:59:35 -070072 if (!mutex_trylock(&vnode->validate_lock))
David Howells08e0e7c2007-04-26 15:55:03 -070073 return; /* someone else is dealing with it */
74
75 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
David Howells00d3b7a2007-04-26 15:57:07 -070076 if (S_ISDIR(vnode->vfs_inode.i_mode))
77 afs_clear_permits(vnode);
78
79 if (afs_vnode_fetch_status(vnode, NULL, NULL) < 0)
David Howells08e0e7c2007-04-26 15:55:03 -070080 goto out;
81
82 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
83 goto out;
84
85 /* if the vnode's data version number changed then its contents
86 * are different */
David Howells416351f2007-05-09 02:33:45 -070087 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
88 afs_zap_data(vnode);
David Howells08e0e7c2007-04-26 15:55:03 -070089 }
90
91out:
David Howells260a9802007-04-26 15:59:35 -070092 mutex_unlock(&vnode->validate_lock);
David Howells08e0e7c2007-04-26 15:55:03 -070093
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);
100 }
101 _leave("");
102}
103
104/*
105 * actually break a callback
106 */
107static void afs_break_callback(struct afs_server *server,
108 struct afs_vnode *vnode)
109{
110 _enter("");
111
112 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
113
114 if (vnode->cb_promised) {
115 spin_lock(&vnode->lock);
116
117 _debug("break callback");
118
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;
123 }
124 spin_unlock(&server->cb_lock);
125
126 queue_work(afs_callback_update_worker, &vnode->cb_broken_work);
127 spin_unlock(&vnode->lock);
128 }
129}
130
131/*
132 * allow the fileserver to explicitly break one callback
133 * - happens when
134 * - the backing file is changed
135 * - a lock is released
136 */
137static void afs_break_one_callback(struct afs_server *server,
138 struct afs_fid *fid)
139{
140 struct afs_vnode *vnode;
141 struct rb_node *p;
142
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;
162 }
163
164 /* not found so we just ignore it (it may have moved to another
165 * server) */
166not_available:
167 _debug("not avail");
168 spin_unlock(&server->fs_lock);
169 _leave("");
170 return;
171
172found:
173 _debug("found");
174 ASSERTCMP(server, ==, vnode->server);
175
176 if (!igrab(AFS_VNODE_TO_I(vnode)))
177 goto not_available;
178 spin_unlock(&server->fs_lock);
179
180 afs_break_callback(server, vnode);
181 iput(&vnode->vfs_inode);
182 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * allow the fileserver to break callback promises
187 */
David Howells08e0e7c2007-04-26 15:55:03 -0700188void afs_break_callbacks(struct afs_server *server, size_t count,
189 struct afs_callback callbacks[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
David Howells08e0e7c2007-04-26 15:55:03 -0700191 _enter("%p,%zu,", server, count);
192
193 ASSERT(server != NULL);
194 ASSERTCMP(count, <=, AFSCBMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 for (; count > 0; callbacks++, count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 _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
204 );
David Howells08e0e7c2007-04-26 15:55:03 -0700205 afs_break_one_callback(server, &callbacks->fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
David Howells08e0e7c2007-04-26 15:55:03 -0700208 _leave("");
209 return;
David Howellsec268152007-04-26 15:49:28 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
David Howells08e0e7c2007-04-26 15:55:03 -0700213 * record the callback for breaking
214 * - the caller must hold server->cb_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
David Howells08e0e7c2007-04-26 15:55:03 -0700216static void afs_do_give_up_callback(struct afs_server *server,
217 struct afs_vnode *vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
David Howells08e0e7c2007-04-26 15:55:03 -0700219 struct afs_callback *cb;
220
221 _enter("%p,%p", server, vnode);
222
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);
232
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;
245 }
246
247 ASSERT(server->cb_promises.rb_node != NULL);
248 rb_erase(&vnode->cb_promise, &server->cb_promises);
249 vnode->cb_promised = false;
250 _leave("");
251}
252
253/*
David Howells260a9802007-04-26 15:59:35 -0700254 * discard the callback on a deleted item
255 */
256void afs_discard_callback_on_delete(struct afs_vnode *vnode)
257{
258 struct afs_server *server = vnode->server;
259
260 _enter("%d", vnode->cb_promised);
261
262 if (!vnode->cb_promised) {
263 _leave(" [not promised]");
264 return;
265 }
266
267 ASSERT(server != NULL);
268
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;
274 }
275 spin_unlock(&server->cb_lock);
276 _leave("");
277}
278
279/*
David Howells08e0e7c2007-04-26 15:55:03 -0700280 * give up the callback registered for a vnode on the file server when the
281 * inode is being cleared
282 */
283void afs_give_up_callback(struct afs_vnode *vnode)
284{
285 struct afs_server *server = vnode->server;
286
287 DECLARE_WAITQUEUE(myself, current);
288
289 _enter("%d", vnode->cb_promised);
290
291 _debug("GIVE UP INODE %p", &vnode->vfs_inode);
292
293 if (!vnode->cb_promised) {
294 _leave(" [not promised]");
295 return;
296 }
297
298 ASSERT(server != NULL);
299
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);
311 }
312 remove_wait_queue(&server->cb_break_waitq, &myself);
313 __set_current_state(TASK_RUNNING);
314 }
315
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);
320
321 spin_unlock(&server->cb_lock);
322 _leave("");
323}
324
325/*
326 * dispatch a deferred give up callbacks operation
327 */
328void afs_dispatch_give_up_callbacks(struct work_struct *work)
329{
330 struct afs_server *server =
331 container_of(work, struct afs_server, cb_break_work.work);
332
333 _enter("");
334
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
339 */
340 afs_fs_give_up_callbacks(server, &afs_async_call);
341}
342
343/*
344 * flush the outstanding callback breaks on a server
345 */
346void afs_flush_callback_breaks(struct afs_server *server)
347{
348 cancel_delayed_work(&server->cb_break_work);
349 queue_delayed_work(afs_callback_update_worker,
350 &server->cb_break_work, 0);
351}
352
353#if 0
354/*
355 * update a bunch of callbacks
356 */
357static void afs_callback_updater(struct work_struct *work)
358{
359 struct afs_server *server;
360 struct afs_vnode *vnode, *xvnode;
361 time_t now;
362 long timeout;
363 int ret;
364
365 server = container_of(work, struct afs_server, updater);
366
367 _enter("");
368
369 now = get_seconds();
370
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;
378 }
379
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;
386 }
387
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;
395 }
396
397 list_del_init(&vnode->update);
398 atomic_inc(&vnode->usage);
399 spin_unlock(&server->cb_lock);
400
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;
406
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;
419 }
420
421 /* and then reschedule */
422 _debug("reschedule");
423 vnode->update_at = get_seconds() + afs_vnode_update_timeout;
424
425 spin_lock(&server->cb_lock);
426
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
431 */
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;
443 }
444
445 list_add_tail(&vnode->update, &server->cb_promises);
446
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);
452}
453#endif
454
455/*
456 * initialise the callback update process
457 */
458int __init afs_callback_update_init(void)
459{
460 afs_callback_update_worker =
461 create_singlethread_workqueue("kafs_callbackd");
462 return afs_callback_update_worker ? 0 : -ENOMEM;
463}
464
465/*
466 * shut down the callback update process
467 */
David Howellsfbb3fcb2007-05-03 03:12:46 -0700468void afs_callback_update_kill(void)
David Howells08e0e7c2007-04-26 15:55:03 -0700469{
470 destroy_workqueue(afs_callback_update_worker);
David Howellsec268152007-04-26 15:49:28 -0700471}