Path: blob/main/sys/contrib/openzfs/module/os/linux/zfs/zpl_ctldir.c
48775 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (C) 2011 Lawrence Livermore National Security, LLC.23* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).24* LLNL-CODE-403049.25* Rewritten for Linux by:26* Rohan Puri <[email protected]>27* Brian Behlendorf <[email protected]>28*/2930#include <sys/zfs_znode.h>31#include <sys/zfs_vfsops.h>32#include <sys/zfs_vnops.h>33#include <sys/zfs_ctldir.h>34#include <sys/zpl.h>35#include <sys/dmu.h>36#include <sys/dsl_dataset.h>37#include <sys/zap.h>3839/*40* Common open routine. Disallow any write access.41*/42static int43zpl_common_open(struct inode *ip, struct file *filp)44{45if (blk_mode_is_open_write(filp->f_mode))46return (-EACCES);4748return (generic_file_open(ip, filp));49}5051/*52* Get root directory contents.53*/54static int55zpl_root_iterate(struct file *filp, struct dir_context *ctx)56{57zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));58int error = 0;5960if (zfsvfs->z_show_ctldir == ZFS_SNAPDIR_DISABLED) {61return (SET_ERROR(ENOENT));62}6364if ((error = zpl_enter(zfsvfs, FTAG)) != 0)65return (error);6667if (!dir_emit_dots(filp, ctx))68goto out;6970if (ctx->pos == 2) {71if (!dir_emit(ctx, ZFS_SNAPDIR_NAME,72strlen(ZFS_SNAPDIR_NAME), ZFSCTL_INO_SNAPDIR, DT_DIR))73goto out;7475ctx->pos++;76}7778if (ctx->pos == 3) {79if (!dir_emit(ctx, ZFS_SHAREDIR_NAME,80strlen(ZFS_SHAREDIR_NAME), ZFSCTL_INO_SHARES, DT_DIR))81goto out;8283ctx->pos++;84}85out:86zpl_exit(zfsvfs, FTAG);8788return (error);89}9091/*92* Get root directory attributes.93*/94static int95#ifdef HAVE_IDMAP_IOPS_GETATTR96zpl_root_getattr_impl(struct mnt_idmap *user_ns,97const struct path *path, struct kstat *stat, u32 request_mask,98unsigned int query_flags)99#elif defined(HAVE_USERNS_IOPS_GETATTR)100zpl_root_getattr_impl(struct user_namespace *user_ns,101const struct path *path, struct kstat *stat, u32 request_mask,102unsigned int query_flags)103#else104zpl_root_getattr_impl(const struct path *path, struct kstat *stat,105u32 request_mask, unsigned int query_flags)106#endif107{108(void) request_mask, (void) query_flags;109struct inode *ip = path->dentry->d_inode;110111#if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))112#ifdef HAVE_GENERIC_FILLATTR_USERNS113generic_fillattr(user_ns, ip, stat);114#elif defined(HAVE_GENERIC_FILLATTR_IDMAP)115generic_fillattr(user_ns, ip, stat);116#elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)117generic_fillattr(user_ns, request_mask, ip, stat);118#else119(void) user_ns;120#endif121#else122generic_fillattr(ip, stat);123#endif124stat->atime = current_time(ip);125126return (0);127}128ZPL_GETATTR_WRAPPER(zpl_root_getattr);129130static struct dentry *131zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)132{133cred_t *cr = CRED();134struct inode *ip;135int error;136137crhold(cr);138error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL);139ASSERT3S(error, <=, 0);140crfree(cr);141142if (error) {143if (error == -ENOENT)144return (d_splice_alias(NULL, dentry));145else146return (ERR_PTR(error));147}148149return (d_splice_alias(ip, dentry));150}151152/*153* The '.zfs' control directory file and inode operations.154*/155const struct file_operations zpl_fops_root = {156.open = zpl_common_open,157.llseek = generic_file_llseek,158.read = generic_read_dir,159.iterate_shared = zpl_root_iterate,160};161162const struct inode_operations zpl_ops_root = {163.lookup = zpl_root_lookup,164.getattr = zpl_root_getattr,165};166167static struct vfsmount *168zpl_snapdir_automount(struct path *path)169{170int error;171172error = -zfsctl_snapshot_mount(path, 0);173if (error)174return (ERR_PTR(error));175176/*177* Rather than returning the new vfsmount for the snapshot we must178* return NULL to indicate a mount collision. This is done because179* the user space mount calls do_add_mount() which adds the vfsmount180* to the name space. If we returned the new mount here it would be181* added again to the vfsmount list resulting in list corruption.182*/183return (NULL);184}185186/*187* Negative dentries must always be revalidated so newly created snapshots188* can be detected and automounted. Normal dentries should be kept because189* as of the 3.18 kernel revaliding the mountpoint dentry will result in190* the snapshot being immediately unmounted.191*/192#ifdef HAVE_D_REVALIDATE_4ARGS193static int194zpl_snapdir_revalidate(struct inode *dir, const struct qstr *name,195struct dentry *dentry, unsigned int flags)196#else197static int198zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)199#endif200{201return (!!dentry->d_inode);202}203204static const struct dentry_operations zpl_dops_snapdirs = {205/*206* Auto mounting of snapshots is only supported for 2.6.37 and207* newer kernels. Prior to this kernel the ops->follow_link()208* callback was used as a hack to trigger the mount. The209* resulting vfsmount was then explicitly grafted in to the210* name space. While it might be possible to add compatibility211* code to accomplish this it would require considerable care.212*/213.d_automount = zpl_snapdir_automount,214.d_revalidate = zpl_snapdir_revalidate,215};216217/*218* For the .zfs control directory to work properly we must be able to override219* the default operations table and register custom .d_automount and220* .d_revalidate callbacks.221*/222static void223set_snapdir_dentry_ops(struct dentry *dentry, unsigned int extraflags) {224static const unsigned int op_flags =225DCACHE_OP_HASH | DCACHE_OP_COMPARE |226DCACHE_OP_REVALIDATE | DCACHE_OP_DELETE |227DCACHE_OP_PRUNE | DCACHE_OP_WEAK_REVALIDATE | DCACHE_OP_REAL;228229#ifdef HAVE_D_SET_D_OP230/*231* d_set_d_op() will set the DCACHE_OP_ flags according to what it232* finds in the passed dentry_operations, so we don't have to.233*234* We clear the flags and the old op table before calling d_set_d_op()235* because issues a warning when the dentry operations table is already236* set.237*/238dentry->d_op = NULL;239dentry->d_flags &= ~op_flags;240d_set_d_op(dentry, &zpl_dops_snapdirs);241dentry->d_flags |= extraflags;242#else243/*244* Since 6.17 there's no exported way to modify dentry ops, so we have245* to reach in and do it ourselves. This should be safe for our very246* narrow use case, which is to create or splice in an entry to give247* access to a snapshot.248*249* We need to set the op flags directly. We hardcode250* DCACHE_OP_REVALIDATE because that's the only operation we have; if251* we ever extend zpl_dops_snapdirs we will need to update the op flags252* to match.253*/254spin_lock(&dentry->d_lock);255dentry->d_op = &zpl_dops_snapdirs;256dentry->d_flags &= ~op_flags;257dentry->d_flags |= DCACHE_OP_REVALIDATE | extraflags;258spin_unlock(&dentry->d_lock);259#endif260}261262static struct dentry *263zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,264unsigned int flags)265{266fstrans_cookie_t cookie;267cred_t *cr = CRED();268struct inode *ip = NULL;269int error;270271crhold(cr);272cookie = spl_fstrans_mark();273error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,2740, cr, NULL, NULL);275ASSERT3S(error, <=, 0);276spl_fstrans_unmark(cookie);277crfree(cr);278279if (error && error != -ENOENT)280return (ERR_PTR(error));281282ASSERT(error == 0 || ip == NULL);283set_snapdir_dentry_ops(dentry, DCACHE_NEED_AUTOMOUNT);284return (d_splice_alias(ip, dentry));285}286287static int288zpl_snapdir_iterate(struct file *filp, struct dir_context *ctx)289{290zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));291fstrans_cookie_t cookie;292char snapname[MAXNAMELEN];293boolean_t case_conflict;294uint64_t id, pos;295int error = 0;296297if ((error = zpl_enter(zfsvfs, FTAG)) != 0)298return (error);299cookie = spl_fstrans_mark();300301if (!dir_emit_dots(filp, ctx))302goto out;303304/* Start the position at 0 if it already emitted . and .. */305pos = (ctx->pos == 2 ? 0 : ctx->pos);306while (error == 0) {307dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);308error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN,309snapname, &id, &pos, &case_conflict);310dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);311if (error)312goto out;313314if (!dir_emit(ctx, snapname, strlen(snapname),315ZFSCTL_INO_SHARES - id, DT_DIR))316goto out;317318ctx->pos = pos;319}320out:321spl_fstrans_unmark(cookie);322zpl_exit(zfsvfs, FTAG);323324if (error == -ENOENT)325return (0);326327return (error);328}329330static int331#ifdef HAVE_IOPS_RENAME_USERNS332zpl_snapdir_rename2(struct user_namespace *user_ns, struct inode *sdip,333struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,334unsigned int flags)335#elif defined(HAVE_IOPS_RENAME_IDMAP)336zpl_snapdir_rename2(struct mnt_idmap *user_ns, struct inode *sdip,337struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,338unsigned int flags)339#else340zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry,341struct inode *tdip, struct dentry *tdentry, unsigned int flags)342#endif343{344cred_t *cr = CRED();345int error;346347/* We probably don't want to support renameat2(2) in ctldir */348if (flags)349return (-EINVAL);350351crhold(cr);352error = -zfsctl_snapdir_rename(sdip, dname(sdentry),353tdip, dname(tdentry), cr, 0);354ASSERT3S(error, <=, 0);355crfree(cr);356357return (error);358}359360#if (!defined(HAVE_RENAME_WANTS_FLAGS) && \361!defined(HAVE_IOPS_RENAME_USERNS) && \362!defined(HAVE_IOPS_RENAME_IDMAP))363static int364zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,365struct inode *tdip, struct dentry *tdentry)366{367return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0));368}369#endif370371static int372zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)373{374cred_t *cr = CRED();375int error;376377crhold(cr);378error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);379ASSERT3S(error, <=, 0);380crfree(cr);381382return (error);383}384385#if defined(HAVE_IOPS_MKDIR_USERNS)386static int387zpl_snapdir_mkdir(struct user_namespace *user_ns, struct inode *dip,388struct dentry *dentry, umode_t mode)389#elif defined(HAVE_IOPS_MKDIR_IDMAP)390static int391zpl_snapdir_mkdir(struct mnt_idmap *user_ns, struct inode *dip,392struct dentry *dentry, umode_t mode)393#elif defined(HAVE_IOPS_MKDIR_DENTRY)394static struct dentry *395zpl_snapdir_mkdir(struct mnt_idmap *user_ns, struct inode *dip,396struct dentry *dentry, umode_t mode)397#else398static int399zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)400#endif401{402cred_t *cr = CRED();403vattr_t *vap;404struct inode *ip;405int error;406407crhold(cr);408vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);409#if (defined(HAVE_IOPS_MKDIR_USERNS) || defined(HAVE_IOPS_MKDIR_IDMAP))410zpl_vap_init(vap, dip, mode | S_IFDIR, cr, user_ns);411#else412zpl_vap_init(vap, dip, mode | S_IFDIR, cr, zfs_init_idmap);413#endif414415error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);416if (error == 0) {417set_snapdir_dentry_ops(dentry, 0);418d_instantiate(dentry, ip);419}420421kmem_free(vap, sizeof (vattr_t));422ASSERT3S(error, <=, 0);423crfree(cr);424425#if defined(HAVE_IOPS_MKDIR_DENTRY)426return (ERR_PTR(error));427#else428return (error);429#endif430}431432/*433* Get snapshot directory attributes.434*/435static int436#ifdef HAVE_IDMAP_IOPS_GETATTR437zpl_snapdir_getattr_impl(struct mnt_idmap *user_ns,438const struct path *path, struct kstat *stat, u32 request_mask,439unsigned int query_flags)440#elif defined(HAVE_USERNS_IOPS_GETATTR)441zpl_snapdir_getattr_impl(struct user_namespace *user_ns,442const struct path *path, struct kstat *stat, u32 request_mask,443unsigned int query_flags)444#else445zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat,446u32 request_mask, unsigned int query_flags)447#endif448{449(void) request_mask, (void) query_flags;450struct inode *ip = path->dentry->d_inode;451zfsvfs_t *zfsvfs = ITOZSB(ip);452int error;453454if ((error = zpl_enter(zfsvfs, FTAG)) != 0)455return (error);456#if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))457#ifdef HAVE_GENERIC_FILLATTR_USERNS458generic_fillattr(user_ns, ip, stat);459#elif defined(HAVE_GENERIC_FILLATTR_IDMAP)460generic_fillattr(user_ns, ip, stat);461#elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)462generic_fillattr(user_ns, request_mask, ip, stat);463#else464(void) user_ns;465#endif466#else467generic_fillattr(ip, stat);468#endif469470stat->nlink = stat->size = 2;471472dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);473if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {474uint64_t snap_count;475int err = zap_count(476dmu_objset_pool(ds->ds_objset)->dp_meta_objset,477dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);478if (err != 0) {479zpl_exit(zfsvfs, FTAG);480return (-err);481}482stat->nlink += snap_count;483}484485stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);486stat->atime = current_time(ip);487zpl_exit(zfsvfs, FTAG);488489return (0);490}491ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr);492493/*494* The '.zfs/snapshot' directory file operations. These mainly control495* generating the list of available snapshots when doing an 'ls' in the496* directory. See zpl_snapdir_readdir().497*/498const struct file_operations zpl_fops_snapdir = {499.open = zpl_common_open,500.llseek = generic_file_llseek,501.read = generic_read_dir,502.iterate_shared = zpl_snapdir_iterate,503504};505506/*507* The '.zfs/snapshot' directory inode operations. These mainly control508* creating an inode for a snapshot directory and initializing the needed509* infrastructure to automount the snapshot. See zpl_snapdir_lookup().510*/511const struct inode_operations zpl_ops_snapdir = {512.lookup = zpl_snapdir_lookup,513.getattr = zpl_snapdir_getattr,514#if (defined(HAVE_RENAME_WANTS_FLAGS) || \515defined(HAVE_IOPS_RENAME_USERNS) || \516defined(HAVE_IOPS_RENAME_IDMAP))517.rename = zpl_snapdir_rename2,518#else519.rename = zpl_snapdir_rename,520#endif521.rmdir = zpl_snapdir_rmdir,522.mkdir = zpl_snapdir_mkdir,523};524525static struct dentry *526zpl_shares_lookup(struct inode *dip, struct dentry *dentry,527unsigned int flags)528{529fstrans_cookie_t cookie;530cred_t *cr = CRED();531struct inode *ip = NULL;532int error;533534crhold(cr);535cookie = spl_fstrans_mark();536error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,5370, cr, NULL, NULL);538ASSERT3S(error, <=, 0);539spl_fstrans_unmark(cookie);540crfree(cr);541542if (error) {543if (error == -ENOENT)544return (d_splice_alias(NULL, dentry));545else546return (ERR_PTR(error));547}548549return (d_splice_alias(ip, dentry));550}551552static int553zpl_shares_iterate(struct file *filp, struct dir_context *ctx)554{555fstrans_cookie_t cookie;556cred_t *cr = CRED();557zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));558znode_t *dzp;559int error = 0;560561if ((error = zpl_enter(zfsvfs, FTAG)) != 0)562return (error);563cookie = spl_fstrans_mark();564565if (zfsvfs->z_shares_dir == 0) {566dir_emit_dots(filp, ctx);567goto out;568}569570error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);571if (error)572goto out;573574crhold(cr);575error = -zfs_readdir(ZTOI(dzp), ctx, cr);576crfree(cr);577578iput(ZTOI(dzp));579out:580spl_fstrans_unmark(cookie);581zpl_exit(zfsvfs, FTAG);582ASSERT3S(error, <=, 0);583584return (error);585}586587static int588#ifdef HAVE_USERNS_IOPS_GETATTR589zpl_shares_getattr_impl(struct user_namespace *user_ns,590const struct path *path, struct kstat *stat, u32 request_mask,591unsigned int query_flags)592#elif defined(HAVE_IDMAP_IOPS_GETATTR)593zpl_shares_getattr_impl(struct mnt_idmap *user_ns,594const struct path *path, struct kstat *stat, u32 request_mask,595unsigned int query_flags)596#else597zpl_shares_getattr_impl(const struct path *path, struct kstat *stat,598u32 request_mask, unsigned int query_flags)599#endif600{601(void) request_mask, (void) query_flags;602struct inode *ip = path->dentry->d_inode;603zfsvfs_t *zfsvfs = ITOZSB(ip);604znode_t *dzp;605int error;606607if ((error = zpl_enter(zfsvfs, FTAG)) != 0)608return (error);609610if (zfsvfs->z_shares_dir == 0) {611#if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))612#ifdef HAVE_GENERIC_FILLATTR_USERNS613generic_fillattr(user_ns, path->dentry->d_inode, stat);614#elif defined(HAVE_GENERIC_FILLATTR_IDMAP)615generic_fillattr(user_ns, path->dentry->d_inode, stat);616#elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)617generic_fillattr(user_ns, request_mask, ip, stat);618#else619(void) user_ns;620#endif621#else622generic_fillattr(path->dentry->d_inode, stat);623#endif624stat->nlink = stat->size = 2;625stat->atime = current_time(ip);626zpl_exit(zfsvfs, FTAG);627return (0);628}629630error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);631if (error == 0) {632#ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK633error = -zfs_getattr_fast(user_ns, request_mask, ZTOI(dzp),634stat);635#elif (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))636error = -zfs_getattr_fast(user_ns, ZTOI(dzp), stat);637#else638error = -zfs_getattr_fast(kcred->user_ns, ZTOI(dzp), stat);639#endif640iput(ZTOI(dzp));641}642643zpl_exit(zfsvfs, FTAG);644ASSERT3S(error, <=, 0);645646return (error);647}648ZPL_GETATTR_WRAPPER(zpl_shares_getattr);649650/*651* The '.zfs/shares' directory file operations.652*/653const struct file_operations zpl_fops_shares = {654.open = zpl_common_open,655.llseek = generic_file_llseek,656.read = generic_read_dir,657.iterate_shared = zpl_shares_iterate,658};659660/*661* The '.zfs/shares' directory inode operations.662*/663const struct inode_operations zpl_ops_shares = {664.lookup = zpl_shares_lookup,665.getattr = zpl_shares_getattr,666};667668669