Support for longnames for files/directories (Linux part)

This patch adds the ability for zfs to support file/dir name up to 1023
bytes. This number is chosen so we can support up to 255 4-byte
characters. This new feature is represented by the new feature flag
feature@longname.

A new dataset property "longname" is also introduced to toggle longname
support for each dataset individually. This property can be disabled,
even if it contains longname files. In such case, new file cannot be
created with longname but existing longname files can still be looked
up.

Note that, to my knowledge native Linux filesystems don't support name
longer than 255 bytes. So there might be programs not able to work with
longname.

Note that NFS server may needs to use exportfs_get_name to reconnect
dentries, and the buffer being passed is limit to NAME_MAX+1 (256). So
NFS may not work when longname is enabled.

Note, FreeBSD vfs layer imposes a limit of 255 name lengh, so even
though we add code to support it here, it won't actually work.

Reviewed-by: Tony Hutter <hutter2@llnl.gov>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Signed-off-by: Chunwei Chen <david.chen@nutanix.com>
Closes #15921
This commit is contained in:
Sanjeev Bagewadi
2021-06-18 08:55:01 +00:00
committed by Brian Behlendorf
parent 3cf2bfa570
commit 20232ecfaa
41 changed files with 1239 additions and 406 deletions
+9
View File
@@ -847,6 +847,15 @@ zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
return (error);
}
/*
* If we added a longname activate the SPA_FEATURE_LONGNAME.
*/
if (strlen(dl->dl_name) >= ZAP_MAXNAMELEN) {
dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
ds->ds_feature_activation[SPA_FEATURE_LONGNAME] =
(void *)B_TRUE;
}
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
&dzp->z_id, sizeof (dzp->z_id));
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
+11 -1
View File
@@ -57,6 +57,7 @@
#include <sys/dmu_objset.h>
#include <sys/dsl_dir.h>
#include <sys/objlist.h>
#include <sys/zfeature.h>
#include <sys/zpl.h>
#include <linux/vfs_compat.h>
#include <linux/fs.h>
@@ -449,6 +450,12 @@ acl_inherit_changed_cb(void *arg, uint64_t newval)
((zfsvfs_t *)arg)->z_acl_inherit = newval;
}
static void
longname_changed_cb(void *arg, uint64_t newval)
{
((zfsvfs_t *)arg)->z_longname = newval;
}
static int
zfs_register_callbacks(vfs_t *vfsp)
{
@@ -509,6 +516,8 @@ zfs_register_callbacks(vfs_t *vfsp)
zfsvfs);
error = error ? error : dsl_prop_register(ds,
zfs_prop_to_name(ZFS_PROP_NBMAND), nbmand_changed_cb, zfsvfs);
error = error ? error : dsl_prop_register(ds,
zfs_prop_to_name(ZFS_PROP_LONGNAME), longname_changed_cb, zfsvfs);
dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
if (error)
goto unregister;
@@ -1140,7 +1149,8 @@ zfs_statvfs(struct inode *ip, struct kstatfs *statp)
statp->f_fsid.val[0] = (uint32_t)fsid;
statp->f_fsid.val[1] = (uint32_t)(fsid >> 32);
statp->f_type = ZFS_SUPER_MAGIC;
statp->f_namelen = MAXNAMELEN - 1;
statp->f_namelen =
zfsvfs->z_longname ? (ZAP_MAXNAMELEN_NEW - 1) : (MAXNAMELEN - 1);
/*
* We have all of 40 characters to stuff a string here.
+41 -1
View File
@@ -542,6 +542,46 @@ zfs_lookup(znode_t *zdp, char *nm, znode_t **zpp, int flags, cred_t *cr,
return (error);
}
/*
* Perform a linear search in directory for the name of specific inode.
* Note we don't pass in the buffer size of name because it's hardcoded to
* NAME_MAX+1(256) in Linux.
*
* IN: dzp - znode of directory to search.
* zp - znode of the target
*
* OUT: name - dentry name of the target
*
* RETURN: 0 on success, error code on failure.
*/
int
zfs_get_name(znode_t *dzp, char *name, znode_t *zp)
{
zfsvfs_t *zfsvfs = ZTOZSB(dzp);
int error = 0;
if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
return (error);
if ((error = zfs_verify_zp(zp)) != 0) {
zfs_exit(zfsvfs, FTAG);
return (error);
}
/* ctldir should have got their name in zfs_vget */
if (dzp->z_is_ctldir || zp->z_is_ctldir) {
zfs_exit(zfsvfs, FTAG);
return (ENOENT);
}
/* buffer len is hardcoded to 256 in Linux kernel */
error = zap_value_search(zfsvfs->z_os, dzp->z_id, zp->z_id,
ZFS_DIRENT_OBJ(-1ULL), name, ZAP_MAXNAMELEN);
zfs_exit(zfsvfs, FTAG);
return (error);
}
/*
* Attempt to create a new entry in a directory. If the entry
* already exists, truncate the file if permissible, else return
@@ -1548,7 +1588,7 @@ zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
os = zfsvfs->z_os;
offset = ctx->pos;
prefetch = zp->z_zn_prefetch;
zap = zap_attribute_alloc();
zap = zap_attribute_long_alloc();
/*
* Initialize the iterator cursor.
+31
View File
@@ -24,6 +24,7 @@
*/
#include <sys/file.h>
#include <sys/zfs_znode.h>
#include <sys/zfs_vnops.h>
#include <sys/zfs_ctldir.h>
@@ -102,6 +103,35 @@ zpl_fh_to_dentry(struct super_block *sb, struct fid *fh,
return (d_obtain_alias(ip));
}
/*
* In case the filesystem contains name longer than 255, we need to override
* the default get_name so we don't get buffer overflow. Unfortunately, since
* the buffer size is hardcoded in Linux, we will get ESTALE error in this
* case.
*/
static int
zpl_get_name(struct dentry *parent, char *name, struct dentry *child)
{
cred_t *cr = CRED();
fstrans_cookie_t cookie;
struct inode *dir = parent->d_inode;
struct inode *ip = child->d_inode;
int error;
if (!dir || !S_ISDIR(dir->i_mode))
return (-ENOTDIR);
crhold(cr);
cookie = spl_fstrans_mark();
spl_inode_lock_shared(dir);
error = -zfs_get_name(ITOZ(dir), name, ITOZ(ip));
spl_inode_unlock_shared(dir);
spl_fstrans_unmark(cookie);
crfree(cr);
return (error);
}
static struct dentry *
zpl_get_parent(struct dentry *child)
{
@@ -146,6 +176,7 @@ zpl_commit_metadata(struct inode *inode)
const struct export_operations zpl_export_operations = {
.encode_fh = zpl_encode_fh,
.fh_to_dentry = zpl_fh_to_dentry,
.get_name = zpl_get_name,
.get_parent = zpl_get_parent,
.commit_metadata = zpl_commit_metadata,
};
+55 -1
View File
@@ -46,9 +46,29 @@ zpl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
pathname_t pn;
int zfs_flags = 0;
zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
size_t dlen = dlen(dentry);
if (dlen(dentry) >= ZAP_MAXNAMELEN)
/*
* If z_longname is disabled, disallow create or rename of names
* longer than ZAP_MAXNAMELEN.
*
* This is needed in cases where longname was enabled first and some
* files/dirs with names > ZAP_MAXNAMELEN were created. And later
* longname was disabled. In such a case allow access to existing
* longnames. But disallow creation newer longnamed entities.
*/
if (!zfsvfs->z_longname && (dlen >= ZAP_MAXNAMELEN)) {
/*
* If this is for create or rename fail it.
*/
if (!dsl_dataset_feature_is_active(ds, SPA_FEATURE_LONGNAME) ||
(flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)))
return (ERR_PTR(-ENAMETOOLONG));
}
if (dlen >= ZAP_MAXNAMELEN_NEW) {
return (ERR_PTR(-ENAMETOOLONG));
}
crhold(cr);
cookie = spl_fstrans_mark();
@@ -131,6 +151,16 @@ zpl_vap_init(vattr_t *vap, struct inode *dir, umode_t mode, cred_t *cr,
}
}
static inline bool
is_nametoolong(struct dentry *dentry)
{
zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
size_t dlen = dlen(dentry);
return ((!zfsvfs->z_longname && dlen >= ZAP_MAXNAMELEN) ||
dlen >= ZAP_MAXNAMELEN_NEW);
}
static int
#ifdef HAVE_IOPS_CREATE_USERNS
zpl_create(struct user_namespace *user_ns, struct inode *dir,
@@ -151,6 +181,10 @@ zpl_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool flag)
zidmap_t *user_ns = kcred->user_ns;
#endif
if (is_nametoolong(dentry)) {
return (-ENAMETOOLONG);
}
crhold(cr);
vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
zpl_vap_init(vap, dir, mode, cr, user_ns);
@@ -201,6 +235,10 @@ zpl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
zidmap_t *user_ns = kcred->user_ns;
#endif
if (is_nametoolong(dentry)) {
return (-ENAMETOOLONG);
}
/*
* We currently expect Linux to supply rdev=0 for all sockets
* and fifos, but we want to know if this behavior ever changes.
@@ -353,6 +391,10 @@ zpl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
zidmap_t *user_ns = kcred->user_ns;
#endif
if (is_nametoolong(dentry)) {
return (-ENAMETOOLONG);
}
crhold(cr);
vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
zpl_vap_init(vap, dir, mode | S_IFDIR, cr, user_ns);
@@ -568,6 +610,10 @@ zpl_rename2(struct inode *sdip, struct dentry *sdentry,
zidmap_t *user_ns = kcred->user_ns;
#endif
if (is_nametoolong(tdentry)) {
return (-ENAMETOOLONG);
}
crhold(cr);
if (rflags & RENAME_WHITEOUT) {
wo_vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
@@ -618,6 +664,10 @@ zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
zidmap_t *user_ns = kcred->user_ns;
#endif
if (is_nametoolong(dentry)) {
return (-ENAMETOOLONG);
}
crhold(cr);
vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr, user_ns);
@@ -707,6 +757,10 @@ zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
int error;
fstrans_cookie_t cookie;
if (is_nametoolong(dentry)) {
return (-ENAMETOOLONG);
}
if (ip->i_nlink >= ZFS_LINK_MAX)
return (-EMLINK);