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 vn=%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
)) {
88 _debug("zap data {%x:%u}",
89 vnode
->fid
.vid
, vnode
->fid
.vnode
);
90 invalidate_remote_inode(&vnode
->vfs_inode
);
95 mutex_unlock(&vnode
->validate_lock
);
97 /* avoid the potential race whereby the mutex_trylock() in this
98 * function happens again between the clear_bit() and the
100 if (test_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
)) {
102 queue_work(afs_callback_update_worker
, &vnode
->cb_broken_work
);
108 * actually break a callback
110 static void afs_break_callback(struct afs_server
*server
,
111 struct afs_vnode
*vnode
)
115 set_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
);
117 if (vnode
->cb_promised
) {
118 spin_lock(&vnode
->lock
);
120 _debug("break callback");
122 spin_lock(&server
->cb_lock
);
123 if (vnode
->cb_promised
) {
124 rb_erase(&vnode
->cb_promise
, &server
->cb_promises
);
125 vnode
->cb_promised
= false;
127 spin_unlock(&server
->cb_lock
);
129 queue_work(afs_callback_update_worker
, &vnode
->cb_broken_work
);
130 spin_unlock(&vnode
->lock
);
135 * allow the fileserver to explicitly break one callback
137 * - the backing file is changed
138 * - a lock is released
140 static void afs_break_one_callback(struct afs_server
*server
,
143 struct afs_vnode
*vnode
;
147 spin_lock(&server
->fs_lock
);
148 p
= server
->fs_vnodes
.rb_node
;
150 vnode
= rb_entry(p
, struct afs_vnode
, server_rb
);
151 if (fid
->vid
< vnode
->fid
.vid
)
153 else if (fid
->vid
> vnode
->fid
.vid
)
155 else if (fid
->vnode
< vnode
->fid
.vnode
)
157 else if (fid
->vnode
> vnode
->fid
.vnode
)
159 else if (fid
->unique
< vnode
->fid
.unique
)
161 else if (fid
->unique
> vnode
->fid
.unique
)
167 /* not found so we just ignore it (it may have moved to another
171 spin_unlock(&server
->fs_lock
);
177 ASSERTCMP(server
, ==, vnode
->server
);
179 if (!igrab(AFS_VNODE_TO_I(vnode
)))
181 spin_unlock(&server
->fs_lock
);
183 afs_break_callback(server
, vnode
);
184 iput(&vnode
->vfs_inode
);
189 * allow the fileserver to break callback promises
191 void afs_break_callbacks(struct afs_server
*server
, size_t count
,
192 struct afs_callback callbacks
[])
194 _enter("%p,%zu,", server
, count
);
196 ASSERT(server
!= NULL
);
197 ASSERTCMP(count
, <=, AFSCBMAX
);
199 for (; count
> 0; callbacks
++, count
--) {
200 _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }",
202 callbacks
->fid
.vnode
,
203 callbacks
->fid
.unique
,
208 afs_break_one_callback(server
, &callbacks
->fid
);
216 * record the callback for breaking
217 * - the caller must hold server->cb_lock
219 static void afs_do_give_up_callback(struct afs_server
*server
,
220 struct afs_vnode
*vnode
)
222 struct afs_callback
*cb
;
224 _enter("%p,%p", server
, vnode
);
226 cb
= &server
->cb_break
[server
->cb_break_head
];
227 cb
->fid
= vnode
->fid
;
228 cb
->version
= vnode
->cb_version
;
229 cb
->expiry
= vnode
->cb_expiry
;
230 cb
->type
= vnode
->cb_type
;
232 server
->cb_break_head
=
233 (server
->cb_break_head
+ 1) &
234 (ARRAY_SIZE(server
->cb_break
) - 1);
236 /* defer the breaking of callbacks to try and collect as many as
237 * possible to ship in one operation */
238 switch (atomic_inc_return(&server
->cb_break_n
)) {
239 case 1 ... AFSCBMAX
- 1:
240 queue_delayed_work(afs_callback_update_worker
,
241 &server
->cb_break_work
, HZ
* 2);
244 afs_flush_callback_breaks(server
);
250 ASSERT(server
->cb_promises
.rb_node
!= NULL
);
251 rb_erase(&vnode
->cb_promise
, &server
->cb_promises
);
252 vnode
->cb_promised
= false;
257 * discard the callback on a deleted item
259 void afs_discard_callback_on_delete(struct afs_vnode
*vnode
)
261 struct afs_server
*server
= vnode
->server
;
263 _enter("%d", vnode
->cb_promised
);
265 if (!vnode
->cb_promised
) {
266 _leave(" [not promised]");
270 ASSERT(server
!= NULL
);
272 spin_lock(&server
->cb_lock
);
273 if (vnode
->cb_promised
) {
274 ASSERT(server
->cb_promises
.rb_node
!= NULL
);
275 rb_erase(&vnode
->cb_promise
, &server
->cb_promises
);
276 vnode
->cb_promised
= false;
278 spin_unlock(&server
->cb_lock
);
283 * give up the callback registered for a vnode on the file server when the
284 * inode is being cleared
286 void afs_give_up_callback(struct afs_vnode
*vnode
)
288 struct afs_server
*server
= vnode
->server
;
290 DECLARE_WAITQUEUE(myself
, current
);
292 _enter("%d", vnode
->cb_promised
);
294 _debug("GIVE UP INODE %p", &vnode
->vfs_inode
);
296 if (!vnode
->cb_promised
) {
297 _leave(" [not promised]");
301 ASSERT(server
!= NULL
);
303 spin_lock(&server
->cb_lock
);
304 if (vnode
->cb_promised
&& afs_breakring_space(server
) == 0) {
305 add_wait_queue(&server
->cb_break_waitq
, &myself
);
307 set_current_state(TASK_UNINTERRUPTIBLE
);
308 if (!vnode
->cb_promised
||
309 afs_breakring_space(server
) != 0)
311 spin_unlock(&server
->cb_lock
);
313 spin_lock(&server
->cb_lock
);
315 remove_wait_queue(&server
->cb_break_waitq
, &myself
);
316 __set_current_state(TASK_RUNNING
);
319 /* of course, it's always possible for the server to break this vnode's
320 * callback first... */
321 if (vnode
->cb_promised
)
322 afs_do_give_up_callback(server
, vnode
);
324 spin_unlock(&server
->cb_lock
);
329 * dispatch a deferred give up callbacks operation
331 void afs_dispatch_give_up_callbacks(struct work_struct
*work
)
333 struct afs_server
*server
=
334 container_of(work
, struct afs_server
, cb_break_work
.work
);
338 /* tell the fileserver to discard the callback promises it has
339 * - in the event of ENOMEM or some other error, we just forget that we
340 * had callbacks entirely, and the server will call us later to break
343 afs_fs_give_up_callbacks(server
, &afs_async_call
);
347 * flush the outstanding callback breaks on a server
349 void afs_flush_callback_breaks(struct afs_server
*server
)
351 cancel_delayed_work(&server
->cb_break_work
);
352 queue_delayed_work(afs_callback_update_worker
,
353 &server
->cb_break_work
, 0);
358 * update a bunch of callbacks
360 static void afs_callback_updater(struct work_struct
*work
)
362 struct afs_server
*server
;
363 struct afs_vnode
*vnode
, *xvnode
;
368 server
= container_of(work
, struct afs_server
, updater
);
374 /* find the first vnode to update */
375 spin_lock(&server
->cb_lock
);
377 if (RB_EMPTY_ROOT(&server
->cb_promises
)) {
378 spin_unlock(&server
->cb_lock
);
379 _leave(" [nothing]");
383 vnode
= rb_entry(rb_first(&server
->cb_promises
),
384 struct afs_vnode
, cb_promise
);
385 if (atomic_read(&vnode
->usage
) > 0)
387 rb_erase(&vnode
->cb_promise
, &server
->cb_promises
);
388 vnode
->cb_promised
= false;
391 timeout
= vnode
->update_at
- now
;
393 queue_delayed_work(afs_vnode_update_worker
,
394 &afs_vnode_update
, timeout
* HZ
);
395 spin_unlock(&server
->cb_lock
);
396 _leave(" [nothing]");
400 list_del_init(&vnode
->update
);
401 atomic_inc(&vnode
->usage
);
402 spin_unlock(&server
->cb_lock
);
404 /* we can now perform the update */
405 _debug("update %s", vnode
->vldb
.name
);
406 vnode
->state
= AFS_VL_UPDATING
;
407 vnode
->upd_rej_cnt
= 0;
408 vnode
->upd_busy_cnt
= 0;
410 ret
= afs_vnode_update_record(vl
, &vldb
);
413 afs_vnode_apply_update(vl
, &vldb
);
414 vnode
->state
= AFS_VL_UPDATING
;
417 vnode
->state
= AFS_VL_VOLUME_DELETED
;
420 vnode
->state
= AFS_VL_UNCERTAIN
;
424 /* and then reschedule */
425 _debug("reschedule");
426 vnode
->update_at
= get_seconds() + afs_vnode_update_timeout
;
428 spin_lock(&server
->cb_lock
);
430 if (!list_empty(&server
->cb_promises
)) {
431 /* next update in 10 minutes, but wait at least 1 second more
432 * than the newest record already queued so that we don't spam
433 * the VL server suddenly with lots of requests
435 xvnode
= list_entry(server
->cb_promises
.prev
,
436 struct afs_vnode
, update
);
437 if (vnode
->update_at
<= xvnode
->update_at
)
438 vnode
->update_at
= xvnode
->update_at
+ 1;
439 xvnode
= list_entry(server
->cb_promises
.next
,
440 struct afs_vnode
, update
);
441 timeout
= xvnode
->update_at
- now
;
445 timeout
= afs_vnode_update_timeout
;
448 list_add_tail(&vnode
->update
, &server
->cb_promises
);
450 _debug("timeout %ld", timeout
);
451 queue_delayed_work(afs_vnode_update_worker
,
452 &afs_vnode_update
, timeout
* HZ
);
453 spin_unlock(&server
->cb_lock
);
459 * initialise the callback update process
461 int __init
afs_callback_update_init(void)
463 afs_callback_update_worker
=
464 create_singlethread_workqueue("kafs_callbackd");
465 return afs_callback_update_worker
? 0 : -ENOMEM
;
469 * shut down the callback update process
471 void __exit
afs_callback_update_kill(void)
473 destroy_workqueue(afs_callback_update_worker
);