Print this page
    
NEX-3562 filename normalization doesn't work for removes (sync with upstream)
NEX-8972 Async-delete side-effect that may cause unmount EBUSY
Reviewed by: Alek Pinchuk <alek@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
NEX-8466 Failed to do 'pkg update' because of beadm failed to create BE, because of EBUSY in umount
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
NEX-5108 make deletes interruptible on pool export
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
NEX-5036 case-insensitive matching is not working on casesensitivity=mixed, normalization=none FS
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
NEX-3562 filename normalization doesn't work for removes
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
NEX-4290 'insensitive' case sensitivity is broken
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
NEX-3762 Appliance crashes with a NULL pointer dereference during a zpool export when a zfs_vn_rele_taskq thread attempts to check a bogus rwlock from rw_write_held
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
6328 Fix cstyle errors in zfs codebase (fix studio)
6328 Fix cstyle errors in zfs codebase
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Alex Reece <alex@delphix.com>
Reviewed by: Richard Elling <Richard.Elling@RichardElling.com>
Reviewed by: Jorgen Lundman <lundman@lundman.net>
Approved by: Robert Mustacchi <rm@joyent.com>
NEX-3329 libnsl: set_up_connection() over TCP does not adhere the specified timeout
Reviewed by: Dan Fields <dan.fields@nexenta.com>
NEX-3521 CLONE - Port NEX-3209 normalization=formD and casesensitivity=mixed behaves improperly, squashing case
Reviewed by: Jean McCormack <jean.mccormack@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Dan Fields <dan.fields@nexenta.com>
NEX-2965 4.0.3-FP2: deferred deletes causing RSF import failure during fail-over of service
Reviewed by: Josef Sipek <josef.sipek@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
SUP-780 zfs with casesensitivity=mixed and Unicode normalization doesn't normalize properly/fully
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/zfs/zfs_dir.c
          +++ new/usr/src/uts/common/fs/zfs/zfs_dir.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
  25   25   * Copyright 2017 Nexenta Systems, Inc.
  26   26   * Copyright (c) 2015, Joyent, Inc.
  27   27   */
  28   28  
  29   29  #include <sys/types.h>
  30   30  #include <sys/param.h>
  31   31  #include <sys/time.h>
  32   32  #include <sys/systm.h>
  33   33  #include <sys/sysmacros.h>
  34   34  #include <sys/resource.h>
  35   35  #include <sys/vfs.h>
  36   36  #include <sys/vnode.h>
  37   37  #include <sys/file.h>
  38   38  #include <sys/mode.h>
  39   39  #include <sys/kmem.h>
  40   40  #include <sys/uio.h>
  41   41  #include <sys/pathname.h>
  42   42  #include <sys/cmn_err.h>
  43   43  #include <sys/errno.h>
  44   44  #include <sys/stat.h>
  
    | 
      ↓ open down ↓ | 
    44 lines elided | 
    
      ↑ open up ↑ | 
  
  45   45  #include <sys/unistd.h>
  46   46  #include <sys/sunddi.h>
  47   47  #include <sys/random.h>
  48   48  #include <sys/policy.h>
  49   49  #include <sys/zfs_dir.h>
  50   50  #include <sys/zfs_acl.h>
  51   51  #include <sys/fs/zfs.h>
  52   52  #include "fs/fs_subr.h"
  53   53  #include <sys/zap.h>
  54   54  #include <sys/dmu.h>
       55 +#include <sys/dmu_objset.h>
  55   56  #include <sys/atomic.h>
  56   57  #include <sys/zfs_ctldir.h>
  57   58  #include <sys/zfs_fuid.h>
  58   59  #include <sys/sa.h>
  59   60  #include <sys/zfs_sa.h>
  60   61  #include <sys/dnlc.h>
  61   62  #include <sys/extdirent.h>
  62   63  
  63   64  /*
  64   65   * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
  65   66   * of names after deciding which is the appropriate lookup interface.
  66   67   */
  67   68  static int
  68   69  zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, matchtype_t mt,
  69   70      boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
  70   71  {
  71   72          int error;
  72   73  
  73   74          if (zfsvfs->z_norm) {
  74   75                  boolean_t conflict = B_FALSE;
  75   76                  size_t bufsz = 0;
  76   77                  char *buf = NULL;
  77   78  
  78   79                  if (rpnp) {
  79   80                          buf = rpnp->pn_buf;
  80   81                          bufsz = rpnp->pn_bufsize;
  81   82                  }
  82   83  
  83   84                  /*
  84   85                   * In the non-mixed case we only expect there would ever
  85   86                   * be one match, but we need to use the normalizing lookup.
  86   87                   */
  87   88                  error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
  88   89                      zoid, mt, buf, bufsz, &conflict);
  89   90                  if (!error && deflags)
  90   91                          *deflags = conflict ? ED_CASE_CONFLICT : 0;
  91   92          } else {
  92   93                  error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
  93   94          }
  94   95          *zoid = ZFS_DIRENT_OBJ(*zoid);
  95   96  
  96   97          if (error == ENOENT && update)
  97   98                  dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
  98   99  
  99  100          return (error);
 100  101  }
 101  102  
 102  103  /*
 103  104   * Lock a directory entry.  A dirlock on <dzp, name> protects that name
 104  105   * in dzp's directory zap object.  As long as you hold a dirlock, you can
 105  106   * assume two things: (1) dzp cannot be reaped, and (2) no other thread
 106  107   * can change the zap entry for (i.e. link or unlink) this name.
 107  108   *
 108  109   * Input arguments:
 109  110   *      dzp     - znode for directory
 110  111   *      name    - name of entry to lock
 111  112   *      flag    - ZNEW: if the entry already exists, fail with EEXIST.
 112  113   *                ZEXISTS: if the entry does not exist, fail with ENOENT.
 113  114   *                ZSHARED: allow concurrent access with other ZSHARED callers.
 114  115   *                ZXATTR: we want dzp's xattr directory
 115  116   *                ZCILOOK: On a mixed sensitivity file system,
 116  117   *                         this lookup should be case-insensitive.
 117  118   *                ZCIEXACT: On a purely case-insensitive file system,
 118  119   *                          this lookup should be case-sensitive.
 119  120   *                ZRENAMING: we are locking for renaming, force narrow locks
 120  121   *                ZHAVELOCK: Don't grab the z_name_lock for this call. The
 121  122   *                           current thread already holds it.
 122  123   *
 123  124   * Output arguments:
 124  125   *      zpp     - pointer to the znode for the entry (NULL if there isn't one)
 125  126   *      dlpp    - pointer to the dirlock for this entry (NULL on error)
 126  127   *      direntflags - (case-insensitive lookup only)
 127  128   *              flags if multiple case-sensitive matches exist in directory
 128  129   *      realpnp     - (case-insensitive lookup only)
 129  130   *              actual name matched within the directory
 130  131   *
 131  132   * Return value: 0 on success or errno on failure.
 132  133   *
 133  134   * NOTE: Always checks for, and rejects, '.' and '..'.
 134  135   * NOTE: For case-insensitive file systems we take wide locks (see below),
 135  136   *       but return znode pointers to a single match.
 136  137   */
 137  138  int
 138  139  zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
 139  140      int flag, int *direntflags, pathname_t *realpnp)
 140  141  {
 141  142          zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
 142  143          zfs_dirlock_t   *dl;
 143  144          boolean_t       update;
 144  145          matchtype_t     mt = 0;
 145  146          uint64_t        zoid;
 146  147          vnode_t         *vp = NULL;
 147  148          int             error = 0;
 148  149          int             cmpflags;
 149  150  
 150  151          *zpp = NULL;
 151  152          *dlpp = NULL;
 152  153  
 153  154          /*
 154  155           * Verify that we are not trying to lock '.', '..', or '.zfs'
 155  156           */
 156  157          if (name[0] == '.' &&
 157  158              (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
 158  159              zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
 159  160                  return (SET_ERROR(EEXIST));
 160  161  
 161  162          /*
 162  163           * Case sensitivity and normalization preferences are set when
 163  164           * the file system is created.  These are stored in the
 164  165           * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
 165  166           * affect what vnodes can be cached in the DNLC, how we
 166  167           * perform zap lookups, and the "width" of our dirlocks.
 167  168           *
 168  169           * A normal dirlock locks a single name.  Note that with
 169  170           * normalization a name can be composed multiple ways, but
 170  171           * when normalized, these names all compare equal.  A wide
 171  172           * dirlock locks multiple names.  We need these when the file
 172  173           * system is supporting mixed-mode access.  It is sometimes
 173  174           * necessary to lock all case permutations of file name at
 174  175           * once so that simultaneous case-insensitive/case-sensitive
 175  176           * behaves as rationally as possible.
 176  177           */
 177  178  
 178  179          /*
 179  180           * When matching we may need to normalize & change case according to
 180  181           * FS settings.
 181  182           *
 182  183           * Note that a normalized match is necessary for a case insensitive
 183  184           * filesystem when the lookup request is not exact because normalization
 184  185           * can fold case independent of normalizing code point sequences.
 185  186           *
 186  187           * See the table above zfs_dropname().
 187  188           */
 188  189          if (zfsvfs->z_norm != 0) {
 189  190                  mt = MT_NORMALIZE;
 190  191  
 191  192                  /*
 192  193                   * Determine if the match needs to honor the case specified in
 193  194                   * lookup, and if so keep track of that so that during
 194  195                   * normalization we don't fold case.
 195  196                   */
 196  197                  if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
 197  198                      (flag & ZCIEXACT)) ||
 198  199                      (zfsvfs->z_case == ZFS_CASE_MIXED && !(flag & ZCILOOK))) {
 199  200                          mt |= MT_MATCH_CASE;
 200  201                  }
 201  202          }
 202  203  
 203  204          /*
 204  205           * Only look in or update the DNLC if we are looking for the
 205  206           * name on a file system that does not require normalization
 206  207           * or case folding.  We can also look there if we happen to be
 207  208           * on a non-normalizing, mixed sensitivity file system IF we
 208  209           * are looking for the exact name.
 209  210           *
 210  211           * Maybe can add TO-UPPERed version of name to dnlc in ci-only
 211  212           * case for performance improvement?
 212  213           */
 213  214          update = !zfsvfs->z_norm ||
 214  215              (zfsvfs->z_case == ZFS_CASE_MIXED &&
 215  216              !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
 216  217  
 217  218          /*
 218  219           * ZRENAMING indicates we are in a situation where we should
 219  220           * take narrow locks regardless of the file system's
 220  221           * preferences for normalizing and case folding.  This will
 221  222           * prevent us deadlocking trying to grab the same wide lock
 222  223           * twice if the two names happen to be case-insensitive
 223  224           * matches.
 224  225           */
 225  226          if (flag & ZRENAMING)
 226  227                  cmpflags = 0;
 227  228          else
 228  229                  cmpflags = zfsvfs->z_norm;
 229  230  
 230  231          /*
 231  232           * Wait until there are no locks on this name.
 232  233           *
 233  234           * Don't grab the the lock if it is already held. However, cannot
 234  235           * have both ZSHARED and ZHAVELOCK together.
 235  236           */
 236  237          ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK));
 237  238          if (!(flag & ZHAVELOCK))
 238  239                  rw_enter(&dzp->z_name_lock, RW_READER);
 239  240  
 240  241          mutex_enter(&dzp->z_lock);
 241  242          for (;;) {
 242  243                  if (dzp->z_unlinked) {
 243  244                          mutex_exit(&dzp->z_lock);
 244  245                          if (!(flag & ZHAVELOCK))
 245  246                                  rw_exit(&dzp->z_name_lock);
 246  247                          return (SET_ERROR(ENOENT));
 247  248                  }
 248  249                  for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
 249  250                          if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
 250  251                              U8_UNICODE_LATEST, &error) == 0) || error != 0)
 251  252                                  break;
 252  253                  }
 253  254                  if (error != 0) {
 254  255                          mutex_exit(&dzp->z_lock);
 255  256                          if (!(flag & ZHAVELOCK))
 256  257                                  rw_exit(&dzp->z_name_lock);
 257  258                          return (SET_ERROR(ENOENT));
 258  259                  }
 259  260                  if (dl == NULL) {
 260  261                          /*
 261  262                           * Allocate a new dirlock and add it to the list.
 262  263                           */
 263  264                          dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
 264  265                          cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
 265  266                          dl->dl_name = name;
 266  267                          dl->dl_sharecnt = 0;
 267  268                          dl->dl_namelock = 0;
 268  269                          dl->dl_namesize = 0;
 269  270                          dl->dl_dzp = dzp;
 270  271                          dl->dl_next = dzp->z_dirlocks;
 271  272                          dzp->z_dirlocks = dl;
 272  273                          break;
 273  274                  }
 274  275                  if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
 275  276                          break;
 276  277                  cv_wait(&dl->dl_cv, &dzp->z_lock);
 277  278          }
 278  279  
 279  280          /*
 280  281           * If the z_name_lock was NOT held for this dirlock record it.
 281  282           */
 282  283          if (flag & ZHAVELOCK)
 283  284                  dl->dl_namelock = 1;
 284  285  
 285  286          if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
 286  287                  /*
 287  288                   * We're the second shared reference to dl.  Make a copy of
 288  289                   * dl_name in case the first thread goes away before we do.
 289  290                   * Note that we initialize the new name before storing its
 290  291                   * pointer into dl_name, because the first thread may load
 291  292                   * dl->dl_name at any time.  It'll either see the old value,
 292  293                   * which belongs to it, or the new shared copy; either is OK.
 293  294                   */
 294  295                  dl->dl_namesize = strlen(dl->dl_name) + 1;
 295  296                  name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
 296  297                  bcopy(dl->dl_name, name, dl->dl_namesize);
 297  298                  dl->dl_name = name;
 298  299          }
 299  300  
 300  301          mutex_exit(&dzp->z_lock);
 301  302  
 302  303          /*
 303  304           * We have a dirlock on the name.  (Note that it is the dirlock,
 304  305           * not the dzp's z_lock, that protects the name in the zap object.)
 305  306           * See if there's an object by this name; if so, put a hold on it.
 306  307           */
 307  308          if (flag & ZXATTR) {
 308  309                  error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
 309  310                      sizeof (zoid));
 310  311                  if (error == 0)
 311  312                          error = (zoid == 0 ? ENOENT : 0);
 312  313          } else {
 313  314                  if (update)
 314  315                          vp = dnlc_lookup(ZTOV(dzp), name);
 315  316                  if (vp == DNLC_NO_VNODE) {
 316  317                          VN_RELE(vp);
 317  318                          error = SET_ERROR(ENOENT);
 318  319                  } else if (vp) {
 319  320                          if (flag & ZNEW) {
 320  321                                  zfs_dirent_unlock(dl);
 321  322                                  VN_RELE(vp);
 322  323                                  return (SET_ERROR(EEXIST));
 323  324                          }
 324  325                          *dlpp = dl;
 325  326                          *zpp = VTOZ(vp);
 326  327                          return (0);
 327  328                  } else {
 328  329                          error = zfs_match_find(zfsvfs, dzp, name, mt,
 329  330                              update, direntflags, realpnp, &zoid);
 330  331                  }
 331  332          }
 332  333          if (error) {
 333  334                  if (error != ENOENT || (flag & ZEXISTS)) {
 334  335                          zfs_dirent_unlock(dl);
 335  336                          return (error);
 336  337                  }
 337  338          } else {
 338  339                  if (flag & ZNEW) {
 339  340                          zfs_dirent_unlock(dl);
 340  341                          return (SET_ERROR(EEXIST));
 341  342                  }
 342  343                  error = zfs_zget(zfsvfs, zoid, zpp);
 343  344                  if (error) {
 344  345                          zfs_dirent_unlock(dl);
 345  346                          return (error);
 346  347                  }
 347  348                  if (!(flag & ZXATTR) && update)
 348  349                          dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
 349  350          }
 350  351  
 351  352          *dlpp = dl;
 352  353  
 353  354          return (0);
 354  355  }
 355  356  
 356  357  /*
 357  358   * Unlock this directory entry and wake anyone who was waiting for it.
 358  359   */
 359  360  void
 360  361  zfs_dirent_unlock(zfs_dirlock_t *dl)
 361  362  {
 362  363          znode_t *dzp = dl->dl_dzp;
 363  364          zfs_dirlock_t **prev_dl, *cur_dl;
 364  365  
 365  366          mutex_enter(&dzp->z_lock);
 366  367  
 367  368          if (!dl->dl_namelock)
 368  369                  rw_exit(&dzp->z_name_lock);
 369  370  
 370  371          if (dl->dl_sharecnt > 1) {
 371  372                  dl->dl_sharecnt--;
 372  373                  mutex_exit(&dzp->z_lock);
 373  374                  return;
 374  375          }
 375  376          prev_dl = &dzp->z_dirlocks;
 376  377          while ((cur_dl = *prev_dl) != dl)
 377  378                  prev_dl = &cur_dl->dl_next;
 378  379          *prev_dl = dl->dl_next;
 379  380          cv_broadcast(&dl->dl_cv);
 380  381          mutex_exit(&dzp->z_lock);
 381  382  
 382  383          if (dl->dl_namesize != 0)
 383  384                  kmem_free(dl->dl_name, dl->dl_namesize);
 384  385          cv_destroy(&dl->dl_cv);
 385  386          kmem_free(dl, sizeof (*dl));
 386  387  }
 387  388  
 388  389  /*
 389  390   * Look up an entry in a directory.
 390  391   *
 391  392   * NOTE: '.' and '..' are handled as special cases because
 392  393   *      no directory entries are actually stored for them.  If this is
 393  394   *      the root of a filesystem, then '.zfs' is also treated as a
 394  395   *      special pseudo-directory.
 395  396   */
 396  397  int
 397  398  zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
 398  399      int *deflg, pathname_t *rpnp)
 399  400  {
 400  401          zfs_dirlock_t *dl;
 401  402          znode_t *zp;
 402  403          int error = 0;
 403  404          uint64_t parent;
 404  405  
 405  406          if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
 406  407                  *vpp = ZTOV(dzp);
 407  408                  VN_HOLD(*vpp);
 408  409          } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
 409  410                  zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
 410  411  
 411  412                  /*
 412  413                   * If we are a snapshot mounted under .zfs, return
 413  414                   * the vp for the snapshot directory.
 414  415                   */
 415  416                  if ((error = sa_lookup(dzp->z_sa_hdl,
 416  417                      SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
 417  418                          return (error);
 418  419                  if (parent == dzp->z_id && zfsvfs->z_parent != zfsvfs) {
 419  420                          error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
 420  421                              "snapshot", vpp, NULL, 0, NULL, kcred,
 421  422                              NULL, NULL, NULL);
 422  423                          return (error);
 423  424                  }
 424  425                  rw_enter(&dzp->z_parent_lock, RW_READER);
 425  426                  error = zfs_zget(zfsvfs, parent, &zp);
 426  427                  if (error == 0)
 427  428                          *vpp = ZTOV(zp);
 428  429                  rw_exit(&dzp->z_parent_lock);
 429  430          } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
 430  431                  *vpp = zfsctl_root(dzp);
 431  432          } else {
 432  433                  int zf;
 433  434  
 434  435                  zf = ZEXISTS | ZSHARED;
 435  436                  if (flags & FIGNORECASE)
 436  437                          zf |= ZCILOOK;
 437  438  
 438  439                  error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
 439  440                  if (error == 0) {
 440  441                          *vpp = ZTOV(zp);
 441  442                          zfs_dirent_unlock(dl);
 442  443                          dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
 443  444                  }
 444  445                  rpnp = NULL;
 445  446          }
 446  447  
 447  448          if ((flags & FIGNORECASE) && rpnp && !error)
 448  449                  (void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
 449  450  
 450  451          return (error);
 451  452  }
 452  453  
 453  454  /*
 454  455   * unlinked Set (formerly known as the "delete queue") Error Handling
 455  456   *
 456  457   * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
 457  458   * don't specify the name of the entry that we will be manipulating.  We
 458  459   * also fib and say that we won't be adding any new entries to the
 459  460   * unlinked set, even though we might (this is to lower the minimum file
 460  461   * size that can be deleted in a full filesystem).  So on the small
 461  462   * chance that the nlink list is using a fat zap (ie. has more than
 462  463   * 2000 entries), we *may* not pre-read a block that's needed.
 463  464   * Therefore it is remotely possible for some of the assertions
 464  465   * regarding the unlinked set below to fail due to i/o error.  On a
 465  466   * nondebug system, this will result in the space being leaked.
 466  467   */
 467  468  void
 468  469  zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
 469  470  {
 470  471          zfsvfs_t *zfsvfs = zp->z_zfsvfs;
 471  472  
 472  473          ASSERT(zp->z_unlinked);
  
    | 
      ↓ open down ↓ | 
    408 lines elided | 
    
      ↑ open up ↑ | 
  
 473  474          ASSERT(zp->z_links == 0);
 474  475  
 475  476          VERIFY3U(0, ==,
 476  477              zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
 477  478  }
 478  479  
 479  480  /*
 480  481   * Clean up any znodes that had no links when we either crashed or
 481  482   * (force) umounted the file system.
 482  483   */
 483      -void
 484      -zfs_unlinked_drain(zfsvfs_t *zfsvfs)
      484 +static void
      485 +zfs_unlinked_drain_impl(zfsvfs_t *zfsvfs)
 485  486  {
 486  487          zap_cursor_t    zc;
 487  488          zap_attribute_t zap;
 488  489          dmu_object_info_t doi;
 489  490          znode_t         *zp;
 490  491          int             error;
 491  492  
      493 +        ASSERT(zfsvfs->z_drain_state != ZFS_DRAIN_SHUTDOWN);
 492  494          /*
 493  495           * Interate over the contents of the unlinked set.
 494  496           */
 495  497          for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
 496      -            zap_cursor_retrieve(&zc, &zap) == 0;
      498 +            zap_cursor_retrieve(&zc, &zap) == 0 &&
      499 +            /* Only checking for a shutdown request, so no locking reqd. */
      500 +            zfsvfs->z_drain_state == ZFS_DRAIN_RUNNING;
 497  501              zap_cursor_advance(&zc)) {
 498  502  
 499  503                  /*
 500  504                   * See what kind of object we have in list
 501  505                   */
 502  506  
 503  507                  error = dmu_object_info(zfsvfs->z_os,
 504  508                      zap.za_first_integer, &doi);
 505  509                  if (error != 0)
 506  510                          continue;
 507  511  
 508  512                  ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
 509  513                      (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
 510  514                  /*
 511  515                   * We need to re-mark these list entries for deletion,
 512  516                   * so we pull them back into core and set zp->z_unlinked.
 513  517                   */
 514  518                  error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
 515  519  
 516  520                  /*
  
    | 
      ↓ open down ↓ | 
    10 lines elided | 
    
      ↑ open up ↑ | 
  
 517  521                   * We may pick up znodes that are already marked for deletion.
 518  522                   * This could happen during the purge of an extended attribute
 519  523                   * directory.  All we need to do is skip over them, since they
 520  524                   * are already in the system marked z_unlinked.
 521  525                   */
 522  526                  if (error != 0)
 523  527                          continue;
 524  528  
 525  529                  zp->z_unlinked = B_TRUE;
 526  530                  VN_RELE(ZTOV(zp));
      531 +
      532 +                ASSERT(!zfsvfs->z_unmounted);
 527  533          }
 528  534          zap_cursor_fini(&zc);
      535 +
      536 +        mutex_enter(&zfsvfs->z_drain_lock);
      537 +        zfsvfs->z_drain_state = ZFS_DRAIN_SHUTDOWN;
      538 +        cv_broadcast(&zfsvfs->z_drain_cv);
      539 +        mutex_exit(&zfsvfs->z_drain_lock);
 529  540  }
 530  541  
      542 +/*
      543 + * Setup required hold. After that tries to dispatch
      544 + * async unlinked drain logic. Otherwise executes
      545 + * the logic synchronously.
      546 + */
      547 +void
      548 +zfs_unlinked_drain(zfsvfs_t *zfsvfs)
      549 +{
      550 +        ASSERT(!zfsvfs->z_unmounted);
      551 +
      552 +        mutex_enter(&zfsvfs->z_drain_lock);
      553 +        ASSERT(zfsvfs->z_drain_state == ZFS_DRAIN_SHUTDOWN);
      554 +        zfsvfs->z_drain_state = ZFS_DRAIN_RUNNING;
      555 +        mutex_exit(&zfsvfs->z_drain_lock);
      556 +
      557 +        if (taskq_dispatch(dsl_pool_vnrele_taskq(
      558 +            spa_get_dsl(zfsvfs->z_os->os_spa)),
      559 +            (void (*)(void *))zfs_unlinked_drain_impl, zfsvfs,
      560 +            TQ_NOSLEEP) == NULL) {
      561 +                cmn_err(CE_WARN, "async zfs_unlinked_drain dispatch failed");
      562 +                zfs_unlinked_drain_impl(zfsvfs);
      563 +        }
      564 +}
      565 +
      566 +/*
      567 + * Stops an asynchronous zfs_unlinked_drain. This must be called prior to
      568 + * destroying the zfsvfs_t, as the drain doesn't hold the z_teardown_lock.
      569 + */
      570 +void
      571 +zfs_unlinked_drain_stop_wait(zfsvfs_t *zfsvfs)
      572 +{
      573 +        ASSERT(!zfsvfs->z_unmounted);
      574 +
      575 +        mutex_enter(&zfsvfs->z_drain_lock);
      576 +        while (zfsvfs->z_drain_state != ZFS_DRAIN_SHUTDOWN) {
      577 +                zfsvfs->z_drain_state = ZFS_DRAIN_SHUTDOWN_REQ;
      578 +                cv_wait(&zfsvfs->z_drain_cv, &zfsvfs->z_drain_lock);
      579 +        }
      580 +        mutex_exit(&zfsvfs->z_drain_lock);
      581 +}
      582 +
 531  583  /*
 532  584   * Delete the entire contents of a directory.  Return a count
 533  585   * of the number of entries that could not be deleted. If we encounter
 534  586   * an error, return a count of at least one so that the directory stays
 535  587   * in the unlinked set.
 536  588   *
 537  589   * NOTE: this function assumes that the directory is inactive,
 538  590   *      so there is no need to lock its entries before deletion.
 539  591   *      Also, it assumes the directory contents is *only* regular
 540  592   *      files.
 541  593   */
 542  594  static int
 543  595  zfs_purgedir(znode_t *dzp)
 544  596  {
 545  597          zap_cursor_t    zc;
 546  598          zap_attribute_t zap;
 547  599          znode_t         *xzp;
 548  600          dmu_tx_t        *tx;
 549  601          zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
 550  602          zfs_dirlock_t   dl;
 551  603          int skipped = 0;
 552  604          int error;
 553  605  
 554  606          for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
 555  607              (error = zap_cursor_retrieve(&zc, &zap)) == 0;
 556  608              zap_cursor_advance(&zc)) {
 557  609                  error = zfs_zget(zfsvfs,
 558  610                      ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
 559  611                  if (error) {
 560  612                          skipped += 1;
 561  613                          continue;
 562  614                  }
 563  615  
 564  616                  ASSERT((ZTOV(xzp)->v_type == VREG) ||
 565  617                      (ZTOV(xzp)->v_type == VLNK));
 566  618  
 567  619                  tx = dmu_tx_create(zfsvfs->z_os);
 568  620                  dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
 569  621                  dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
 570  622                  dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
 571  623                  dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
 572  624                  /* Is this really needed ? */
 573  625                  zfs_sa_upgrade_txholds(tx, xzp);
 574  626                  dmu_tx_mark_netfree(tx);
 575  627                  error = dmu_tx_assign(tx, TXG_WAIT);
 576  628                  if (error) {
 577  629                          dmu_tx_abort(tx);
 578  630                          VN_RELE(ZTOV(xzp));
 579  631                          skipped += 1;
 580  632                          continue;
 581  633                  }
 582  634                  bzero(&dl, sizeof (dl));
 583  635                  dl.dl_dzp = dzp;
 584  636                  dl.dl_name = zap.za_name;
 585  637  
 586  638                  error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
 587  639                  if (error)
 588  640                          skipped += 1;
 589  641                  dmu_tx_commit(tx);
 590  642  
 591  643                  VN_RELE(ZTOV(xzp));
 592  644          }
 593  645          zap_cursor_fini(&zc);
 594  646          if (error != ENOENT)
 595  647                  skipped += 1;
 596  648          return (skipped);
 597  649  }
 598  650  
 599  651  void
 600  652  zfs_rmnode(znode_t *zp)
 601  653  {
 602  654          zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
 603  655          objset_t        *os = zfsvfs->z_os;
 604  656          znode_t         *xzp = NULL;
 605  657          dmu_tx_t        *tx;
 606  658          uint64_t        acl_obj;
 607  659          uint64_t        xattr_obj;
 608  660          int             error;
 609  661  
 610  662          ASSERT(zp->z_links == 0);
 611  663          ASSERT(ZTOV(zp)->v_count == 0);
 612  664  
 613  665          /*
 614  666           * If this is an attribute directory, purge its contents.
 615  667           */
 616  668          if (ZTOV(zp)->v_type == VDIR && (zp->z_pflags & ZFS_XATTR)) {
 617  669                  if (zfs_purgedir(zp) != 0) {
 618  670                          /*
 619  671                           * Not enough space to delete some xattrs.
 620  672                           * Leave it in the unlinked set.
 621  673                           */
 622  674                          zfs_znode_dmu_fini(zp);
 623  675                          zfs_znode_free(zp);
 624  676                          return;
 625  677                  }
 626  678          } else {
 627  679                  /*
 628  680                   * Free up all the data in the file.  We don't do this for
 629  681                   * XATTR directories because we need truncate and remove to be
 630  682                   * in the same tx, like in zfs_znode_delete(). Otherwise, if
 631  683                   * we crash here we'll end up with an inconsistent truncated
 632  684                   * zap object in the delete queue.  Note a truncated file is
 633  685                   * harmless since it only contains user data.
 634  686                   */
 635  687                  error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
 636  688                  if (error) {
 637  689                          /*
 638  690                           * Not enough space or we were interrupted by unmount.
 639  691                           * Leave the file in the unlinked set.
 640  692                           */
 641  693                          zfs_znode_dmu_fini(zp);
 642  694                          zfs_znode_free(zp);
 643  695                          return;
 644  696                  }
 645  697          }
 646  698  
 647  699          /*
 648  700           * If the file has extended attributes, we're going to unlink
 649  701           * the xattr dir.
 650  702           */
 651  703          error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
 652  704              &xattr_obj, sizeof (xattr_obj));
 653  705          if (error == 0 && xattr_obj) {
 654  706                  error = zfs_zget(zfsvfs, xattr_obj, &xzp);
 655  707                  ASSERT(error == 0);
 656  708          }
 657  709  
 658  710          acl_obj = zfs_external_acl(zp);
 659  711  
 660  712          /*
 661  713           * Set up the final transaction.
 662  714           */
 663  715          tx = dmu_tx_create(os);
 664  716          dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
 665  717          dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
 666  718          if (xzp) {
 667  719                  dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
 668  720                  dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
 669  721          }
 670  722          if (acl_obj)
 671  723                  dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
 672  724  
 673  725          zfs_sa_upgrade_txholds(tx, zp);
 674  726          error = dmu_tx_assign(tx, TXG_WAIT);
 675  727          if (error) {
 676  728                  /*
 677  729                   * Not enough space to delete the file.  Leave it in the
 678  730                   * unlinked set, leaking it until the fs is remounted (at
 679  731                   * which point we'll call zfs_unlinked_drain() to process it).
 680  732                   */
 681  733                  dmu_tx_abort(tx);
 682  734                  zfs_znode_dmu_fini(zp);
 683  735                  zfs_znode_free(zp);
 684  736                  goto out;
 685  737          }
 686  738  
 687  739          if (xzp) {
 688  740                  ASSERT(error == 0);
 689  741                  mutex_enter(&xzp->z_lock);
 690  742                  xzp->z_unlinked = B_TRUE;       /* mark xzp for deletion */
 691  743                  xzp->z_links = 0;       /* no more links to it */
 692  744                  VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
 693  745                      &xzp->z_links, sizeof (xzp->z_links), tx));
 694  746                  mutex_exit(&xzp->z_lock);
 695  747                  zfs_unlinked_add(xzp, tx);
 696  748          }
 697  749  
 698  750          /* Remove this znode from the unlinked set */
 699  751          VERIFY3U(0, ==,
 700  752              zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
 701  753  
 702  754          zfs_znode_delete(zp, tx);
 703  755  
 704  756          dmu_tx_commit(tx);
 705  757  out:
 706  758          if (xzp)
 707  759                  VN_RELE(ZTOV(xzp));
 708  760  }
 709  761  
 710  762  static uint64_t
 711  763  zfs_dirent(znode_t *zp, uint64_t mode)
 712  764  {
 713  765          uint64_t de = zp->z_id;
 714  766  
 715  767          if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
 716  768                  de |= IFTODT(mode) << 60;
 717  769          return (de);
 718  770  }
 719  771  
 720  772  /*
 721  773   * Link zp into dl.  Can only fail if zp has been unlinked.
 722  774   */
 723  775  int
 724  776  zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
 725  777  {
 726  778          znode_t *dzp = dl->dl_dzp;
 727  779          zfsvfs_t *zfsvfs = zp->z_zfsvfs;
 728  780          vnode_t *vp = ZTOV(zp);
 729  781          uint64_t value;
 730  782          int zp_is_dir = (vp->v_type == VDIR);
 731  783          sa_bulk_attr_t bulk[5];
 732  784          uint64_t mtime[2], ctime[2];
 733  785          int count = 0;
 734  786          int error;
 735  787  
 736  788          mutex_enter(&zp->z_lock);
 737  789  
 738  790          if (!(flag & ZRENAMING)) {
 739  791                  if (zp->z_unlinked) {   /* no new links to unlinked zp */
 740  792                          ASSERT(!(flag & (ZNEW | ZEXISTS)));
 741  793                          mutex_exit(&zp->z_lock);
 742  794                          return (SET_ERROR(ENOENT));
 743  795                  }
 744  796                  zp->z_links++;
 745  797                  SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
 746  798                      &zp->z_links, sizeof (zp->z_links));
 747  799  
 748  800          }
 749  801          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
 750  802              &dzp->z_id, sizeof (dzp->z_id));
 751  803          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
 752  804              &zp->z_pflags, sizeof (zp->z_pflags));
 753  805  
 754  806          if (!(flag & ZNEW)) {
 755  807                  SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
 756  808                      ctime, sizeof (ctime));
 757  809                  zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
 758  810                      ctime, B_TRUE);
 759  811          }
 760  812          error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
 761  813          ASSERT(error == 0);
 762  814  
 763  815          mutex_exit(&zp->z_lock);
 764  816  
 765  817          mutex_enter(&dzp->z_lock);
 766  818          dzp->z_size++;
 767  819          dzp->z_links += zp_is_dir;
 768  820          count = 0;
 769  821          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
 770  822              &dzp->z_size, sizeof (dzp->z_size));
 771  823          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
 772  824              &dzp->z_links, sizeof (dzp->z_links));
 773  825          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
 774  826              mtime, sizeof (mtime));
 775  827          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
 776  828              ctime, sizeof (ctime));
 777  829          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
 778  830              &dzp->z_pflags, sizeof (dzp->z_pflags));
 779  831          zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
 780  832          error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
 781  833          ASSERT(error == 0);
 782  834          mutex_exit(&dzp->z_lock);
 783  835  
 784  836          value = zfs_dirent(zp, zp->z_mode);
 785  837          error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
 786  838              8, 1, &value, tx);
 787  839          ASSERT(error == 0);
 788  840  
 789  841          dnlc_update(ZTOV(dzp), dl->dl_name, vp);
 790  842  
 791  843          return (0);
 792  844  }
 793  845  
 794  846  /*
 795  847   * The match type in the code for this function should conform to:
 796  848   *
 797  849   * ------------------------------------------------------------------------
 798  850   * fs type  | z_norm      | lookup type | match type
 799  851   * ---------|-------------|-------------|----------------------------------
 800  852   * CS !norm | 0           |           0 | 0 (exact)
 801  853   * CS  norm | formX       |           0 | MT_NORMALIZE
 802  854   * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
 803  855   * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
 804  856   * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
 805  857   * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
 806  858   * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
 807  859   * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
 808  860   * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
 809  861   * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
 810  862   *
 811  863   * Abbreviations:
 812  864   *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
 813  865   *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
 814  866   *    formX = unicode normalization form set on fs creation
 815  867   */
 816  868  static int
 817  869  zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx,
 818  870      int flag)
 819  871  {
 820  872          int error;
 821  873  
 822  874          if (zp->z_zfsvfs->z_norm) {
 823  875                  matchtype_t mt = MT_NORMALIZE;
 824  876  
 825  877                  if ((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
 826  878                      (flag & ZCIEXACT)) ||
 827  879                      (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED &&
 828  880                      !(flag & ZCILOOK))) {
 829  881                          mt |= MT_MATCH_CASE;
 830  882                  }
 831  883  
 832  884                  error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
 833  885                      dl->dl_name, mt, tx);
 834  886          } else {
 835  887                  error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
 836  888                      tx);
 837  889          }
 838  890  
 839  891          return (error);
 840  892  }
 841  893  
 842  894  /*
 843  895   * Unlink zp from dl, and mark zp for deletion if this was the last link.
 844  896   * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
 845  897   * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
 846  898   * If it's non-NULL, we use it to indicate whether the znode needs deletion,
 847  899   * and it's the caller's job to do it.
 848  900   */
 849  901  int
 850  902  zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
 851  903      boolean_t *unlinkedp)
 852  904  {
 853  905          znode_t *dzp = dl->dl_dzp;
 854  906          zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
 855  907          vnode_t *vp = ZTOV(zp);
 856  908          int zp_is_dir = (vp->v_type == VDIR);
 857  909          boolean_t unlinked = B_FALSE;
 858  910          sa_bulk_attr_t bulk[5];
 859  911          uint64_t mtime[2], ctime[2];
 860  912          int count = 0;
 861  913          int error;
 862  914  
 863  915          dnlc_remove(ZTOV(dzp), dl->dl_name);
 864  916  
 865  917          if (!(flag & ZRENAMING)) {
 866  918                  if (vn_vfswlock(vp))            /* prevent new mounts on zp */
 867  919                          return (SET_ERROR(EBUSY));
 868  920  
 869  921                  if (vn_ismntpt(vp)) {           /* don't remove mount point */
 870  922                          vn_vfsunlock(vp);
 871  923                          return (SET_ERROR(EBUSY));
 872  924                  }
 873  925  
 874  926                  mutex_enter(&zp->z_lock);
 875  927  
 876  928                  if (zp_is_dir && !zfs_dirempty(zp)) {
 877  929                          mutex_exit(&zp->z_lock);
 878  930                          vn_vfsunlock(vp);
 879  931                          return (SET_ERROR(EEXIST));
 880  932                  }
 881  933  
 882  934                  /*
 883  935                   * If we get here, we are going to try to remove the object.
 884  936                   * First try removing the name from the directory; if that
 885  937                   * fails, return the error.
 886  938                   */
 887  939                  error = zfs_dropname(dl, zp, dzp, tx, flag);
 888  940                  if (error != 0) {
 889  941                          mutex_exit(&zp->z_lock);
 890  942                          vn_vfsunlock(vp);
 891  943                          return (error);
 892  944                  }
 893  945  
 894  946                  if (zp->z_links <= zp_is_dir) {
 895  947                          zfs_panic_recover("zfs: link count on %s is %u, "
 896  948                              "should be at least %u",
 897  949                              zp->z_vnode->v_path != vn_vpath_empty ?
 898  950                              zp->z_vnode->v_path : "<unknown>",
 899  951                              (int)zp->z_links, zp_is_dir + 1);
 900  952                          zp->z_links = zp_is_dir + 1;
 901  953                  }
 902  954                  if (--zp->z_links == zp_is_dir) {
 903  955                          zp->z_unlinked = B_TRUE;
 904  956                          zp->z_links = 0;
 905  957                          unlinked = B_TRUE;
 906  958                  } else {
 907  959                          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
 908  960                              NULL, &ctime, sizeof (ctime));
 909  961                          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
 910  962                              NULL, &zp->z_pflags, sizeof (zp->z_pflags));
 911  963                          zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
 912  964                              B_TRUE);
 913  965                  }
 914  966                  SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
 915  967                      NULL, &zp->z_links, sizeof (zp->z_links));
 916  968                  error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
 917  969                  count = 0;
 918  970                  ASSERT(error == 0);
 919  971                  mutex_exit(&zp->z_lock);
 920  972                  vn_vfsunlock(vp);
 921  973          } else {
 922  974                  error = zfs_dropname(dl, zp, dzp, tx, flag);
 923  975                  if (error != 0)
 924  976                          return (error);
 925  977          }
 926  978  
 927  979          mutex_enter(&dzp->z_lock);
 928  980          dzp->z_size--;          /* one dirent removed */
 929  981          dzp->z_links -= zp_is_dir;      /* ".." link from zp */
 930  982          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
 931  983              NULL, &dzp->z_links, sizeof (dzp->z_links));
 932  984          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
 933  985              NULL, &dzp->z_size, sizeof (dzp->z_size));
 934  986          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
 935  987              NULL, ctime, sizeof (ctime));
 936  988          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
 937  989              NULL, mtime, sizeof (mtime));
 938  990          SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
 939  991              NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
 940  992          zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
 941  993          error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
 942  994          ASSERT(error == 0);
 943  995          mutex_exit(&dzp->z_lock);
 944  996  
 945  997          if (unlinkedp != NULL)
 946  998                  *unlinkedp = unlinked;
 947  999          else if (unlinked)
 948 1000                  zfs_unlinked_add(zp, tx);
 949 1001  
 950 1002          return (0);
 951 1003  }
 952 1004  
 953 1005  /*
 954 1006   * Indicate whether the directory is empty.  Works with or without z_lock
 955 1007   * held, but can only be consider a hint in the latter case.  Returns true
 956 1008   * if only "." and ".." remain and there's no work in progress.
 957 1009   */
 958 1010  boolean_t
 959 1011  zfs_dirempty(znode_t *dzp)
 960 1012  {
 961 1013          return (dzp->z_size == 2 && dzp->z_dirlocks == 0);
 962 1014  }
 963 1015  
 964 1016  int
 965 1017  zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
 966 1018  {
 967 1019          zfsvfs_t *zfsvfs = zp->z_zfsvfs;
 968 1020          znode_t *xzp;
 969 1021          dmu_tx_t *tx;
 970 1022          int error;
 971 1023          zfs_acl_ids_t acl_ids;
 972 1024          boolean_t fuid_dirtied;
 973 1025          uint64_t parent;
 974 1026  
 975 1027          *xvpp = NULL;
 976 1028  
 977 1029          if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
 978 1030                  return (error);
 979 1031  
 980 1032          if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
 981 1033              &acl_ids)) != 0)
 982 1034                  return (error);
 983 1035          if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
 984 1036                  zfs_acl_ids_free(&acl_ids);
 985 1037                  return (SET_ERROR(EDQUOT));
 986 1038          }
 987 1039  
 988 1040          tx = dmu_tx_create(zfsvfs->z_os);
 989 1041          dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
 990 1042              ZFS_SA_BASE_ATTR_SIZE);
 991 1043          dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
 992 1044          dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
 993 1045          fuid_dirtied = zfsvfs->z_fuid_dirty;
 994 1046          if (fuid_dirtied)
 995 1047                  zfs_fuid_txhold(zfsvfs, tx);
 996 1048          error = dmu_tx_assign(tx, TXG_WAIT);
 997 1049          if (error) {
 998 1050                  zfs_acl_ids_free(&acl_ids);
 999 1051                  dmu_tx_abort(tx);
1000 1052                  return (error);
1001 1053          }
1002 1054          zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
1003 1055  
1004 1056          if (fuid_dirtied)
1005 1057                  zfs_fuid_sync(zfsvfs, tx);
1006 1058  
1007 1059  #ifdef DEBUG
1008 1060          error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1009 1061              &parent, sizeof (parent));
1010 1062          ASSERT(error == 0 && parent == zp->z_id);
1011 1063  #endif
1012 1064  
1013 1065          VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
1014 1066              sizeof (xzp->z_id), tx));
1015 1067  
1016 1068          (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
1017 1069              xzp, "", NULL, acl_ids.z_fuidp, vap);
1018 1070  
1019 1071          zfs_acl_ids_free(&acl_ids);
1020 1072          dmu_tx_commit(tx);
1021 1073  
1022 1074          *xvpp = ZTOV(xzp);
1023 1075  
1024 1076          return (0);
1025 1077  }
1026 1078  
1027 1079  /*
1028 1080   * Return a znode for the extended attribute directory for zp.
1029 1081   * ** If the directory does not already exist, it is created **
1030 1082   *
1031 1083   *      IN:     zp      - znode to obtain attribute directory from
1032 1084   *              cr      - credentials of caller
1033 1085   *              flags   - flags from the VOP_LOOKUP call
1034 1086   *
1035 1087   *      OUT:    xzpp    - pointer to extended attribute znode
1036 1088   *
1037 1089   *      RETURN: 0 on success
1038 1090   *              error number on failure
1039 1091   */
1040 1092  int
1041 1093  zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
1042 1094  {
1043 1095          zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
1044 1096          znode_t         *xzp;
1045 1097          zfs_dirlock_t   *dl;
1046 1098          vattr_t         va;
1047 1099          int             error;
1048 1100  top:
1049 1101          error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
1050 1102          if (error)
1051 1103                  return (error);
1052 1104  
1053 1105          if (xzp != NULL) {
1054 1106                  *xvpp = ZTOV(xzp);
1055 1107                  zfs_dirent_unlock(dl);
1056 1108                  return (0);
1057 1109          }
1058 1110  
1059 1111  
1060 1112          if (!(flags & CREATE_XATTR_DIR)) {
1061 1113                  zfs_dirent_unlock(dl);
1062 1114                  return (SET_ERROR(ENOENT));
1063 1115          }
1064 1116  
1065 1117          if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1066 1118                  zfs_dirent_unlock(dl);
1067 1119                  return (SET_ERROR(EROFS));
1068 1120          }
1069 1121  
1070 1122          /*
1071 1123           * The ability to 'create' files in an attribute
1072 1124           * directory comes from the write_xattr permission on the base file.
1073 1125           *
1074 1126           * The ability to 'search' an attribute directory requires
1075 1127           * read_xattr permission on the base file.
1076 1128           *
1077 1129           * Once in a directory the ability to read/write attributes
1078 1130           * is controlled by the permissions on the attribute file.
1079 1131           */
1080 1132          va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
1081 1133          va.va_type = VDIR;
1082 1134          va.va_mode = S_IFDIR | S_ISVTX | 0777;
1083 1135          zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
1084 1136  
1085 1137          error = zfs_make_xattrdir(zp, &va, xvpp, cr);
1086 1138          zfs_dirent_unlock(dl);
1087 1139  
1088 1140          if (error == ERESTART) {
1089 1141                  /* NB: we already did dmu_tx_wait() if necessary */
1090 1142                  goto top;
1091 1143          }
1092 1144  
1093 1145          return (error);
1094 1146  }
1095 1147  
1096 1148  /*
1097 1149   * Decide whether it is okay to remove within a sticky directory.
1098 1150   *
1099 1151   * In sticky directories, write access is not sufficient;
1100 1152   * you can remove entries from a directory only if:
1101 1153   *
1102 1154   *      you own the directory,
1103 1155   *      you own the entry,
1104 1156   *      the entry is a plain file and you have write access,
1105 1157   *      or you are privileged (checked in secpolicy...).
1106 1158   *
1107 1159   * The function returns 0 if remove access is granted.
1108 1160   */
1109 1161  int
1110 1162  zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1111 1163  {
1112 1164          uid_t           uid;
1113 1165          uid_t           downer;
1114 1166          uid_t           fowner;
1115 1167          zfsvfs_t        *zfsvfs = zdp->z_zfsvfs;
1116 1168  
1117 1169          if (zdp->z_zfsvfs->z_replay)
1118 1170                  return (0);
1119 1171  
1120 1172          if ((zdp->z_mode & S_ISVTX) == 0)
1121 1173                  return (0);
1122 1174  
1123 1175          downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
1124 1176          fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
1125 1177  
1126 1178          if ((uid = crgetuid(cr)) == downer || uid == fowner ||
1127 1179              (ZTOV(zp)->v_type == VREG &&
1128 1180              zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
1129 1181                  return (0);
1130 1182          else
1131 1183                  return (secpolicy_vnode_remove(cr));
1132 1184  }
  
    | 
      ↓ open down ↓ | 
    592 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX