blob: b8243945818de3abd394435953acb8ed8e88eeef [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);
David Howellse8d6c552007-07-15 23:40:12 -0700128 if (list_empty(&vnode->granted_locks) &&
129 !list_empty(&vnode->pending_locks))
130 afs_lock_may_be_available(vnode);
David Howells08e0e7c2007-04-26 15:55:03 -0700131 spin_unlock(&vnode->lock);
132 }
133}
134
135/*
136 * allow the fileserver to explicitly break one callback
137 * - happens when
138 * - the backing file is changed
139 * - a lock is released
140 */
141static void afs_break_one_callback(struct afs_server *server,
142 struct afs_fid *fid)
143{
144 struct afs_vnode *vnode;
145 struct rb_node *p;
146
147 _debug("find");
148 spin_lock(&server->fs_lock);
149 p = server->fs_vnodes.rb_node;
150 while (p) {
151 vnode = rb_entry(p, struct afs_vnode, server_rb);
152 if (fid->vid < vnode->fid.vid)
153 p = p->rb_left;
154 else if (fid->vid > vnode->fid.vid)
155 p = p->rb_right;
156 else if (fid->vnode < vnode->fid.vnode)
157 p = p->rb_left;
158 else if (fid->vnode > vnode->fid.vnode)
159 p = p->rb_right;
160 else if (fid->unique < vnode->fid.unique)
161 p = p->rb_left;
162 else if (fid->unique > vnode->fid.unique)
163 p = p->rb_right;
164 else
165 goto found;
166 }
167
168 /* not found so we just ignore it (it may have moved to another
169 * server) */
170not_available:
171 _debug("not avail");
172 spin_unlock(&server->fs_lock);
173 _leave("");
174 return;
175
176found:
177 _debug("found");
178 ASSERTCMP(server, ==, vnode->server);
179
180 if (!igrab(AFS_VNODE_TO_I(vnode)))
181 goto not_available;
182 spin_unlock(&server->fs_lock);
183
184 afs_break_callback(server, vnode);
185 iput(&vnode->vfs_inode);
186 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * allow the fileserver to break callback promises
191 */
David Howells08e0e7c2007-04-26 15:55:03 -0700192void afs_break_callbacks(struct afs_server *server, size_t count,
193 struct afs_callback callbacks[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
David Howells08e0e7c2007-04-26 15:55:03 -0700195 _enter("%p,%zu,", server, count);
196
197 ASSERT(server != NULL);
198 ASSERTCMP(count, <=, AFSCBMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 for (; count > 0; callbacks++, count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }",
202 callbacks->fid.vid,
203 callbacks->fid.vnode,
204 callbacks->fid.unique,
205 callbacks->version,
206 callbacks->expiry,
207 callbacks->type
208 );
David Howells08e0e7c2007-04-26 15:55:03 -0700209 afs_break_one_callback(server, &callbacks->fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
David Howells08e0e7c2007-04-26 15:55:03 -0700212 _leave("");
213 return;
David Howellsec268152007-04-26 15:49:28 -0700214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
David Howells08e0e7c2007-04-26 15:55:03 -0700217 * record the callback for breaking
218 * - the caller must hold server->cb_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
David Howells08e0e7c2007-04-26 15:55:03 -0700220static void afs_do_give_up_callback(struct afs_server *server,
221 struct afs_vnode *vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
David Howells08e0e7c2007-04-26 15:55:03 -0700223 struct afs_callback *cb;
224
225 _enter("%p,%p", server, vnode);
226
227 cb = &server->cb_break[server->cb_break_head];
228 cb->fid = vnode->fid;
229 cb->version = vnode->cb_version;
230 cb->expiry = vnode->cb_expiry;
231 cb->type = vnode->cb_type;
232 smp_wmb();
233 server->cb_break_head =
234 (server->cb_break_head + 1) &
235 (ARRAY_SIZE(server->cb_break) - 1);
236
237 /* defer the breaking of callbacks to try and collect as many as
238 * possible to ship in one operation */
239 switch (atomic_inc_return(&server->cb_break_n)) {
240 case 1 ... AFSCBMAX - 1:
241 queue_delayed_work(afs_callback_update_worker,
242 &server->cb_break_work, HZ * 2);
243 break;
244 case AFSCBMAX:
245 afs_flush_callback_breaks(server);
246 break;
247 default:
248 break;
249 }
250
251 ASSERT(server->cb_promises.rb_node != NULL);
252 rb_erase(&vnode->cb_promise, &server->cb_promises);
253 vnode->cb_promised = false;
254 _leave("");
255}
256
257/*
David Howells260a9802007-04-26 15:59:35 -0700258 * discard the callback on a deleted item
259 */
260void afs_discard_callback_on_delete(struct afs_vnode *vnode)
261{
262 struct afs_server *server = vnode->server;
263
264 _enter("%d", vnode->cb_promised);
265
266 if (!vnode->cb_promised) {
267 _leave(" [not promised]");
268 return;
269 }
270
271 ASSERT(server != NULL);
272
273 spin_lock(&server->cb_lock);
274 if (vnode->cb_promised) {
275 ASSERT(server->cb_promises.rb_node != NULL);
276 rb_erase(&vnode->cb_promise, &server->cb_promises);
277 vnode->cb_promised = false;
278 }
279 spin_unlock(&server->cb_lock);
280 _leave("");
281}
282
283/*
David Howells08e0e7c2007-04-26 15:55:03 -0700284 * give up the callback registered for a vnode on the file server when the
285 * inode is being cleared
286 */
287void afs_give_up_callback(struct afs_vnode *vnode)
288{
289 struct afs_server *server = vnode->server;
290
291 DECLARE_WAITQUEUE(myself, current);
292
293 _enter("%d", vnode->cb_promised);
294
295 _debug("GIVE UP INODE %p", &vnode->vfs_inode);
296
297 if (!vnode->cb_promised) {
298 _leave(" [not promised]");
299 return;
300 }
301
302 ASSERT(server != NULL);
303
304 spin_lock(&server->cb_lock);
305 if (vnode->cb_promised && afs_breakring_space(server) == 0) {
306 add_wait_queue(&server->cb_break_waitq, &myself);
307 for (;;) {
308 set_current_state(TASK_UNINTERRUPTIBLE);
309 if (!vnode->cb_promised ||
310 afs_breakring_space(server) != 0)
311 break;
312 spin_unlock(&server->cb_lock);
313 schedule();
314 spin_lock(&server->cb_lock);
315 }
316 remove_wait_queue(&server->cb_break_waitq, &myself);
317 __set_current_state(TASK_RUNNING);
318 }
319
320 /* of course, it's always possible for the server to break this vnode's
321 * callback first... */
322 if (vnode->cb_promised)
323 afs_do_give_up_callback(server, vnode);
324
325 spin_unlock(&server->cb_lock);
326 _leave("");
327}
328
329/*
330 * dispatch a deferred give up callbacks operation
331 */
332void afs_dispatch_give_up_callbacks(struct work_struct *work)
333{
334 struct afs_server *server =
335 container_of(work, struct afs_server, cb_break_work.work);
336
337 _enter("");
338
339 /* tell the fileserver to discard the callback promises it has
340 * - in the event of ENOMEM or some other error, we just forget that we
341 * had callbacks entirely, and the server will call us later to break
342 * them
343 */
344 afs_fs_give_up_callbacks(server, &afs_async_call);
345}
346
347/*
348 * flush the outstanding callback breaks on a server
349 */
350void afs_flush_callback_breaks(struct afs_server *server)
351{
352 cancel_delayed_work(&server->cb_break_work);
353 queue_delayed_work(afs_callback_update_worker,
354 &server->cb_break_work, 0);
355}
356
357#if 0
358/*
359 * update a bunch of callbacks
360 */
361static void afs_callback_updater(struct work_struct *work)
362{
363 struct afs_server *server;
364 struct afs_vnode *vnode, *xvnode;
365 time_t now;
366 long timeout;
367 int ret;
368
369 server = container_of(work, struct afs_server, updater);
370
371 _enter("");
372
373 now = get_seconds();
374
375 /* find the first vnode to update */
376 spin_lock(&server->cb_lock);
377 for (;;) {
378 if (RB_EMPTY_ROOT(&server->cb_promises)) {
379 spin_unlock(&server->cb_lock);
380 _leave(" [nothing]");
381 return;
382 }
383
384 vnode = rb_entry(rb_first(&server->cb_promises),
385 struct afs_vnode, cb_promise);
386 if (atomic_read(&vnode->usage) > 0)
387 break;
388 rb_erase(&vnode->cb_promise, &server->cb_promises);
389 vnode->cb_promised = false;
390 }
391
392 timeout = vnode->update_at - now;
393 if (timeout > 0) {
394 queue_delayed_work(afs_vnode_update_worker,
395 &afs_vnode_update, timeout * HZ);
396 spin_unlock(&server->cb_lock);
397 _leave(" [nothing]");
398 return;
399 }
400
401 list_del_init(&vnode->update);
402 atomic_inc(&vnode->usage);
403 spin_unlock(&server->cb_lock);
404
405 /* we can now perform the update */
406 _debug("update %s", vnode->vldb.name);
407 vnode->state = AFS_VL_UPDATING;
408 vnode->upd_rej_cnt = 0;
409 vnode->upd_busy_cnt = 0;
410
411 ret = afs_vnode_update_record(vl, &vldb);
412 switch (ret) {
413 case 0:
414 afs_vnode_apply_update(vl, &vldb);
415 vnode->state = AFS_VL_UPDATING;
416 break;
417 case -ENOMEDIUM:
418 vnode->state = AFS_VL_VOLUME_DELETED;
419 break;
420 default:
421 vnode->state = AFS_VL_UNCERTAIN;
422 break;
423 }
424
425 /* and then reschedule */
426 _debug("reschedule");
427 vnode->update_at = get_seconds() + afs_vnode_update_timeout;
428
429 spin_lock(&server->cb_lock);
430
431 if (!list_empty(&server->cb_promises)) {
432 /* next update in 10 minutes, but wait at least 1 second more
433 * than the newest record already queued so that we don't spam
434 * the VL server suddenly with lots of requests
435 */
436 xvnode = list_entry(server->cb_promises.prev,
437 struct afs_vnode, update);
438 if (vnode->update_at <= xvnode->update_at)
439 vnode->update_at = xvnode->update_at + 1;
440 xvnode = list_entry(server->cb_promises.next,
441 struct afs_vnode, update);
442 timeout = xvnode->update_at - now;
443 if (timeout < 0)
444 timeout = 0;
445 } else {
446 timeout = afs_vnode_update_timeout;
447 }
448
449 list_add_tail(&vnode->update, &server->cb_promises);
450
451 _debug("timeout %ld", timeout);
452 queue_delayed_work(afs_vnode_update_worker,
453 &afs_vnode_update, timeout * HZ);
454 spin_unlock(&server->cb_lock);
455 afs_put_vnode(vl);
456}
457#endif
458
459/*
460 * initialise the callback update process
461 */
462int __init afs_callback_update_init(void)
463{
464 afs_callback_update_worker =
465 create_singlethread_workqueue("kafs_callbackd");
466 return afs_callback_update_worker ? 0 : -ENOMEM;
467}
468
469/*
470 * shut down the callback update process
471 */
David Howellsfbb3fcb2007-05-03 03:12:46 -0700472void afs_callback_update_kill(void)
David Howells08e0e7c2007-04-26 15:55:03 -0700473{
474 destroy_workqueue(afs_callback_update_worker);
David Howellsec268152007-04-26 15:49:28 -0700475}