Add zfs_file_* interface, remove vnodes

Provide a common zfs_file_* interface which can be implemented on all 
platforms to perform normal file access from either the kernel module
or the libzpool library.

This allows all non-portable vnode_t usage in the common code to be 
replaced by the new portable zfs_file_t.  The associated vnode and
kobj compatibility functions, types, and macros have been removed
from the SPL.  Moving forward, vnodes should only be used in platform
specific code when provided by the native operating system.

Reviewed-by: Sean Eric Fagan <sef@ixsystems.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Igor Kozhukhov <igor@dilos.org>
Reviewed-by: Jorgen Lundman <lundman@lundman.net>
Signed-off-by: Matt Macy <mmacy@FreeBSD.org>
Closes #9556
This commit is contained in:
Matthew Macy
2019-11-21 09:32:57 -08:00
committed by Brian Behlendorf
parent 67a6c3bc9f
commit da92d5cbb3
53 changed files with 1169 additions and 1554 deletions
+33 -49
View File
@@ -37,8 +37,8 @@
#include <sys/systeminfo.h>
#include <sys/sunddi.h>
#include <sys/zfeature.h>
#include <sys/zfs_file.h>
#ifdef _KERNEL
#include <sys/kobj.h>
#include <sys/zone.h>
#endif
@@ -80,8 +80,10 @@ spa_config_load(void)
nvlist_t *nvlist, *child;
nvpair_t *nvpair;
char *pathname;
struct _buf *file;
zfs_file_t *fp;
zfs_file_attr_t zfa;
uint64_t fsize;
int err;
#ifdef _KERNEL
if (zfs_autoimport_disable)
@@ -95,22 +97,23 @@ spa_config_load(void)
(void) snprintf(pathname, MAXPATHLEN, "%s", spa_config_path);
file = kobj_open_file(pathname);
err = zfs_file_open(pathname, O_RDONLY, 0, &fp);
kmem_free(pathname, MAXPATHLEN);
if (file == (struct _buf *)-1)
if (err)
return;
if (kobj_get_filesize(file, &fsize) != 0)
if (zfs_file_getattr(fp, &zfa))
goto out;
fsize = zfa.zfa_size;
buf = kmem_alloc(fsize, KM_SLEEP);
/*
* Read the nvlist from the file.
*/
if (kobj_read_file(file, buf, fsize, 0) < 0)
if (zfs_file_read(fp, buf, fsize, NULL) < 0)
goto out;
/*
@@ -143,27 +146,32 @@ out:
if (buf != NULL)
kmem_free(buf, fsize);
kobj_close_file(file);
zfs_file_close(fp);
}
static int
spa_config_remove(spa_config_dirent_t *dp)
{
#if defined(__linux__) && defined(_KERNEL)
int error, flags = FWRITE | FTRUNC;
uio_seg_t seg = UIO_SYSSPACE;
vnode_t *vp;
int error = 0;
error = vn_open(dp->scd_path, seg, flags, 0644, &vp, 0, 0);
if (error == 0) {
(void) VOP_FSYNC(vp, FSYNC, kcred, NULL);
(void) VOP_CLOSE(vp, 0, 1, 0, kcred, NULL);
/*
* Remove the cache file. If zfs_file_unlink() in not supported by the
* platform fallback to truncating the file which is functionally
* equivalent.
*/
error = zfs_file_unlink(dp->scd_path);
if (error == EOPNOTSUPP) {
int flags = O_RDWR | O_TRUNC;
zfs_file_t *fp;
error = zfs_file_open(dp->scd_path, flags, 0644, &fp);
if (error == 0) {
(void) zfs_file_fsync(fp, O_SYNC);
(void) zfs_file_close(fp);
}
}
return (error);
#else
return (vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE));
#endif
}
static int
@@ -171,10 +179,10 @@ spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
{
size_t buflen;
char *buf;
vnode_t *vp;
int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
int oflags = O_RDWR | O_TRUNC | O_CREAT | O_LARGEFILE;
char *temp;
int err;
zfs_file_t *fp;
/*
* If the nvlist is empty (NULL), then remove the old cachefile.
@@ -193,46 +201,22 @@ spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
buf = fnvlist_pack(nvl, &buflen);
temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
#if defined(__linux__) && defined(_KERNEL)
/*
* Write the configuration to disk. Due to the complexity involved
* in performing a rename and remove from within the kernel the file
* is instead truncated and overwritten in place. This way we always
* have a consistent view of the data or a zero length file.
*/
err = vn_open(dp->scd_path, UIO_SYSSPACE, oflags, 0644, &vp, 0, 0);
err = zfs_file_open(dp->scd_path, oflags, 0644, &fp);
if (err == 0) {
err = vn_rdwr(UIO_WRITE, vp, buf, buflen, 0,
UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, NULL);
err = zfs_file_write(fp, buf, buflen, NULL);
if (err == 0)
err = VOP_FSYNC(vp, FSYNC, kcred, NULL);
err = zfs_file_fsync(fp, O_SYNC);
(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
zfs_file_close(fp);
if (err)
(void) spa_config_remove(dp);
}
#else
/*
* Write the configuration to disk. We need to do the traditional
* 'write to temporary file, sync, move over original' to make sure we
* always have a consistent view of the data.
*/
(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
err = vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0);
if (err == 0) {
err = vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
0, RLIM64_INFINITY, kcred, NULL);
if (err == 0)
err = VOP_FSYNC(vp, FSYNC, kcred, NULL);
if (err == 0)
err = vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
}
(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
#endif
fnvlist_pack_free(buf, buflen);
kmem_free(temp, MAXPATHLEN);
return (err);
@@ -258,7 +242,7 @@ spa_write_cachefile(spa_t *target, boolean_t removing, boolean_t postsysevent)
ASSERT(MUTEX_HELD(&spa_namespace_lock));
if (rootdir == NULL || !(spa_mode_global & FWRITE))
if (!(spa_mode_global & SPA_MODE_WRITE))
return;
/*