AFS: implement basic file write support
[safe/jmp/linux-2.6] / fs / afs / callback.c
1 /*
2  * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
3  *
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>
19 #include <linux/circ_buf.h>
20 #include "internal.h"
21
22 unsigned 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
30 static struct workqueue_struct *afs_callback_update_worker;
31
32 /*
33  * allow the fileserver to request callback state (re-)initialisation
34  */
35 void afs_init_callback_state(struct afs_server *server)
36 {
37         struct afs_vnode *vnode;
38
39         _enter("{%p}", server);
40
41         spin_lock(&server->cb_lock);
42
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;
51         }
52
53         spin_unlock(&server->cb_lock);
54         _leave("");
55 }
56
57 /*
58  * handle the data invalidation side of a callback being broken
59  */
60 void 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 */
72         if (!mutex_trylock(&vnode->validate_lock))
73                 return; /* someone else is dealing with it */
74
75         if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
76                 if (S_ISDIR(vnode->vfs_inode.i_mode))
77                         afs_clear_permits(vnode);
78
79                 if (afs_vnode_fetch_status(vnode, NULL, NULL) < 0)
80                         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 */
87                 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
88                         afs_zap_data(vnode);
89         }
90
91 out:
92         mutex_unlock(&vnode->validate_lock);
93
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  */
107 static 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  */
137 static 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) */
166 not_available:
167         _debug("not avail");
168         spin_unlock(&server->fs_lock);
169         _leave("");
170         return;
171
172 found:
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("");
183 }
184
185 /*
186  * allow the fileserver to break callback promises
187  */
188 void afs_break_callbacks(struct afs_server *server, size_t count,
189                          struct afs_callback callbacks[])
190 {
191         _enter("%p,%zu,", server, count);
192
193         ASSERT(server != NULL);
194         ASSERTCMP(count, <=, AFSCBMAX);
195
196         for (; count > 0; callbacks++, count--) {
197                 _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                        );
205                 afs_break_one_callback(server, &callbacks->fid);
206         }
207
208         _leave("");
209         return;
210 }
211
212 /*
213  * record the callback for breaking
214  * - the caller must hold server->cb_lock
215  */
216 static void afs_do_give_up_callback(struct afs_server *server,
217                                     struct afs_vnode *vnode)
218 {
219         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 /*
254  * discard the callback on a deleted item
255  */
256 void 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 /*
280  * give up the callback registered for a vnode on the file server when the
281  * inode is being cleared
282  */
283 void 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  */
328 void 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  */
346 void 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  */
357 static 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  */
458 int __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  */
468 void afs_callback_update_kill(void)
469 {
470         destroy_workqueue(afs_callback_update_worker);
471 }