Fix build failures on PaX/GRSecurity patched kernels
[zfs.git] / module / zfs / zfs_ioctl.c
index efb6bf5..65b0a19 100644 (file)
  */
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Portions Copyright 2011 Martin Matuska
+ * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
+ * Copyright (c) 2012, Joyent, Inc. All rights reserved.
+ */
+/*
+ * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright (c) 2011 by Delphix. All rights reserved.
  */
 
 #include <sys/types.h>
@@ -58,6 +65,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 +127,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 +563,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 +587,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 +603,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 +620,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 +673,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 +686,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 +1085,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;
        }
@@ -1456,6 +1432,20 @@ zfs_ioc_pool_get_history(zfs_cmd_t *zc)
 }
 
 static int
+zfs_ioc_pool_reguid(zfs_cmd_t *zc)
+{
+       spa_t *spa;
+       int error;
+
+       error = spa_open(zc->zc_name, &spa, FTAG);
+       if (error == 0) {
+               error = spa_change_guid(spa);
+               spa_close(spa, FTAG);
+       }
+       return (error);
+}
+
+static int
 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
 {
        int error;
@@ -1952,8 +1942,10 @@ top:
                uint64_t cookie = 0;
                int len = sizeof (zc->zc_name) - (p - zc->zc_name);
 
-               while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
-                       (void) dmu_objset_prefetch(p, NULL);
+               while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
+                       if (!dataset_name_hidden(zc->zc_name))
+                               (void) dmu_objset_prefetch(zc->zc_name, NULL);
+               }
        }
 
        do {
@@ -2000,7 +1992,7 @@ zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
        int error;
 
 top:
-       if (zc->zc_cookie == 0)
+       if (zc->zc_cookie == 0 && !zc->zc_simple)
                (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
                    NULL, DS_FIND_SNAPSHOTS);
 
@@ -2022,7 +2014,7 @@ top:
            zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
            NULL);
 
-       if (error == 0) {
+       if (error == 0 && !zc->zc_simple) {
                dsl_dataset_t *ds;
                dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
 
@@ -2171,7 +2163,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 +2714,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)
@@ -2787,6 +2753,7 @@ zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
        uint64_t sense = ZFS_PROP_UNDEFINED;
        uint64_t norm = ZFS_PROP_UNDEFINED;
        uint64_t u8 = ZFS_PROP_UNDEFINED;
+       int error;
 
        ASSERT(zplprops != NULL);
 
@@ -2830,8 +2797,9 @@ zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
        VERIFY(nvlist_add_uint64(zplprops,
            zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
 
-       if (norm == ZFS_PROP_UNDEFINED)
-               VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
+       if (norm == ZFS_PROP_UNDEFINED &&
+           (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
+               return (error);
        VERIFY(nvlist_add_uint64(zplprops,
            zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
 
@@ -2840,13 +2808,15 @@ zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
         */
        if (norm)
                u8 = 1;
-       if (u8 == ZFS_PROP_UNDEFINED)
-               VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
+       if (u8 == ZFS_PROP_UNDEFINED &&
+           (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
+               return (error);
        VERIFY(nvlist_add_uint64(zplprops,
            zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
 
-       if (sense == ZFS_PROP_UNDEFINED)
-               VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
+       if (sense == ZFS_PROP_UNDEFINED &&
+           (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
+               return (error);
        VERIFY(nvlist_add_uint64(zplprops,
            zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
 
@@ -3098,38 +3068,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 +4256,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 +4275,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 +4404,7 @@ zfs_ioc_smb_acl(zfs_cmd_t *zc)
        return (error);
 #else
        return (ENOTSUP);
-#endif /* HAVE_SHARE */
+#endif /* HAVE_SMB_SHARE */
 }
 
 /*
@@ -4732,10 +4716,12 @@ static zfs_ioc_vec_t zfs_ioc_vec[] = {
            B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
        { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
            POOL_CHECK_SUSPENDED },
+        { zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE,
+            POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
        { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
            POOL_CHECK_NONE },
        { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
-           POOL_CHECK_NONE },
+           POOL_CHECK_NONE }
 };
 
 int
@@ -5042,9 +5028,9 @@ _init(void)
        tsd_create(&zfs_fsyncer_key, NULL);
        tsd_create(&rrw_tsd_key, NULL);
 
-       printk(KERN_NOTICE "ZFS: Loaded module v%s%s, "
+       printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
               "ZFS pool version %s, ZFS filesystem version %s\n",
-              ZFS_META_VERSION, ZFS_DEBUG_STR,
+              ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
               SPA_VERSION_STRING, ZPL_VERSION_STRING);
 
        return (0);
@@ -5054,8 +5040,9 @@ out2:
 out1:
        zfs_fini();
        spa_fini();
-       printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s%s"
-              ", rc = %d\n", ZFS_META_VERSION, ZFS_DEBUG_STR, error);
+       printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
+              ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
+              ZFS_DEBUG_STR, error);
 
        return (error);
 }
@@ -5071,8 +5058,8 @@ _fini(void)
        tsd_destroy(&zfs_fsyncer_key);
        tsd_destroy(&rrw_tsd_key);
 
-       printk(KERN_NOTICE "ZFS: Unloaded module v%s%s\n",
-              ZFS_META_VERSION, ZFS_DEBUG_STR);
+       printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
+              ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
 
        return (0);
 }