JFFS2: add UBI support
[linux-2.6] / fs / afs / vnode.c
1 /* vnode.c: AFS vnode management
2  *
3  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/fs.h>
17 #include <linux/pagemap.h>
18 #include "volume.h"
19 #include "cell.h"
20 #include "cmservice.h"
21 #include "fsclient.h"
22 #include "vlclient.h"
23 #include "vnode.h"
24 #include "internal.h"
25
26 static void afs_vnode_cb_timed_out(struct afs_timer *timer);
27
28 struct afs_timer_ops afs_vnode_cb_timed_out_ops = {
29         .timed_out      = afs_vnode_cb_timed_out,
30 };
31
32 #ifdef AFS_CACHING_SUPPORT
33 static cachefs_match_val_t afs_vnode_cache_match(void *target,
34                                                  const void *entry);
35 static void afs_vnode_cache_update(void *source, void *entry);
36
37 struct cachefs_index_def afs_vnode_cache_index_def = {
38         .name           = "vnode",
39         .data_size      = sizeof(struct afs_cache_vnode),
40         .keys[0]        = { CACHEFS_INDEX_KEYS_BIN, 4 },
41         .match          = afs_vnode_cache_match,
42         .update         = afs_vnode_cache_update,
43 };
44 #endif
45
46 /*****************************************************************************/
47 /*
48  * handle a callback timing out
49  * TODO: retain a ref to vnode struct for an outstanding callback timeout
50  */
51 static void afs_vnode_cb_timed_out(struct afs_timer *timer)
52 {
53         struct afs_server *oldserver;
54         struct afs_vnode *vnode;
55
56         vnode = list_entry(timer, struct afs_vnode, cb_timeout);
57
58         _enter("%p", vnode);
59
60         /* set the changed flag in the vnode and release the server */
61         spin_lock(&vnode->lock);
62
63         oldserver = xchg(&vnode->cb_server, NULL);
64         if (oldserver) {
65                 vnode->flags |= AFS_VNODE_CHANGED;
66
67                 spin_lock(&afs_cb_hash_lock);
68                 list_del_init(&vnode->cb_hash_link);
69                 spin_unlock(&afs_cb_hash_lock);
70
71                 spin_lock(&oldserver->cb_lock);
72                 list_del_init(&vnode->cb_link);
73                 spin_unlock(&oldserver->cb_lock);
74         }
75
76         spin_unlock(&vnode->lock);
77
78         afs_put_server(oldserver);
79
80         _leave("");
81 } /* end afs_vnode_cb_timed_out() */
82
83 /*****************************************************************************/
84 /*
85  * finish off updating the recorded status of a file
86  * - starts callback expiry timer
87  * - adds to server's callback list
88  */
89 static void afs_vnode_finalise_status_update(struct afs_vnode *vnode,
90                                              struct afs_server *server,
91                                              int ret)
92 {
93         struct afs_server *oldserver = NULL;
94
95         _enter("%p,%p,%d", vnode, server, ret);
96
97         spin_lock(&vnode->lock);
98
99         vnode->flags &= ~AFS_VNODE_CHANGED;
100
101         if (ret == 0) {
102                 /* adjust the callback timeout appropriately */
103                 afs_kafstimod_add_timer(&vnode->cb_timeout,
104                                         vnode->cb_expiry * HZ);
105
106                 spin_lock(&afs_cb_hash_lock);
107                 list_move_tail(&vnode->cb_hash_link,
108                               &afs_cb_hash(server, &vnode->fid));
109                 spin_unlock(&afs_cb_hash_lock);
110
111                 /* swap ref to old callback server with that for new callback
112                  * server */
113                 oldserver = xchg(&vnode->cb_server, server);
114                 if (oldserver != server) {
115                         if (oldserver) {
116                                 spin_lock(&oldserver->cb_lock);
117                                 list_del_init(&vnode->cb_link);
118                                 spin_unlock(&oldserver->cb_lock);
119                         }
120
121                         afs_get_server(server);
122                         spin_lock(&server->cb_lock);
123                         list_add_tail(&vnode->cb_link, &server->cb_promises);
124                         spin_unlock(&server->cb_lock);
125                 }
126                 else {
127                         /* same server */
128                         oldserver = NULL;
129                 }
130         }
131         else if (ret == -ENOENT) {
132                 /* the file was deleted - clear the callback timeout */
133                 oldserver = xchg(&vnode->cb_server, NULL);
134                 afs_kafstimod_del_timer(&vnode->cb_timeout);
135
136                 _debug("got NOENT from server - marking file deleted");
137                 vnode->flags |= AFS_VNODE_DELETED;
138         }
139
140         vnode->update_cnt--;
141
142         spin_unlock(&vnode->lock);
143
144         wake_up_all(&vnode->update_waitq);
145
146         afs_put_server(oldserver);
147
148         _leave("");
149
150 } /* end afs_vnode_finalise_status_update() */
151
152 /*****************************************************************************/
153 /*
154  * fetch file status from the volume
155  * - don't issue a fetch if:
156  *   - the changed bit is not set and there's a valid callback
157  *   - there are any outstanding ops that will fetch the status
158  * - TODO implement local caching
159  */
160 int afs_vnode_fetch_status(struct afs_vnode *vnode)
161 {
162         struct afs_server *server;
163         int ret;
164
165         DECLARE_WAITQUEUE(myself, current);
166
167         _enter("%s,{%u,%u,%u}",
168                vnode->volume->vlocation->vldb.name,
169                vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
170
171         if (!(vnode->flags & AFS_VNODE_CHANGED) && vnode->cb_server) {
172                 _leave(" [unchanged]");
173                 return 0;
174         }
175
176         if (vnode->flags & AFS_VNODE_DELETED) {
177                 _leave(" [deleted]");
178                 return -ENOENT;
179         }
180
181         spin_lock(&vnode->lock);
182
183         if (!(vnode->flags & AFS_VNODE_CHANGED)) {
184                 spin_unlock(&vnode->lock);
185                 _leave(" [unchanged]");
186                 return 0;
187         }
188
189         if (vnode->update_cnt > 0) {
190                 /* someone else started a fetch */
191                 set_current_state(TASK_UNINTERRUPTIBLE);
192                 add_wait_queue(&vnode->update_waitq, &myself);
193
194                 /* wait for the status to be updated */
195                 for (;;) {
196                         if (!(vnode->flags & AFS_VNODE_CHANGED))
197                                 break;
198                         if (vnode->flags & AFS_VNODE_DELETED)
199                                 break;
200
201                         /* it got updated and invalidated all before we saw
202                          * it */
203                         if (vnode->update_cnt == 0) {
204                                 remove_wait_queue(&vnode->update_waitq,
205                                                   &myself);
206                                 set_current_state(TASK_RUNNING);
207                                 goto get_anyway;
208                         }
209
210                         spin_unlock(&vnode->lock);
211
212                         schedule();
213                         set_current_state(TASK_UNINTERRUPTIBLE);
214
215                         spin_lock(&vnode->lock);
216                 }
217
218                 remove_wait_queue(&vnode->update_waitq, &myself);
219                 spin_unlock(&vnode->lock);
220                 set_current_state(TASK_RUNNING);
221
222                 return vnode->flags & AFS_VNODE_DELETED ? -ENOENT : 0;
223         }
224
225  get_anyway:
226         /* okay... we're going to have to initiate the op */
227         vnode->update_cnt++;
228
229         spin_unlock(&vnode->lock);
230
231         /* merge AFS status fetches and clear outstanding callback on this
232          * vnode */
233         do {
234                 /* pick a server to query */
235                 ret = afs_volume_pick_fileserver(vnode->volume, &server);
236                 if (ret<0)
237                         return ret;
238
239                 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
240
241                 ret = afs_rxfs_fetch_file_status(server, vnode, NULL);
242
243         } while (!afs_volume_release_fileserver(vnode->volume, server, ret));
244
245         /* adjust the flags */
246         afs_vnode_finalise_status_update(vnode, server, ret);
247
248         _leave(" = %d", ret);
249         return ret;
250 } /* end afs_vnode_fetch_status() */
251
252 /*****************************************************************************/
253 /*
254  * fetch file data from the volume
255  * - TODO implement caching and server failover
256  */
257 int afs_vnode_fetch_data(struct afs_vnode *vnode,
258                          struct afs_rxfs_fetch_descriptor *desc)
259 {
260         struct afs_server *server;
261         int ret;
262
263         _enter("%s,{%u,%u,%u}",
264                vnode->volume->vlocation->vldb.name,
265                vnode->fid.vid,
266                vnode->fid.vnode,
267                vnode->fid.unique);
268
269         /* this op will fetch the status */
270         spin_lock(&vnode->lock);
271         vnode->update_cnt++;
272         spin_unlock(&vnode->lock);
273
274         /* merge in AFS status fetches and clear outstanding callback on this
275          * vnode */
276         do {
277                 /* pick a server to query */
278                 ret = afs_volume_pick_fileserver(vnode->volume, &server);
279                 if (ret < 0)
280                         return ret;
281
282                 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
283
284                 ret = afs_rxfs_fetch_file_data(server, vnode, desc, NULL);
285
286         } while (!afs_volume_release_fileserver(vnode->volume, server, ret));
287
288         /* adjust the flags */
289         afs_vnode_finalise_status_update(vnode, server, ret);
290
291         _leave(" = %d", ret);
292         return ret;
293
294 } /* end afs_vnode_fetch_data() */
295
296 /*****************************************************************************/
297 /*
298  * break any outstanding callback on a vnode
299  * - only relevent to server that issued it
300  */
301 int afs_vnode_give_up_callback(struct afs_vnode *vnode)
302 {
303         struct afs_server *server;
304         int ret;
305
306         _enter("%s,{%u,%u,%u}",
307                vnode->volume->vlocation->vldb.name,
308                vnode->fid.vid,
309                vnode->fid.vnode,
310                vnode->fid.unique);
311
312         spin_lock(&afs_cb_hash_lock);
313         list_del_init(&vnode->cb_hash_link);
314         spin_unlock(&afs_cb_hash_lock);
315
316         /* set the changed flag in the vnode and release the server */
317         spin_lock(&vnode->lock);
318
319         afs_kafstimod_del_timer(&vnode->cb_timeout);
320
321         server = xchg(&vnode->cb_server, NULL);
322         if (server) {
323                 vnode->flags |= AFS_VNODE_CHANGED;
324
325                 spin_lock(&server->cb_lock);
326                 list_del_init(&vnode->cb_link);
327                 spin_unlock(&server->cb_lock);
328         }
329
330         spin_unlock(&vnode->lock);
331
332         ret = 0;
333         if (server) {
334                 ret = afs_rxfs_give_up_callback(server, vnode);
335                 afs_put_server(server);
336         }
337
338         _leave(" = %d", ret);
339         return ret;
340 } /* end afs_vnode_give_up_callback() */
341
342 /*****************************************************************************/
343 /*
344  * match a vnode record stored in the cache
345  */
346 #ifdef AFS_CACHING_SUPPORT
347 static cachefs_match_val_t afs_vnode_cache_match(void *target,
348                                                  const void *entry)
349 {
350         const struct afs_cache_vnode *cvnode = entry;
351         struct afs_vnode *vnode = target;
352
353         _enter("{%x,%x,%Lx},{%x,%x,%Lx}",
354                vnode->fid.vnode,
355                vnode->fid.unique,
356                vnode->status.version,
357                cvnode->vnode_id,
358                cvnode->vnode_unique,
359                cvnode->data_version);
360
361         if (vnode->fid.vnode != cvnode->vnode_id) {
362                 _leave(" = FAILED");
363                 return CACHEFS_MATCH_FAILED;
364         }
365
366         if (vnode->fid.unique != cvnode->vnode_unique ||
367             vnode->status.version != cvnode->data_version) {
368                 _leave(" = DELETE");
369                 return CACHEFS_MATCH_SUCCESS_DELETE;
370         }
371
372         _leave(" = SUCCESS");
373         return CACHEFS_MATCH_SUCCESS;
374 } /* end afs_vnode_cache_match() */
375 #endif
376
377 /*****************************************************************************/
378 /*
379  * update a vnode record stored in the cache
380  */
381 #ifdef AFS_CACHING_SUPPORT
382 static void afs_vnode_cache_update(void *source, void *entry)
383 {
384         struct afs_cache_vnode *cvnode = entry;
385         struct afs_vnode *vnode = source;
386
387         _enter("");
388
389         cvnode->vnode_id        = vnode->fid.vnode;
390         cvnode->vnode_unique    = vnode->fid.unique;
391         cvnode->data_version    = vnode->status.version;
392
393 } /* end afs_vnode_cache_update() */
394 #endif