Skip to content

Commit

Permalink
afs: Overhaul invalidation handling to better support RO volumes
Browse files Browse the repository at this point in the history
Overhaul the third party-induced invalidation handling, making use of the
previously added volume-level event counters (cb_scrub and cb_ro_snapshot)
that are now being parsed out of the VolSync record returned by the
fileserver in many of its replies.

This allows better handling of RO (and Backup) volumes.  Since these are
snapshot of a RW volume that are updated atomically simultantanously across
all servers that host them, they only require a single callback promise for
the entire volume.  The currently upstream code assumes that RO volumes
operate in the same manner as RW volumes, and that each file has its own
individual callback - which means that it does a status fetch for *every*
file in a RO volume, whether or not the volume got "released" (volume
callback breaks can occur for other reasons too, such as the volumeserver
taking ownership of a volume from a fileserver).

To this end, make the following changes:

 (1) Change the meaning of the volume's cb_v_break counter so that it is
     now a hint that we need to issue a status fetch to work out the state
     of a volume.  cb_v_break is incremented by volume break callbacks and
     by server initialisation callbacks.

 (2) Add a second counter, cb_v_check, to the afs_volume struct such that
     if this differs from cb_v_break, we need to do a check.  When the
     check is complete, cb_v_check is advanced to what cb_v_break was at
     the start of the status fetch.

 (3) Move the list of mmap'd vnodes to the volume and trigger removal of
     PTEs that map to files on a volume break rather than on a server
     break.

 (4) When a server reinitialisation callback comes in, use the
     server-to-volume reverse mapping added in a preceding patch to iterate
     over all the volumes using that server and clear the volume callback
     promises for that server and the general volume promise as a whole to
     trigger reanalysis.

 (5) Replace the AFS_VNODE_CB_PROMISED flag with an AFS_NO_CB_PROMISE
     (TIME64_MIN) value in the cb_expires_at field, reducing the number of
     checks we need to make.

 (6) Change afs_check_validity() to quickly see if various event counters
     have been incremented or if the vnode or volume callback promise is
     due to expire/has expired without making any changes to the state.
     That is now left to afs_validate() as this may get more complicated in
     future as we may have to examine server records too.

 (7) Overhaul afs_validate() so that it does a single status fetch if we
     need to check the state of either the vnode or the volume - and do so
     under appropriate locking.  The function does the following steps:

     (A) If the vnode/volume is no longer seen as valid, then we take the
     vnode validation lock and, if the volume promise has expired, the
     volume check lock also.  The latter prevents redundant checks being
     made to find out if a new version of the volume got released.

     (B) If a previous RPC call found that the volsync changed unexpectedly
     or that a RO volume was updated, then we unmap all PTEs pointing to
     the file to stop mmap being used for access.

     (C) If the vnode is still seen to be of uncertain validity, then we
     perform an FS.FetchStatus RPC op to jointly update the volume status
     and the vnode status.  This assessment is done as part of parsing the
     reply:

	If the RO volume creation timestamp advances, cb_ro_snapshot is
	incremented; if either the creation or update timestamps changes in
	an unexpected way, the cb_scrub counter is incremented

	If the Data Version returned doesn't match the copy we have
	locally, then we ask for the pagecache to be zapped.  This takes
	care of handling RO update.

     (D) If cb_scrub differs between volume and vnode, the vnode's
     pagecache is zapped and the vnode's cb_scrub is updated unless the
     file is marked as having been deleted.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: linux-afs@lists.infradead.org
  • Loading branch information
David Howells committed Jan 1, 2024
1 parent 16069e1 commit 453924d
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 203 deletions.
122 changes: 76 additions & 46 deletions fs/afs/callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,20 @@ void afs_invalidate_mmap_work(struct work_struct *work)
unmap_mapping_pages(vnode->netfs.inode.i_mapping, 0, 0, false);
}

static void afs_server_init_callback(struct afs_server *server)
static void afs_volume_init_callback(struct afs_volume *volume)
{
struct afs_vnode *vnode;
struct afs_cell *cell = server->cell;

down_read(&cell->fs_open_mmaps_lock);
down_read(&volume->open_mmaps_lock);

list_for_each_entry(vnode, &cell->fs_open_mmaps, cb_mmap_link) {
if (vnode->cb_server == server) {
clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
list_for_each_entry(vnode, &volume->open_mmaps, cb_mmap_link) {
if (vnode->cb_v_check != atomic_read(&volume->cb_v_break)) {
atomic64_set(&vnode->cb_expires_at, AFS_NO_CB_PROMISE);
queue_work(system_unbound_wq, &vnode->cb_work);
}
}

up_read(&cell->fs_open_mmaps_lock);
up_read(&volume->open_mmaps_lock);
}

/*
Expand All @@ -56,19 +55,20 @@ static void afs_server_init_callback(struct afs_server *server)
*/
void afs_init_callback_state(struct afs_server *server)
{
struct afs_cell *cell = server->cell;
struct afs_server_entry *se;

down_read(&cell->vs_lock);
down_read(&server->cell->vs_lock);

do {
server->cb_s_break++;
atomic_inc(&server->cell->fs_s_break);
if (!list_empty(&server->cell->fs_open_mmaps))
afs_server_init_callback(server);

} while ((server = rcu_dereference(server->uuid_next)));
list_for_each_entry(se, &server->volumes, slink) {
se->cb_expires_at = AFS_NO_CB_PROMISE;
se->volume->cb_expires_at = AFS_NO_CB_PROMISE;
trace_afs_cb_v_break(se->volume->vid, atomic_read(&se->volume->cb_v_break),
afs_cb_break_for_s_reinit);
if (!list_empty(&se->volume->open_mmaps))
afs_volume_init_callback(se->volume);
}

up_read(&cell->vs_lock);
up_read(&server->cell->vs_lock);
}

/*
Expand All @@ -79,9 +79,9 @@ void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reas
_enter("");

clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
if (atomic64_xchg(&vnode->cb_expires_at, AFS_NO_CB_PROMISE) != AFS_NO_CB_PROMISE) {
vnode->cb_break++;
vnode->cb_v_break = atomic_read(&vnode->volume->cb_v_break);
vnode->cb_v_check = atomic_read(&vnode->volume->cb_v_break);
afs_clear_permits(vnode);

if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
Expand Down Expand Up @@ -147,29 +147,51 @@ static struct afs_volume *afs_lookup_volume_rcu(struct afs_cell *cell,
return volume;
}

/*
* Allow the fileserver to break callbacks at the volume-level. This is
* typically done when, for example, a R/W volume is snapshotted to a R/O
* volume (the only way to change an R/O volume). It may also, however, happen
* when a volserver takes control of a volume (offlining it, moving it, etc.).
*
* Every file in that volume will need to be reevaluated.
*/
static void afs_break_volume_callback(struct afs_server *server,
struct afs_volume *volume)
__releases(RCU)
{
struct afs_server_list *slist = rcu_dereference(volume->servers);
unsigned int i, cb_v_break;

write_lock(&volume->cb_v_break_lock);

for (i = 0; i < slist->nr_servers; i++)
if (slist->servers[i].server == server)
slist->servers[i].cb_expires_at = AFS_NO_CB_PROMISE;
volume->cb_expires_at = AFS_NO_CB_PROMISE;

cb_v_break = atomic_inc_return_release(&volume->cb_v_break);
trace_afs_cb_v_break(volume->vid, cb_v_break, afs_cb_break_for_volume_callback);

write_unlock(&volume->cb_v_break_lock);
rcu_read_unlock();

if (!list_empty(&volume->open_mmaps))
afs_volume_init_callback(volume);
}

/*
* allow the fileserver to explicitly break one callback
* - happens when
* - the backing file is changed
* - a lock is released
*/
static void afs_break_one_callback(struct afs_volume *volume,
static void afs_break_one_callback(struct afs_server *server,
struct afs_volume *volume,
struct afs_fid *fid)
{
struct super_block *sb;
struct afs_vnode *vnode;
struct inode *inode;
unsigned int cb_v_break;

if (fid->vnode == 0 && fid->unique == 0) {
/* The callback break applies to an entire volume. */
write_lock(&volume->cb_v_break_lock);
cb_v_break = atomic_inc_return(&volume->cb_v_break);
trace_afs_cb_break(fid, cb_v_break,
afs_cb_break_for_volume_callback, false);
write_unlock(&volume->cb_v_break_lock);
return;
}

/* See if we can find a matching inode - even an I_NEW inode needs to
* be marked as it can have its callback broken before we finish
Expand Down Expand Up @@ -199,24 +221,32 @@ static void afs_break_some_callbacks(struct afs_server *server,

rcu_read_lock();
volume = afs_lookup_volume_rcu(server->cell, vid);
/* TODO: Find all matching volumes if we couldn't match the server and
* break them anyway.
*/
for (i = *_count; i > 0; cbb++, i--) {
if (cbb->fid.vid == vid) {
_debug("- Fid { vl=%08llx n=%llu u=%u }",
cbb->fid.vid,
cbb->fid.vnode,
cbb->fid.unique);
--*_count;
if (volume)
afs_break_one_callback(volume, &cbb->fid);
} else {
*residue++ = *cbb;
if (cbb->fid.vnode == 0 && cbb->fid.unique == 0) {
afs_break_volume_callback(server, volume);
*_count -= 1;
if (*_count)
memmove(cbb, cbb + 1, sizeof(*cbb) * *_count);
} else {
/* TODO: Find all matching volumes if we couldn't match the server and
* break them anyway.
*/

for (i = *_count; i > 0; cbb++, i--) {
if (cbb->fid.vid == vid) {
_debug("- Fid { vl=%08llx n=%llu u=%u }",
cbb->fid.vid,
cbb->fid.vnode,
cbb->fid.unique);
--*_count;
if (volume)
afs_break_one_callback(server, volume, &cbb->fid);
} else {
*residue++ = *cbb;
}
}
rcu_read_unlock();
}

rcu_read_unlock();
afs_put_volume(volume, afs_volume_trace_put_callback);
}

Expand Down
2 changes: 0 additions & 2 deletions fs/afs/cell.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ static struct afs_cell *afs_alloc_cell(struct afs_net *net,
seqlock_init(&cell->volume_lock);
cell->fs_servers = RB_ROOT;
seqlock_init(&cell->fs_lock);
INIT_LIST_HEAD(&cell->fs_open_mmaps);
init_rwsem(&cell->fs_open_mmaps_lock);
rwlock_init(&cell->vl_servers_lock);
cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);

Expand Down
10 changes: 8 additions & 2 deletions fs/afs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,12 @@ static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
dir = AFS_FS_I(d_inode(parent));

/* validate the parent directory */
afs_validate(dir, key);
ret = afs_validate(dir, key);
if (ret == -ERESTARTSYS) {
dput(parent);
key_put(key);
return ret;
}

if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
_debug("%pd: parent dir deleted", dentry);
Expand Down Expand Up @@ -1260,6 +1265,7 @@ void afs_check_for_remote_deletion(struct afs_operation *op)
switch (afs_op_abort_code(op)) {
case VNOVNODE:
set_bit(AFS_VNODE_DELETED, &vnode->flags);
clear_nlink(&vnode->netfs.inode);
afs_break_callback(vnode, afs_cb_break_for_deleted);
}
}
Expand Down Expand Up @@ -1375,7 +1381,7 @@ static void afs_dir_remove_subdir(struct dentry *dentry)

clear_nlink(&vnode->netfs.inode);
set_bit(AFS_VNODE_DELETED, &vnode->flags);
clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
atomic64_set(&vnode->cb_expires_at, AFS_NO_CB_PROMISE);
clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
}
}
Expand Down
13 changes: 6 additions & 7 deletions fs/afs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,12 @@ static bool afs_release_folio(struct folio *folio, gfp_t gfp)
static void afs_add_open_mmap(struct afs_vnode *vnode)
{
if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
down_write(&vnode->volume->cell->fs_open_mmaps_lock);
down_write(&vnode->volume->open_mmaps_lock);

if (list_empty(&vnode->cb_mmap_link))
list_add_tail(&vnode->cb_mmap_link,
&vnode->volume->cell->fs_open_mmaps);
list_add_tail(&vnode->cb_mmap_link, &vnode->volume->open_mmaps);

up_write(&vnode->volume->cell->fs_open_mmaps_lock);
up_write(&vnode->volume->open_mmaps_lock);
}
}

Expand All @@ -529,12 +528,12 @@ static void afs_drop_open_mmap(struct afs_vnode *vnode)
if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
return;

down_write(&vnode->volume->cell->fs_open_mmaps_lock);
down_write(&vnode->volume->open_mmaps_lock);

if (atomic_read(&vnode->cb_nr_mmap) == 0)
list_del_init(&vnode->cb_mmap_link);

up_write(&vnode->volume->cell->fs_open_mmaps_lock);
up_write(&vnode->volume->open_mmaps_lock);
flush_work(&vnode->cb_work);
}

Expand Down Expand Up @@ -570,7 +569,7 @@ static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pg
{
struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));

if (afs_pagecache_valid(vnode))
if (afs_check_validity(vnode))
return filemap_map_pages(vmf, start_pgoff, end_pgoff);
return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions fs/afs/fs_operation.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct afs_operation *afs_alloc_operation(struct key *key, struct afs_volume *vo
op->pre_volsync.creation = volume->creation_time;
op->pre_volsync.update = volume->update_time;
op->debug_id = atomic_inc_return(&afs_operation_debug_counter);
op->nr_iterations = -1;
op->nr_iterations = -1;
afs_op_set_error(op, -EDESTADDRREQ);

_leave(" = [op=%08x]", op->debug_id);
Expand Down Expand Up @@ -184,7 +184,6 @@ void afs_wait_for_operation(struct afs_operation *op)
op->call_responded = false;
op->call_error = 0;
op->call_abort_code = 0;
op->cb_s_break = op->server->cb_s_break;
if (test_bit(AFS_SERVER_FL_IS_YFS, &op->server->flags) &&
op->ops->issue_yfs_rpc)
op->ops->issue_yfs_rpc(op);
Expand Down
21 changes: 10 additions & 11 deletions fs/afs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ static int afs_inode_init_from_status(struct afs_operation *op,

write_seqlock(&vnode->cb_lock);

vnode->cb_v_break = op->cb_v_break;
vnode->cb_s_break = op->cb_s_break;
vnode->cb_v_check = op->cb_v_break;
vnode->status = *status;

t = status->mtime_client;
Expand Down Expand Up @@ -146,11 +145,10 @@ static int afs_inode_init_from_status(struct afs_operation *op,
if (!vp->scb.have_cb) {
/* it's a symlink we just created (the fileserver
* didn't give us a callback) */
vnode->cb_expires_at = ktime_get_real_seconds();
atomic64_set(&vnode->cb_expires_at, AFS_NO_CB_PROMISE);
} else {
vnode->cb_expires_at = vp->scb.callback.expires_at;
vnode->cb_server = op->server;
set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
atomic64_set(&vnode->cb_expires_at, vp->scb.callback.expires_at);
}

write_sequnlock(&vnode->cb_lock);
Expand Down Expand Up @@ -214,7 +212,8 @@ static void afs_apply_status(struct afs_operation *op,
vnode->status = *status;

if (vp->dv_before + vp->dv_delta != status->data_version) {
if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags))
if (vnode->cb_ro_snapshot == atomic_read(&vnode->volume->cb_ro_snapshot) &&
atomic64_read(&vnode->cb_expires_at) != AFS_NO_CB_PROMISE)
pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s (op=%x)\n",
vnode->fid.vid, vnode->fid.vnode,
(unsigned long long)vp->dv_before + vp->dv_delta,
Expand Down Expand Up @@ -268,9 +267,9 @@ static void afs_apply_callback(struct afs_operation *op,
struct afs_vnode *vnode = vp->vnode;

if (!afs_cb_is_broken(vp->cb_break_before, vnode)) {
vnode->cb_expires_at = cb->expires_at;
vnode->cb_server = op->server;
set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
if (op->volume->type == AFSVL_RWVOL)
vnode->cb_server = op->server;
atomic64_set(&vnode->cb_expires_at, cb->expires_at);
}
}

Expand Down Expand Up @@ -542,7 +541,7 @@ struct inode *afs_root_iget(struct super_block *sb, struct key *key)
BUG_ON(!(inode->i_state & I_NEW));

vnode = AFS_FS_I(inode);
vnode->cb_v_break = atomic_read(&as->volume->cb_v_break),
vnode->cb_v_check = atomic_read(&as->volume->cb_v_break),
afs_set_netfs_context(vnode);

op = afs_alloc_operation(key, as->volume);
Expand Down Expand Up @@ -587,7 +586,7 @@ int afs_getattr(struct mnt_idmap *idmap, const struct path *path,

if (vnode->volume &&
!(query_flags & AT_STATX_DONT_SYNC) &&
!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
atomic64_read(&vnode->cb_expires_at) == AFS_NO_CB_PROMISE) {
key = afs_request_key(vnode->volume->cell);
if (IS_ERR(key))
return PTR_ERR(key);
Expand Down
Loading

0 comments on commit 453924d

Please sign in to comment.