123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- /* vnode.c: AFS vnode management
- *
- * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
- * Written by David Howells (dhowells@redhat.com)
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/slab.h>
- #include <linux/fs.h>
- #include <linux/pagemap.h>
- #include "volume.h"
- #include "cell.h"
- #include "cmservice.h"
- #include "fsclient.h"
- #include "vlclient.h"
- #include "vnode.h"
- #include "internal.h"
- static void afs_vnode_cb_timed_out(struct afs_timer *timer);
- struct afs_timer_ops afs_vnode_cb_timed_out_ops = {
- .timed_out = afs_vnode_cb_timed_out,
- };
- #ifdef AFS_CACHING_SUPPORT
- static cachefs_match_val_t afs_vnode_cache_match(void *target,
- const void *entry);
- static void afs_vnode_cache_update(void *source, void *entry);
- struct cachefs_index_def afs_vnode_cache_index_def = {
- .name = "vnode",
- .data_size = sizeof(struct afs_cache_vnode),
- .keys[0] = { CACHEFS_INDEX_KEYS_BIN, 4 },
- .match = afs_vnode_cache_match,
- .update = afs_vnode_cache_update,
- };
- #endif
- /*****************************************************************************/
- /*
- * handle a callback timing out
- * TODO: retain a ref to vnode struct for an outstanding callback timeout
- */
- static void afs_vnode_cb_timed_out(struct afs_timer *timer)
- {
- struct afs_server *oldserver;
- struct afs_vnode *vnode;
- vnode = list_entry(timer, struct afs_vnode, cb_timeout);
- _enter("%p", vnode);
- /* set the changed flag in the vnode and release the server */
- spin_lock(&vnode->lock);
- oldserver = xchg(&vnode->cb_server, NULL);
- if (oldserver) {
- vnode->flags |= AFS_VNODE_CHANGED;
- spin_lock(&afs_cb_hash_lock);
- list_del_init(&vnode->cb_hash_link);
- spin_unlock(&afs_cb_hash_lock);
- spin_lock(&oldserver->cb_lock);
- list_del_init(&vnode->cb_link);
- spin_unlock(&oldserver->cb_lock);
- }
- spin_unlock(&vnode->lock);
- afs_put_server(oldserver);
- _leave("");
- } /* end afs_vnode_cb_timed_out() */
- /*****************************************************************************/
- /*
- * finish off updating the recorded status of a file
- * - starts callback expiry timer
- * - adds to server's callback list
- */
- static void afs_vnode_finalise_status_update(struct afs_vnode *vnode,
- struct afs_server *server,
- int ret)
- {
- struct afs_server *oldserver = NULL;
- _enter("%p,%p,%d", vnode, server, ret);
- spin_lock(&vnode->lock);
- vnode->flags &= ~AFS_VNODE_CHANGED;
- if (ret == 0) {
- /* adjust the callback timeout appropriately */
- afs_kafstimod_add_timer(&vnode->cb_timeout,
- vnode->cb_expiry * HZ);
- spin_lock(&afs_cb_hash_lock);
- list_del(&vnode->cb_hash_link);
- list_add_tail(&vnode->cb_hash_link,
- &afs_cb_hash(server, &vnode->fid));
- spin_unlock(&afs_cb_hash_lock);
- /* swap ref to old callback server with that for new callback
- * server */
- oldserver = xchg(&vnode->cb_server, server);
- if (oldserver != server) {
- if (oldserver) {
- spin_lock(&oldserver->cb_lock);
- list_del_init(&vnode->cb_link);
- spin_unlock(&oldserver->cb_lock);
- }
- afs_get_server(server);
- spin_lock(&server->cb_lock);
- list_add_tail(&vnode->cb_link, &server->cb_promises);
- spin_unlock(&server->cb_lock);
- }
- else {
- /* same server */
- oldserver = NULL;
- }
- }
- else if (ret == -ENOENT) {
- /* the file was deleted - clear the callback timeout */
- oldserver = xchg(&vnode->cb_server, NULL);
- afs_kafstimod_del_timer(&vnode->cb_timeout);
- _debug("got NOENT from server - marking file deleted");
- vnode->flags |= AFS_VNODE_DELETED;
- }
- vnode->update_cnt--;
- spin_unlock(&vnode->lock);
- wake_up_all(&vnode->update_waitq);
- afs_put_server(oldserver);
- _leave("");
- } /* end afs_vnode_finalise_status_update() */
- /*****************************************************************************/
- /*
- * fetch file status from the volume
- * - don't issue a fetch if:
- * - the changed bit is not set and there's a valid callback
- * - there are any outstanding ops that will fetch the status
- * - TODO implement local caching
- */
- int afs_vnode_fetch_status(struct afs_vnode *vnode)
- {
- struct afs_server *server;
- int ret;
- DECLARE_WAITQUEUE(myself, current);
- _enter("%s,{%u,%u,%u}",
- vnode->volume->vlocation->vldb.name,
- vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
- if (!(vnode->flags & AFS_VNODE_CHANGED) && vnode->cb_server) {
- _leave(" [unchanged]");
- return 0;
- }
- if (vnode->flags & AFS_VNODE_DELETED) {
- _leave(" [deleted]");
- return -ENOENT;
- }
- spin_lock(&vnode->lock);
- if (!(vnode->flags & AFS_VNODE_CHANGED)) {
- spin_unlock(&vnode->lock);
- _leave(" [unchanged]");
- return 0;
- }
- if (vnode->update_cnt > 0) {
- /* someone else started a fetch */
- set_current_state(TASK_UNINTERRUPTIBLE);
- add_wait_queue(&vnode->update_waitq, &myself);
- /* wait for the status to be updated */
- for (;;) {
- if (!(vnode->flags & AFS_VNODE_CHANGED))
- break;
- if (vnode->flags & AFS_VNODE_DELETED)
- break;
- /* it got updated and invalidated all before we saw
- * it */
- if (vnode->update_cnt == 0) {
- remove_wait_queue(&vnode->update_waitq,
- &myself);
- set_current_state(TASK_RUNNING);
- goto get_anyway;
- }
- spin_unlock(&vnode->lock);
- schedule();
- set_current_state(TASK_UNINTERRUPTIBLE);
- spin_lock(&vnode->lock);
- }
- remove_wait_queue(&vnode->update_waitq, &myself);
- spin_unlock(&vnode->lock);
- set_current_state(TASK_RUNNING);
- return vnode->flags & AFS_VNODE_DELETED ? -ENOENT : 0;
- }
- get_anyway:
- /* okay... we're going to have to initiate the op */
- vnode->update_cnt++;
- spin_unlock(&vnode->lock);
- /* merge AFS status fetches and clear outstanding callback on this
- * vnode */
- do {
- /* pick a server to query */
- ret = afs_volume_pick_fileserver(vnode->volume, &server);
- if (ret<0)
- return ret;
- _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
- ret = afs_rxfs_fetch_file_status(server, vnode, NULL);
- } while (!afs_volume_release_fileserver(vnode->volume, server, ret));
- /* adjust the flags */
- afs_vnode_finalise_status_update(vnode, server, ret);
- _leave(" = %d", ret);
- return ret;
- } /* end afs_vnode_fetch_status() */
- /*****************************************************************************/
- /*
- * fetch file data from the volume
- * - TODO implement caching and server failover
- */
- int afs_vnode_fetch_data(struct afs_vnode *vnode,
- struct afs_rxfs_fetch_descriptor *desc)
- {
- struct afs_server *server;
- int ret;
- _enter("%s,{%u,%u,%u}",
- vnode->volume->vlocation->vldb.name,
- vnode->fid.vid,
- vnode->fid.vnode,
- vnode->fid.unique);
- /* this op will fetch the status */
- spin_lock(&vnode->lock);
- vnode->update_cnt++;
- spin_unlock(&vnode->lock);
- /* merge in AFS status fetches and clear outstanding callback on this
- * vnode */
- do {
- /* pick a server to query */
- ret = afs_volume_pick_fileserver(vnode->volume, &server);
- if (ret < 0)
- return ret;
- _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
- ret = afs_rxfs_fetch_file_data(server, vnode, desc, NULL);
- } while (!afs_volume_release_fileserver(vnode->volume, server, ret));
- /* adjust the flags */
- afs_vnode_finalise_status_update(vnode, server, ret);
- _leave(" = %d", ret);
- return ret;
- } /* end afs_vnode_fetch_data() */
- /*****************************************************************************/
- /*
- * break any outstanding callback on a vnode
- * - only relevent to server that issued it
- */
- int afs_vnode_give_up_callback(struct afs_vnode *vnode)
- {
- struct afs_server *server;
- int ret;
- _enter("%s,{%u,%u,%u}",
- vnode->volume->vlocation->vldb.name,
- vnode->fid.vid,
- vnode->fid.vnode,
- vnode->fid.unique);
- spin_lock(&afs_cb_hash_lock);
- list_del_init(&vnode->cb_hash_link);
- spin_unlock(&afs_cb_hash_lock);
- /* set the changed flag in the vnode and release the server */
- spin_lock(&vnode->lock);
- afs_kafstimod_del_timer(&vnode->cb_timeout);
- server = xchg(&vnode->cb_server, NULL);
- if (server) {
- vnode->flags |= AFS_VNODE_CHANGED;
- spin_lock(&server->cb_lock);
- list_del_init(&vnode->cb_link);
- spin_unlock(&server->cb_lock);
- }
- spin_unlock(&vnode->lock);
- ret = 0;
- if (server) {
- ret = afs_rxfs_give_up_callback(server, vnode);
- afs_put_server(server);
- }
- _leave(" = %d", ret);
- return ret;
- } /* end afs_vnode_give_up_callback() */
- /*****************************************************************************/
- /*
- * match a vnode record stored in the cache
- */
- #ifdef AFS_CACHING_SUPPORT
- static cachefs_match_val_t afs_vnode_cache_match(void *target,
- const void *entry)
- {
- const struct afs_cache_vnode *cvnode = entry;
- struct afs_vnode *vnode = target;
- _enter("{%x,%x,%Lx},{%x,%x,%Lx}",
- vnode->fid.vnode,
- vnode->fid.unique,
- vnode->status.version,
- cvnode->vnode_id,
- cvnode->vnode_unique,
- cvnode->data_version);
- if (vnode->fid.vnode != cvnode->vnode_id) {
- _leave(" = FAILED");
- return CACHEFS_MATCH_FAILED;
- }
- if (vnode->fid.unique != cvnode->vnode_unique ||
- vnode->status.version != cvnode->data_version) {
- _leave(" = DELETE");
- return CACHEFS_MATCH_SUCCESS_DELETE;
- }
- _leave(" = SUCCESS");
- return CACHEFS_MATCH_SUCCESS;
- } /* end afs_vnode_cache_match() */
- #endif
- /*****************************************************************************/
- /*
- * update a vnode record stored in the cache
- */
- #ifdef AFS_CACHING_SUPPORT
- static void afs_vnode_cache_update(void *source, void *entry)
- {
- struct afs_cache_vnode *cvnode = entry;
- struct afs_vnode *vnode = source;
- _enter("");
- cvnode->vnode_id = vnode->fid.vnode;
- cvnode->vnode_unique = vnode->fid.unique;
- cvnode->data_version = vnode->status.version;
- } /* end afs_vnode_cache_update() */
- #endif
|