The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_dir.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * CDDL HEADER START
    3  *
    4  * The contents of this file are subject to the terms of the
    5  * 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.LICENSE
    9  * or http://www.opensolaris.org/os/licensing.
   10  * See the License for the specific language governing permissions
   11  * and limitations under the License.
   12  *
   13  * When distributing Covered Code, include this CDDL HEADER in each
   14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
   15  * If applicable, add the following below this CDDL HEADER, with the
   16  * fields enclosed by brackets "[]" replaced with your own identifying
   17  * information: Portions Copyright [yyyy] [name of copyright owner]
   18  *
   19  * CDDL HEADER END
   20  */
   21 
   22 /*
   23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   24  * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
   25  * Copyright 2017 Nexenta Systems, Inc.
   26  */
   27 
   28 #include <sys/types.h>
   29 #include <sys/param.h>
   30 #include <sys/time.h>
   31 #include <sys/systm.h>
   32 #include <sys/sysmacros.h>
   33 #include <sys/resource.h>
   34 #include <sys/vfs.h>
   35 #include <sys/vnode.h>
   36 #include <sys/file.h>
   37 #include <sys/kmem.h>
   38 #include <sys/uio.h>
   39 #include <sys/cmn_err.h>
   40 #include <sys/errno.h>
   41 #include <sys/stat.h>
   42 #include <sys/unistd.h>
   43 #include <sys/sunddi.h>
   44 #include <sys/random.h>
   45 #include <sys/policy.h>
   46 #include <sys/kcondvar.h>
   47 #include <sys/callb.h>
   48 #include <sys/smp.h>
   49 #include <sys/zfs_dir.h>
   50 #include <sys/zfs_acl.h>
   51 #include <sys/fs/zfs.h>
   52 #include <sys/zap.h>
   53 #include <sys/dmu.h>
   54 #include <sys/atomic.h>
   55 #include <sys/zfs_ctldir.h>
   56 #include <sys/zfs_fuid.h>
   57 #include <sys/sa.h>
   58 #include <sys/zfs_sa.h>
   59 #include <sys/dnlc.h>
   60 #include <sys/extdirent.h>
   61 
   62 /*
   63  * zfs_match_find() is used by zfs_dirent_lookup() to peform zap lookups
   64  * of names after deciding which is the appropriate lookup interface.
   65  */
   66 static int
   67 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, const char *name,
   68     matchtype_t mt, uint64_t *zoid)
   69 {
   70         int error;
   71 
   72         if (zfsvfs->z_norm) {
   73 
   74                 /*
   75                  * In the non-mixed case we only expect there would ever
   76                  * be one match, but we need to use the normalizing lookup.
   77                  */
   78                 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
   79                     zoid, mt, NULL, 0, NULL);
   80         } else {
   81                 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
   82         }
   83         *zoid = ZFS_DIRENT_OBJ(*zoid);
   84 
   85         return (error);
   86 }
   87 
   88 /*
   89  * Look up a directory entry under a locked vnode.
   90  * dvp being locked gives us a guarantee that there are no concurrent
   91  * modification of the directory and, thus, if a node can be found in
   92  * the directory, then it must not be unlinked.
   93  *
   94  * Input arguments:
   95  *      dzp     - znode for directory
   96  *      name    - name of entry to lock
   97  *      flag    - ZNEW: if the entry already exists, fail with EEXIST.
   98  *                ZEXISTS: if the entry does not exist, fail with ENOENT.
   99  *                ZXATTR: we want dzp's xattr directory
  100  *
  101  * Output arguments:
  102  *      zpp     - pointer to the znode for the entry (NULL if there isn't one)
  103  *
  104  * Return value: 0 on success or errno on failure.
  105  *
  106  * NOTE: Always checks for, and rejects, '.' and '..'.
  107  */
  108 int
  109 zfs_dirent_lookup(znode_t *dzp, const char *name, znode_t **zpp, int flag)
  110 {
  111         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
  112         matchtype_t     mt = 0;
  113         uint64_t        zoid;
  114         vnode_t         *vp = NULL;
  115         int             error = 0;
  116 
  117         ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
  118 
  119         *zpp = NULL;
  120 
  121         /*
  122          * Verify that we are not trying to lock '.', '..', or '.zfs'
  123          */
  124         if (name[0] == '.' &&
  125             (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
  126             zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
  127                 return (SET_ERROR(EEXIST));
  128 
  129         /*
  130          * Case sensitivity and normalization preferences are set when
  131          * the file system is created.  These are stored in the
  132          * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
  133          * affect how we perform zap lookups.
  134          *
  135          * When matching we may need to normalize & change case according to
  136          * FS settings.
  137          *
  138          * Note that a normalized match is necessary for a case insensitive
  139          * filesystem when the lookup request is not exact because normalization
  140          * can fold case independent of normalizing code point sequences.
  141          *
  142          * See the table above zfs_dropname().
  143          */
  144         if (zfsvfs->z_norm != 0) {
  145                 mt = MT_NORMALIZE;
  146 
  147                 /*
  148                  * Determine if the match needs to honor the case specified in
  149                  * lookup, and if so keep track of that so that during
  150                  * normalization we don't fold case.
  151                  */
  152                 if (zfsvfs->z_case == ZFS_CASE_MIXED) {
  153                         mt |= MT_MATCH_CASE;
  154                 }
  155         }
  156 
  157         /*
  158          * Only look in or update the DNLC if we are looking for the
  159          * name on a file system that does not require normalization
  160          * or case folding.  We can also look there if we happen to be
  161          * on a non-normalizing, mixed sensitivity file system IF we
  162          * are looking for the exact name.
  163          *
  164          * NB: we do not need to worry about this flag for ZFS_CASE_SENSITIVE
  165          * because in that case MT_EXACT and MT_FIRST should produce exactly
  166          * the same result.
  167          */
  168 
  169         if (dzp->z_unlinked && !(flag & ZXATTR))
  170                 return (ENOENT);
  171         if (flag & ZXATTR) {
  172                 error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
  173                     sizeof (zoid));
  174                 if (error == 0)
  175                         error = (zoid == 0 ? ENOENT : 0);
  176         } else {
  177                 error = zfs_match_find(zfsvfs, dzp, name, mt, &zoid);
  178         }
  179         if (error) {
  180                 if (error != ENOENT || (flag & ZEXISTS)) {
  181                         return (error);
  182                 }
  183         } else {
  184                 if (flag & ZNEW) {
  185                         return (SET_ERROR(EEXIST));
  186                 }
  187                 error = zfs_zget(zfsvfs, zoid, zpp);
  188                 if (error)
  189                         return (error);
  190                 ASSERT(!(*zpp)->z_unlinked);
  191         }
  192 
  193         return (0);
  194 }
  195 
  196 static int
  197 zfs_dd_lookup(znode_t *dzp, znode_t **zpp)
  198 {
  199         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
  200         znode_t *zp;
  201         uint64_t parent;
  202         int error;
  203 
  204         ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
  205         ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock));
  206 
  207         if (dzp->z_unlinked)
  208                 return (ENOENT);
  209 
  210         if ((error = sa_lookup(dzp->z_sa_hdl,
  211             SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
  212                 return (error);
  213 
  214         error = zfs_zget(zfsvfs, parent, &zp);
  215         if (error == 0)
  216                 *zpp = zp;
  217         return (error);
  218 }
  219 
  220 int
  221 zfs_dirlook(znode_t *dzp, const char *name, znode_t **zpp)
  222 {
  223         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
  224         znode_t *zp;
  225         int error = 0;
  226 
  227         ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
  228         ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock));
  229 
  230         if (dzp->z_unlinked)
  231                 return (SET_ERROR(ENOENT));
  232 
  233         if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
  234                 *zpp = dzp;
  235         } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
  236                 error = zfs_dd_lookup(dzp, zpp);
  237         } else {
  238                 error = zfs_dirent_lookup(dzp, name, &zp, ZEXISTS);
  239                 if (error == 0) {
  240                         dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
  241                         *zpp = zp;
  242                 }
  243         }
  244         return (error);
  245 }
  246 
  247 /*
  248  * unlinked Set (formerly known as the "delete queue") Error Handling
  249  *
  250  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
  251  * don't specify the name of the entry that we will be manipulating.  We
  252  * also fib and say that we won't be adding any new entries to the
  253  * unlinked set, even though we might (this is to lower the minimum file
  254  * size that can be deleted in a full filesystem).  So on the small
  255  * chance that the nlink list is using a fat zap (ie. has more than
  256  * 2000 entries), we *may* not pre-read a block that's needed.
  257  * Therefore it is remotely possible for some of the assertions
  258  * regarding the unlinked set below to fail due to i/o error.  On a
  259  * nondebug system, this will result in the space being leaked.
  260  */
  261 void
  262 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
  263 {
  264         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
  265 
  266         ASSERT(zp->z_unlinked);
  267         ASSERT(zp->z_links == 0);
  268 
  269         VERIFY3U(0, ==,
  270             zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
  271 }
  272 
  273 /*
  274  * Clean up any znodes that had no links when we either crashed or
  275  * (force) umounted the file system.
  276  */
  277 void
  278 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
  279 {
  280         zap_cursor_t    zc;
  281         zap_attribute_t zap;
  282         dmu_object_info_t doi;
  283         znode_t         *zp;
  284         dmu_tx_t        *tx;
  285         int             error;
  286 
  287         /*
  288          * Interate over the contents of the unlinked set.
  289          */
  290         for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
  291             zap_cursor_retrieve(&zc, &zap) == 0;
  292             zap_cursor_advance(&zc)) {
  293 
  294                 /*
  295                  * See what kind of object we have in list
  296                  */
  297 
  298                 error = dmu_object_info(zfsvfs->z_os,
  299                     zap.za_first_integer, &doi);
  300                 if (error != 0)
  301                         continue;
  302 
  303                 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
  304                     (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
  305                 /*
  306                  * We need to re-mark these list entries for deletion,
  307                  * so we pull them back into core and set zp->z_unlinked.
  308                  */
  309                 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
  310 
  311                 /*
  312                  * We may pick up znodes that are already marked for deletion.
  313                  * This could happen during the purge of an extended attribute
  314                  * directory.  All we need to do is skip over them, since they
  315                  * are already in the system marked z_unlinked.
  316                  */
  317                 if (error != 0)
  318                         continue;
  319 
  320                 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
  321 #if defined(__FreeBSD__)
  322                 /*
  323                  * Due to changes in zfs_rmnode we need to make sure the
  324                  * link count is set to zero here.
  325                  */
  326                 if (zp->z_links != 0) {
  327                         tx = dmu_tx_create(zfsvfs->z_os);
  328                         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
  329                         error = dmu_tx_assign(tx, TXG_WAIT);
  330                         if (error != 0) {
  331                                 dmu_tx_abort(tx);
  332                                 vput(ZTOV(zp));
  333                                 continue;
  334                         }
  335                         zp->z_links = 0;
  336                         VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
  337                             &zp->z_links, sizeof (zp->z_links), tx));
  338                         dmu_tx_commit(tx);
  339                 }
  340 #endif
  341                 zp->z_unlinked = B_TRUE;
  342                 vput(ZTOV(zp));
  343         }
  344         zap_cursor_fini(&zc);
  345 }
  346 
  347 /*
  348  * Delete the entire contents of a directory.  Return a count
  349  * of the number of entries that could not be deleted. If we encounter
  350  * an error, return a count of at least one so that the directory stays
  351  * in the unlinked set.
  352  *
  353  * NOTE: this function assumes that the directory is inactive,
  354  *      so there is no need to lock its entries before deletion.
  355  *      Also, it assumes the directory contents is *only* regular
  356  *      files.
  357  */
  358 static int
  359 zfs_purgedir(znode_t *dzp)
  360 {
  361         zap_cursor_t    zc;
  362         zap_attribute_t zap;
  363         znode_t         *xzp;
  364         dmu_tx_t        *tx;
  365         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
  366         int skipped = 0;
  367         int error;
  368 
  369         for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
  370             (error = zap_cursor_retrieve(&zc, &zap)) == 0;
  371             zap_cursor_advance(&zc)) {
  372                 error = zfs_zget(zfsvfs,
  373                     ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
  374                 if (error) {
  375                         skipped += 1;
  376                         continue;
  377                 }
  378 
  379                 vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY);
  380                 ASSERT((ZTOV(xzp)->v_type == VREG) ||
  381                     (ZTOV(xzp)->v_type == VLNK));
  382 
  383                 tx = dmu_tx_create(zfsvfs->z_os);
  384                 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
  385                 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
  386                 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
  387                 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
  388                 /* Is this really needed ? */
  389                 zfs_sa_upgrade_txholds(tx, xzp);
  390                 dmu_tx_mark_netfree(tx);
  391                 error = dmu_tx_assign(tx, TXG_WAIT);
  392                 if (error) {
  393                         dmu_tx_abort(tx);
  394                         vput(ZTOV(xzp));
  395                         skipped += 1;
  396                         continue;
  397                 }
  398 
  399                 error = zfs_link_destroy(dzp, zap.za_name, xzp, tx, 0, NULL);
  400                 if (error)
  401                         skipped += 1;
  402                 dmu_tx_commit(tx);
  403 
  404                 vput(ZTOV(xzp));
  405         }
  406         zap_cursor_fini(&zc);
  407         if (error != ENOENT)
  408                 skipped += 1;
  409         return (skipped);
  410 }
  411 
  412 #if defined(__FreeBSD__)
  413 extern taskq_t *zfsvfs_taskq;
  414 #endif
  415 
  416 void
  417 zfs_rmnode(znode_t *zp)
  418 {
  419         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
  420         objset_t        *os = zfsvfs->z_os;
  421         dmu_tx_t        *tx;
  422         uint64_t        acl_obj;
  423         uint64_t        xattr_obj;
  424         int             error;
  425 
  426         ASSERT(zp->z_links == 0);
  427         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
  428 
  429         /*
  430          * If this is an attribute directory, purge its contents.
  431          */
  432         if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR &&
  433             (zp->z_pflags & ZFS_XATTR)) {
  434                 if (zfs_purgedir(zp) != 0) {
  435                         /*
  436                          * Not enough space to delete some xattrs.
  437                          * Leave it in the unlinked set.
  438                          */
  439                         zfs_znode_dmu_fini(zp);
  440                         zfs_znode_free(zp);
  441                         return;
  442                 }
  443         } else {
  444                 /*
  445                  * Free up all the data in the file.  We don't do this for
  446                  * XATTR directories because we need truncate and remove to be
  447                  * in the same tx, like in zfs_znode_delete(). Otherwise, if
  448                  * we crash here we'll end up with an inconsistent truncated
  449                  * zap object in the delete queue.  Note a truncated file is
  450                  * harmless since it only contains user data.
  451                  */
  452                 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
  453                 if (error) {
  454                         /*
  455                          * Not enough space or we were interrupted by unmount.
  456                          * Leave the file in the unlinked set.
  457                          */
  458                         zfs_znode_dmu_fini(zp);
  459                         zfs_znode_free(zp);
  460                         return;
  461                 }
  462         }
  463 
  464         /*
  465          * If the file has extended attributes, we're going to unlink
  466          * the xattr dir.
  467          */
  468         error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
  469             &xattr_obj, sizeof (xattr_obj));
  470         if (error)
  471                 xattr_obj = 0;
  472 
  473         acl_obj = zfs_external_acl(zp);
  474 
  475         /*
  476          * Set up the final transaction.
  477          */
  478         tx = dmu_tx_create(os);
  479         dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
  480         dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
  481         if (xattr_obj)
  482                 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
  483         if (acl_obj)
  484                 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
  485 
  486         zfs_sa_upgrade_txholds(tx, zp);
  487         error = dmu_tx_assign(tx, TXG_WAIT);
  488         if (error) {
  489                 /*
  490                  * Not enough space to delete the file.  Leave it in the
  491                  * unlinked set, leaking it until the fs is remounted (at
  492                  * which point we'll call zfs_unlinked_drain() to process it).
  493                  */
  494                 dmu_tx_abort(tx);
  495                 zfs_znode_dmu_fini(zp);
  496                 zfs_znode_free(zp);
  497                 return;
  498         }
  499 
  500 #if defined(__FreeBSD__)
  501         /*
  502          * FreeBSD's implemention of zfs_zget requires a vnode to back it.
  503          * This means that we could end up calling into getnewvnode while
  504          * calling zfs_rmnode as a result of a prior call to getnewvnode
  505          * trying to clear vnodes out of the cache. If this repeats we can
  506          * recurse enough that we overflow our stack. To avoid this, we
  507          * avoid calling zfs_zget on the xattr znode and instead simply add
  508          * it to the unlinked set and schedule a call to zfs_unlinked_drain.
  509          */
  510         if (xattr_obj) {
  511                 /* Add extended attribute directory to the unlinked set. */
  512                 VERIFY3U(0, ==,
  513                     zap_add_int(os, zfsvfs->z_unlinkedobj, xattr_obj, tx));
  514         }
  515 #else
  516         if (xzp) {
  517                 ASSERT(error == 0);
  518                 xzp->z_unlinked = B_TRUE;       /* mark xzp for deletion */
  519                 xzp->z_links = 0;       /* no more links to it */
  520                 VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
  521                     &xzp->z_links, sizeof (xzp->z_links), tx));
  522                 zfs_unlinked_add(xzp, tx);
  523         }
  524 #endif
  525 
  526         /* Remove this znode from the unlinked set */
  527         VERIFY3U(0, ==,
  528             zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
  529 
  530         zfs_znode_delete(zp, tx);
  531 
  532         dmu_tx_commit(tx);
  533 
  534 #if defined(__FreeBSD__)
  535         if (xattr_obj) {
  536                 /*
  537                  * We're using the FreeBSD taskqueue API here instead of
  538                  * the Solaris taskq API since the FreeBSD API allows for a
  539                  * task to be enqueued multiple times but executed once.
  540                  */
  541                 taskqueue_enqueue(zfsvfs_taskq->tq_queue,
  542                     &zfsvfs->z_unlinked_drain_task);
  543         }
  544 #endif
  545 }
  546 
  547 static uint64_t
  548 zfs_dirent(znode_t *zp, uint64_t mode)
  549 {
  550         uint64_t de = zp->z_id;
  551 
  552         if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
  553                 de |= IFTODT(mode) << 60;
  554         return (de);
  555 }
  556 
  557 /*
  558  * Link zp into dzp.  Can only fail if zp has been unlinked.
  559  */
  560 int
  561 zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
  562     int flag)
  563 {
  564         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
  565         vnode_t *vp = ZTOV(zp);
  566         uint64_t value;
  567         int zp_is_dir = (vp->v_type == VDIR);
  568         sa_bulk_attr_t bulk[5];
  569         uint64_t mtime[2], ctime[2];
  570         int count = 0;
  571         int error;
  572 
  573         ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
  574         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
  575 #if 0
  576         if (zp_is_dir) {
  577                 error = 0;
  578                 if (dzp->z_links >= LINK_MAX)
  579                         error = SET_ERROR(EMLINK);
  580                 return (error);
  581         }
  582 #endif
  583         if (!(flag & ZRENAMING)) {
  584                 if (zp->z_unlinked) {   /* no new links to unlinked zp */
  585                         ASSERT(!(flag & (ZNEW | ZEXISTS)));
  586                         return (SET_ERROR(ENOENT));
  587                 }
  588 #if 0
  589                 if (zp->z_links >= LINK_MAX) {
  590                         return (SET_ERROR(EMLINK));
  591                 }
  592 #endif
  593                 zp->z_links++;
  594                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
  595                     &zp->z_links, sizeof (zp->z_links));
  596 
  597         } else {
  598                 ASSERT(zp->z_unlinked == 0);
  599         }
  600         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
  601             &dzp->z_id, sizeof (dzp->z_id));
  602         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
  603             &zp->z_pflags, sizeof (zp->z_pflags));
  604 
  605         if (!(flag & ZNEW)) {
  606                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
  607                     ctime, sizeof (ctime));
  608                 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
  609                     ctime, B_TRUE);
  610         }
  611         error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
  612         ASSERT0(error);
  613 
  614         dzp->z_size++;
  615         dzp->z_links += zp_is_dir;
  616         count = 0;
  617         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
  618             &dzp->z_size, sizeof (dzp->z_size));
  619         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
  620             &dzp->z_links, sizeof (dzp->z_links));
  621         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
  622             mtime, sizeof (mtime));
  623         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
  624             ctime, sizeof (ctime));
  625         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
  626             &dzp->z_pflags, sizeof (dzp->z_pflags));
  627         zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
  628         error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
  629         ASSERT0(error);
  630 
  631         value = zfs_dirent(zp, zp->z_mode);
  632         error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name,
  633             8, 1, &value, tx);
  634         VERIFY0(error);
  635 
  636         return (0);
  637 }
  638 
  639 /*
  640  * The match type in the code for this function should conform to:
  641  *
  642  * ------------------------------------------------------------------------
  643  * fs type  | z_norm      | lookup type | match type
  644  * ---------|-------------|-------------|----------------------------------
  645  * CS !norm | 0           |           0 | 0 (exact)
  646  * CS  norm | formX       |           0 | MT_NORMALIZE
  647  * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
  648  * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
  649  * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
  650  * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
  651  * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
  652  * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
  653  * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
  654  * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
  655  *
  656  * Abbreviations:
  657  *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
  658  *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
  659  *    formX = unicode normalization form set on fs creation
  660  */
  661 static int
  662 zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
  663     int flag)
  664 {
  665         int error;
  666 
  667         if (zp->z_zfsvfs->z_norm) {
  668                 matchtype_t mt = MT_NORMALIZE;
  669 
  670                 if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) {
  671                         mt |= MT_MATCH_CASE;
  672                 }
  673 
  674                 error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
  675                     name, mt, tx);
  676         } else {
  677                 error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, name, tx);
  678         }
  679 
  680         return (error);
  681 }
  682 
  683 /*
  684  * Unlink zp from dzp, and mark zp for deletion if this was the last link.
  685  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
  686  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
  687  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
  688  * and it's the caller's job to do it.
  689  */
  690 int
  691 zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
  692     int flag, boolean_t *unlinkedp)
  693 {
  694         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
  695         vnode_t *vp = ZTOV(zp);
  696         int zp_is_dir = (vp->v_type == VDIR);
  697         boolean_t unlinked = B_FALSE;
  698         sa_bulk_attr_t bulk[5];
  699         uint64_t mtime[2], ctime[2];
  700         int count = 0;
  701         int error;
  702 
  703         ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
  704         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
  705 
  706         if (!(flag & ZRENAMING)) {
  707 
  708                 if (zp_is_dir && !zfs_dirempty(zp)) {
  709 #ifdef illumos
  710                         return (SET_ERROR(EEXIST));
  711 #else
  712                         return (SET_ERROR(ENOTEMPTY));
  713 #endif
  714                 }
  715 
  716                 /*
  717                  * If we get here, we are going to try to remove the object.
  718                  * First try removing the name from the directory; if that
  719                  * fails, return the error.
  720                  */
  721                 error = zfs_dropname(dzp, name, zp, tx, flag);
  722                 if (error != 0) {
  723                         return (error);
  724                 }
  725 
  726                 if (zp->z_links <= zp_is_dir) {
  727                         zfs_panic_recover("zfs: link count on vnode %p is %u, "
  728                             "should be at least %u", zp->z_vnode,
  729                             (int)zp->z_links,
  730                             zp_is_dir + 1);
  731                         zp->z_links = zp_is_dir + 1;
  732                 }
  733                 if (--zp->z_links == zp_is_dir) {
  734                         zp->z_unlinked = B_TRUE;
  735                         zp->z_links = 0;
  736                         unlinked = B_TRUE;
  737                 } else {
  738                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
  739                             NULL, &ctime, sizeof (ctime));
  740                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
  741                             NULL, &zp->z_pflags, sizeof (zp->z_pflags));
  742                         zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
  743                             B_TRUE);
  744                 }
  745                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
  746                     NULL, &zp->z_links, sizeof (zp->z_links));
  747                 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
  748                 count = 0;
  749                 ASSERT0(error);
  750         } else {
  751                 ASSERT(zp->z_unlinked == 0);
  752                 error = zfs_dropname(dzp, name, zp, tx, flag);
  753                 if (error != 0)
  754                         return (error);
  755         }
  756 
  757         dzp->z_size--;          /* one dirent removed */
  758         dzp->z_links -= zp_is_dir;      /* ".." link from zp */
  759         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
  760             NULL, &dzp->z_links, sizeof (dzp->z_links));
  761         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
  762             NULL, &dzp->z_size, sizeof (dzp->z_size));
  763         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
  764             NULL, ctime, sizeof (ctime));
  765         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
  766             NULL, mtime, sizeof (mtime));
  767         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
  768             NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
  769         zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
  770         error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
  771         ASSERT0(error);
  772 
  773         if (unlinkedp != NULL)
  774                 *unlinkedp = unlinked;
  775         else if (unlinked)
  776                 zfs_unlinked_add(zp, tx);
  777 
  778         return (0);
  779 }
  780 
  781 /*
  782  * Indicate whether the directory is empty.
  783  */
  784 boolean_t
  785 zfs_dirempty(znode_t *dzp)
  786 {
  787         return (dzp->z_size == 2);
  788 }
  789 
  790 int
  791 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
  792 {
  793         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
  794         znode_t *xzp;
  795         dmu_tx_t *tx;
  796         int error;
  797         zfs_acl_ids_t acl_ids;
  798         boolean_t fuid_dirtied;
  799         uint64_t parent;
  800 
  801         *xvpp = NULL;
  802 
  803         /*
  804          * In FreeBSD, access checking for creating an EA is being done
  805          * in zfs_setextattr(),
  806          */
  807 #ifndef __FreeBSD_kernel__
  808         if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
  809                 return (error);
  810 #endif
  811 
  812         if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
  813             &acl_ids)) != 0)
  814                 return (error);
  815         if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
  816                 zfs_acl_ids_free(&acl_ids);
  817                 return (SET_ERROR(EDQUOT));
  818         }
  819 
  820         getnewvnode_reserve(1);
  821 
  822         tx = dmu_tx_create(zfsvfs->z_os);
  823         dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
  824             ZFS_SA_BASE_ATTR_SIZE);
  825         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
  826         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
  827         fuid_dirtied = zfsvfs->z_fuid_dirty;
  828         if (fuid_dirtied)
  829                 zfs_fuid_txhold(zfsvfs, tx);
  830         error = dmu_tx_assign(tx, TXG_WAIT);
  831         if (error) {
  832                 zfs_acl_ids_free(&acl_ids);
  833                 dmu_tx_abort(tx);
  834                 return (error);
  835         }
  836         zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
  837 
  838         if (fuid_dirtied)
  839                 zfs_fuid_sync(zfsvfs, tx);
  840 
  841 #ifdef DEBUG
  842         error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
  843             &parent, sizeof (parent));
  844         ASSERT(error == 0 && parent == zp->z_id);
  845 #endif
  846 
  847         VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
  848             sizeof (xzp->z_id), tx));
  849 
  850         (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
  851             xzp, "", NULL, acl_ids.z_fuidp, vap);
  852 
  853         zfs_acl_ids_free(&acl_ids);
  854         dmu_tx_commit(tx);
  855 
  856         getnewvnode_drop_reserve();
  857 
  858         *xvpp = ZTOV(xzp);
  859 
  860         return (0);
  861 }
  862 
  863 /*
  864  * Return a znode for the extended attribute directory for zp.
  865  * ** If the directory does not already exist, it is created **
  866  *
  867  *      IN:     zp      - znode to obtain attribute directory from
  868  *              cr      - credentials of caller
  869  *              flags   - flags from the VOP_LOOKUP call
  870  *
  871  *      OUT:    xzpp    - pointer to extended attribute znode
  872  *
  873  *      RETURN: 0 on success
  874  *              error number on failure
  875  */
  876 int
  877 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
  878 {
  879         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
  880         znode_t         *xzp;
  881         vattr_t         va;
  882         int             error;
  883 top:
  884         error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR);
  885         if (error)
  886                 return (error);
  887 
  888         if (xzp != NULL) {
  889                 *xvpp = ZTOV(xzp);
  890                 return (0);
  891         }
  892 
  893 
  894         if (!(flags & CREATE_XATTR_DIR)) {
  895 #ifdef illumos
  896                 return (SET_ERROR(ENOENT));
  897 #else
  898                 return (SET_ERROR(ENOATTR));
  899 #endif
  900         }
  901 
  902         if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
  903                 return (SET_ERROR(EROFS));
  904         }
  905 
  906         /*
  907          * The ability to 'create' files in an attribute
  908          * directory comes from the write_xattr permission on the base file.
  909          *
  910          * The ability to 'search' an attribute directory requires
  911          * read_xattr permission on the base file.
  912          *
  913          * Once in a directory the ability to read/write attributes
  914          * is controlled by the permissions on the attribute file.
  915          */
  916         va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
  917         va.va_type = VDIR;
  918         va.va_mode = S_IFDIR | S_ISVTX | 0777;
  919         zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
  920 
  921         error = zfs_make_xattrdir(zp, &va, xvpp, cr);
  922 
  923         if (error == ERESTART) {
  924                 /* NB: we already did dmu_tx_wait() if necessary */
  925                 goto top;
  926         }
  927         if (error == 0)
  928                 VOP_UNLOCK(*xvpp, 0);
  929 
  930         return (error);
  931 }
  932 
  933 /*
  934  * Decide whether it is okay to remove within a sticky directory.
  935  *
  936  * In sticky directories, write access is not sufficient;
  937  * you can remove entries from a directory only if:
  938  *
  939  *      you own the directory,
  940  *      you own the entry,
  941  *      the entry is a plain file and you have write access,
  942  *      or you are privileged (checked in secpolicy...).
  943  *
  944  * The function returns 0 if remove access is granted.
  945  */
  946 int
  947 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
  948 {
  949         uid_t           uid;
  950         uid_t           downer;
  951         uid_t           fowner;
  952         zfsvfs_t        *zfsvfs = zdp->z_zfsvfs;
  953 
  954         if (zdp->z_zfsvfs->z_replay)
  955                 return (0);
  956 
  957         if ((zdp->z_mode & S_ISVTX) == 0)
  958                 return (0);
  959 
  960         downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
  961         fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
  962 
  963         if ((uid = crgetuid(cr)) == downer || uid == fowner ||
  964             (ZTOV(zp)->v_type == VREG &&
  965             zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
  966                 return (0);
  967         else
  968                 return (secpolicy_vnode_remove(ZTOV(zp), cr));
  969 }

Cache object: f7754f48f431aefdc9d86436da46836e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.