Add .zfs control directory
[zfs.git] / module / zfs / zfs_ioctl.c
index efb6bf5..d2ad1af 100644 (file)
@@ -58,6 +58,7 @@
 #include <sys/mount.h>
 #include <sys/sdt.h>
 #include <sys/fs/zfs.h>
+#include <sys/zfs_ctldir.h>
 #include <sys/zfs_dir.h>
 #include <sys/zfs_onexit.h>
 #include <sys/zvol.h>
@@ -119,42 +120,6 @@ static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
     boolean_t *);
 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
 
-/* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
-void
-__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
-{
-       const char *newfile;
-       char buf[512];
-       va_list adx;
-
-       /*
-        * Get rid of annoying "../common/" prefix to filename.
-        */
-       newfile = strrchr(file, '/');
-       if (newfile != NULL) {
-               newfile = newfile + 1; /* Get rid of leading / */
-       } else {
-               newfile = file;
-       }
-
-       va_start(adx, fmt);
-       (void) vsnprintf(buf, sizeof (buf), fmt, adx);
-       va_end(adx);
-
-       /*
-        * To get this data, use the zfs-dprintf probe as so:
-        * dtrace -q -n 'zfs-dprintf \
-        *      /stringof(arg0) == "dbuf.c"/ \
-        *      {printf("%s: %s", stringof(arg1), stringof(arg3))}'
-        * arg0 = file name
-        * arg1 = function name
-        * arg2 = line number
-        * arg3 = message
-        */
-       DTRACE_PROBE4(zfs__dprintf,
-           char *, newfile, char *, func, int, line, char *, buf);
-}
-
 static void
 history_str_free(char *buf)
 {
@@ -591,7 +556,7 @@ zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
        return (error);
 }
 
-#ifdef HAVE_SHARE
+#ifdef HAVE_SMB_SHARE
 static int
 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
 {
@@ -615,12 +580,12 @@ zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
        return (dsl_deleg_access(zc->zc_name,
            ZFS_DELEG_PERM_SHARE, cr));
 }
-#endif /* HAVE_SHARE */
+#endif /* HAVE_SMB_SHARE */
 
 int
 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
 {
-#ifdef HAVE_SHARE
+#ifdef HAVE_SMB_SHARE
        if (!INGLOBALZONE(curproc))
                return (EPERM);
 
@@ -631,13 +596,13 @@ zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
        }
 #else
        return (ENOTSUP);
-#endif /* HAVE_SHARE */
+#endif /* HAVE_SMB_SHARE */
 }
 
 int
 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
 {
-#ifdef HAVE_SHARE
+#ifdef HAVE_SMB_SHARE
        if (!INGLOBALZONE(curproc))
                return (EPERM);
 
@@ -648,7 +613,7 @@ zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
        }
 #else
        return (ENOTSUP);
-#endif /* HAVE_SHARE */
+#endif /* HAVE_SMB_SHARE */
 }
 
 static int
@@ -701,6 +666,9 @@ zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
  * and destroying snapshots requires descendent permissions, a successfull
  * check of the top level snapshot applies to snapshots of all descendent
  * datasets as well.
+ *
+ * The target snapshot may not exist when doing a recursive destroy.
+ * In this case fallback to permissions of the parent dataset.
  */
 static int
 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
@@ -711,6 +679,8 @@ zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
        dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
 
        error = zfs_secpolicy_destroy_perms(dsname, cr);
+       if (error == ENOENT)
+               error = zfs_secpolicy_destroy_perms(zc->zc_name, cr);
 
        strfree(dsname);
        return (error);
@@ -1108,8 +1078,7 @@ get_zfs_sb(const char *dsname, zfs_sb_t **zsbp)
        mutex_enter(&os->os_user_ptr_lock);
        *zsbp = dmu_objset_get_user(os);
        if (*zsbp && (*zsbp)->z_sb) {
-               if (atomic_inc_not_zero(&((*zsbp)->z_sb->s_active)))
-                       error = ESRCH;
+               atomic_inc(&((*zsbp)->z_sb->s_active));
        } else {
                error = ESRCH;
        }
@@ -2171,7 +2140,8 @@ zfs_prop_set_special(const char *dsname, zprop_source_t source,
                if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
                        zfs_cmd_t *zc;
 
-                       zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
+                       zc = kmem_zalloc(sizeof (zfs_cmd_t),
+                           KM_SLEEP | KM_NODEBUG);
                        (void) strcpy(zc->zc_name, dsname);
                        (void) zfs_ioc_userspace_upgrade(zc);
                        kmem_free(zc, sizeof (zfs_cmd_t));
@@ -2721,33 +2691,6 @@ zfs_ioc_get_fsacl(zfs_cmd_t *zc)
        return (error);
 }
 
-#ifdef HAVE_SNAPSHOT
-/*
- * Search the vfs list for a specified resource.  Returns a pointer to it
- * or NULL if no suitable entry is found. The caller of this routine
- * is responsible for releasing the returned vfs pointer.
- */
-static vfs_t *
-zfs_get_vfs(const char *resource)
-{
-       struct vfs *vfsp;
-       struct vfs *vfs_found = NULL;
-
-       vfs_list_read_lock();
-       vfsp = rootvfs;
-       do {
-               if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
-                       mntget(vfsp);
-                       vfs_found = vfsp;
-                       break;
-               }
-               vfsp = vfsp->vfs_next;
-       } while (vfsp != rootvfs);
-       vfs_list_unlock();
-       return (vfs_found);
-}
-#endif /* HAVE_SNAPSHOT */
-
 /* ARGSUSED */
 static void
 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
@@ -3098,38 +3041,52 @@ out:
        return (error);
 }
 
+/*
+ * inputs:
+ * name                dataset name, or when 'arg == NULL' the full snapshot name
+ * arg         short snapshot name (i.e. part after the '@')
+ */
 int
 zfs_unmount_snap(const char *name, void *arg)
 {
-#ifdef HAVE_SNAPSHOT
-       vfs_t *vfsp = NULL;
+       zfs_sb_t *zsb = NULL;
+       char *dsname;
+       char *snapname;
+       char *fullname;
+       char *ptr;
+       int error;
 
        if (arg) {
-               char *snapname = arg;
-               char *fullname = kmem_asprintf("%s@%s", name, snapname);
-               vfsp = zfs_get_vfs(fullname);
-               strfree(fullname);
-       } else if (strchr(name, '@')) {
-               vfsp = zfs_get_vfs(name);
+               dsname = strdup(name);
+               snapname = strdup(arg);
+       } else {
+               ptr = strchr(name, '@');
+               if (ptr) {
+                       dsname = strdup(name);
+                       dsname[ptr - name] = '\0';
+                       snapname = strdup(ptr + 1);
+               } else {
+                       return (0);
+               }
        }
 
-       if (vfsp) {
-               /*
-                * Always force the unmount for snapshots.
-                */
-               int flag = MS_FORCE;
-               int err;
+       fullname = kmem_asprintf("%s@%s", dsname, snapname);
 
-               if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
-                       mntput(vfsp);
-                       return (err);
-               }
-               mntput(vfsp);
-               if ((err = dounmount(vfsp, flag, kcred)) != 0)
-                       return (err);
+       error = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
+       if (error == 0) {
+               error = zfsctl_unmount_snapshot(zsb, fullname, MNT_FORCE);
+               zfs_sb_rele(zsb, FTAG);
+
+               /* Allow ENOENT for consistency with upstream */
+               if (error == ENOENT)
+                       error = 0;
        }
-#endif /* HAVE_SNAPSHOT */
-       return (0);
+
+       strfree(dsname);
+       strfree(snapname);
+       strfree(fullname);
+
+       return (error);
 }
 
 /*
@@ -4272,7 +4229,7 @@ zfs_ioc_diff(zfs_cmd_t *zc)
 /*
  * Remove all ACL files in shares dir
  */
-#ifdef HAVE_SHARE
+#ifdef HAVE_SMB_SHARE
 static int
 zfs_smb_acl_purge(znode_t *dzp)
 {
@@ -4291,12 +4248,12 @@ zfs_smb_acl_purge(znode_t *dzp)
        zap_cursor_fini(&zc);
        return (error);
 }
-#endif /* HAVE SHARE */
+#endif /* HAVE_SMB_SHARE */
 
 static int
 zfs_ioc_smb_acl(zfs_cmd_t *zc)
 {
-#ifdef HAVE_SHARE
+#ifdef HAVE_SMB_SHARE
        vnode_t *vp;
        znode_t *dzp;
        vnode_t *resourcevp = NULL;
@@ -4420,7 +4377,7 @@ zfs_ioc_smb_acl(zfs_cmd_t *zc)
        return (error);
 #else
        return (ENOTSUP);
-#endif /* HAVE_SHARE */
+#endif /* HAVE_SMB_SHARE */
 }
 
 /*