Management of the lifetime of afs_cell struct has some problems due to the
usage counter being used to determine whether objects of that type are in
use in addition to whether anyone might be interested in the structure.
This is made trickier by cell objects being cached for a period of time in
case they're quickly reused as they hold the result of a setup process that
may be slow (DNS lookups, AFS RPC ops).
Problems include the cached root volume from alias resolution pinning its
parent cell record, rmmod occasionally hanging and occasionally producing
assertion failures.
Fix this by splitting the count of active users from the struct reference
count. Things then work as follows:
(1) The cell cache keeps +1 on the cell's activity count and this has to
be dropped before the cell can be removed. afs_manage_cell() tries to
exchange the 1 to a 0 with the cells_lock write-locked, and if
successful, the record is removed from the net->cells.
(2) One struct ref is 'owned' by the activity count. That is put when the
active count is reduced to 0 (final_destruction label).
(3) A ref can be held on a cell whilst it is queued for management on a
work queue without confusing the active count. afs_queue_cell() is
added to wrap this.
(4) The queue's ref is dropped at the end of the management. This is
split out into a separate function, afs_manage_cell_work().
(5) The root volume record is put after a cell is removed (at the
final_destruction label) rather then in the RCU destruction routine.
(6) Volumes hold struct refs, but aren't active users.
(7) Both counts are displayed in /proc/net/afs/cells.
There are some management function changes:
(*) afs_put_cell() now just decrements the refcount and triggers the RCU
destruction if it becomes 0. It no longer sets a timer to have the
manager do this.
(*) afs_use_cell() and afs_unuse_cell() are added to increase and decrease
the active count. afs_unuse_cell() sets the management timer.
(*) afs_queue_cell() is added to queue a cell with approprate refs.
There are also some other fixes:
(*) Don't let /proc/net/afs/cells access a cell's vllist if it's NULL.
(*) Make sure that candidate cells in lookups are properly destroyed
rather than being simply kfree'd. This ensures the bits it points to
are destroyed also.
(*) afs_dec_cells_outstanding() is now called in cell destruction rather
than at "final_destruction". This ensures that cell->net is still
valid to the end of the destructor.
(*) As a consequence of the previous two changes, move the increment of
net->cells_outstanding that was at the point of insertion into the
tree to the allocation routine to correctly balance things.
Fixes: 989782dcdc
("afs: Overhaul cell database management")
Signed-off-by: David Howells <dhowells@redhat.com>
227 lines
5.1 KiB
C
227 lines
5.1 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* mountpoint management
|
|
*
|
|
* Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/pagemap.h>
|
|
#include <linux/mount.h>
|
|
#include <linux/namei.h>
|
|
#include <linux/gfp.h>
|
|
#include <linux/fs_context.h>
|
|
#include "internal.h"
|
|
|
|
|
|
static struct dentry *afs_mntpt_lookup(struct inode *dir,
|
|
struct dentry *dentry,
|
|
unsigned int flags);
|
|
static int afs_mntpt_open(struct inode *inode, struct file *file);
|
|
static void afs_mntpt_expiry_timed_out(struct work_struct *work);
|
|
|
|
const struct file_operations afs_mntpt_file_operations = {
|
|
.open = afs_mntpt_open,
|
|
.llseek = noop_llseek,
|
|
};
|
|
|
|
const struct inode_operations afs_mntpt_inode_operations = {
|
|
.lookup = afs_mntpt_lookup,
|
|
.readlink = page_readlink,
|
|
.getattr = afs_getattr,
|
|
.listxattr = afs_listxattr,
|
|
};
|
|
|
|
const struct inode_operations afs_autocell_inode_operations = {
|
|
.getattr = afs_getattr,
|
|
};
|
|
|
|
static LIST_HEAD(afs_vfsmounts);
|
|
static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out);
|
|
|
|
static unsigned long afs_mntpt_expiry_timeout = 10 * 60;
|
|
|
|
static const char afs_root_volume[] = "root.cell";
|
|
|
|
/*
|
|
* no valid lookup procedure on this sort of dir
|
|
*/
|
|
static struct dentry *afs_mntpt_lookup(struct inode *dir,
|
|
struct dentry *dentry,
|
|
unsigned int flags)
|
|
{
|
|
_enter("%p,%p{%pd2}", dir, dentry, dentry);
|
|
return ERR_PTR(-EREMOTE);
|
|
}
|
|
|
|
/*
|
|
* no valid open procedure on this sort of dir
|
|
*/
|
|
static int afs_mntpt_open(struct inode *inode, struct file *file)
|
|
{
|
|
_enter("%p,%p{%pD2}", inode, file, file);
|
|
return -EREMOTE;
|
|
}
|
|
|
|
/*
|
|
* Set the parameters for the proposed superblock.
|
|
*/
|
|
static int afs_mntpt_set_params(struct fs_context *fc, struct dentry *mntpt)
|
|
{
|
|
struct afs_fs_context *ctx = fc->fs_private;
|
|
struct afs_super_info *src_as = AFS_FS_S(mntpt->d_sb);
|
|
struct afs_vnode *vnode = AFS_FS_I(d_inode(mntpt));
|
|
struct afs_cell *cell;
|
|
const char *p;
|
|
int ret;
|
|
|
|
if (fc->net_ns != src_as->net_ns) {
|
|
put_net(fc->net_ns);
|
|
fc->net_ns = get_net(src_as->net_ns);
|
|
}
|
|
|
|
if (src_as->volume && src_as->volume->type == AFSVL_RWVOL) {
|
|
ctx->type = AFSVL_RWVOL;
|
|
ctx->force = true;
|
|
}
|
|
if (ctx->cell) {
|
|
afs_unuse_cell(ctx->net, ctx->cell);
|
|
ctx->cell = NULL;
|
|
}
|
|
if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) {
|
|
/* if the directory is a pseudo directory, use the d_name */
|
|
unsigned size = mntpt->d_name.len;
|
|
|
|
if (size < 2)
|
|
return -ENOENT;
|
|
|
|
p = mntpt->d_name.name;
|
|
if (mntpt->d_name.name[0] == '.') {
|
|
size--;
|
|
p++;
|
|
ctx->type = AFSVL_RWVOL;
|
|
ctx->force = true;
|
|
}
|
|
if (size > AFS_MAXCELLNAME)
|
|
return -ENAMETOOLONG;
|
|
|
|
cell = afs_lookup_cell(ctx->net, p, size, NULL, false);
|
|
if (IS_ERR(cell)) {
|
|
pr_err("kAFS: unable to lookup cell '%pd'\n", mntpt);
|
|
return PTR_ERR(cell);
|
|
}
|
|
ctx->cell = cell;
|
|
|
|
ctx->volname = afs_root_volume;
|
|
ctx->volnamesz = sizeof(afs_root_volume) - 1;
|
|
} else {
|
|
/* read the contents of the AFS special symlink */
|
|
struct page *page;
|
|
loff_t size = i_size_read(d_inode(mntpt));
|
|
char *buf;
|
|
|
|
if (src_as->cell)
|
|
ctx->cell = afs_use_cell(src_as->cell);
|
|
|
|
if (size < 2 || size > PAGE_SIZE - 1)
|
|
return -EINVAL;
|
|
|
|
page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL);
|
|
if (IS_ERR(page))
|
|
return PTR_ERR(page);
|
|
|
|
if (PageError(page)) {
|
|
ret = afs_bad(AFS_FS_I(d_inode(mntpt)), afs_file_error_mntpt);
|
|
put_page(page);
|
|
return ret;
|
|
}
|
|
|
|
buf = kmap(page);
|
|
ret = -EINVAL;
|
|
if (buf[size - 1] == '.')
|
|
ret = vfs_parse_fs_string(fc, "source", buf, size - 1);
|
|
kunmap(page);
|
|
put_page(page);
|
|
if (ret < 0)
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* create a vfsmount to be automounted
|
|
*/
|
|
static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt)
|
|
{
|
|
struct fs_context *fc;
|
|
struct vfsmount *mnt;
|
|
int ret;
|
|
|
|
BUG_ON(!d_inode(mntpt));
|
|
|
|
fc = fs_context_for_submount(&afs_fs_type, mntpt);
|
|
if (IS_ERR(fc))
|
|
return ERR_CAST(fc);
|
|
|
|
ret = afs_mntpt_set_params(fc, mntpt);
|
|
if (!ret)
|
|
mnt = fc_mount(fc);
|
|
else
|
|
mnt = ERR_PTR(ret);
|
|
|
|
put_fs_context(fc);
|
|
return mnt;
|
|
}
|
|
|
|
/*
|
|
* handle an automount point
|
|
*/
|
|
struct vfsmount *afs_d_automount(struct path *path)
|
|
{
|
|
struct vfsmount *newmnt;
|
|
|
|
_enter("{%pd}", path->dentry);
|
|
|
|
newmnt = afs_mntpt_do_automount(path->dentry);
|
|
if (IS_ERR(newmnt))
|
|
return newmnt;
|
|
|
|
mntget(newmnt); /* prevent immediate expiration */
|
|
mnt_set_expiry(newmnt, &afs_vfsmounts);
|
|
queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
|
|
afs_mntpt_expiry_timeout * HZ);
|
|
_leave(" = %p", newmnt);
|
|
return newmnt;
|
|
}
|
|
|
|
/*
|
|
* handle mountpoint expiry timer going off
|
|
*/
|
|
static void afs_mntpt_expiry_timed_out(struct work_struct *work)
|
|
{
|
|
_enter("");
|
|
|
|
if (!list_empty(&afs_vfsmounts)) {
|
|
mark_mounts_for_expiry(&afs_vfsmounts);
|
|
queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer,
|
|
afs_mntpt_expiry_timeout * HZ);
|
|
}
|
|
|
|
_leave("");
|
|
}
|
|
|
|
/*
|
|
* kill the AFS mountpoint timer if it's still running
|
|
*/
|
|
void afs_mntpt_kill_timer(void)
|
|
{
|
|
_enter("");
|
|
|
|
ASSERT(list_empty(&afs_vfsmounts));
|
|
cancel_delayed_work_sync(&afs_mntpt_expiry_timer);
|
|
}
|