]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/zfs_dir.c
Encrypted dnode blocks should be prefetched raw
[mirror_zfs.git] / module / zfs / zfs_dir.c
index aced2886aba0fbf2e219d4ea0c24d544b877460d..7eb426b781191370722e16498546fa653019c062 100644 (file)
  *
  * CDDL HEADER END
  */
+
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
+ * Copyright 2017 Nexenta Systems, Inc.
  */
 
-#ifdef HAVE_ZPL
-
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/time.h>
 #include <sys/policy.h>
 #include <sys/zfs_dir.h>
 #include <sys/zfs_acl.h>
+#include <sys/zfs_vnops.h>
 #include <sys/fs/zfs.h>
 #include "fs/fs_subr.h"
 #include <sys/zap.h>
 #include <sys/dmu.h>
 #include <sys/atomic.h>
+#include <sys/zfs_ctldir.h>
 #include <sys/zfs_fuid.h>
 #include <sys/sa.h>
 #include <sys/zfs_sa.h>
  * of names after deciding which is the appropriate lookup interface.
  */
 static int
-zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
+zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, matchtype_t mt,
     boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
 {
+       boolean_t conflict = B_FALSE;
        int error;
 
        if (zfsvfs->z_norm) {
-               matchtype_t mt = MT_FIRST;
-               boolean_t conflict = B_FALSE;
                size_t bufsz = 0;
                char *buf = NULL;
 
@@ -77,23 +79,36 @@ zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
                        buf = rpnp->pn_buf;
                        bufsz = rpnp->pn_bufsize;
                }
-               if (exact)
-                       mt = MT_EXACT;
+
                /*
                 * In the non-mixed case we only expect there would ever
                 * be one match, but we need to use the normalizing lookup.
                 */
                error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
                    zoid, mt, buf, bufsz, &conflict);
-               if (!error && deflags)
-                       *deflags = conflict ? ED_CASE_CONFLICT : 0;
        } else {
                error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
        }
+
+       /*
+        * Allow multiple entries provided the first entry is
+        * the object id.  Non-zpl consumers may safely make
+        * use of the additional space.
+        *
+        * XXX: This should be a feature flag for compatibility
+        */
+       if (error == EOVERFLOW)
+               error = 0;
+
+       if (zfsvfs->z_norm && !error && deflags)
+               *deflags = conflict ? ED_CASE_CONFLICT : 0;
+
        *zoid = ZFS_DIRENT_OBJ(*zoid);
 
+#ifdef HAVE_DNLC
        if (error == ENOENT && update)
-               dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
+               dnlc_update(ZTOI(dzp), name, DNLC_NO_VNODE);
+#endif /* HAVE_DNLC */
 
        return (error);
 }
@@ -137,12 +152,14 @@ int
 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
     int flag, int *direntflags, pathname_t *realpnp)
 {
-       zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
+       zfsvfs_t        *zfsvfs = ZTOZSB(dzp);
        zfs_dirlock_t   *dl;
        boolean_t       update;
-       boolean_t       exact;
+       matchtype_t     mt = 0;
        uint64_t        zoid;
+#ifdef HAVE_DNLC
        vnode_t         *vp = NULL;
+#endif /* HAVE_DNLC */
        int             error = 0;
        int             cmpflags;
 
@@ -155,7 +172,7 @@ zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
        if ((name[0] == '.' &&
            (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) ||
            (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0))
-               return (EEXIST);
+               return (SET_ERROR(EEXIST));
 
        /*
         * Case sensitivity and normalization preferences are set when
@@ -175,13 +192,29 @@ zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
         */
 
        /*
-        * Decide if exact matches should be requested when performing
-        * a zap lookup on file systems supporting case-insensitive
-        * access.
+        * When matching we may need to normalize & change case according to
+        * FS settings.
+        *
+        * Note that a normalized match is necessary for a case insensitive
+        * filesystem when the lookup request is not exact because normalization
+        * can fold case independent of normalizing code point sequences.
+        *
+        * See the table above zfs_dropname().
         */
-       exact =
-           ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) ||
-           ((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK));
+       if (zfsvfs->z_norm != 0) {
+               mt = MT_NORMALIZE;
+
+               /*
+                * Determine if the match needs to honor the case specified in
+                * lookup, and if so keep track of that so that during
+                * normalization we don't fold case.
+                */
+               if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
+                   (flag & ZCIEXACT)) ||
+                   (zfsvfs->z_case == ZFS_CASE_MIXED && !(flag & ZCILOOK))) {
+                       mt |= MT_MATCH_CASE;
+               }
+       }
 
        /*
         * Only look in or update the DNLC if we are looking for the
@@ -194,7 +227,7 @@ zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
         * case for performance improvement?
         */
        update = !zfsvfs->z_norm ||
-           ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
+           (zfsvfs->z_case == ZFS_CASE_MIXED &&
            !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
 
        /*
@@ -222,11 +255,11 @@ zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
 
        mutex_enter(&dzp->z_lock);
        for (;;) {
-               if (dzp->z_unlinked) {
+               if (dzp->z_unlinked && !(flag & ZXATTR)) {
                        mutex_exit(&dzp->z_lock);
                        if (!(flag & ZHAVELOCK))
                                rw_exit(&dzp->z_name_lock);
-                       return (ENOENT);
+                       return (SET_ERROR(ENOENT));
                }
                for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
                        if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
@@ -237,7 +270,7 @@ zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
                        mutex_exit(&dzp->z_lock);
                        if (!(flag & ZHAVELOCK))
                                rw_exit(&dzp->z_name_lock);
-                       return (ENOENT);
+                       return (SET_ERROR(ENOENT));
                }
                if (dl == NULL) {
                        /*
@@ -271,8 +304,8 @@ zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
                 * dl_name in case the first thread goes away before we do.
                 * Note that we initialize the new name before storing its
                 * pointer into dl_name, because the first thread may load
-                * dl->dl_name at any time.  He'll either see the old value,
-                * which is his, or the new shared copy; either is OK.
+                * dl->dl_name at any time.  It'll either see the old value,
+                * which belongs to it, or the new shared copy; either is OK.
                 */
                dl->dl_namesize = strlen(dl->dl_name) + 1;
                name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
@@ -291,26 +324,31 @@ zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
                error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
                    sizeof (zoid));
                if (error == 0)
-                       error = (zoid == 0 ? ENOENT : 0);
+                       error = (zoid == 0 ? SET_ERROR(ENOENT) : 0);
        } else {
+#ifdef HAVE_DNLC
                if (update)
-                       vp = dnlc_lookup(ZTOV(dzp), name);
+                       vp = dnlc_lookup(ZTOI(dzp), name);
                if (vp == DNLC_NO_VNODE) {
-                       VN_RELE(vp);
-                       error = ENOENT;
+                       iput(vp);
+                       error = SET_ERROR(ENOENT);
                } else if (vp) {
                        if (flag & ZNEW) {
                                zfs_dirent_unlock(dl);
-                               VN_RELE(vp);
-                               return (EEXIST);
+                               iput(vp);
+                               return (SET_ERROR(EEXIST));
                        }
                        *dlpp = dl;
                        *zpp = VTOZ(vp);
                        return (0);
                } else {
-                       error = zfs_match_find(zfsvfs, dzp, name, exact,
+                       error = zfs_match_find(zfsvfs, dzp, name, mt,
                            update, direntflags, realpnp, &zoid);
                }
+#else
+               error = zfs_match_find(zfsvfs, dzp, name, mt,
+                   update, direntflags, realpnp, &zoid);
+#endif /* HAVE_DNLC */
        }
        if (error) {
                if (error != ENOENT || (flag & ZEXISTS)) {
@@ -320,15 +358,17 @@ zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
        } else {
                if (flag & ZNEW) {
                        zfs_dirent_unlock(dl);
-                       return (EEXIST);
+                       return (SET_ERROR(EEXIST));
                }
                error = zfs_zget(zfsvfs, zoid, zpp);
                if (error) {
                        zfs_dirent_unlock(dl);
                        return (error);
                }
+#ifdef HAVE_DNLC
                if (!(flag & ZXATTR) && update)
-                       dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
+                       dnlc_update(ZTOI(dzp), name, ZTOI(*zpp));
+#endif /* HAVE_DNLC */
        }
 
        *dlpp = dl;
@@ -377,7 +417,7 @@ zfs_dirent_unlock(zfs_dirlock_t *dl)
  *     special pseudo-directory.
  */
 int
-zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
+zfs_dirlook(znode_t *dzp, char *name, struct inode **ipp, int flags,
     int *deflg, pathname_t *rpnp)
 {
        zfs_dirlock_t *dl;
@@ -386,31 +426,31 @@ zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
        uint64_t parent;
 
        if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
-               *vpp = ZTOV(dzp);
-               VN_HOLD(*vpp);
+               *ipp = ZTOI(dzp);
+               igrab(*ipp);
        } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
-               zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
+               zfsvfs_t *zfsvfs = ZTOZSB(dzp);
 
                /*
                 * If we are a snapshot mounted under .zfs, return
-                * the vp for the snapshot directory.
+                * the inode pointer for the snapshot directory.
                 */
                if ((error = sa_lookup(dzp->z_sa_hdl,
                    SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
                        return (error);
+
                if (parent == dzp->z_id && zfsvfs->z_parent != zfsvfs) {
                        error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
-                           "snapshot", vpp, NULL, 0, NULL, kcred,
-                           NULL, NULL, NULL);
+                           "snapshot", ipp, 0, kcred, NULL, NULL);
                        return (error);
                }
                rw_enter(&dzp->z_parent_lock, RW_READER);
                error = zfs_zget(zfsvfs, parent, &zp);
                if (error == 0)
-                       *vpp = ZTOV(zp);
+                       *ipp = ZTOI(zp);
                rw_exit(&dzp->z_parent_lock);
        } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
-               *vpp = zfsctl_root(dzp);
+               *ipp = zfsctl_root(dzp);
        } else {
                int zf;
 
@@ -420,7 +460,7 @@ zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
 
                error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
                if (error == 0) {
-                       *vpp = ZTOV(zp);
+                       *ipp = ZTOI(zp);
                        zfs_dirent_unlock(dl);
                        dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
                }
@@ -450,10 +490,10 @@ zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
 void
 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
 {
-       zfsvfs_t *zfsvfs = zp->z_zfsvfs;
+       zfsvfs_t *zfsvfs = ZTOZSB(zp);
 
        ASSERT(zp->z_unlinked);
-       ASSERT(zp->z_links == 0);
+       ASSERT(ZTOI(zp)->i_nlink == 0);
 
        VERIFY3U(0, ==,
            zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
@@ -473,7 +513,7 @@ zfs_unlinked_drain(zfsvfs_t *zfsvfs)
        int             error;
 
        /*
-        * Interate over the contents of the unlinked set.
+        * Iterate over the contents of the unlinked set.
         */
        for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
            zap_cursor_retrieve(&zc, &zap) == 0;
@@ -506,7 +546,7 @@ zfs_unlinked_drain(zfsvfs_t *zfsvfs)
                        continue;
 
                zp->z_unlinked = B_TRUE;
-               VN_RELE(ZTOV(zp));
+               iput(ZTOI(zp));
        }
        zap_cursor_fini(&zc);
 }
@@ -529,7 +569,7 @@ zfs_purgedir(znode_t *dzp)
        zap_attribute_t zap;
        znode_t         *xzp;
        dmu_tx_t        *tx;
-       zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
+       zfsvfs_t        *zfsvfs = ZTOZSB(dzp);
        zfs_dirlock_t   dl;
        int skipped = 0;
        int error;
@@ -544,8 +584,8 @@ zfs_purgedir(znode_t *dzp)
                        continue;
                }
 
-               ASSERT((ZTOV(xzp)->v_type == VREG) ||
-                   (ZTOV(xzp)->v_type == VLNK));
+               ASSERT(S_ISREG(ZTOI(xzp)->i_mode) ||
+                   S_ISLNK(ZTOI(xzp)->i_mode));
 
                tx = dmu_tx_create(zfsvfs->z_os);
                dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
@@ -554,10 +594,11 @@ zfs_purgedir(znode_t *dzp)
                dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
                /* Is this really needed ? */
                zfs_sa_upgrade_txholds(tx, xzp);
+               dmu_tx_mark_netfree(tx);
                error = dmu_tx_assign(tx, TXG_WAIT);
                if (error) {
                        dmu_tx_abort(tx);
-                       VN_RELE(ZTOV(xzp));
+                       zfs_iput_async(ZTOI(xzp));
                        skipped += 1;
                        continue;
                }
@@ -570,7 +611,7 @@ zfs_purgedir(znode_t *dzp)
                        skipped += 1;
                dmu_tx_commit(tx);
 
-               VN_RELE(ZTOV(xzp));
+               zfs_iput_async(ZTOI(xzp));
        }
        zap_cursor_fini(&zc);
        if (error != ENOENT)
@@ -581,43 +622,50 @@ zfs_purgedir(znode_t *dzp)
 void
 zfs_rmnode(znode_t *zp)
 {
-       zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
+       zfsvfs_t        *zfsvfs = ZTOZSB(zp);
        objset_t        *os = zfsvfs->z_os;
        znode_t         *xzp = NULL;
        dmu_tx_t        *tx;
        uint64_t        acl_obj;
        uint64_t        xattr_obj;
+       uint64_t        links;
        int             error;
 
-       ASSERT(zp->z_links == 0);
-       ASSERT(ZTOV(zp)->v_count == 0);
+       ASSERT(ZTOI(zp)->i_nlink == 0);
+       ASSERT(atomic_read(&ZTOI(zp)->i_count) == 0);
 
        /*
         * If this is an attribute directory, purge its contents.
         */
-       if (ZTOV(zp)->v_type == VDIR && (zp->z_pflags & ZFS_XATTR)) {
+       if (S_ISDIR(ZTOI(zp)->i_mode) && (zp->z_pflags & ZFS_XATTR)) {
                if (zfs_purgedir(zp) != 0) {
                        /*
                         * Not enough space to delete some xattrs.
                         * Leave it in the unlinked set.
                         */
                        zfs_znode_dmu_fini(zp);
-                       zfs_znode_free(zp);
+
                        return;
                }
        }
 
        /*
-        * Free up all the data in the file.
+        * Free up all the data in the file.  We don't do this for directories
+        * because we need truncate and remove to be in the same tx, like in
+        * zfs_znode_delete(). Otherwise, if we crash here we'll end up with
+        * an inconsistent truncated zap object in the delete queue.  Note a
+        * truncated file is harmless since it only contains user data.
         */
-       error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
-       if (error) {
-               /*
-                * Not enough space.  Leave the file in the unlinked set.
-                */
-               zfs_znode_dmu_fini(zp);
-               zfs_znode_free(zp);
-               return;
+       if (S_ISREG(ZTOI(zp)->i_mode)) {
+               error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
+               if (error) {
+                       /*
+                        * Not enough space or we were interrupted by unmount.
+                        * Leave the file in the unlinked set.
+                        */
+                       zfs_znode_dmu_fini(zp);
+                       return;
+               }
        }
 
        /*
@@ -656,7 +704,6 @@ zfs_rmnode(znode_t *zp)
                 */
                dmu_tx_abort(tx);
                zfs_znode_dmu_fini(zp);
-               zfs_znode_free(zp);
                goto out;
        }
 
@@ -664,9 +711,10 @@ zfs_rmnode(znode_t *zp)
                ASSERT(error == 0);
                mutex_enter(&xzp->z_lock);
                xzp->z_unlinked = B_TRUE;       /* mark xzp for deletion */
-               xzp->z_links = 0;       /* no more links to it */
+               clear_nlink(ZTOI(xzp));         /* no more links to it */
+               links = 0;
                VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
-                   &xzp->z_links, sizeof (xzp->z_links), tx));
+                   &links, sizeof (links), tx));
                mutex_exit(&xzp->z_lock);
                zfs_unlinked_add(xzp, tx);
        }
@@ -680,7 +728,7 @@ zfs_rmnode(znode_t *zp)
        dmu_tx_commit(tx);
 out:
        if (xzp)
-               VN_RELE(ZTOV(xzp));
+               zfs_iput_async(ZTOI(xzp));
 }
 
 static uint64_t
@@ -688,24 +736,28 @@ zfs_dirent(znode_t *zp, uint64_t mode)
 {
        uint64_t de = zp->z_id;
 
-       if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
+       if (ZTOZSB(zp)->z_version >= ZPL_VERSION_DIRENT_TYPE)
                de |= IFTODT(mode) << 60;
        return (de);
 }
 
 /*
- * Link zp into dl.  Can only fail if zp has been unlinked.
+ * Link zp into dl.  Can fail in the following cases :
+ * - if zp has been unlinked.
+ * - if the number of entries with the same hash (aka. colliding entries)
+ *    exceed the capacity of a leaf-block of fatzap and splitting of the
+ *    leaf-block does not help.
  */
 int
 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
 {
        znode_t *dzp = dl->dl_dzp;
-       zfsvfs_t *zfsvfs = zp->z_zfsvfs;
-       vnode_t *vp = ZTOV(zp);
+       zfsvfs_t *zfsvfs = ZTOZSB(zp);
        uint64_t value;
-       int zp_is_dir = (vp->v_type == VDIR);
+       int zp_is_dir = S_ISDIR(ZTOI(zp)->i_mode);
        sa_bulk_attr_t bulk[5];
        uint64_t mtime[2], ctime[2];
+       uint64_t links;
        int count = 0;
        int error;
 
@@ -715,13 +767,37 @@ zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
                if (zp->z_unlinked) {   /* no new links to unlinked zp */
                        ASSERT(!(flag & (ZNEW | ZEXISTS)));
                        mutex_exit(&zp->z_lock);
-                       return (ENOENT);
+                       return (SET_ERROR(ENOENT));
                }
-               zp->z_links++;
-               SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
-                   &zp->z_links, sizeof (zp->z_links));
+               if (!(flag & ZNEW)) {
+                       /*
+                        * ZNEW nodes come from zfs_mknode() where the link
+                        * count has already been initialised
+                        */
+                       inc_nlink(ZTOI(zp));
+                       links = ZTOI(zp)->i_nlink;
+                       SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
+                           NULL, &links, sizeof (links));
+               }
+       }
 
+       value = zfs_dirent(zp, zp->z_mode);
+       error = zap_add(ZTOZSB(zp)->z_os, dzp->z_id, dl->dl_name, 8, 1,
+           &value, tx);
+
+       /*
+        * zap_add could fail to add the entry if it exceeds the capacity of the
+        * leaf-block and zap_leaf_split() failed to help.
+        * The caller of this routine is responsible for failing the transaction
+        * which will rollback the SA updates done above.
+        */
+       if (error != 0) {
+               if (!(flag & ZRENAMING) && !(flag & ZNEW))
+                       drop_nlink(ZTOI(zp));
+               mutex_exit(&zp->z_lock);
+               return (error);
        }
+
        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,
@@ -731,7 +807,7 @@ zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
                SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
                    ctime, sizeof (ctime));
                zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
-                   ctime, B_TRUE);
+                   ctime);
        }
        error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
        ASSERT(error == 0);
@@ -740,95 +816,107 @@ zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
 
        mutex_enter(&dzp->z_lock);
        dzp->z_size++;
-       dzp->z_links += zp_is_dir;
+       if (zp_is_dir)
+               inc_nlink(ZTOI(dzp));
+       links = ZTOI(dzp)->i_nlink;
        count = 0;
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
            &dzp->z_size, sizeof (dzp->z_size));
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
-           &dzp->z_links, sizeof (dzp->z_links));
+           &links, sizeof (links));
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
            mtime, sizeof (mtime));
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
            ctime, sizeof (ctime));
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
            &dzp->z_pflags, sizeof (dzp->z_pflags));
-       zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
+       zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
        error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
        ASSERT(error == 0);
        mutex_exit(&dzp->z_lock);
 
-       value = zfs_dirent(zp, zp->z_mode);
-       error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
-           8, 1, &value, tx);
-       ASSERT(error == 0);
-
-       dnlc_update(ZTOV(dzp), dl->dl_name, vp);
-
        return (0);
 }
 
+/*
+ * The match type in the code for this function should conform to:
+ *
+ * ------------------------------------------------------------------------
+ * fs type  | z_norm      | lookup type | match type
+ * ---------|-------------|-------------|----------------------------------
+ * CS !norm | 0           |           0 | 0 (exact)
+ * CS  norm | formX       |           0 | MT_NORMALIZE
+ * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
+ * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
+ * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
+ * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
+ * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
+ * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
+ * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
+ * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
+ *
+ * Abbreviations:
+ *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
+ *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
+ *    formX = unicode normalization form set on fs creation
+ */
 static int
 zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx,
     int flag)
 {
        int error;
 
-       if (zp->z_zfsvfs->z_norm) {
-               if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
+       if (ZTOZSB(zp)->z_norm) {
+               matchtype_t mt = MT_NORMALIZE;
+
+               if ((ZTOZSB(zp)->z_case == ZFS_CASE_INSENSITIVE &&
                    (flag & ZCIEXACT)) ||
-                   ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
-                   !(flag & ZCILOOK)))
-                       error = zap_remove_norm(zp->z_zfsvfs->z_os,
-                           dzp->z_id, dl->dl_name, MT_EXACT, tx);
-               else
-                       error = zap_remove_norm(zp->z_zfsvfs->z_os,
-                           dzp->z_id, dl->dl_name, MT_FIRST, tx);
+                   (ZTOZSB(zp)->z_case == ZFS_CASE_MIXED &&
+                   !(flag & ZCILOOK))) {
+                       mt |= MT_MATCH_CASE;
+               }
+
+               error = zap_remove_norm(ZTOZSB(zp)->z_os, dzp->z_id,
+                   dl->dl_name, mt, tx);
        } else {
-               error = zap_remove(zp->z_zfsvfs->z_os,
-                   dzp->z_id, dl->dl_name, tx);
+               error = zap_remove(ZTOZSB(zp)->z_os, dzp->z_id, dl->dl_name,
+                   tx);
        }
 
        return (error);
 }
 
 /*
- * Unlink zp from dl, and mark zp for deletion if this was the last link.
- * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
+ * Unlink zp from dl, and mark zp for deletion if this was the last link. Can
+ * fail if zp is a mount point (EBUSY) or a non-empty directory (ENOTEMPTY).
  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
  * and it's the caller's job to do it.
  */
 int
 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
-       boolean_t *unlinkedp)
+    boolean_t *unlinkedp)
 {
        znode_t *dzp = dl->dl_dzp;
-       zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
-       vnode_t *vp = ZTOV(zp);
-       int zp_is_dir = (vp->v_type == VDIR);
+       zfsvfs_t *zfsvfs = ZTOZSB(dzp);
+       int zp_is_dir = S_ISDIR(ZTOI(zp)->i_mode);
        boolean_t unlinked = B_FALSE;
        sa_bulk_attr_t bulk[5];
        uint64_t mtime[2], ctime[2];
+       uint64_t links;
        int count = 0;
        int error;
 
-       dnlc_remove(ZTOV(dzp), dl->dl_name);
+#ifdef HAVE_DNLC
+       dnlc_remove(ZTOI(dzp), dl->dl_name);
+#endif /* HAVE_DNLC */
 
        if (!(flag & ZRENAMING)) {
-               if (vn_vfswlock(vp))            /* prevent new mounts on zp */
-                       return (EBUSY);
-
-               if (vn_ismntpt(vp)) {           /* don't remove mount point */
-                       vn_vfsunlock(vp);
-                       return (EBUSY);
-               }
-
                mutex_enter(&zp->z_lock);
 
                if (zp_is_dir && !zfs_dirempty(zp)) {
                        mutex_exit(&zp->z_lock);
-                       vn_vfsunlock(vp);
-                       return (EEXIST);
+                       return (SET_ERROR(ENOTEMPTY));
                }
 
                /*
@@ -839,37 +927,35 @@ zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
                error = zfs_dropname(dl, zp, dzp, tx, flag);
                if (error != 0) {
                        mutex_exit(&zp->z_lock);
-                       vn_vfsunlock(vp);
                        return (error);
                }
 
-               if (zp->z_links <= zp_is_dir) {
-                       zfs_panic_recover("zfs: link count on %s is %u, "
-                           "should be at least %u",
-                           zp->z_vnode->v_path ? zp->z_vnode->v_path :
-                           "<unknown>", (int)zp->z_links,
-                           zp_is_dir + 1);
-                       zp->z_links = zp_is_dir + 1;
+               if (ZTOI(zp)->i_nlink <= zp_is_dir) {
+                       zfs_panic_recover("zfs: link count on %lu is %u, "
+                           "should be at least %u", zp->z_id,
+                           (int)ZTOI(zp)->i_nlink, zp_is_dir + 1);
+                       set_nlink(ZTOI(zp), zp_is_dir + 1);
                }
-               if (--zp->z_links == zp_is_dir) {
+               drop_nlink(ZTOI(zp));
+               if (ZTOI(zp)->i_nlink == zp_is_dir) {
                        zp->z_unlinked = B_TRUE;
-                       zp->z_links = 0;
+                       clear_nlink(ZTOI(zp));
                        unlinked = B_TRUE;
                } else {
                        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
                            NULL, &ctime, sizeof (ctime));
                        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
                            NULL, &zp->z_pflags, sizeof (zp->z_pflags));
-                       zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
-                           B_TRUE);
+                       zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
+                           ctime);
                }
+               links = ZTOI(zp)->i_nlink;
                SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
-                   NULL, &zp->z_links, sizeof (zp->z_links));
+                   NULL, &links, sizeof (links));
                error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
                count = 0;
                ASSERT(error == 0);
                mutex_exit(&zp->z_lock);
-               vn_vfsunlock(vp);
        } else {
                error = zfs_dropname(dl, zp, dzp, tx, flag);
                if (error != 0)
@@ -878,9 +964,11 @@ zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
 
        mutex_enter(&dzp->z_lock);
        dzp->z_size--;          /* one dirent removed */
-       dzp->z_links -= zp_is_dir;      /* ".." link from zp */
+       if (zp_is_dir)
+               drop_nlink(ZTOI(dzp));  /* ".." link from zp */
+       links = ZTOI(dzp)->i_nlink;
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
-           NULL, &dzp->z_links, sizeof (dzp->z_links));
+           NULL, &links, sizeof (links));
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
            NULL, &dzp->z_size, sizeof (dzp->z_size));
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
@@ -889,7 +977,7 @@ zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
            NULL, mtime, sizeof (mtime));
        SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
            NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
-       zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
+       zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
        error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
        ASSERT(error == 0);
        mutex_exit(&dzp->z_lock);
@@ -906,38 +994,53 @@ zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
  * Indicate whether the directory is empty.  Works with or without z_lock
  * held, but can only be consider a hint in the latter case.  Returns true
  * if only "." and ".." remain and there's no work in progress.
+ *
+ * The internal ZAP size, rather than zp->z_size, needs to be checked since
+ * some consumers (Lustre) do not strictly maintain an accurate SA_ZPL_SIZE.
  */
 boolean_t
 zfs_dirempty(znode_t *dzp)
 {
-       return (dzp->z_size == 2 && dzp->z_dirlocks == 0);
+       zfsvfs_t *zfsvfs = ZTOZSB(dzp);
+       uint64_t count;
+       int error;
+
+       if (dzp->z_dirlocks != NULL)
+               return (B_FALSE);
+
+       error = zap_count(zfsvfs->z_os, dzp->z_id, &count);
+       if (error != 0 || count != 0)
+               return (B_FALSE);
+
+       return (B_TRUE);
 }
 
 int
-zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
+zfs_make_xattrdir(znode_t *zp, vattr_t *vap, struct inode **xipp, cred_t *cr)
 {
-       zfsvfs_t *zfsvfs = zp->z_zfsvfs;
+       zfsvfs_t *zfsvfs = ZTOZSB(zp);
        znode_t *xzp;
        dmu_tx_t *tx;
        int error;
        zfs_acl_ids_t acl_ids;
        boolean_t fuid_dirtied;
+#ifdef DEBUG
        uint64_t parent;
+#endif
 
-       *xvpp = NULL;
+       *xipp = NULL;
 
-       if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
+       if ((error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr)))
                return (error);
 
        if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
            &acl_ids)) != 0)
                return (error);
-       if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
+       if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zp->z_projid)) {
                zfs_acl_ids_free(&acl_ids);
-               return (EDQUOT);
+               return (SET_ERROR(EDQUOT));
        }
 
-top:
        tx = dmu_tx_create(zfsvfs->z_os);
        dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
            ZFS_SA_BASE_ATTR_SIZE);
@@ -946,13 +1049,8 @@ top:
        fuid_dirtied = zfsvfs->z_fuid_dirty;
        if (fuid_dirtied)
                zfs_fuid_txhold(zfsvfs, tx);
-       error = dmu_tx_assign(tx, TXG_NOWAIT);
+       error = dmu_tx_assign(tx, TXG_WAIT);
        if (error) {
-               if (error == ERESTART) {
-                       dmu_tx_wait(tx);
-                       dmu_tx_abort(tx);
-                       goto top;
-               }
                zfs_acl_ids_free(&acl_ids);
                dmu_tx_abort(tx);
                return (error);
@@ -971,13 +1069,14 @@ top:
        VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
            sizeof (xzp->z_id), tx));
 
-       (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
-           xzp, "", NULL, acl_ids.z_fuidp, vap);
+       if (!zp->z_unlinked)
+               (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
+                   xzp, "", NULL, acl_ids.z_fuidp, vap);
 
        zfs_acl_ids_free(&acl_ids);
        dmu_tx_commit(tx);
 
-       *xvpp = ZTOV(xzp);
+       *xipp = ZTOI(xzp);
 
        return (0);
 }
@@ -990,15 +1089,15 @@ top:
  *             cr      - credentials of caller
  *             flags   - flags from the VOP_LOOKUP call
  *
- *     OUT:    xzpp    - pointer to extended attribute znode
+ *     OUT:    xipp    - pointer to extended attribute znode
  *
  *     RETURN: 0 on success
  *             error number on failure
  */
 int
-zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
+zfs_get_xattrdir(znode_t *zp, struct inode **xipp, cred_t *cr, int flags)
 {
-       zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
+       zfsvfs_t        *zfsvfs = ZTOZSB(zp);
        znode_t         *xzp;
        zfs_dirlock_t   *dl;
        vattr_t         va;
@@ -1009,20 +1108,19 @@ top:
                return (error);
 
        if (xzp != NULL) {
-               *xvpp = ZTOV(xzp);
+               *xipp = ZTOI(xzp);
                zfs_dirent_unlock(dl);
                return (0);
        }
 
-
        if (!(flags & CREATE_XATTR_DIR)) {
                zfs_dirent_unlock(dl);
-               return (ENOENT);
+               return (SET_ERROR(ENOENT));
        }
 
-       if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
+       if (zfs_is_readonly(zfsvfs)) {
                zfs_dirent_unlock(dl);
-               return (EROFS);
+               return (SET_ERROR(EROFS));
        }
 
        /*
@@ -1035,12 +1133,12 @@ top:
         * Once in a directory the ability to read/write attributes
         * is controlled by the permissions on the attribute file.
         */
-       va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
-       va.va_type = VDIR;
+       va.va_mask = ATTR_MODE | ATTR_UID | ATTR_GID;
        va.va_mode = S_IFDIR | S_ISVTX | 0777;
        zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
 
-       error = zfs_make_xattrdir(zp, &va, xvpp, cr);
+       va.va_dentry = NULL;
+       error = zfs_make_xattrdir(zp, &va, xipp, cr);
        zfs_dirent_unlock(dl);
 
        if (error == ERESTART) {
@@ -1059,7 +1157,7 @@ top:
  *
  *     you own the directory,
  *     you own the entry,
- *     the entry is a plain file and you have write access,
+ *     you have write access to the entry,
  *     or you are privileged (checked in secpolicy...).
  *
  * The function returns 0 if remove access is granted.
@@ -1067,25 +1165,25 @@ top:
 int
 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
 {
-       uid_t           uid;
+       uid_t           uid;
        uid_t           downer;
        uid_t           fowner;
-       zfsvfs_t        *zfsvfs = zdp->z_zfsvfs;
+       zfsvfs_t        *zfsvfs = ZTOZSB(zdp);
 
-       if (zdp->z_zfsvfs->z_replay)
+       if (zfsvfs->z_replay)
                return (0);
 
        if ((zdp->z_mode & S_ISVTX) == 0)
                return (0);
 
-       downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
-       fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
+       downer = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(ZTOI(zdp)->i_uid),
+           cr, ZFS_OWNER);
+       fowner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(ZTOI(zp)->i_uid),
+           cr, ZFS_OWNER);
 
        if ((uid = crgetuid(cr)) == downer || uid == fowner ||
-           (ZTOV(zp)->v_type == VREG &&
-           zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
+           zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0)
                return (0);
        else
                return (secpolicy_vnode_remove(cr));
 }
-#endif /* HAVE_ZPL */