Print this page
    
NEX-5164 backport illumos 6514 AS_* lock macros simplification
Reviewed by: Kevin Crowe <kevin.crowe@nexenta.com>
6514 AS_* lock macros simplification
Reviewed by: Piotr Jasiukajtis <estibi@me.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Albert Lee <trisk@omniti.com>
Approved by: Dan McDonald <danmcd@omniti.com>
re #13613 rb4516 Tunables needs volatile keyword
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/ufs/ufs_vnops.c
          +++ new/usr/src/uts/common/fs/ufs/ufs_vnops.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
  
    | 
      ↓ open down ↓ | 
    13 lines elided | 
    
      ↑ open up ↑ | 
  
  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) 1984, 2010, Oracle and/or its affiliates. All rights reserved.
       24 + * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  24   25   * Copyright 2017 Joyent, Inc.
  25   26   * Copyright (c) 2016 by Delphix. All rights reserved.
  26   27   */
  27   28  
  28   29  /*      Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T     */
  29   30  /*        All Rights Reserved   */
  30   31  
  31   32  /*
  32   33   * Portions of this source code were derived from Berkeley 4.3 BSD
  33   34   * under license from the Regents of the University of California.
  34   35   */
  35   36  
  36   37  #include <sys/types.h>
  37   38  #include <sys/t_lock.h>
  38   39  #include <sys/ksynch.h>
  39   40  #include <sys/param.h>
  40   41  #include <sys/time.h>
  41   42  #include <sys/systm.h>
  42   43  #include <sys/sysmacros.h>
  43   44  #include <sys/resource.h>
  44   45  #include <sys/signal.h>
  45   46  #include <sys/cred.h>
  46   47  #include <sys/user.h>
  47   48  #include <sys/buf.h>
  48   49  #include <sys/vfs.h>
  49   50  #include <sys/vfs_opreg.h>
  50   51  #include <sys/vnode.h>
  51   52  #include <sys/proc.h>
  52   53  #include <sys/disp.h>
  53   54  #include <sys/file.h>
  54   55  #include <sys/fcntl.h>
  55   56  #include <sys/flock.h>
  56   57  #include <sys/atomic.h>
  57   58  #include <sys/kmem.h>
  58   59  #include <sys/uio.h>
  59   60  #include <sys/dnlc.h>
  60   61  #include <sys/conf.h>
  61   62  #include <sys/mman.h>
  62   63  #include <sys/pathname.h>
  63   64  #include <sys/debug.h>
  64   65  #include <sys/vmsystm.h>
  65   66  #include <sys/cmn_err.h>
  66   67  #include <sys/filio.h>
  67   68  #include <sys/policy.h>
  68   69  
  69   70  #include <sys/fs/ufs_fs.h>
  70   71  #include <sys/fs/ufs_lockfs.h>
  71   72  #include <sys/fs/ufs_filio.h>
  72   73  #include <sys/fs/ufs_inode.h>
  73   74  #include <sys/fs/ufs_fsdir.h>
  74   75  #include <sys/fs/ufs_quota.h>
  75   76  #include <sys/fs/ufs_log.h>
  76   77  #include <sys/fs/ufs_snap.h>
  77   78  #include <sys/fs/ufs_trans.h>
  78   79  #include <sys/fs/ufs_panic.h>
  79   80  #include <sys/fs/ufs_bio.h>
  80   81  #include <sys/dirent.h>         /* must be AFTER <sys/fs/fsdir.h>! */
  81   82  #include <sys/errno.h>
  82   83  #include <sys/fssnap_if.h>
  83   84  #include <sys/unistd.h>
  84   85  #include <sys/sunddi.h>
  85   86  
  86   87  #include <sys/filio.h>          /* _FIOIO */
  87   88  
  88   89  #include <vm/hat.h>
  89   90  #include <vm/page.h>
  90   91  #include <vm/pvn.h>
  91   92  #include <vm/as.h>
  92   93  #include <vm/seg.h>
  93   94  #include <vm/seg_map.h>
  94   95  #include <vm/seg_vn.h>
  95   96  #include <vm/seg_kmem.h>
  96   97  #include <vm/rm.h>
  97   98  #include <sys/swap.h>
  98   99  
  99  100  #include <fs/fs_subr.h>
 100  101  
 101  102  #include <sys/fs/decomp.h>
 102  103  
 103  104  static struct instats ins;
 104  105  
 105  106  static  int ufs_getpage_ra(struct vnode *, u_offset_t, struct seg *, caddr_t);
 106  107  static  int ufs_getpage_miss(struct vnode *, u_offset_t, size_t, struct seg *,
 107  108                  caddr_t, struct page **, size_t, enum seg_rw, int);
 108  109  static  int ufs_open(struct vnode **, int, struct cred *, caller_context_t *);
 109  110  static  int ufs_close(struct vnode *, int, int, offset_t, struct cred *,
 110  111                  caller_context_t *);
 111  112  static  int ufs_read(struct vnode *, struct uio *, int, struct cred *,
 112  113                  struct caller_context *);
 113  114  static  int ufs_write(struct vnode *, struct uio *, int, struct cred *,
 114  115                  struct caller_context *);
 115  116  static  int ufs_ioctl(struct vnode *, int, intptr_t, int, struct cred *,
 116  117                  int *, caller_context_t *);
 117  118  static  int ufs_getattr(struct vnode *, struct vattr *, int, struct cred *,
 118  119                  caller_context_t *);
 119  120  static  int ufs_setattr(struct vnode *, struct vattr *, int, struct cred *,
 120  121                  caller_context_t *);
 121  122  static  int ufs_access(struct vnode *, int, int, struct cred *,
 122  123                  caller_context_t *);
 123  124  static  int ufs_lookup(struct vnode *, char *, struct vnode **,
 124  125                  struct pathname *, int, struct vnode *, struct cred *,
 125  126                  caller_context_t *, int *, pathname_t *);
 126  127  static  int ufs_create(struct vnode *, char *, struct vattr *, enum vcexcl,
 127  128                  int, struct vnode **, struct cred *, int,
 128  129                  caller_context_t *, vsecattr_t  *);
 129  130  static  int ufs_remove(struct vnode *, char *, struct cred *,
 130  131                  caller_context_t *, int);
 131  132  static  int ufs_link(struct vnode *, struct vnode *, char *, struct cred *,
 132  133                  caller_context_t *, int);
 133  134  static  int ufs_rename(struct vnode *, char *, struct vnode *, char *,
 134  135                  struct cred *, caller_context_t *, int);
 135  136  static  int ufs_mkdir(struct vnode *, char *, struct vattr *, struct vnode **,
 136  137                  struct cred *, caller_context_t *, int, vsecattr_t *);
 137  138  static  int ufs_rmdir(struct vnode *, char *, struct vnode *, struct cred *,
 138  139                  caller_context_t *, int);
 139  140  static  int ufs_readdir(struct vnode *, struct uio *, struct cred *, int *,
 140  141                  caller_context_t *, int);
 141  142  static  int ufs_symlink(struct vnode *, char *, struct vattr *, char *,
 142  143                  struct cred *, caller_context_t *, int);
 143  144  static  int ufs_readlink(struct vnode *, struct uio *, struct cred *,
 144  145                  caller_context_t *);
 145  146  static  int ufs_fsync(struct vnode *, int, struct cred *, caller_context_t *);
 146  147  static  void ufs_inactive(struct vnode *, struct cred *, caller_context_t *);
 147  148  static  int ufs_fid(struct vnode *, struct fid *, caller_context_t *);
 148  149  static  int ufs_rwlock(struct vnode *, int, caller_context_t *);
 149  150  static  void ufs_rwunlock(struct vnode *, int, caller_context_t *);
 150  151  static  int ufs_seek(struct vnode *, offset_t, offset_t *, caller_context_t *);
 151  152  static  int ufs_frlock(struct vnode *, int, struct flock64 *, int, offset_t,
 152  153                  struct flk_callback *, struct cred *,
 153  154                  caller_context_t *);
 154  155  static  int ufs_space(struct vnode *, int, struct flock64 *, int, offset_t,
 155  156                  cred_t *, caller_context_t *);
 156  157  static  int ufs_getpage(struct vnode *, offset_t, size_t, uint_t *,
 157  158                  struct page **, size_t, struct seg *, caddr_t,
 158  159                  enum seg_rw, struct cred *, caller_context_t *);
 159  160  static  int ufs_putpage(struct vnode *, offset_t, size_t, int, struct cred *,
 160  161                  caller_context_t *);
 161  162  static  int ufs_putpages(struct vnode *, offset_t, size_t, int, struct cred *);
 162  163  static  int ufs_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
 163  164                  uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
 164  165  static  int ufs_addmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
 165  166                  uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
 166  167  static  int ufs_delmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
 167  168                  uint_t, uint_t, uint_t, struct cred *, caller_context_t *);
 168  169  static  int ufs_poll(vnode_t *, short, int, short *, struct pollhead **,
 169  170                  caller_context_t *);
 170  171  static  int ufs_dump(vnode_t *, caddr_t, offset_t, offset_t,
 171  172      caller_context_t *);
 172  173  static  int ufs_l_pathconf(struct vnode *, int, ulong_t *, struct cred *,
 173  174                  caller_context_t *);
 174  175  static  int ufs_pageio(struct vnode *, struct page *, u_offset_t, size_t, int,
 175  176                  struct cred *, caller_context_t *);
 176  177  static  int ufs_dumpctl(vnode_t *, int, offset_t *, caller_context_t *);
 177  178  static  daddr32_t *save_dblks(struct inode *, struct ufsvfs *, daddr32_t *,
 178  179                  daddr32_t *, int, int);
 179  180  static  int ufs_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
 180  181                  caller_context_t *);
 181  182  static  int ufs_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
 182  183                  caller_context_t *);
 183  184  static  int ufs_priv_access(void *, int, struct cred *);
 184  185  static  int ufs_eventlookup(struct vnode *, char *, struct cred *,
 185  186      struct vnode **);
 186  187  extern int as_map_locked(struct as *, caddr_t, size_t, int ((*)()), void *);
 187  188  
 188  189  /*
 189  190   * For lockfs: ulockfs begin/end is now inlined in the ufs_xxx functions.
 190  191   *
 191  192   * XXX - ULOCKFS in fs_pathconf and ufs_ioctl is not inlined yet.
 192  193   */
 193  194  struct vnodeops *ufs_vnodeops;
 194  195  
 195  196  /* NOTE: "not blkd" below  means that the operation isn't blocked by lockfs */
 196  197  const fs_operation_def_t ufs_vnodeops_template[] = {
 197  198          VOPNAME_OPEN,           { .vop_open = ufs_open },       /* not blkd */
 198  199          VOPNAME_CLOSE,          { .vop_close = ufs_close },     /* not blkd */
 199  200          VOPNAME_READ,           { .vop_read = ufs_read },
 200  201          VOPNAME_WRITE,          { .vop_write = ufs_write },
 201  202          VOPNAME_IOCTL,          { .vop_ioctl = ufs_ioctl },
 202  203          VOPNAME_GETATTR,        { .vop_getattr = ufs_getattr },
 203  204          VOPNAME_SETATTR,        { .vop_setattr = ufs_setattr },
 204  205          VOPNAME_ACCESS,         { .vop_access = ufs_access },
 205  206          VOPNAME_LOOKUP,         { .vop_lookup = ufs_lookup },
 206  207          VOPNAME_CREATE,         { .vop_create = ufs_create },
 207  208          VOPNAME_REMOVE,         { .vop_remove = ufs_remove },
 208  209          VOPNAME_LINK,           { .vop_link = ufs_link },
 209  210          VOPNAME_RENAME,         { .vop_rename = ufs_rename },
 210  211          VOPNAME_MKDIR,          { .vop_mkdir = ufs_mkdir },
 211  212          VOPNAME_RMDIR,          { .vop_rmdir = ufs_rmdir },
 212  213          VOPNAME_READDIR,        { .vop_readdir = ufs_readdir },
 213  214          VOPNAME_SYMLINK,        { .vop_symlink = ufs_symlink },
 214  215          VOPNAME_READLINK,       { .vop_readlink = ufs_readlink },
 215  216          VOPNAME_FSYNC,          { .vop_fsync = ufs_fsync },
 216  217          VOPNAME_INACTIVE,       { .vop_inactive = ufs_inactive }, /* not blkd */
 217  218          VOPNAME_FID,            { .vop_fid = ufs_fid },
 218  219          VOPNAME_RWLOCK,         { .vop_rwlock = ufs_rwlock },   /* not blkd */
 219  220          VOPNAME_RWUNLOCK,       { .vop_rwunlock = ufs_rwunlock }, /* not blkd */
 220  221          VOPNAME_SEEK,           { .vop_seek = ufs_seek },
 221  222          VOPNAME_FRLOCK,         { .vop_frlock = ufs_frlock },
 222  223          VOPNAME_SPACE,          { .vop_space = ufs_space },
 223  224          VOPNAME_GETPAGE,        { .vop_getpage = ufs_getpage },
 224  225          VOPNAME_PUTPAGE,        { .vop_putpage = ufs_putpage },
 225  226          VOPNAME_MAP,            { .vop_map = ufs_map },
 226  227          VOPNAME_ADDMAP,         { .vop_addmap = ufs_addmap },   /* not blkd */
 227  228          VOPNAME_DELMAP,         { .vop_delmap = ufs_delmap },   /* not blkd */
 228  229          VOPNAME_POLL,           { .vop_poll = ufs_poll },       /* not blkd */
 229  230          VOPNAME_DUMP,           { .vop_dump = ufs_dump },
 230  231          VOPNAME_PATHCONF,       { .vop_pathconf = ufs_l_pathconf },
 231  232          VOPNAME_PAGEIO,         { .vop_pageio = ufs_pageio },
 232  233          VOPNAME_DUMPCTL,        { .vop_dumpctl = ufs_dumpctl },
 233  234          VOPNAME_GETSECATTR,     { .vop_getsecattr = ufs_getsecattr },
 234  235          VOPNAME_SETSECATTR,     { .vop_setsecattr = ufs_setsecattr },
 235  236          VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
 236  237          NULL,                   NULL
 237  238  };
 238  239  
 239  240  #define MAX_BACKFILE_COUNT      9999
 240  241  
 241  242  /*
 242  243   * Created by ufs_dumpctl() to store a file's disk block info into memory.
 243  244   * Used by ufs_dump() to dump data to disk directly.
 244  245   */
 245  246  struct dump {
 246  247          struct inode    *ip;            /* the file we contain */
 247  248          daddr_t         fsbs;           /* number of blocks stored */
 248  249          struct timeval32 time;          /* time stamp for the struct */
 249  250          daddr32_t       dblk[1];        /* place holder for block info */
 250  251  };
 251  252  
 252  253  static struct dump *dump_info = NULL;
 253  254  
 254  255  /*
 255  256   * Previously there was no special action required for ordinary files.
 256  257   * (Devices are handled through the device file system.)
 257  258   * Now we support Large Files and Large File API requires open to
 258  259   * fail if file is large.
 259  260   * We could take care to prevent data corruption
 260  261   * by doing an atomic check of size and truncate if file is opened with
 261  262   * FTRUNC flag set but traditionally this is being done by the vfs/vnode
 262  263   * layers. So taking care of truncation here is a change in the existing
 263  264   * semantics of VOP_OPEN and therefore we chose not to implement any thing
 264  265   * here. The check for the size of the file > 2GB is being done at the
 265  266   * vfs layer in routine vn_open().
 266  267   */
 267  268  
 268  269  /* ARGSUSED */
 269  270  static int
 270  271  ufs_open(struct vnode **vpp, int flag, struct cred *cr, caller_context_t *ct)
 271  272  {
 272  273          return (0);
 273  274  }
 274  275  
 275  276  /*ARGSUSED*/
 276  277  static int
 277  278  ufs_close(struct vnode *vp, int flag, int count, offset_t offset,
 278  279      struct cred *cr, caller_context_t *ct)
 279  280  {
 280  281          cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
 281  282          cleanshares(vp, ttoproc(curthread)->p_pid);
 282  283  
 283  284          /*
 284  285           * Push partially filled cluster at last close.
 285  286           * ``last close'' is approximated because the dnlc
 286  287           * may have a hold on the vnode.
 287  288           * Checking for VBAD here will also act as a forced umount check.
 288  289           */
 289  290          if (vp->v_count <= 2 && vp->v_type != VBAD) {
 290  291                  struct inode *ip = VTOI(vp);
 291  292                  if (ip->i_delaylen) {
 292  293                          ins.in_poc.value.ul++;
 293  294                          (void) ufs_putpages(vp, ip->i_delayoff, ip->i_delaylen,
 294  295                              B_ASYNC | B_FREE, cr);
 295  296                          ip->i_delaylen = 0;
 296  297                  }
 297  298          }
 298  299  
 299  300          return (0);
 300  301  }
 301  302  
 302  303  /*ARGSUSED*/
 303  304  static int
 304  305  ufs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cr,
 305  306      struct caller_context *ct)
 306  307  {
 307  308          struct inode *ip = VTOI(vp);
 308  309          struct ufsvfs *ufsvfsp;
 309  310          struct ulockfs *ulp = NULL;
 310  311          int error = 0;
 311  312          int intrans = 0;
 312  313  
 313  314          ASSERT(RW_READ_HELD(&ip->i_rwlock));
 314  315  
 315  316          /*
 316  317           * Mandatory locking needs to be done before ufs_lockfs_begin()
 317  318           * and TRANS_BEGIN_SYNC() calls since mandatory locks can sleep.
 318  319           */
 319  320          if (MANDLOCK(vp, ip->i_mode)) {
 320  321                  /*
 321  322                   * ufs_getattr ends up being called by chklock
 322  323                   */
 323  324                  error = chklock(vp, FREAD, uiop->uio_loffset,
 324  325                      uiop->uio_resid, uiop->uio_fmode, ct);
 325  326                  if (error)
 326  327                          goto out;
 327  328          }
 328  329  
 329  330          ufsvfsp = ip->i_ufsvfs;
 330  331          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READ_MASK);
 331  332          if (error)
 332  333                  goto out;
 333  334  
 334  335          /*
 335  336           * In the case that a directory is opened for reading as a file
 336  337           * (eg "cat .") with the  O_RSYNC, O_SYNC and O_DSYNC flags set.
 337  338           * The locking order had to be changed to avoid a deadlock with
 338  339           * an update taking place on that directory at the same time.
 339  340           */
 340  341          if ((ip->i_mode & IFMT) == IFDIR) {
 341  342  
 342  343                  rw_enter(&ip->i_contents, RW_READER);
 343  344                  error = rdip(ip, uiop, ioflag, cr);
 344  345                  rw_exit(&ip->i_contents);
 345  346  
 346  347                  if (error) {
 347  348                          if (ulp)
 348  349                                  ufs_lockfs_end(ulp);
 349  350                          goto out;
 350  351                  }
 351  352  
 352  353                  if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
 353  354                      TRANS_ISTRANS(ufsvfsp)) {
 354  355                          rw_exit(&ip->i_rwlock);
 355  356                          TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
 356  357                              error);
 357  358                          ASSERT(!error);
 358  359                          TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
 359  360                              TOP_READ_SIZE);
 360  361                          rw_enter(&ip->i_rwlock, RW_READER);
 361  362                  }
 362  363          } else {
 363  364                  /*
 364  365                   * Only transact reads to files opened for sync-read and
 365  366                   * sync-write on a file system that is not write locked.
 366  367                   *
 367  368                   * The ``not write locked'' check prevents problems with
 368  369                   * enabling/disabling logging on a busy file system.  E.g.,
 369  370                   * logging exists at the beginning of the read but does not
 370  371                   * at the end.
 371  372                   *
 372  373                   */
 373  374                  if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
 374  375                      TRANS_ISTRANS(ufsvfsp)) {
 375  376                          TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
 376  377                              error);
 377  378                          ASSERT(!error);
 378  379                          intrans = 1;
 379  380                  }
 380  381  
 381  382                  rw_enter(&ip->i_contents, RW_READER);
 382  383                  error = rdip(ip, uiop, ioflag, cr);
 383  384                  rw_exit(&ip->i_contents);
 384  385  
 385  386                  if (intrans) {
 386  387                          TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
 387  388                              TOP_READ_SIZE);
 388  389                  }
  
    | 
      ↓ open down ↓ | 
    355 lines elided | 
    
      ↑ open up ↑ | 
  
 389  390          }
 390  391  
 391  392          if (ulp) {
 392  393                  ufs_lockfs_end(ulp);
 393  394          }
 394  395  out:
 395  396  
 396  397          return (error);
 397  398  }
 398  399  
 399      -extern  int     ufs_HW;         /* high water mark */
 400      -extern  int     ufs_LW;         /* low water mark */
 401      -int     ufs_WRITES = 1;         /* XXX - enable/disable */
      400 +extern  volatile int    ufs_HW; /* high water mark */
      401 +extern  volatile int    ufs_LW; /* low water mark */
      402 +volatile int    ufs_WRITES = 1; /* XXX - enable/disable */
 402  403  int     ufs_throttles = 0;      /* throttling count */
 403  404  int     ufs_allow_shared_writes = 1;    /* directio shared writes */
 404  405  
 405  406  static int
 406  407  ufs_check_rewrite(struct inode *ip, struct uio *uiop, int ioflag)
 407  408  {
 408  409          int     shared_write;
 409  410  
 410  411          /*
 411  412           * If the FDSYNC flag is set then ignore the global
 412  413           * ufs_allow_shared_writes in this case.
 413  414           */
 414  415          shared_write = (ioflag & FDSYNC) | ufs_allow_shared_writes;
 415  416  
 416  417          /*
 417  418           * Filter to determine if this request is suitable as a
 418  419           * concurrent rewrite. This write must not allocate blocks
 419  420           * by extending the file or filling in holes. No use trying
 420  421           * through FSYNC descriptors as the inode will be synchronously
 421  422           * updated after the write. The uio structure has not yet been
 422  423           * checked for sanity, so assume nothing.
 423  424           */
 424  425          return (((ip->i_mode & IFMT) == IFREG) && !(ioflag & FAPPEND) &&
 425  426              (uiop->uio_loffset >= (offset_t)0) &&
 426  427              (uiop->uio_loffset < ip->i_size) && (uiop->uio_resid > 0) &&
 427  428              ((ip->i_size - uiop->uio_loffset) >= uiop->uio_resid) &&
 428  429              !(ioflag & FSYNC) && !bmap_has_holes(ip) &&
 429  430              shared_write);
 430  431  }
 431  432  
 432  433  /*ARGSUSED*/
 433  434  static int
 434  435  ufs_write(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cr,
 435  436      caller_context_t *ct)
 436  437  {
 437  438          struct inode *ip = VTOI(vp);
 438  439          struct ufsvfs *ufsvfsp;
 439  440          struct ulockfs *ulp;
 440  441          int retry = 1;
 441  442          int error, resv, resid = 0;
 442  443          int directio_status;
 443  444          int exclusive;
 444  445          int rewriteflg;
 445  446          long start_resid = uiop->uio_resid;
 446  447  
 447  448          ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
 448  449  
 449  450  retry_mandlock:
 450  451          /*
 451  452           * Mandatory locking needs to be done before ufs_lockfs_begin()
 452  453           * and TRANS_BEGIN_[A]SYNC() calls since mandatory locks can sleep.
 453  454           * Check for forced unmounts normally done in ufs_lockfs_begin().
 454  455           */
 455  456          if ((ufsvfsp = ip->i_ufsvfs) == NULL) {
 456  457                  error = EIO;
 457  458                  goto out;
 458  459          }
 459  460          if (MANDLOCK(vp, ip->i_mode)) {
 460  461  
 461  462                  ASSERT(RW_WRITE_HELD(&ip->i_rwlock));
 462  463  
 463  464                  /*
 464  465                   * ufs_getattr ends up being called by chklock
 465  466                   */
 466  467                  error = chklock(vp, FWRITE, uiop->uio_loffset,
 467  468                      uiop->uio_resid, uiop->uio_fmode, ct);
 468  469                  if (error)
 469  470                          goto out;
 470  471          }
 471  472  
 472  473          /* i_rwlock can change in chklock */
 473  474          exclusive = rw_write_held(&ip->i_rwlock);
 474  475          rewriteflg = ufs_check_rewrite(ip, uiop, ioflag);
 475  476  
 476  477          /*
 477  478           * Check for fast-path special case of directio re-writes.
 478  479           */
 479  480          if ((ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) &&
 480  481              !exclusive && rewriteflg) {
 481  482  
 482  483                  error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
 483  484                  if (error)
 484  485                          goto out;
 485  486  
 486  487                  rw_enter(&ip->i_contents, RW_READER);
 487  488                  error = ufs_directio_write(ip, uiop, ioflag, 1, cr,
 488  489                      &directio_status);
 489  490                  if (directio_status == DIRECTIO_SUCCESS) {
 490  491                          uint_t i_flag_save;
 491  492  
 492  493                          if (start_resid != uiop->uio_resid)
 493  494                                  error = 0;
 494  495                          /*
 495  496                           * Special treatment of access times for re-writes.
 496  497                           * If IMOD is not already set, then convert it
 497  498                           * to IMODACC for this operation. This defers
 498  499                           * entering a delta into the log until the inode
 499  500                           * is flushed. This mimics what is done for read
 500  501                           * operations and inode access time.
 501  502                           */
 502  503                          mutex_enter(&ip->i_tlock);
 503  504                          i_flag_save = ip->i_flag;
 504  505                          ip->i_flag |= IUPD | ICHG;
 505  506                          ip->i_seq++;
 506  507                          ITIMES_NOLOCK(ip);
 507  508                          if ((i_flag_save & IMOD) == 0) {
 508  509                                  ip->i_flag &= ~IMOD;
 509  510                                  ip->i_flag |= IMODACC;
 510  511                          }
 511  512                          mutex_exit(&ip->i_tlock);
 512  513                          rw_exit(&ip->i_contents);
 513  514                          if (ulp)
 514  515                                  ufs_lockfs_end(ulp);
 515  516                          goto out;
 516  517                  }
 517  518                  rw_exit(&ip->i_contents);
 518  519                  if (ulp)
 519  520                          ufs_lockfs_end(ulp);
 520  521          }
 521  522  
 522  523          if (!exclusive && !rw_tryupgrade(&ip->i_rwlock)) {
 523  524                  rw_exit(&ip->i_rwlock);
 524  525                  rw_enter(&ip->i_rwlock, RW_WRITER);
 525  526                  /*
 526  527                   * Mandatory locking could have been enabled
 527  528                   * after dropping the i_rwlock.
 528  529                   */
 529  530                  if (MANDLOCK(vp, ip->i_mode))
 530  531                          goto retry_mandlock;
 531  532          }
 532  533  
 533  534          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
 534  535          if (error)
 535  536                  goto out;
 536  537  
 537  538          /*
 538  539           * Amount of log space needed for this write
 539  540           */
 540  541          if (!rewriteflg || !(ioflag & FDSYNC))
 541  542                  TRANS_WRITE_RESV(ip, uiop, ulp, &resv, &resid);
 542  543  
 543  544          /*
 544  545           * Throttle writes.
 545  546           */
 546  547          if (ufs_WRITES && (ip->i_writes > ufs_HW)) {
 547  548                  mutex_enter(&ip->i_tlock);
 548  549                  while (ip->i_writes > ufs_HW) {
 549  550                          ufs_throttles++;
 550  551                          cv_wait(&ip->i_wrcv, &ip->i_tlock);
 551  552                  }
 552  553                  mutex_exit(&ip->i_tlock);
 553  554          }
 554  555  
 555  556          /*
 556  557           * Enter Transaction
 557  558           *
 558  559           * If the write is a rewrite there is no need to open a transaction
 559  560           * if the FDSYNC flag is set and not the FSYNC.  In this case just
 560  561           * set the IMODACC flag to modify do the update at a later time
 561  562           * thus avoiding the overhead of the logging transaction that is
 562  563           * not required.
 563  564           */
 564  565          if (ioflag & (FSYNC|FDSYNC)) {
 565  566                  if (ulp) {
 566  567                          if (rewriteflg) {
 567  568                                  uint_t i_flag_save;
 568  569  
 569  570                                  rw_enter(&ip->i_contents, RW_READER);
 570  571                                  mutex_enter(&ip->i_tlock);
 571  572                                  i_flag_save = ip->i_flag;
 572  573                                  ip->i_flag |= IUPD | ICHG;
 573  574                                  ip->i_seq++;
 574  575                                  ITIMES_NOLOCK(ip);
 575  576                                  if ((i_flag_save & IMOD) == 0) {
 576  577                                          ip->i_flag &= ~IMOD;
 577  578                                          ip->i_flag |= IMODACC;
 578  579                                  }
 579  580                                  mutex_exit(&ip->i_tlock);
 580  581                                  rw_exit(&ip->i_contents);
 581  582                          } else {
 582  583                                  int terr = 0;
 583  584                                  TRANS_BEGIN_SYNC(ufsvfsp, TOP_WRITE_SYNC, resv,
 584  585                                      terr);
 585  586                                  ASSERT(!terr);
 586  587                          }
 587  588                  }
 588  589          } else {
 589  590                  if (ulp)
 590  591                          TRANS_BEGIN_ASYNC(ufsvfsp, TOP_WRITE, resv);
 591  592          }
 592  593  
 593  594          /*
 594  595           * Write the file
 595  596           */
 596  597          rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
 597  598          rw_enter(&ip->i_contents, RW_WRITER);
 598  599          if ((ioflag & FAPPEND) != 0 && (ip->i_mode & IFMT) == IFREG) {
 599  600                  /*
 600  601                   * In append mode start at end of file.
 601  602                   */
 602  603                  uiop->uio_loffset = ip->i_size;
 603  604          }
 604  605  
 605  606          /*
 606  607           * Mild optimisation, don't call ufs_trans_write() unless we have to
 607  608           * Also, suppress file system full messages if we will retry.
 608  609           */
 609  610          if (retry)
 610  611                  ip->i_flag |= IQUIET;
 611  612          if (resid) {
 612  613                  TRANS_WRITE(ip, uiop, ioflag, error, ulp, cr, resv, resid);
 613  614          } else {
 614  615                  error = wrip(ip, uiop, ioflag, cr);
 615  616          }
 616  617          ip->i_flag &= ~IQUIET;
 617  618  
 618  619          rw_exit(&ip->i_contents);
 619  620          rw_exit(&ufsvfsp->vfs_dqrwlock);
 620  621  
 621  622          /*
 622  623           * Leave Transaction
 623  624           */
 624  625          if (ulp) {
 625  626                  if (ioflag & (FSYNC|FDSYNC)) {
 626  627                          if (!rewriteflg) {
 627  628                                  int terr = 0;
 628  629  
 629  630                                  TRANS_END_SYNC(ufsvfsp, terr, TOP_WRITE_SYNC,
 630  631                                      resv);
 631  632                                  if (error == 0)
 632  633                                          error = terr;
 633  634                          }
 634  635                  } else {
 635  636                          TRANS_END_ASYNC(ufsvfsp, TOP_WRITE, resv);
 636  637                  }
 637  638                  ufs_lockfs_end(ulp);
 638  639          }
 639  640  out:
 640  641          if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
 641  642                  /*
 642  643                   * Any blocks tied up in pending deletes?
 643  644                   */
 644  645                  ufs_delete_drain_wait(ufsvfsp, 1);
 645  646                  retry = 0;
 646  647                  goto retry_mandlock;
 647  648          }
 648  649  
 649  650          if (error == ENOSPC && (start_resid != uiop->uio_resid))
 650  651                  error = 0;
 651  652  
 652  653          return (error);
 653  654  }
 654  655  
 655  656  /*
  
    | 
      ↓ open down ↓ | 
    244 lines elided | 
    
      ↑ open up ↑ | 
  
 656  657   * Don't cache write blocks to files with the sticky bit set.
 657  658   * Used to keep swap files from blowing the page cache on a server.
 658  659   */
 659  660  int stickyhack = 1;
 660  661  
 661  662  /*
 662  663   * Free behind hacks.  The pager is busted.
 663  664   * XXX - need to pass the information down to writedone() in a flag like B_SEQ
 664  665   * or B_FREE_IF_TIGHT_ON_MEMORY.
 665  666   */
 666      -int     freebehind = 1;
 667      -int     smallfile = 0;
      667 +volatile int    freebehind = 1;
      668 +volatile int    smallfile = 0;
 668  669  u_offset_t smallfile64 = 32 * 1024;
 669  670  
 670  671  /*
 671  672   * While we should, in most cases, cache the pages for write, we
 672  673   * may also want to cache the pages for read as long as they are
 673  674   * frequently re-usable.
 674  675   *
 675  676   * If cache_read_ahead = 1, the pages for read will go to the tail
 676  677   * of the cache list when they are released, otherwise go to the head.
 677  678   */
 678  679  int     cache_read_ahead = 0;
 679  680  
 680  681  /*
 681  682   * Freebehind exists  so that as we read  large files  sequentially we
 682  683   * don't consume most of memory with pages  from a few files. It takes
 683  684   * longer to re-read from disk multiple small files as it does reading
 684  685   * one large one sequentially.  As system  memory grows customers need
 685  686   * to retain bigger chunks   of files in  memory.   The advent of  the
 686  687   * cachelist opens up of the possibility freeing pages  to the head or
 687  688   * tail of the list.
 688  689   *
 689  690   * Not freeing a page is a bet that the page will be read again before
 690  691   * it's segmap slot is needed for something else. If we loose the bet,
 691  692   * it means some  other thread is  burdened with the  page free we did
 692  693   * not do. If we win we save a free and reclaim.
 693  694   *
 694  695   * Freeing it at the tail  vs the head of cachelist  is a bet that the
 695  696   * page will survive until the next  read.  It's also saying that this
 696  697   * page is more likely to  be re-used than a  page freed some time ago
 697  698   * and never reclaimed.
 698  699   *
 699  700   * Freebehind maintains a  range of  file offset [smallfile1; smallfile2]
 700  701   *
 701  702   *            0 < offset < smallfile1 : pages are not freed.
 702  703   *   smallfile1 < offset < smallfile2 : pages freed to tail of cachelist.
 703  704   *   smallfile2 < offset              : pages freed to head of cachelist.
 704  705   *
 705  706   * The range  is  computed  at most  once  per second  and  depends on
 706  707   * freemem  and  ncpus_online.  Both parameters  are   bounded to be
 707  708   * >= smallfile && >= smallfile64.
 708  709   *
 709  710   * smallfile1 = (free memory / ncpu) / 1000
 710  711   * smallfile2 = (free memory / ncpu) / 10
 711  712   *
 712  713   * A few examples values:
 713  714   *
 714  715   *       Free Mem (in Bytes) [smallfile1; smallfile2]  [smallfile1; smallfile2]
 715  716   *                                 ncpus_online = 4          ncpus_online = 64
 716  717   *       ------------------  -----------------------   -----------------------
 717  718   *             1G                   [256K;  25M]               [32K; 1.5M]
 718  719   *            10G                   [2.5M; 250M]              [156K; 15M]
 719  720   *           100G                    [25M; 2.5G]              [1.5M; 150M]
 720  721   *
 721  722   */
 722  723  
 723  724  #define SMALLFILE1_D 1000
 724  725  #define SMALLFILE2_D 10
 725  726  static u_offset_t smallfile1 = 32 * 1024;
 726  727  static u_offset_t smallfile2 = 32 * 1024;
 727  728  static clock_t smallfile_update = 0; /* lbolt value of when to recompute */
 728  729  uint_t smallfile1_d = SMALLFILE1_D;
 729  730  uint_t smallfile2_d = SMALLFILE2_D;
 730  731  
 731  732  /*
 732  733   * wrip does the real work of write requests for ufs.
 733  734   */
 734  735  int
 735  736  wrip(struct inode *ip, struct uio *uio, int ioflag, struct cred *cr)
 736  737  {
 737  738          rlim64_t limit = uio->uio_llimit;
 738  739          u_offset_t off;
 739  740          u_offset_t old_i_size;
 740  741          struct fs *fs;
 741  742          struct vnode *vp;
 742  743          struct ufsvfs *ufsvfsp;
 743  744          caddr_t base;
 744  745          long start_resid = uio->uio_resid;      /* save starting resid */
 745  746          long premove_resid;                     /* resid before uiomove() */
 746  747          uint_t flags;
 747  748          int newpage;
 748  749          int iupdat_flag, directio_status;
 749  750          int n, on, mapon;
 750  751          int error, pagecreate;
 751  752          int do_dqrwlock;                /* drop/reacquire vfs_dqrwlock */
 752  753          int32_t iblocks;
 753  754          int     new_iblocks;
 754  755  
 755  756          /*
 756  757           * ip->i_size is incremented before the uiomove
 757  758           * is done on a write.  If the move fails (bad user
 758  759           * address) reset ip->i_size.
 759  760           * The better way would be to increment ip->i_size
 760  761           * only if the uiomove succeeds.
 761  762           */
 762  763          int i_size_changed = 0;
 763  764          o_mode_t type;
 764  765          int i_seq_needed = 0;
 765  766  
 766  767          vp = ITOV(ip);
 767  768  
 768  769          /*
 769  770           * check for forced unmount - should not happen as
 770  771           * the request passed the lockfs checks.
 771  772           */
 772  773          if ((ufsvfsp = ip->i_ufsvfs) == NULL)
 773  774                  return (EIO);
 774  775  
 775  776          fs = ip->i_fs;
 776  777  
 777  778          ASSERT(RW_WRITE_HELD(&ip->i_contents));
 778  779  
 779  780          /* check for valid filetype */
 780  781          type = ip->i_mode & IFMT;
 781  782          if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
 782  783              (type != IFLNK) && (type != IFSHAD)) {
 783  784                  return (EIO);
 784  785          }
 785  786  
 786  787          /*
 787  788           * the actual limit of UFS file size
 788  789           * is UFS_MAXOFFSET_T
 789  790           */
 790  791          if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
 791  792                  limit = MAXOFFSET_T;
 792  793  
 793  794          if (uio->uio_loffset >= limit) {
 794  795                  proc_t *p = ttoproc(curthread);
 795  796  
 796  797                  mutex_enter(&p->p_lock);
 797  798                  (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
 798  799                      p, RCA_UNSAFE_SIGINFO);
 799  800                  mutex_exit(&p->p_lock);
 800  801                  return (EFBIG);
 801  802          }
 802  803  
 803  804          /*
 804  805           * if largefiles are disallowed, the limit is
 805  806           * the pre-largefiles value of 2GB
 806  807           */
 807  808          if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
 808  809                  limit = MIN(UFS_MAXOFFSET_T, limit);
 809  810          else
 810  811                  limit = MIN(MAXOFF32_T, limit);
 811  812  
 812  813          if (uio->uio_loffset < (offset_t)0) {
 813  814                  return (EINVAL);
 814  815          }
 815  816          if (uio->uio_resid == 0) {
 816  817                  return (0);
 817  818          }
 818  819  
 819  820          if (uio->uio_loffset >= limit)
 820  821                  return (EFBIG);
 821  822  
 822  823          ip->i_flag |= INOACC;   /* don't update ref time in getpage */
 823  824  
 824  825          if (ioflag & (FSYNC|FDSYNC)) {
 825  826                  ip->i_flag |= ISYNC;
 826  827                  iupdat_flag = 1;
 827  828          }
 828  829          /*
 829  830           * Try to go direct
 830  831           */
 831  832          if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
 832  833                  uio->uio_llimit = limit;
 833  834                  error = ufs_directio_write(ip, uio, ioflag, 0, cr,
 834  835                      &directio_status);
 835  836                  /*
 836  837                   * If ufs_directio wrote to the file or set the flags,
 837  838                   * we need to update i_seq, but it may be deferred.
 838  839                   */
 839  840                  if (start_resid != uio->uio_resid ||
 840  841                      (ip->i_flag & (ICHG|IUPD))) {
 841  842                          i_seq_needed = 1;
 842  843                          ip->i_flag |= ISEQ;
 843  844                  }
 844  845                  if (directio_status == DIRECTIO_SUCCESS)
 845  846                          goto out;
 846  847          }
 847  848  
 848  849          /*
 849  850           * Behavior with respect to dropping/reacquiring vfs_dqrwlock:
 850  851           *
 851  852           * o shadow inodes: vfs_dqrwlock is not held at all
 852  853           * o quota updates: vfs_dqrwlock is read or write held
 853  854           * o other updates: vfs_dqrwlock is read held
 854  855           *
 855  856           * The first case is the only one where we do not hold
 856  857           * vfs_dqrwlock at all while entering wrip().
 857  858           * We must make sure not to downgrade/drop vfs_dqrwlock if we
 858  859           * have it as writer, i.e. if we are updating the quota inode.
 859  860           * There is no potential deadlock scenario in this case as
 860  861           * ufs_getpage() takes care of this and avoids reacquiring
 861  862           * vfs_dqrwlock in that case.
 862  863           *
 863  864           * This check is done here since the above conditions do not change
 864  865           * and we possibly loop below, so save a few cycles.
 865  866           */
 866  867          if ((type == IFSHAD) ||
 867  868              (rw_owner(&ufsvfsp->vfs_dqrwlock) == curthread)) {
 868  869                  do_dqrwlock = 0;
 869  870          } else {
 870  871                  do_dqrwlock = 1;
 871  872          }
 872  873  
 873  874          /*
 874  875           * Large Files: We cast MAXBMASK to offset_t
 875  876           * inorder to mask out the higher bits. Since offset_t
 876  877           * is a signed value, the high order bit set in MAXBMASK
 877  878           * value makes it do the right thing by having all bits 1
 878  879           * in the higher word. May be removed for _SOLARIS64_.
 879  880           */
 880  881  
 881  882          fs = ip->i_fs;
 882  883          do {
 883  884                  u_offset_t uoff = uio->uio_loffset;
 884  885                  off = uoff & (offset_t)MAXBMASK;
 885  886                  mapon = (int)(uoff & (offset_t)MAXBOFFSET);
 886  887                  on = (int)blkoff(fs, uoff);
 887  888                  n = (int)MIN(fs->fs_bsize - on, uio->uio_resid);
 888  889                  new_iblocks = 1;
 889  890  
 890  891                  if (type == IFREG && uoff + n >= limit) {
 891  892                          if (uoff >= limit) {
 892  893                                  error = EFBIG;
 893  894                                  goto out;
 894  895                          }
 895  896                          /*
 896  897                           * since uoff + n >= limit,
 897  898                           * therefore n >= limit - uoff, and n is an int
 898  899                           * so it is safe to cast it to an int
 899  900                           */
 900  901                          n = (int)(limit - (rlim64_t)uoff);
 901  902                  }
 902  903                  if (uoff + n > ip->i_size) {
 903  904                          /*
 904  905                           * We are extending the length of the file.
 905  906                           * bmap is used so that we are sure that
 906  907                           * if we need to allocate new blocks, that it
 907  908                           * is done here before we up the file size.
 908  909                           */
 909  910                          error = bmap_write(ip, uoff, (int)(on + n),
 910  911                              mapon == 0, NULL, cr);
 911  912                          /*
 912  913                           * bmap_write never drops i_contents so if
 913  914                           * the flags are set it changed the file.
 914  915                           */
 915  916                          if (ip->i_flag & (ICHG|IUPD)) {
 916  917                                  i_seq_needed = 1;
 917  918                                  ip->i_flag |= ISEQ;
 918  919                          }
 919  920                          if (error)
 920  921                                  break;
 921  922                          /*
 922  923                           * There is a window of vulnerability here.
 923  924                           * The sequence of operations: allocate file
 924  925                           * system blocks, uiomove the data into pages,
 925  926                           * and then update the size of the file in the
 926  927                           * inode, must happen atomically.  However, due
 927  928                           * to current locking constraints, this can not
 928  929                           * be done.
 929  930                           */
 930  931                          ASSERT(ip->i_writer == NULL);
 931  932                          ip->i_writer = curthread;
 932  933                          i_size_changed = 1;
 933  934                          /*
 934  935                           * If we are writing from the beginning of
 935  936                           * the mapping, we can just create the
 936  937                           * pages without having to read them.
 937  938                           */
 938  939                          pagecreate = (mapon == 0);
 939  940                  } else if (n == MAXBSIZE) {
 940  941                          /*
 941  942                           * Going to do a whole mappings worth,
 942  943                           * so we can just create the pages w/o
 943  944                           * having to read them in.  But before
 944  945                           * we do that, we need to make sure any
 945  946                           * needed blocks are allocated first.
 946  947                           */
 947  948                          iblocks = ip->i_blocks;
 948  949                          error = bmap_write(ip, uoff, (int)(on + n),
 949  950                              BI_ALLOC_ONLY, NULL, cr);
 950  951                          /*
 951  952                           * bmap_write never drops i_contents so if
 952  953                           * the flags are set it changed the file.
 953  954                           */
 954  955                          if (ip->i_flag & (ICHG|IUPD)) {
 955  956                                  i_seq_needed = 1;
 956  957                                  ip->i_flag |= ISEQ;
 957  958                          }
 958  959                          if (error)
 959  960                                  break;
 960  961                          pagecreate = 1;
 961  962                          /*
 962  963                           * check if the new created page needed the
 963  964                           * allocation of new disk blocks.
 964  965                           */
 965  966                          if (iblocks == ip->i_blocks)
 966  967                                  new_iblocks = 0; /* no new blocks allocated */
 967  968                  } else {
 968  969                          pagecreate = 0;
 969  970                          /*
 970  971                           * In sync mode flush the indirect blocks which
 971  972                           * may have been allocated and not written on
 972  973                           * disk. In above cases bmap_write will allocate
 973  974                           * in sync mode.
 974  975                           */
 975  976                          if (ioflag & (FSYNC|FDSYNC)) {
 976  977                                  error = ufs_indirblk_sync(ip, uoff);
 977  978                                  if (error)
 978  979                                          break;
 979  980                          }
 980  981                  }
 981  982  
 982  983                  /*
 983  984                   * At this point we can enter ufs_getpage() in one
 984  985                   * of two ways:
 985  986                   * 1) segmap_getmapflt() calls ufs_getpage() when the
 986  987                   *    forcefault parameter is true (pagecreate == 0)
 987  988                   * 2) uiomove() causes a page fault.
 988  989                   *
 989  990                   * We have to drop the contents lock to prevent the VM
 990  991                   * system from trying to reacquire it in ufs_getpage()
 991  992                   * should the uiomove cause a pagefault.
 992  993                   *
 993  994                   * We have to drop the reader vfs_dqrwlock here as well.
 994  995                   */
 995  996                  rw_exit(&ip->i_contents);
 996  997                  if (do_dqrwlock) {
 997  998                          ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
 998  999                          ASSERT(!(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock)));
 999 1000                          rw_exit(&ufsvfsp->vfs_dqrwlock);
1000 1001                  }
1001 1002  
1002 1003                  newpage = 0;
1003 1004                  premove_resid = uio->uio_resid;
1004 1005  
1005 1006                  /*
1006 1007                   * Touch the page and fault it in if it is not in core
1007 1008                   * before segmap_getmapflt or vpm_data_copy can lock it.
1008 1009                   * This is to avoid the deadlock if the buffer is mapped
1009 1010                   * to the same file through mmap which we want to write.
1010 1011                   */
1011 1012                  uio_prefaultpages((long)n, uio);
1012 1013  
1013 1014                  if (vpm_enable) {
1014 1015                          /*
1015 1016                           * Copy data. If new pages are created, part of
1016 1017                           * the page that is not written will be initizliazed
1017 1018                           * with zeros.
1018 1019                           */
1019 1020                          error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
1020 1021                              uio, !pagecreate, &newpage, 0, S_WRITE);
1021 1022                  } else {
1022 1023  
1023 1024                          base = segmap_getmapflt(segkmap, vp, (off + mapon),
1024 1025                              (uint_t)n, !pagecreate, S_WRITE);
1025 1026  
1026 1027                          /*
1027 1028                           * segmap_pagecreate() returns 1 if it calls
1028 1029                           * page_create_va() to allocate any pages.
1029 1030                           */
1030 1031  
1031 1032                          if (pagecreate)
1032 1033                                  newpage = segmap_pagecreate(segkmap, base,
1033 1034                                      (size_t)n, 0);
1034 1035  
1035 1036                          error = uiomove(base + mapon, (long)n, UIO_WRITE, uio);
1036 1037                  }
1037 1038  
1038 1039                  /*
1039 1040                   * If "newpage" is set, then a new page was created and it
1040 1041                   * does not contain valid data, so it needs to be initialized
1041 1042                   * at this point.
1042 1043                   * Otherwise the page contains old data, which was overwritten
1043 1044                   * partially or as a whole in uiomove.
1044 1045                   * If there is only one iovec structure within uio, then
1045 1046                   * on error uiomove will not be able to update uio->uio_loffset
1046 1047                   * and we would zero the whole page here!
1047 1048                   *
1048 1049                   * If uiomove fails because of an error, the old valid data
1049 1050                   * is kept instead of filling the rest of the page with zero's.
1050 1051                   */
1051 1052                  if (!vpm_enable && newpage &&
1052 1053                      uio->uio_loffset < roundup(off + mapon + n, PAGESIZE)) {
1053 1054                          /*
1054 1055                           * We created pages w/o initializing them completely,
1055 1056                           * thus we need to zero the part that wasn't set up.
1056 1057                           * This happens on most EOF write cases and if
1057 1058                           * we had some sort of error during the uiomove.
1058 1059                           */
1059 1060                          int nzero, nmoved;
1060 1061  
1061 1062                          nmoved = (int)(uio->uio_loffset - (off + mapon));
1062 1063                          ASSERT(nmoved >= 0 && nmoved <= n);
1063 1064                          nzero = roundup(on + n, PAGESIZE) - nmoved;
1064 1065                          ASSERT(nzero > 0 && mapon + nmoved + nzero <= MAXBSIZE);
1065 1066                          (void) kzero(base + mapon + nmoved, (uint_t)nzero);
1066 1067                  }
1067 1068  
1068 1069                  /*
1069 1070                   * Unlock the pages allocated by page_create_va()
1070 1071                   * in segmap_pagecreate()
1071 1072                   */
1072 1073                  if (!vpm_enable && newpage)
1073 1074                          segmap_pageunlock(segkmap, base, (size_t)n, S_WRITE);
1074 1075  
1075 1076                  /*
1076 1077                   * If the size of the file changed, then update the
1077 1078                   * size field in the inode now.  This can't be done
1078 1079                   * before the call to segmap_pageunlock or there is
1079 1080                   * a potential deadlock with callers to ufs_putpage().
1080 1081                   * They will be holding i_contents and trying to lock
1081 1082                   * a page, while this thread is holding a page locked
1082 1083                   * and trying to acquire i_contents.
1083 1084                   */
1084 1085                  if (i_size_changed) {
1085 1086                          rw_enter(&ip->i_contents, RW_WRITER);
1086 1087                          old_i_size = ip->i_size;
1087 1088                          UFS_SET_ISIZE(uoff + n, ip);
1088 1089                          TRANS_INODE(ufsvfsp, ip);
1089 1090                          /*
1090 1091                           * file has grown larger than 2GB. Set flag
1091 1092                           * in superblock to indicate this, if it
1092 1093                           * is not already set.
1093 1094                           */
1094 1095                          if ((ip->i_size > MAXOFF32_T) &&
1095 1096                              !(fs->fs_flags & FSLARGEFILES)) {
1096 1097                                  ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
1097 1098                                  mutex_enter(&ufsvfsp->vfs_lock);
1098 1099                                  fs->fs_flags |= FSLARGEFILES;
1099 1100                                  ufs_sbwrite(ufsvfsp);
1100 1101                                  mutex_exit(&ufsvfsp->vfs_lock);
1101 1102                          }
1102 1103                          mutex_enter(&ip->i_tlock);
1103 1104                          ip->i_writer = NULL;
1104 1105                          cv_broadcast(&ip->i_wrcv);
1105 1106                          mutex_exit(&ip->i_tlock);
1106 1107                          rw_exit(&ip->i_contents);
1107 1108                  }
1108 1109  
1109 1110                  if (error) {
1110 1111                          /*
1111 1112                           * If we failed on a write, we may have already
1112 1113                           * allocated file blocks as well as pages.  It's
1113 1114                           * hard to undo the block allocation, but we must
1114 1115                           * be sure to invalidate any pages that may have
1115 1116                           * been allocated.
1116 1117                           *
1117 1118                           * If the page was created without initialization
1118 1119                           * then we must check if it should be possible
1119 1120                           * to destroy the new page and to keep the old data
1120 1121                           * on the disk.
1121 1122                           *
1122 1123                           * It is possible to destroy the page without
1123 1124                           * having to write back its contents only when
1124 1125                           * - the size of the file keeps unchanged
1125 1126                           * - bmap_write() did not allocate new disk blocks
1126 1127                           *   it is possible to create big files using "seek" and
1127 1128                           *   write to the end of the file. A "write" to a
1128 1129                           *   position before the end of the file would not
1129 1130                           *   change the size of the file but it would allocate
1130 1131                           *   new disk blocks.
1131 1132                           * - uiomove intended to overwrite the whole page.
1132 1133                           * - a new page was created (newpage == 1).
1133 1134                           */
1134 1135  
1135 1136                          if (i_size_changed == 0 && new_iblocks == 0 &&
1136 1137                              newpage) {
1137 1138  
1138 1139                                  /* unwind what uiomove eventually last did */
1139 1140                                  uio->uio_resid = premove_resid;
1140 1141  
1141 1142                                  /*
1142 1143                                   * destroy the page, do not write ambiguous
1143 1144                                   * data to the disk.
1144 1145                                   */
1145 1146                                  flags = SM_DESTROY;
1146 1147                          } else {
1147 1148                                  /*
1148 1149                                   * write the page back to the disk, if dirty,
1149 1150                                   * and remove the page from the cache.
1150 1151                                   */
1151 1152                                  flags = SM_INVAL;
1152 1153                          }
1153 1154  
1154 1155                          if (vpm_enable) {
1155 1156                                  /*
1156 1157                                   *  Flush pages.
1157 1158                                   */
1158 1159                                  (void) vpm_sync_pages(vp, off, n, flags);
1159 1160                          } else {
1160 1161                                  (void) segmap_release(segkmap, base, flags);
1161 1162                          }
1162 1163                  } else {
1163 1164                          flags = 0;
1164 1165                          /*
1165 1166                           * Force write back for synchronous write cases.
1166 1167                           */
1167 1168                          if ((ioflag & (FSYNC|FDSYNC)) || type == IFDIR) {
1168 1169                                  /*
1169 1170                                   * If the sticky bit is set but the
1170 1171                                   * execute bit is not set, we do a
1171 1172                                   * synchronous write back and free
1172 1173                                   * the page when done.  We set up swap
1173 1174                                   * files to be handled this way to
1174 1175                                   * prevent servers from keeping around
1175 1176                                   * the client's swap pages too long.
1176 1177                                   * XXX - there ought to be a better way.
1177 1178                                   */
1178 1179                                  if (IS_SWAPVP(vp)) {
1179 1180                                          flags = SM_WRITE | SM_FREE |
1180 1181                                              SM_DONTNEED;
1181 1182                                          iupdat_flag = 0;
1182 1183                                  } else {
1183 1184                                          flags = SM_WRITE;
1184 1185                                  }
1185 1186                          } else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
1186 1187                                  /*
1187 1188                                   * Have written a whole block.
1188 1189                                   * Start an asynchronous write and
1189 1190                                   * mark the buffer to indicate that
1190 1191                                   * it won't be needed again soon.
1191 1192                                   */
1192 1193                                  flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
1193 1194                          }
1194 1195                          if (vpm_enable) {
1195 1196                                  /*
1196 1197                                   * Flush pages.
1197 1198                                   */
1198 1199                                  error = vpm_sync_pages(vp, off, n, flags);
1199 1200                          } else {
1200 1201                                  error = segmap_release(segkmap, base, flags);
1201 1202                          }
1202 1203                          /*
1203 1204                           * If the operation failed and is synchronous,
1204 1205                           * then we need to unwind what uiomove() last
1205 1206                           * did so we can potentially return an error to
1206 1207                           * the caller.  If this write operation was
1207 1208                           * done in two pieces and the first succeeded,
1208 1209                           * then we won't return an error for the second
1209 1210                           * piece that failed.  However, we only want to
1210 1211                           * return a resid value that reflects what was
1211 1212                           * really done.
1212 1213                           *
1213 1214                           * Failures for non-synchronous operations can
1214 1215                           * be ignored since the page subsystem will
1215 1216                           * retry the operation until it succeeds or the
1216 1217                           * file system is unmounted.
1217 1218                           */
1218 1219                          if (error) {
1219 1220                                  if ((ioflag & (FSYNC | FDSYNC)) ||
1220 1221                                      type == IFDIR) {
1221 1222                                          uio->uio_resid = premove_resid;
1222 1223                                  } else {
1223 1224                                          error = 0;
1224 1225                                  }
1225 1226                          }
1226 1227                  }
1227 1228  
1228 1229                  /*
1229 1230                   * Re-acquire contents lock.
1230 1231                   * If it was dropped, reacquire reader vfs_dqrwlock as well.
1231 1232                   */
1232 1233                  if (do_dqrwlock)
1233 1234                          rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1234 1235                  rw_enter(&ip->i_contents, RW_WRITER);
1235 1236  
1236 1237                  /*
1237 1238                   * If the uiomove() failed or if a synchronous
1238 1239                   * page push failed, fix up i_size.
1239 1240                   */
1240 1241                  if (error) {
1241 1242                          if (i_size_changed) {
1242 1243                                  /*
1243 1244                                   * The uiomove failed, and we
1244 1245                                   * allocated blocks,so get rid
1245 1246                                   * of them.
1246 1247                                   */
1247 1248                                  (void) ufs_itrunc(ip, old_i_size, 0, cr);
1248 1249                          }
1249 1250                  } else {
1250 1251                          /*
1251 1252                           * XXX - Can this be out of the loop?
1252 1253                           */
1253 1254                          ip->i_flag |= IUPD | ICHG;
1254 1255                          /*
1255 1256                           * Only do one increase of i_seq for multiple
1256 1257                           * pieces.  Because we drop locks, record
1257 1258                           * the fact that we changed the timestamp and
1258 1259                           * are deferring the increase in case another thread
1259 1260                           * pushes our timestamp update.
1260 1261                           */
1261 1262                          i_seq_needed = 1;
1262 1263                          ip->i_flag |= ISEQ;
1263 1264                          if (i_size_changed)
1264 1265                                  ip->i_flag |= IATTCHG;
1265 1266                          if ((ip->i_mode & (IEXEC | (IEXEC >> 3) |
1266 1267                              (IEXEC >> 6))) != 0 &&
1267 1268                              (ip->i_mode & (ISUID | ISGID)) != 0 &&
1268 1269                              secpolicy_vnode_setid_retain(cr,
1269 1270                              (ip->i_mode & ISUID) != 0 && ip->i_uid == 0) != 0) {
1270 1271                                  /*
1271 1272                                   * Clear Set-UID & Set-GID bits on
1272 1273                                   * successful write if not privileged
1273 1274                                   * and at least one of the execute bits
1274 1275                                   * is set.  If we always clear Set-GID,
1275 1276                                   * mandatory file and record locking is
1276 1277                                   * unuseable.
1277 1278                                   */
1278 1279                                  ip->i_mode &= ~(ISUID | ISGID);
1279 1280                          }
1280 1281                  }
1281 1282                  /*
1282 1283                   * In the case the FDSYNC flag is set and this is a
1283 1284                   * "rewrite" we won't log a delta.
1284 1285                   * The FSYNC flag overrides all cases.
1285 1286                   */
1286 1287                  if (!ufs_check_rewrite(ip, uio, ioflag) || !(ioflag & FDSYNC)) {
1287 1288                          TRANS_INODE(ufsvfsp, ip);
1288 1289                  }
1289 1290          } while (error == 0 && uio->uio_resid > 0 && n != 0);
1290 1291  
1291 1292  out:
1292 1293          /*
1293 1294           * Make sure i_seq is increased at least once per write
1294 1295           */
1295 1296          if (i_seq_needed) {
1296 1297                  ip->i_seq++;
1297 1298                  ip->i_flag &= ~ISEQ;    /* no longer deferred */
1298 1299          }
1299 1300  
1300 1301          /*
1301 1302           * Inode is updated according to this table -
1302 1303           *
1303 1304           *   FSYNC        FDSYNC(posix.4)
1304 1305           *   --------------------------
1305 1306           *   always@      IATTCHG|IBDWRITE
1306 1307           *
1307 1308           * @ -  If we are doing synchronous write the only time we should
1308 1309           *      not be sync'ing the ip here is if we have the stickyhack
1309 1310           *      activated, the file is marked with the sticky bit and
1310 1311           *      no exec bit, the file length has not been changed and
1311 1312           *      no new blocks have been allocated during this write.
1312 1313           */
1313 1314  
1314 1315          if ((ip->i_flag & ISYNC) != 0) {
1315 1316                  /*
1316 1317                   * we have eliminated nosync
1317 1318                   */
1318 1319                  if ((ip->i_flag & (IATTCHG|IBDWRITE)) ||
1319 1320                      ((ioflag & FSYNC) && iupdat_flag)) {
1320 1321                          ufs_iupdat(ip, 1);
1321 1322                  }
1322 1323          }
1323 1324  
1324 1325          /*
1325 1326           * If we've already done a partial-write, terminate
1326 1327           * the write but return no error unless the error is ENOSPC
1327 1328           * because the caller can detect this and free resources and
1328 1329           * try again.
1329 1330           */
1330 1331          if ((start_resid != uio->uio_resid) && (error != ENOSPC))
1331 1332                  error = 0;
1332 1333  
1333 1334          ip->i_flag &= ~(INOACC | ISYNC);
1334 1335          ITIMES_NOLOCK(ip);
1335 1336          return (error);
1336 1337  }
1337 1338  
1338 1339  /*
1339 1340   * rdip does the real work of read requests for ufs.
1340 1341   */
1341 1342  int
1342 1343  rdip(struct inode *ip, struct uio *uio, int ioflag, cred_t *cr)
1343 1344  {
1344 1345          u_offset_t off;
1345 1346          caddr_t base;
1346 1347          struct fs *fs;
1347 1348          struct ufsvfs *ufsvfsp;
1348 1349          struct vnode *vp;
1349 1350          long oresid = uio->uio_resid;
1350 1351          u_offset_t n, on, mapon;
1351 1352          int error = 0;
1352 1353          int doupdate = 1;
1353 1354          uint_t flags;
1354 1355          int dofree, directio_status;
1355 1356          krw_t rwtype;
1356 1357          o_mode_t type;
1357 1358          clock_t now;
1358 1359  
1359 1360          vp = ITOV(ip);
1360 1361  
1361 1362          ASSERT(RW_LOCK_HELD(&ip->i_contents));
1362 1363  
1363 1364          ufsvfsp = ip->i_ufsvfs;
1364 1365  
1365 1366          if (ufsvfsp == NULL)
1366 1367                  return (EIO);
1367 1368  
1368 1369          fs = ufsvfsp->vfs_fs;
1369 1370  
1370 1371          /* check for valid filetype */
1371 1372          type = ip->i_mode & IFMT;
1372 1373          if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
1373 1374              (type != IFLNK) && (type != IFSHAD)) {
1374 1375                  return (EIO);
1375 1376          }
1376 1377  
1377 1378          if (uio->uio_loffset > UFS_MAXOFFSET_T) {
1378 1379                  error = 0;
1379 1380                  goto out;
1380 1381          }
1381 1382          if (uio->uio_loffset < (offset_t)0) {
1382 1383                  return (EINVAL);
1383 1384          }
1384 1385          if (uio->uio_resid == 0) {
1385 1386                  return (0);
1386 1387          }
1387 1388  
1388 1389          if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (fs->fs_ronly == 0) &&
1389 1390              (!ufsvfsp->vfs_noatime)) {
1390 1391                  mutex_enter(&ip->i_tlock);
1391 1392                  ip->i_flag |= IACC;
1392 1393                  mutex_exit(&ip->i_tlock);
1393 1394          }
1394 1395          /*
1395 1396           * Try to go direct
1396 1397           */
1397 1398          if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
1398 1399                  error = ufs_directio_read(ip, uio, cr, &directio_status);
1399 1400                  if (directio_status == DIRECTIO_SUCCESS)
1400 1401                          goto out;
1401 1402          }
1402 1403  
1403 1404          rwtype = (rw_write_held(&ip->i_contents)?RW_WRITER:RW_READER);
1404 1405  
1405 1406          do {
1406 1407                  offset_t diff;
1407 1408                  u_offset_t uoff = uio->uio_loffset;
1408 1409                  off = uoff & (offset_t)MAXBMASK;
1409 1410                  mapon = (u_offset_t)(uoff & (offset_t)MAXBOFFSET);
1410 1411                  on = (u_offset_t)blkoff(fs, uoff);
1411 1412                  n = MIN((u_offset_t)fs->fs_bsize - on,
1412 1413                      (u_offset_t)uio->uio_resid);
1413 1414  
1414 1415                  diff = ip->i_size - uoff;
1415 1416  
1416 1417                  if (diff <= (offset_t)0) {
1417 1418                          error = 0;
1418 1419                          goto out;
1419 1420                  }
1420 1421                  if (diff < (offset_t)n)
1421 1422                          n = (int)diff;
1422 1423  
1423 1424                  /*
1424 1425                   * We update smallfile2 and smallfile1 at most every second.
1425 1426                   */
1426 1427                  now = ddi_get_lbolt();
1427 1428                  if (now >= smallfile_update) {
1428 1429                          uint64_t percpufreeb;
1429 1430                          if (smallfile1_d == 0) smallfile1_d = SMALLFILE1_D;
1430 1431                          if (smallfile2_d == 0) smallfile2_d = SMALLFILE2_D;
1431 1432                          percpufreeb = ptob((uint64_t)freemem) / ncpus_online;
1432 1433                          smallfile1 = percpufreeb / smallfile1_d;
1433 1434                          smallfile2 = percpufreeb / smallfile2_d;
1434 1435                          smallfile1 = MAX(smallfile1, smallfile);
1435 1436                          smallfile1 = MAX(smallfile1, smallfile64);
1436 1437                          smallfile2 = MAX(smallfile1, smallfile2);
1437 1438                          smallfile_update = now + hz;
1438 1439                  }
1439 1440  
1440 1441                  dofree = freebehind &&
1441 1442                      ip->i_nextr == (off & PAGEMASK) && off > smallfile1;
1442 1443  
1443 1444                  /*
1444 1445                   * At this point we can enter ufs_getpage() in one of two
1445 1446                   * ways:
1446 1447                   * 1) segmap_getmapflt() calls ufs_getpage() when the
1447 1448                   *    forcefault parameter is true (value of 1 is passed)
1448 1449                   * 2) uiomove() causes a page fault.
1449 1450                   *
1450 1451                   * We cannot hold onto an i_contents reader lock without
1451 1452                   * risking deadlock in ufs_getpage() so drop a reader lock.
1452 1453                   * The ufs_getpage() dolock logic already allows for a
1453 1454                   * thread holding i_contents as writer to work properly
1454 1455                   * so we keep a writer lock.
1455 1456                   */
1456 1457                  if (rwtype == RW_READER)
1457 1458                          rw_exit(&ip->i_contents);
1458 1459  
1459 1460                  if (vpm_enable) {
1460 1461                          /*
1461 1462                           * Copy data.
1462 1463                           */
1463 1464                          error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
1464 1465                              uio, 1, NULL, 0, S_READ);
1465 1466                  } else {
1466 1467                          base = segmap_getmapflt(segkmap, vp, (off + mapon),
1467 1468                              (uint_t)n, 1, S_READ);
1468 1469                          error = uiomove(base + mapon, (long)n, UIO_READ, uio);
1469 1470                  }
1470 1471  
1471 1472                  flags = 0;
1472 1473                  if (!error) {
1473 1474                          /*
1474 1475                           * If  reading sequential  we won't need  this
1475 1476                           * buffer again  soon.  For  offsets in  range
1476 1477                           * [smallfile1,  smallfile2] release the pages
1477 1478                           * at   the  tail  of the   cache list, larger
1478 1479                           * offsets are released at the head.
1479 1480                           */
1480 1481                          if (dofree) {
1481 1482                                  flags = SM_FREE | SM_ASYNC;
1482 1483                                  if ((cache_read_ahead == 0) &&
1483 1484                                      (off > smallfile2))
1484 1485                                          flags |=  SM_DONTNEED;
1485 1486                          }
1486 1487                          /*
1487 1488                           * In POSIX SYNC (FSYNC and FDSYNC) read mode,
1488 1489                           * we want to make sure that the page which has
1489 1490                           * been read, is written on disk if it is dirty.
1490 1491                           * And corresponding indirect blocks should also
1491 1492                           * be flushed out.
1492 1493                           */
1493 1494                          if ((ioflag & FRSYNC) && (ioflag & (FSYNC|FDSYNC))) {
1494 1495                                  flags &= ~SM_ASYNC;
1495 1496                                  flags |= SM_WRITE;
1496 1497                          }
1497 1498                          if (vpm_enable) {
1498 1499                                  error = vpm_sync_pages(vp, off, n, flags);
1499 1500                          } else {
1500 1501                                  error = segmap_release(segkmap, base, flags);
1501 1502                          }
1502 1503                  } else {
1503 1504                          if (vpm_enable) {
1504 1505                                  (void) vpm_sync_pages(vp, off, n, flags);
1505 1506                          } else {
1506 1507                                  (void) segmap_release(segkmap, base, flags);
1507 1508                          }
1508 1509                  }
1509 1510  
1510 1511                  if (rwtype == RW_READER)
1511 1512                          rw_enter(&ip->i_contents, rwtype);
1512 1513          } while (error == 0 && uio->uio_resid > 0 && n != 0);
1513 1514  out:
1514 1515          /*
1515 1516           * Inode is updated according to this table if FRSYNC is set.
1516 1517           *
1517 1518           *   FSYNC        FDSYNC(posix.4)
1518 1519           *   --------------------------
1519 1520           *   always       IATTCHG|IBDWRITE
1520 1521           */
1521 1522          /*
1522 1523           * The inode is not updated if we're logging and the inode is a
1523 1524           * directory with FRSYNC, FSYNC and FDSYNC flags set.
1524 1525           */
1525 1526          if (ioflag & FRSYNC) {
1526 1527                  if (TRANS_ISTRANS(ufsvfsp) && ((ip->i_mode & IFMT) == IFDIR)) {
1527 1528                          doupdate = 0;
1528 1529                  }
1529 1530                  if (doupdate) {
1530 1531                          if ((ioflag & FSYNC) ||
1531 1532                              ((ioflag & FDSYNC) &&
1532 1533                              (ip->i_flag & (IATTCHG|IBDWRITE)))) {
1533 1534                                  ufs_iupdat(ip, 1);
1534 1535                          }
1535 1536                  }
1536 1537          }
1537 1538          /*
1538 1539           * If we've already done a partial read, terminate
1539 1540           * the read but return no error.
1540 1541           */
1541 1542          if (oresid != uio->uio_resid)
1542 1543                  error = 0;
1543 1544          ITIMES(ip);
1544 1545  
1545 1546          return (error);
1546 1547  }
1547 1548  
1548 1549  /* ARGSUSED */
1549 1550  static int
1550 1551  ufs_ioctl(
1551 1552          struct vnode    *vp,
1552 1553          int             cmd,
1553 1554          intptr_t        arg,
1554 1555          int             flag,
  
    | 
      ↓ open down ↓ | 
    877 lines elided | 
    
      ↑ open up ↑ | 
  
1555 1556          struct cred     *cr,
1556 1557          int             *rvalp,
1557 1558          caller_context_t *ct)
1558 1559  {
1559 1560          struct lockfs   lockfs, lockfs_out;
1560 1561          struct ufsvfs   *ufsvfsp = VTOI(vp)->i_ufsvfs;
1561 1562          char            *comment, *original_comment;
1562 1563          struct fs       *fs;
1563 1564          struct ulockfs  *ulp;
1564 1565          offset_t        off;
1565      -        extern int      maxphys;
1566 1566          int             error;
1567 1567          int             issync;
1568 1568          int             trans_size;
1569 1569  
1570 1570  
1571 1571          /*
1572 1572           * forcibly unmounted
1573 1573           */
1574 1574          if (ufsvfsp == NULL || vp->v_vfsp == NULL ||
1575 1575              vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
1576 1576                  return (EIO);
1577 1577          fs = ufsvfsp->vfs_fs;
1578 1578  
1579 1579          if (cmd == Q_QUOTACTL) {
1580 1580                  error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_QUOTA_MASK);
1581 1581                  if (error)
1582 1582                          return (error);
1583 1583  
1584 1584                  if (ulp) {
1585 1585                          TRANS_BEGIN_ASYNC(ufsvfsp, TOP_QUOTA,
1586 1586                              TOP_SETQUOTA_SIZE(fs));
1587 1587                  }
1588 1588  
1589 1589                  error = quotactl(vp, arg, flag, cr);
1590 1590  
1591 1591                  if (ulp) {
1592 1592                          TRANS_END_ASYNC(ufsvfsp, TOP_QUOTA,
1593 1593                              TOP_SETQUOTA_SIZE(fs));
1594 1594                          ufs_lockfs_end(ulp);
1595 1595                  }
1596 1596                  return (error);
1597 1597          }
1598 1598  
1599 1599          switch (cmd) {
1600 1600                  case _FIOLFS:
1601 1601                          /*
1602 1602                           * file system locking
1603 1603                           */
1604 1604                          if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1605 1605                                  return (EPERM);
1606 1606  
1607 1607                          if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1608 1608                                  if (copyin((caddr_t)arg, &lockfs,
1609 1609                                      sizeof (struct lockfs)))
1610 1610                                          return (EFAULT);
1611 1611                          }
1612 1612  #ifdef _SYSCALL32_IMPL
1613 1613                          else {
1614 1614                                  struct lockfs32 lockfs32;
1615 1615                                  /* Translate ILP32 lockfs to LP64 lockfs */
1616 1616                                  if (copyin((caddr_t)arg, &lockfs32,
1617 1617                                      sizeof (struct lockfs32)))
1618 1618                                          return (EFAULT);
1619 1619                                  lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
1620 1620                                  lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
1621 1621                                  lockfs.lf_key = (ulong_t)lockfs32.lf_key;
1622 1622                                  lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
1623 1623                                  lockfs.lf_comment =
1624 1624                                      (caddr_t)(uintptr_t)lockfs32.lf_comment;
1625 1625                          }
1626 1626  #endif /* _SYSCALL32_IMPL */
1627 1627  
1628 1628                          if (lockfs.lf_comlen) {
1629 1629                                  if (lockfs.lf_comlen > LOCKFS_MAXCOMMENTLEN)
1630 1630                                          return (ENAMETOOLONG);
1631 1631                                  comment =
1632 1632                                      kmem_alloc(lockfs.lf_comlen, KM_SLEEP);
1633 1633                                  if (copyin(lockfs.lf_comment, comment,
1634 1634                                      lockfs.lf_comlen)) {
1635 1635                                          kmem_free(comment, lockfs.lf_comlen);
1636 1636                                          return (EFAULT);
1637 1637                                  }
1638 1638                                  original_comment = lockfs.lf_comment;
1639 1639                                  lockfs.lf_comment = comment;
1640 1640                          }
1641 1641                          if ((error = ufs_fiolfs(vp, &lockfs, 0)) == 0) {
1642 1642                                  lockfs.lf_comment = original_comment;
1643 1643  
1644 1644                                  if ((flag & DATAMODEL_MASK) ==
1645 1645                                      DATAMODEL_NATIVE) {
1646 1646                                          (void) copyout(&lockfs, (caddr_t)arg,
1647 1647                                              sizeof (struct lockfs));
1648 1648                                  }
1649 1649  #ifdef _SYSCALL32_IMPL
1650 1650                                  else {
1651 1651                                          struct lockfs32 lockfs32;
1652 1652                                          /* Translate LP64 to ILP32 lockfs */
1653 1653                                          lockfs32.lf_lock =
1654 1654                                              (uint32_t)lockfs.lf_lock;
1655 1655                                          lockfs32.lf_flags =
1656 1656                                              (uint32_t)lockfs.lf_flags;
1657 1657                                          lockfs32.lf_key =
1658 1658                                              (uint32_t)lockfs.lf_key;
1659 1659                                          lockfs32.lf_comlen =
1660 1660                                              (uint32_t)lockfs.lf_comlen;
1661 1661                                          lockfs32.lf_comment =
1662 1662                                              (uint32_t)(uintptr_t)
1663 1663                                              lockfs.lf_comment;
1664 1664                                          (void) copyout(&lockfs32, (caddr_t)arg,
1665 1665                                              sizeof (struct lockfs32));
1666 1666                                  }
1667 1667  #endif /* _SYSCALL32_IMPL */
1668 1668  
1669 1669                          } else {
1670 1670                                  if (lockfs.lf_comlen)
1671 1671                                          kmem_free(comment, lockfs.lf_comlen);
1672 1672                          }
1673 1673                          return (error);
1674 1674  
1675 1675                  case _FIOLFSS:
1676 1676                          /*
1677 1677                           * get file system locking status
1678 1678                           */
1679 1679  
1680 1680                          if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1681 1681                                  if (copyin((caddr_t)arg, &lockfs,
1682 1682                                      sizeof (struct lockfs)))
1683 1683                                          return (EFAULT);
1684 1684                          }
1685 1685  #ifdef _SYSCALL32_IMPL
1686 1686                          else {
1687 1687                                  struct lockfs32 lockfs32;
1688 1688                                  /* Translate ILP32 lockfs to LP64 lockfs */
1689 1689                                  if (copyin((caddr_t)arg, &lockfs32,
1690 1690                                      sizeof (struct lockfs32)))
1691 1691                                          return (EFAULT);
1692 1692                                  lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
1693 1693                                  lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
1694 1694                                  lockfs.lf_key = (ulong_t)lockfs32.lf_key;
1695 1695                                  lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
1696 1696                                  lockfs.lf_comment =
1697 1697                                      (caddr_t)(uintptr_t)lockfs32.lf_comment;
1698 1698                          }
1699 1699  #endif /* _SYSCALL32_IMPL */
1700 1700  
1701 1701                          if (error =  ufs_fiolfss(vp, &lockfs_out))
1702 1702                                  return (error);
1703 1703                          lockfs.lf_lock = lockfs_out.lf_lock;
1704 1704                          lockfs.lf_key = lockfs_out.lf_key;
1705 1705                          lockfs.lf_flags = lockfs_out.lf_flags;
1706 1706                          lockfs.lf_comlen = MIN(lockfs.lf_comlen,
1707 1707                              lockfs_out.lf_comlen);
1708 1708  
1709 1709                          if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1710 1710                                  if (copyout(&lockfs, (caddr_t)arg,
1711 1711                                      sizeof (struct lockfs)))
1712 1712                                          return (EFAULT);
1713 1713                          }
1714 1714  #ifdef _SYSCALL32_IMPL
1715 1715                          else {
1716 1716                                  /* Translate LP64 to ILP32 lockfs */
1717 1717                                  struct lockfs32 lockfs32;
1718 1718                                  lockfs32.lf_lock = (uint32_t)lockfs.lf_lock;
1719 1719                                  lockfs32.lf_flags = (uint32_t)lockfs.lf_flags;
1720 1720                                  lockfs32.lf_key = (uint32_t)lockfs.lf_key;
1721 1721                                  lockfs32.lf_comlen = (uint32_t)lockfs.lf_comlen;
1722 1722                                  lockfs32.lf_comment =
1723 1723                                      (uint32_t)(uintptr_t)lockfs.lf_comment;
1724 1724                                  if (copyout(&lockfs32, (caddr_t)arg,
1725 1725                                      sizeof (struct lockfs32)))
1726 1726                                          return (EFAULT);
1727 1727                          }
1728 1728  #endif /* _SYSCALL32_IMPL */
1729 1729  
1730 1730                          if (lockfs.lf_comlen &&
1731 1731                              lockfs.lf_comment && lockfs_out.lf_comment)
1732 1732                                  if (copyout(lockfs_out.lf_comment,
1733 1733                                      lockfs.lf_comment, lockfs.lf_comlen))
1734 1734                                          return (EFAULT);
1735 1735                          return (0);
1736 1736  
1737 1737                  case _FIOSATIME:
1738 1738                          /*
1739 1739                           * set access time
1740 1740                           */
1741 1741  
1742 1742                          /*
1743 1743                           * if mounted w/o atime, return quietly.
1744 1744                           * I briefly thought about returning ENOSYS, but
1745 1745                           * figured that most apps would consider this fatal
1746 1746                           * but the idea is to make this as seamless as poss.
1747 1747                           */
1748 1748                          if (ufsvfsp->vfs_noatime)
1749 1749                                  return (0);
1750 1750  
1751 1751                          error = ufs_lockfs_begin(ufsvfsp, &ulp,
1752 1752                              ULOCKFS_SETATTR_MASK);
1753 1753                          if (error)
1754 1754                                  return (error);
1755 1755  
1756 1756                          if (ulp) {
1757 1757                                  trans_size = (int)TOP_SETATTR_SIZE(VTOI(vp));
1758 1758                                  TRANS_BEGIN_CSYNC(ufsvfsp, issync,
1759 1759                                      TOP_SETATTR, trans_size);
1760 1760                          }
1761 1761  
1762 1762                          error = ufs_fiosatime(vp, (struct timeval *)arg,
1763 1763                              flag, cr);
1764 1764  
1765 1765                          if (ulp) {
1766 1766                                  TRANS_END_CSYNC(ufsvfsp, error, issync,
1767 1767                                      TOP_SETATTR, trans_size);
1768 1768                                  ufs_lockfs_end(ulp);
1769 1769                          }
1770 1770                          return (error);
1771 1771  
1772 1772                  case _FIOSDIO:
1773 1773                          /*
1774 1774                           * set delayed-io
1775 1775                           */
1776 1776                          return (ufs_fiosdio(vp, (uint_t *)arg, flag, cr));
1777 1777  
1778 1778                  case _FIOGDIO:
1779 1779                          /*
1780 1780                           * get delayed-io
1781 1781                           */
1782 1782                          return (ufs_fiogdio(vp, (uint_t *)arg, flag, cr));
1783 1783  
1784 1784                  case _FIOIO:
1785 1785                          /*
1786 1786                           * inode open
1787 1787                           */
1788 1788                          error = ufs_lockfs_begin(ufsvfsp, &ulp,
1789 1789                              ULOCKFS_VGET_MASK);
1790 1790                          if (error)
1791 1791                                  return (error);
1792 1792  
1793 1793                          error = ufs_fioio(vp, (struct fioio *)arg, flag, cr);
1794 1794  
1795 1795                          if (ulp) {
1796 1796                                  ufs_lockfs_end(ulp);
1797 1797                          }
1798 1798                          return (error);
1799 1799  
1800 1800                  case _FIOFFS:
1801 1801                          /*
1802 1802                           * file system flush (push w/invalidate)
1803 1803                           */
1804 1804                          if ((caddr_t)arg != NULL)
1805 1805                                  return (EINVAL);
1806 1806                          return (ufs_fioffs(vp, NULL, cr));
1807 1807  
1808 1808                  case _FIOISBUSY:
1809 1809                          /*
1810 1810                           * Contract-private interface for Legato
1811 1811                           * Purge this vnode from the DNLC and decide
1812 1812                           * if this vnode is busy (*arg == 1) or not
1813 1813                           * (*arg == 0)
1814 1814                           */
1815 1815                          if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1816 1816                                  return (EPERM);
1817 1817                          error = ufs_fioisbusy(vp, (int *)arg, cr);
1818 1818                          return (error);
1819 1819  
1820 1820                  case _FIODIRECTIO:
1821 1821                          return (ufs_fiodirectio(vp, (int)arg, cr));
1822 1822  
1823 1823                  case _FIOTUNE:
1824 1824                          /*
1825 1825                           * Tune the file system (aka setting fs attributes)
1826 1826                           */
1827 1827                          error = ufs_lockfs_begin(ufsvfsp, &ulp,
1828 1828                              ULOCKFS_SETATTR_MASK);
1829 1829                          if (error)
1830 1830                                  return (error);
1831 1831  
1832 1832                          error = ufs_fiotune(vp, (struct fiotune *)arg, cr);
1833 1833  
1834 1834                          if (ulp)
1835 1835                                  ufs_lockfs_end(ulp);
1836 1836                          return (error);
1837 1837  
1838 1838                  case _FIOLOGENABLE:
1839 1839                          if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1840 1840                                  return (EPERM);
1841 1841                          return (ufs_fiologenable(vp, (void *)arg, cr, flag));
1842 1842  
1843 1843                  case _FIOLOGDISABLE:
1844 1844                          if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1845 1845                                  return (EPERM);
1846 1846                          return (ufs_fiologdisable(vp, (void *)arg, cr, flag));
1847 1847  
1848 1848                  case _FIOISLOG:
1849 1849                          return (ufs_fioislog(vp, (void *)arg, cr, flag));
1850 1850  
1851 1851                  case _FIOSNAPSHOTCREATE_MULTI:
1852 1852                  {
1853 1853                          struct fiosnapcreate_multi      fc, *fcp;
1854 1854                          size_t  fcm_size;
1855 1855  
1856 1856                          if (copyin((void *)arg, &fc, sizeof (fc)))
1857 1857                                  return (EFAULT);
1858 1858                          if (fc.backfilecount > MAX_BACKFILE_COUNT)
1859 1859                                  return (EINVAL);
1860 1860                          fcm_size = sizeof (struct fiosnapcreate_multi) +
1861 1861                              (fc.backfilecount - 1) * sizeof (int);
1862 1862                          fcp = (struct fiosnapcreate_multi *)
1863 1863                              kmem_alloc(fcm_size, KM_SLEEP);
1864 1864                          if (copyin((void *)arg, fcp, fcm_size)) {
1865 1865                                  kmem_free(fcp, fcm_size);
1866 1866                                  return (EFAULT);
1867 1867                          }
1868 1868                          error = ufs_snap_create(vp, fcp, cr);
1869 1869                          /*
1870 1870                           * Do copyout even if there is an error because
1871 1871                           * the details of error is stored in fcp.
1872 1872                           */
1873 1873                          if (copyout(fcp, (void *)arg, fcm_size))
1874 1874                                  error = EFAULT;
1875 1875                          kmem_free(fcp, fcm_size);
1876 1876                          return (error);
1877 1877                  }
1878 1878  
1879 1879                  case _FIOSNAPSHOTDELETE:
1880 1880                  {
1881 1881                          struct fiosnapdelete    fc;
1882 1882  
1883 1883                          if (copyin((void *)arg, &fc, sizeof (fc)))
1884 1884                                  return (EFAULT);
1885 1885                          error = ufs_snap_delete(vp, &fc, cr);
1886 1886                          if (!error && copyout(&fc, (void *)arg, sizeof (fc)))
  
    | 
      ↓ open down ↓ | 
    311 lines elided | 
    
      ↑ open up ↑ | 
  
1887 1887                                  error = EFAULT;
1888 1888                          return (error);
1889 1889                  }
1890 1890  
1891 1891                  case _FIOGETSUPERBLOCK:
1892 1892                          if (copyout(fs, (void *)arg, SBSIZE))
1893 1893                                  return (EFAULT);
1894 1894                          return (0);
1895 1895  
1896 1896                  case _FIOGETMAXPHYS:
1897      -                        if (copyout(&maxphys, (void *)arg, sizeof (maxphys)))
     1897 +                        if (copyout((void *)&maxphys, (void *)arg,
     1898 +                            sizeof (maxphys)))
1898 1899                                  return (EFAULT);
1899 1900                          return (0);
1900 1901  
1901 1902                  /*
1902 1903                   * The following 3 ioctls are for TSufs support
1903 1904                   * although could potentially be used elsewhere
1904 1905                   */
1905 1906                  case _FIO_SET_LUFS_DEBUG:
1906 1907                          if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1907 1908                                  return (EPERM);
1908 1909                          lufs_debug = (uint32_t)arg;
1909 1910                          return (0);
1910 1911  
1911 1912                  case _FIO_SET_LUFS_ERROR:
1912 1913                          if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1913 1914                                  return (EPERM);
1914 1915                          TRANS_SETERROR(ufsvfsp);
1915 1916                          return (0);
1916 1917  
1917 1918                  case _FIO_GET_TOP_STATS:
1918 1919                  {
1919 1920                          fio_lufs_stats_t *ls;
1920 1921                          ml_unit_t *ul = ufsvfsp->vfs_log;
1921 1922  
1922 1923                          ls = kmem_zalloc(sizeof (*ls), KM_SLEEP);
1923 1924                          ls->ls_debug = ul->un_debug; /* return debug value */
1924 1925                          /* Copy stucture if statistics are being kept */
1925 1926                          if (ul->un_logmap->mtm_tops) {
1926 1927                                  ls->ls_topstats = *(ul->un_logmap->mtm_tops);
1927 1928                          }
1928 1929                          error = 0;
1929 1930                          if (copyout(ls, (void *)arg, sizeof (*ls)))
1930 1931                                  error = EFAULT;
1931 1932                          kmem_free(ls, sizeof (*ls));
1932 1933                          return (error);
1933 1934                  }
1934 1935  
1935 1936                  case _FIO_SEEK_DATA:
1936 1937                  case _FIO_SEEK_HOLE:
1937 1938                          if (ddi_copyin((void *)arg, &off, sizeof (off), flag))
1938 1939                                  return (EFAULT);
1939 1940                          /* offset paramater is in/out */
1940 1941                          error = ufs_fio_holey(vp, cmd, &off);
1941 1942                          if (error)
1942 1943                                  return (error);
1943 1944                          if (ddi_copyout(&off, (void *)arg, sizeof (off), flag))
1944 1945                                  return (EFAULT);
1945 1946                          return (0);
1946 1947  
1947 1948                  case _FIO_COMPRESSED:
1948 1949                  {
1949 1950                          /*
1950 1951                           * This is a project private ufs ioctl() to mark
1951 1952                           * the inode as that belonging to a compressed
1952 1953                           * file. This is used to mark individual
1953 1954                           * compressed files in a miniroot archive.
1954 1955                           * The files compressed in this manner are
1955 1956                           * automatically decompressed by the dcfs filesystem
1956 1957                           * (via an interception in ufs_lookup - see decompvp())
1957 1958                           * which is layered on top of ufs on a system running
1958 1959                           * from the archive. See uts/common/fs/dcfs for details.
1959 1960                           * This ioctl only marks the file as compressed - the
1960 1961                           * actual compression is done by fiocompress (a
1961 1962                           * userland utility) which invokes this ioctl().
1962 1963                           */
1963 1964                          struct inode *ip = VTOI(vp);
1964 1965  
1965 1966                          error = ufs_lockfs_begin(ufsvfsp, &ulp,
1966 1967                              ULOCKFS_SETATTR_MASK);
1967 1968                          if (error)
1968 1969                                  return (error);
1969 1970  
1970 1971                          if (ulp) {
1971 1972                                  TRANS_BEGIN_ASYNC(ufsvfsp, TOP_IUPDAT,
1972 1973                                      TOP_IUPDAT_SIZE(ip));
1973 1974                          }
1974 1975  
1975 1976                          error = ufs_mark_compressed(vp);
1976 1977  
1977 1978                          if (ulp) {
1978 1979                                  TRANS_END_ASYNC(ufsvfsp, TOP_IUPDAT,
1979 1980                                      TOP_IUPDAT_SIZE(ip));
1980 1981                                  ufs_lockfs_end(ulp);
1981 1982                          }
1982 1983  
1983 1984                          return (error);
1984 1985  
1985 1986                  }
1986 1987  
1987 1988                  default:
1988 1989                          return (ENOTTY);
1989 1990          }
1990 1991  }
1991 1992  
1992 1993  
1993 1994  /* ARGSUSED */
1994 1995  static int
1995 1996  ufs_getattr(struct vnode *vp, struct vattr *vap, int flags,
1996 1997      struct cred *cr, caller_context_t *ct)
1997 1998  {
1998 1999          struct inode *ip = VTOI(vp);
1999 2000          struct ufsvfs *ufsvfsp;
2000 2001          int err;
2001 2002  
2002 2003          if (vap->va_mask == AT_SIZE) {
2003 2004                  /*
2004 2005                   * for performance, if only the size is requested don't bother
2005 2006                   * with anything else.
2006 2007                   */
2007 2008                  UFS_GET_ISIZE(&vap->va_size, ip);
2008 2009                  return (0);
2009 2010          }
2010 2011  
2011 2012          /*
2012 2013           * inlined lockfs checks
2013 2014           */
2014 2015          ufsvfsp = ip->i_ufsvfs;
2015 2016          if ((ufsvfsp == NULL) || ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs)) {
2016 2017                  err = EIO;
2017 2018                  goto out;
2018 2019          }
2019 2020  
2020 2021          rw_enter(&ip->i_contents, RW_READER);
2021 2022          /*
2022 2023           * Return all the attributes.  This should be refined so
2023 2024           * that it only returns what's asked for.
2024 2025           */
2025 2026  
2026 2027          /*
2027 2028           * Copy from inode table.
2028 2029           */
2029 2030          vap->va_type = vp->v_type;
2030 2031          vap->va_mode = ip->i_mode & MODEMASK;
2031 2032          /*
2032 2033           * If there is an ACL and there is a mask entry, then do the
2033 2034           * extra work that completes the equivalent of an acltomode(3)
2034 2035           * call.  According to POSIX P1003.1e, the acl mask should be
2035 2036           * returned in the group permissions field.
2036 2037           *
2037 2038           * - start with the original permission and mode bits (from above)
2038 2039           * - clear the group owner bits
2039 2040           * - add in the mask bits.
2040 2041           */
2041 2042          if (ip->i_ufs_acl && ip->i_ufs_acl->aclass.acl_ismask) {
2042 2043                  vap->va_mode &= ~((VREAD | VWRITE | VEXEC) >> 3);
2043 2044                  vap->va_mode |=
2044 2045                      (ip->i_ufs_acl->aclass.acl_maskbits & PERMMASK) << 3;
2045 2046          }
2046 2047          vap->va_uid = ip->i_uid;
2047 2048          vap->va_gid = ip->i_gid;
2048 2049          vap->va_fsid = ip->i_dev;
2049 2050          vap->va_nodeid = (ino64_t)ip->i_number;
2050 2051          vap->va_nlink = ip->i_nlink;
2051 2052          vap->va_size = ip->i_size;
2052 2053          if (vp->v_type == VCHR || vp->v_type == VBLK)
2053 2054                  vap->va_rdev = ip->i_rdev;
2054 2055          else
2055 2056                  vap->va_rdev = 0;       /* not a b/c spec. */
2056 2057          mutex_enter(&ip->i_tlock);
2057 2058          ITIMES_NOLOCK(ip);      /* mark correct time in inode */
2058 2059          vap->va_seq = ip->i_seq;
2059 2060          vap->va_atime.tv_sec = (time_t)ip->i_atime.tv_sec;
2060 2061          vap->va_atime.tv_nsec = ip->i_atime.tv_usec*1000;
2061 2062          vap->va_mtime.tv_sec = (time_t)ip->i_mtime.tv_sec;
2062 2063          vap->va_mtime.tv_nsec = ip->i_mtime.tv_usec*1000;
2063 2064          vap->va_ctime.tv_sec = (time_t)ip->i_ctime.tv_sec;
2064 2065          vap->va_ctime.tv_nsec = ip->i_ctime.tv_usec*1000;
2065 2066          mutex_exit(&ip->i_tlock);
2066 2067  
2067 2068          switch (ip->i_mode & IFMT) {
2068 2069  
2069 2070          case IFBLK:
2070 2071                  vap->va_blksize = MAXBSIZE;             /* was BLKDEV_IOSIZE */
2071 2072                  break;
2072 2073  
2073 2074          case IFCHR:
2074 2075                  vap->va_blksize = MAXBSIZE;
2075 2076                  break;
2076 2077  
2077 2078          default:
2078 2079                  vap->va_blksize = ip->i_fs->fs_bsize;
2079 2080                  break;
2080 2081          }
2081 2082          vap->va_nblocks = (fsblkcnt64_t)ip->i_blocks;
2082 2083          rw_exit(&ip->i_contents);
2083 2084          err = 0;
2084 2085  
2085 2086  out:
2086 2087          return (err);
2087 2088  }
2088 2089  
2089 2090  /*
2090 2091   * Special wrapper to provide a callback for secpolicy_vnode_setattr().
2091 2092   * The i_contents lock is already held by the caller and we need to
2092 2093   * declare the inode as 'void *' argument.
2093 2094   */
2094 2095  static int
2095 2096  ufs_priv_access(void *vip, int mode, struct cred *cr)
2096 2097  {
2097 2098          struct inode *ip = vip;
2098 2099  
2099 2100          return (ufs_iaccess(ip, mode, cr, 0));
2100 2101  }
2101 2102  
2102 2103  /*ARGSUSED4*/
2103 2104  static int
2104 2105  ufs_setattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr,
2105 2106      caller_context_t *ct)
2106 2107  {
2107 2108          struct inode *ip = VTOI(vp);
2108 2109          struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
2109 2110          struct fs *fs;
2110 2111          struct ulockfs *ulp;
2111 2112          char *errmsg1;
2112 2113          char *errmsg2;
2113 2114          long blocks;
2114 2115          long int mask = vap->va_mask;
2115 2116          size_t len1, len2;
2116 2117          int issync;
2117 2118          int trans_size;
2118 2119          int dotrans;
2119 2120          int dorwlock;
2120 2121          int error;
2121 2122          int owner_change;
2122 2123          int dodqlock;
2123 2124          timestruc_t now;
2124 2125          vattr_t oldva;
2125 2126          int retry = 1;
2126 2127          int indeadlock;
2127 2128  
2128 2129          /*
2129 2130           * Cannot set these attributes.
2130 2131           */
2131 2132          if ((mask & AT_NOSET) || (mask & AT_XVATTR))
2132 2133                  return (EINVAL);
2133 2134  
2134 2135          /*
2135 2136           * check for forced unmount
2136 2137           */
2137 2138          if (ufsvfsp == NULL)
2138 2139                  return (EIO);
2139 2140  
2140 2141          fs = ufsvfsp->vfs_fs;
2141 2142          if (fs->fs_ronly != 0)
2142 2143                  return (EROFS);
2143 2144  
2144 2145  again:
2145 2146          errmsg1 = NULL;
2146 2147          errmsg2 = NULL;
2147 2148          dotrans = 0;
2148 2149          dorwlock = 0;
2149 2150          dodqlock = 0;
2150 2151  
2151 2152          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK);
2152 2153          if (error)
2153 2154                  goto out;
2154 2155  
2155 2156          /*
2156 2157           * Acquire i_rwlock before TRANS_BEGIN_CSYNC() if this is a file.
2157 2158           * This follows the protocol for read()/write().
2158 2159           */
2159 2160          if (vp->v_type != VDIR) {
2160 2161                  /*
2161 2162                   * ufs_tryirwlock uses rw_tryenter and checks for SLOCK to
2162 2163                   * avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
2163 2164                   * possible, retries the operation.
2164 2165                   */
2165 2166                  ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_file);
2166 2167                  if (indeadlock) {
2167 2168                          if (ulp)
2168 2169                                  ufs_lockfs_end(ulp);
2169 2170                          goto again;
2170 2171                  }
2171 2172                  dorwlock = 1;
2172 2173          }
2173 2174  
2174 2175          /*
2175 2176           * Truncate file.  Must have write permission and not be a directory.
2176 2177           */
2177 2178          if (mask & AT_SIZE) {
2178 2179                  rw_enter(&ip->i_contents, RW_WRITER);
2179 2180                  if (vp->v_type == VDIR) {
2180 2181                          error = EISDIR;
2181 2182                          goto update_inode;
2182 2183                  }
2183 2184                  if (error = ufs_iaccess(ip, IWRITE, cr, 0))
2184 2185                          goto update_inode;
2185 2186  
2186 2187                  rw_exit(&ip->i_contents);
2187 2188                  error = TRANS_ITRUNC(ip, vap->va_size, 0, cr);
2188 2189                  if (error) {
2189 2190                          rw_enter(&ip->i_contents, RW_WRITER);
2190 2191                          goto update_inode;
2191 2192                  }
2192 2193  
2193 2194                  if (error == 0 && vap->va_size)
2194 2195                          vnevent_truncate(vp, ct);
2195 2196          }
2196 2197  
2197 2198          if (ulp) {
2198 2199                  trans_size = (int)TOP_SETATTR_SIZE(ip);
2199 2200                  TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SETATTR, trans_size);
2200 2201                  ++dotrans;
2201 2202          }
2202 2203  
2203 2204          /*
2204 2205           * Acquire i_rwlock after TRANS_BEGIN_CSYNC() if this is a directory.
2205 2206           * This follows the protocol established by
2206 2207           * ufs_link/create/remove/rename/mkdir/rmdir/symlink.
2207 2208           */
2208 2209          if (vp->v_type == VDIR) {
2209 2210                  ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_SETATTR,
2210 2211                      retry_dir);
2211 2212                  if (indeadlock)
2212 2213                          goto again;
2213 2214                  dorwlock = 1;
2214 2215          }
2215 2216  
2216 2217          /*
2217 2218           * Grab quota lock if we are changing the file's owner.
2218 2219           */
2219 2220          if (mask & AT_UID) {
2220 2221                  rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2221 2222                  dodqlock = 1;
2222 2223          }
2223 2224          rw_enter(&ip->i_contents, RW_WRITER);
2224 2225  
2225 2226          oldva.va_mode = ip->i_mode;
2226 2227          oldva.va_uid = ip->i_uid;
2227 2228          oldva.va_gid = ip->i_gid;
2228 2229  
2229 2230          vap->va_mask &= ~AT_SIZE;
2230 2231  
2231 2232          error = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2232 2233              ufs_priv_access, ip);
2233 2234          if (error)
2234 2235                  goto update_inode;
2235 2236  
2236 2237          mask = vap->va_mask;
2237 2238  
2238 2239          /*
2239 2240           * Change file access modes.
2240 2241           */
2241 2242          if (mask & AT_MODE) {
2242 2243                  ip->i_mode = (ip->i_mode & IFMT) | (vap->va_mode & ~IFMT);
2243 2244                  TRANS_INODE(ufsvfsp, ip);
2244 2245                  ip->i_flag |= ICHG;
2245 2246                  if (stickyhack) {
2246 2247                          mutex_enter(&vp->v_lock);
2247 2248                          if ((ip->i_mode & (ISVTX | IEXEC | IFDIR)) == ISVTX)
2248 2249                                  vp->v_flag |= VSWAPLIKE;
2249 2250                          else
2250 2251                                  vp->v_flag &= ~VSWAPLIKE;
2251 2252                          mutex_exit(&vp->v_lock);
2252 2253                  }
2253 2254          }
2254 2255          if (mask & (AT_UID|AT_GID)) {
2255 2256                  if (mask & AT_UID) {
2256 2257                          /*
2257 2258                           * Don't change ownership of the quota inode.
2258 2259                           */
2259 2260                          if (ufsvfsp->vfs_qinod == ip) {
2260 2261                                  ASSERT(ufsvfsp->vfs_qflags & MQ_ENABLED);
2261 2262                                  error = EINVAL;
2262 2263                                  goto update_inode;
2263 2264                          }
2264 2265  
2265 2266                          /*
2266 2267                           * No real ownership change.
2267 2268                           */
2268 2269                          if (ip->i_uid == vap->va_uid) {
2269 2270                                  blocks = 0;
2270 2271                                  owner_change = 0;
2271 2272                          }
2272 2273                          /*
2273 2274                           * Remove the blocks and the file, from the old user's
2274 2275                           * quota.
2275 2276                           */
2276 2277                          else {
2277 2278                                  blocks = ip->i_blocks;
2278 2279                                  owner_change = 1;
2279 2280  
2280 2281                                  (void) chkdq(ip, -blocks, /* force */ 1, cr,
2281 2282                                      (char **)NULL, (size_t *)NULL);
2282 2283                                  (void) chkiq(ufsvfsp, /* change */ -1, ip,
2283 2284                                      (uid_t)ip->i_uid, /* force */ 1, cr,
2284 2285                                      (char **)NULL, (size_t *)NULL);
2285 2286                                  dqrele(ip->i_dquot);
2286 2287                          }
2287 2288  
2288 2289                          ip->i_uid = vap->va_uid;
2289 2290  
2290 2291                          /*
2291 2292                           * There is a real ownership change.
2292 2293                           */
2293 2294                          if (owner_change) {
2294 2295                                  /*
2295 2296                                   * Add the blocks and the file to the new
2296 2297                                   * user's quota.
2297 2298                                   */
2298 2299                                  ip->i_dquot = getinoquota(ip);
2299 2300                                  (void) chkdq(ip, blocks, /* force */ 1, cr,
2300 2301                                      &errmsg1, &len1);
2301 2302                                  (void) chkiq(ufsvfsp, /* change */ 1,
2302 2303                                      (struct inode *)NULL, (uid_t)ip->i_uid,
2303 2304                                      /* force */ 1, cr, &errmsg2, &len2);
2304 2305                          }
2305 2306                  }
2306 2307                  if (mask & AT_GID) {
2307 2308                          ip->i_gid = vap->va_gid;
2308 2309                  }
2309 2310                  TRANS_INODE(ufsvfsp, ip);
2310 2311                  ip->i_flag |= ICHG;
2311 2312          }
2312 2313          /*
2313 2314           * Change file access or modified times.
2314 2315           */
2315 2316          if (mask & (AT_ATIME|AT_MTIME)) {
2316 2317                  /* Check that the time value is within ufs range */
2317 2318                  if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2318 2319                      ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2319 2320                          error = EOVERFLOW;
2320 2321                          goto update_inode;
2321 2322                  }
2322 2323  
2323 2324                  /*
2324 2325                   * if the "noaccess" mount option is set and only atime
2325 2326                   * update is requested, do nothing. No error is returned.
2326 2327                   */
2327 2328                  if ((ufsvfsp->vfs_noatime) &&
2328 2329                      ((mask & (AT_ATIME|AT_MTIME)) == AT_ATIME))
2329 2330                          goto skip_atime;
2330 2331  
2331 2332                  if (mask & AT_ATIME) {
2332 2333                          ip->i_atime.tv_sec = vap->va_atime.tv_sec;
2333 2334                          ip->i_atime.tv_usec = vap->va_atime.tv_nsec / 1000;
2334 2335                          ip->i_flag &= ~IACC;
2335 2336                  }
2336 2337                  if (mask & AT_MTIME) {
2337 2338                          ip->i_mtime.tv_sec = vap->va_mtime.tv_sec;
2338 2339                          ip->i_mtime.tv_usec = vap->va_mtime.tv_nsec / 1000;
2339 2340                          gethrestime(&now);
2340 2341                          if (now.tv_sec > TIME32_MAX) {
2341 2342                                  /*
2342 2343                                   * In 2038, ctime sticks forever..
2343 2344                                   */
2344 2345                                  ip->i_ctime.tv_sec = TIME32_MAX;
2345 2346                                  ip->i_ctime.tv_usec = 0;
2346 2347                          } else {
2347 2348                                  ip->i_ctime.tv_sec = now.tv_sec;
2348 2349                                  ip->i_ctime.tv_usec = now.tv_nsec / 1000;
2349 2350                          }
2350 2351                          ip->i_flag &= ~(IUPD|ICHG);
2351 2352                          ip->i_flag |= IMODTIME;
2352 2353                  }
2353 2354                  TRANS_INODE(ufsvfsp, ip);
2354 2355                  ip->i_flag |= IMOD;
2355 2356          }
2356 2357  
2357 2358  skip_atime:
2358 2359          /*
2359 2360           * The presence of a shadow inode may indicate an ACL, but does
2360 2361           * not imply an ACL.  Future FSD types should be handled here too
2361 2362           * and check for the presence of the attribute-specific data
2362 2363           * before referencing it.
2363 2364           */
2364 2365          if (ip->i_shadow) {
2365 2366                  /*
2366 2367                   * XXX if ufs_iupdat is changed to sandbagged write fix
2367 2368                   * ufs_acl_setattr to push ip to keep acls consistent
2368 2369                   *
2369 2370                   * Suppress out of inodes messages if we will retry.
2370 2371                   */
2371 2372                  if (retry)
2372 2373                          ip->i_flag |= IQUIET;
2373 2374                  error = ufs_acl_setattr(ip, vap, cr);
2374 2375                  ip->i_flag &= ~IQUIET;
2375 2376          }
2376 2377  
2377 2378  update_inode:
2378 2379          /*
2379 2380           * Setattr always increases the sequence number
2380 2381           */
2381 2382          ip->i_seq++;
2382 2383  
2383 2384          /*
2384 2385           * if nfsd and not logging; push synchronously
2385 2386           */
2386 2387          if ((curthread->t_flag & T_DONTPEND) && !TRANS_ISTRANS(ufsvfsp)) {
2387 2388                  ufs_iupdat(ip, 1);
2388 2389          } else {
2389 2390                  ITIMES_NOLOCK(ip);
2390 2391          }
2391 2392  
2392 2393          rw_exit(&ip->i_contents);
2393 2394          if (dodqlock) {
2394 2395                  rw_exit(&ufsvfsp->vfs_dqrwlock);
2395 2396          }
2396 2397          if (dorwlock)
2397 2398                  rw_exit(&ip->i_rwlock);
2398 2399  
2399 2400          if (ulp) {
2400 2401                  if (dotrans) {
2401 2402                          int terr = 0;
2402 2403                          TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SETATTR,
2403 2404                              trans_size);
2404 2405                          if (error == 0)
2405 2406                                  error = terr;
2406 2407                  }
2407 2408                  ufs_lockfs_end(ulp);
2408 2409          }
2409 2410  out:
2410 2411          /*
2411 2412           * If out of inodes or blocks, see if we can free something
2412 2413           * up from the delete queue.
2413 2414           */
2414 2415          if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
2415 2416                  ufs_delete_drain_wait(ufsvfsp, 1);
2416 2417                  retry = 0;
2417 2418                  if (errmsg1 != NULL)
2418 2419                          kmem_free(errmsg1, len1);
2419 2420                  if (errmsg2 != NULL)
2420 2421                          kmem_free(errmsg2, len2);
2421 2422                  goto again;
2422 2423          }
2423 2424          if (errmsg1 != NULL) {
2424 2425                  uprintf(errmsg1);
2425 2426                  kmem_free(errmsg1, len1);
2426 2427          }
2427 2428          if (errmsg2 != NULL) {
2428 2429                  uprintf(errmsg2);
2429 2430                  kmem_free(errmsg2, len2);
2430 2431          }
2431 2432          return (error);
2432 2433  }
2433 2434  
2434 2435  /*ARGSUSED*/
2435 2436  static int
2436 2437  ufs_access(struct vnode *vp, int mode, int flags, struct cred *cr,
2437 2438      caller_context_t *ct)
2438 2439  {
2439 2440          struct inode *ip = VTOI(vp);
2440 2441  
2441 2442          if (ip->i_ufsvfs == NULL)
2442 2443                  return (EIO);
2443 2444  
2444 2445          /*
2445 2446           * The ufs_iaccess function wants to be called with
2446 2447           * mode bits expressed as "ufs specific" bits.
2447 2448           * I.e., VWRITE|VREAD|VEXEC do not make sense to
2448 2449           * ufs_iaccess() but IWRITE|IREAD|IEXEC do.
2449 2450           * But since they're the same we just pass the vnode mode
2450 2451           * bit but just verify that assumption at compile time.
2451 2452           */
2452 2453  #if IWRITE != VWRITE || IREAD != VREAD || IEXEC != VEXEC
2453 2454  #error "ufs_access needs to map Vmodes to Imodes"
2454 2455  #endif
2455 2456          return (ufs_iaccess(ip, mode, cr, 1));
2456 2457  }
2457 2458  
2458 2459  /* ARGSUSED */
2459 2460  static int
2460 2461  ufs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cr,
2461 2462      caller_context_t *ct)
2462 2463  {
2463 2464          struct inode *ip = VTOI(vp);
2464 2465          struct ufsvfs *ufsvfsp;
2465 2466          struct ulockfs *ulp;
2466 2467          int error;
2467 2468          int fastsymlink;
2468 2469  
2469 2470          if (vp->v_type != VLNK) {
2470 2471                  error = EINVAL;
2471 2472                  goto nolockout;
2472 2473          }
2473 2474  
2474 2475          /*
2475 2476           * If the symbolic link is empty there is nothing to read.
2476 2477           * Fast-track these empty symbolic links
2477 2478           */
2478 2479          if (ip->i_size == 0) {
2479 2480                  error = 0;
2480 2481                  goto nolockout;
2481 2482          }
2482 2483  
2483 2484          ufsvfsp = ip->i_ufsvfs;
2484 2485          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READLINK_MASK);
2485 2486          if (error)
2486 2487                  goto nolockout;
2487 2488          /*
2488 2489           * The ip->i_rwlock protects the data blocks used for FASTSYMLINK
2489 2490           */
2490 2491  again:
2491 2492          fastsymlink = 0;
2492 2493          if (ip->i_flag & IFASTSYMLNK) {
2493 2494                  rw_enter(&ip->i_rwlock, RW_READER);
2494 2495                  rw_enter(&ip->i_contents, RW_READER);
2495 2496                  if (ip->i_flag & IFASTSYMLNK) {
2496 2497                          if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
2497 2498                              (ip->i_fs->fs_ronly == 0) &&
2498 2499                              (!ufsvfsp->vfs_noatime)) {
2499 2500                                  mutex_enter(&ip->i_tlock);
2500 2501                                  ip->i_flag |= IACC;
2501 2502                                  mutex_exit(&ip->i_tlock);
2502 2503                          }
2503 2504                          error = uiomove((caddr_t)&ip->i_db[1],
2504 2505                              MIN(ip->i_size, uiop->uio_resid),
2505 2506                              UIO_READ, uiop);
2506 2507                          ITIMES(ip);
2507 2508                          ++fastsymlink;
2508 2509                  }
2509 2510                  rw_exit(&ip->i_contents);
2510 2511                  rw_exit(&ip->i_rwlock);
2511 2512          }
2512 2513          if (!fastsymlink) {
2513 2514                  ssize_t size;   /* number of bytes read  */
2514 2515                  caddr_t basep;  /* pointer to input data */
2515 2516                  ino_t ino;
2516 2517                  long  igen;
2517 2518                  struct uio tuio;        /* temp uio struct */
2518 2519                  struct uio *tuiop;
2519 2520                  iovec_t tiov;           /* temp iovec struct */
2520 2521                  char kbuf[FSL_SIZE];    /* buffer to hold fast symlink */
2521 2522                  int tflag = 0;          /* flag to indicate temp vars used */
2522 2523  
2523 2524                  ino = ip->i_number;
2524 2525                  igen = ip->i_gen;
2525 2526                  size = uiop->uio_resid;
2526 2527                  basep = uiop->uio_iov->iov_base;
2527 2528                  tuiop = uiop;
2528 2529  
2529 2530                  rw_enter(&ip->i_rwlock, RW_WRITER);
2530 2531                  rw_enter(&ip->i_contents, RW_WRITER);
2531 2532                  if (ip->i_flag & IFASTSYMLNK) {
2532 2533                          rw_exit(&ip->i_contents);
2533 2534                          rw_exit(&ip->i_rwlock);
2534 2535                          goto again;
2535 2536                  }
2536 2537  
2537 2538                  /* can this be a fast symlink and is it a user buffer? */
2538 2539                  if (ip->i_size <= FSL_SIZE &&
2539 2540                      (uiop->uio_segflg == UIO_USERSPACE ||
2540 2541                      uiop->uio_segflg == UIO_USERISPACE)) {
2541 2542  
2542 2543                          bzero(&tuio, sizeof (struct uio));
2543 2544                          /*
2544 2545                           * setup a kernel buffer to read link into.  this
2545 2546                           * is to fix a race condition where the user buffer
2546 2547                           * got corrupted before copying it into the inode.
2547 2548                           */
2548 2549                          size = ip->i_size;
2549 2550                          tiov.iov_len = size;
2550 2551                          tiov.iov_base = kbuf;
2551 2552                          tuio.uio_iov = &tiov;
2552 2553                          tuio.uio_iovcnt = 1;
2553 2554                          tuio.uio_offset = uiop->uio_offset;
2554 2555                          tuio.uio_segflg = UIO_SYSSPACE;
2555 2556                          tuio.uio_fmode = uiop->uio_fmode;
2556 2557                          tuio.uio_extflg = uiop->uio_extflg;
2557 2558                          tuio.uio_limit = uiop->uio_limit;
2558 2559                          tuio.uio_resid = size;
2559 2560  
2560 2561                          basep = tuio.uio_iov->iov_base;
2561 2562                          tuiop = &tuio;
2562 2563                          tflag = 1;
2563 2564                  }
2564 2565  
2565 2566                  error = rdip(ip, tuiop, 0, cr);
2566 2567                  if (!(error == 0 && ip->i_number == ino && ip->i_gen == igen)) {
2567 2568                          rw_exit(&ip->i_contents);
2568 2569                          rw_exit(&ip->i_rwlock);
2569 2570                          goto out;
2570 2571                  }
2571 2572  
2572 2573                  if (tflag == 0)
2573 2574                          size -= uiop->uio_resid;
2574 2575  
2575 2576                  if ((tflag == 0 && ip->i_size <= FSL_SIZE &&
2576 2577                      ip->i_size == size) || (tflag == 1 &&
2577 2578                      tuio.uio_resid == 0)) {
2578 2579                          error = kcopy(basep, &ip->i_db[1], ip->i_size);
2579 2580                          if (error == 0) {
2580 2581                                  ip->i_flag |= IFASTSYMLNK;
2581 2582                                  /*
2582 2583                                   * free page
2583 2584                                   */
2584 2585                                  (void) VOP_PUTPAGE(ITOV(ip),
2585 2586                                      (offset_t)0, PAGESIZE,
2586 2587                                      (B_DONTNEED | B_FREE | B_FORCE | B_ASYNC),
2587 2588                                      cr, ct);
2588 2589                          } else {
2589 2590                                  int i;
2590 2591                                  /* error, clear garbage left behind */
2591 2592                                  for (i = 1; i < NDADDR; i++)
2592 2593                                          ip->i_db[i] = 0;
2593 2594                                  for (i = 0; i < NIADDR; i++)
2594 2595                                          ip->i_ib[i] = 0;
2595 2596                          }
2596 2597                  }
2597 2598                  if (tflag == 1) {
2598 2599                          /* now, copy it into the user buffer */
2599 2600                          error = uiomove((caddr_t)kbuf,
2600 2601                              MIN(size, uiop->uio_resid),
2601 2602                              UIO_READ, uiop);
2602 2603                  }
2603 2604                  rw_exit(&ip->i_contents);
2604 2605                  rw_exit(&ip->i_rwlock);
2605 2606          }
2606 2607  out:
2607 2608          if (ulp) {
2608 2609                  ufs_lockfs_end(ulp);
2609 2610          }
2610 2611  nolockout:
2611 2612          return (error);
2612 2613  }
2613 2614  
2614 2615  /* ARGSUSED */
2615 2616  static int
2616 2617  ufs_fsync(struct vnode *vp, int syncflag, struct cred *cr, caller_context_t *ct)
2617 2618  {
2618 2619          struct inode *ip = VTOI(vp);
2619 2620          struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
2620 2621          struct ulockfs *ulp;
2621 2622          int error;
2622 2623  
2623 2624          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_FSYNC_MASK);
2624 2625          if (error)
2625 2626                  return (error);
2626 2627  
2627 2628          if (TRANS_ISTRANS(ufsvfsp)) {
2628 2629                  /*
2629 2630                   * First push out any data pages
2630 2631                   */
2631 2632                  if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2632 2633                      (vp->v_type != VCHR) && !(IS_SWAPVP(vp))) {
2633 2634                          error = VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
2634 2635                              0, CRED(), ct);
2635 2636                          if (error)
2636 2637                                  goto out;
2637 2638                  }
2638 2639  
2639 2640                  /*
2640 2641                   * Delta any delayed inode times updates
2641 2642                   * and push inode to log.
2642 2643                   * All other inode deltas will have already been delta'd
2643 2644                   * and will be pushed during the commit.
2644 2645                   */
2645 2646                  if (!(syncflag & FDSYNC) &&
2646 2647                      ((ip->i_flag & (IMOD|IMODACC)) == IMODACC)) {
2647 2648                          if (ulp) {
2648 2649                                  TRANS_BEGIN_ASYNC(ufsvfsp, TOP_FSYNC,
2649 2650                                      TOP_SYNCIP_SIZE);
2650 2651                          }
2651 2652                          rw_enter(&ip->i_contents, RW_READER);
2652 2653                          mutex_enter(&ip->i_tlock);
2653 2654                          ip->i_flag &= ~IMODTIME;
2654 2655                          mutex_exit(&ip->i_tlock);
2655 2656                          ufs_iupdat(ip, I_SYNC);
2656 2657                          rw_exit(&ip->i_contents);
2657 2658                          if (ulp) {
2658 2659                                  TRANS_END_ASYNC(ufsvfsp, TOP_FSYNC,
2659 2660                                      TOP_SYNCIP_SIZE);
2660 2661                          }
2661 2662                  }
2662 2663  
2663 2664                  /*
2664 2665                   * Commit the Moby transaction
2665 2666                   *
2666 2667                   * Deltas have already been made so we just need to
2667 2668                   * commit them with a synchronous transaction.
2668 2669                   * TRANS_BEGIN_SYNC() will return an error
2669 2670                   * if there are no deltas to commit, for an
2670 2671                   * empty transaction.
2671 2672                   */
2672 2673                  if (ulp) {
2673 2674                          TRANS_BEGIN_SYNC(ufsvfsp, TOP_FSYNC, TOP_COMMIT_SIZE,
2674 2675                              error);
2675 2676                          if (error) {
2676 2677                                  error = 0; /* commit wasn't needed */
2677 2678                                  goto out;
2678 2679                          }
2679 2680                          TRANS_END_SYNC(ufsvfsp, error, TOP_FSYNC,
2680 2681                              TOP_COMMIT_SIZE);
2681 2682                  }
2682 2683          } else {        /* not logging */
2683 2684                  if (!(IS_SWAPVP(vp)))
2684 2685                          if (syncflag & FNODSYNC) {
2685 2686                                  /* Just update the inode only */
2686 2687                                  TRANS_IUPDAT(ip, 1);
2687 2688                                  error = 0;
2688 2689                          } else if (syncflag & FDSYNC)
2689 2690                                  /* Do data-synchronous writes */
2690 2691                                  error = TRANS_SYNCIP(ip, 0, I_DSYNC, TOP_FSYNC);
2691 2692                          else
2692 2693                                  /* Do synchronous writes */
2693 2694                                  error = TRANS_SYNCIP(ip, 0, I_SYNC, TOP_FSYNC);
2694 2695  
2695 2696                  rw_enter(&ip->i_contents, RW_WRITER);
2696 2697                  if (!error)
2697 2698                          error = ufs_sync_indir(ip);
2698 2699                  rw_exit(&ip->i_contents);
2699 2700          }
2700 2701  out:
2701 2702          if (ulp) {
2702 2703                  ufs_lockfs_end(ulp);
2703 2704          }
2704 2705          return (error);
2705 2706  }
2706 2707  
2707 2708  /*ARGSUSED*/
2708 2709  static void
2709 2710  ufs_inactive(struct vnode *vp, struct cred *cr, caller_context_t *ct)
2710 2711  {
2711 2712          ufs_iinactive(VTOI(vp));
2712 2713  }
2713 2714  
2714 2715  /*
2715 2716   * Unix file system operations having to do with directory manipulation.
2716 2717   */
2717 2718  int ufs_lookup_idle_count = 2;  /* Number of inodes to idle each time */
2718 2719  /* ARGSUSED */
2719 2720  static int
2720 2721  ufs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
2721 2722      struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cr,
2722 2723      caller_context_t *ct, int *direntflags, pathname_t *realpnp)
2723 2724  {
2724 2725          struct inode *ip;
2725 2726          struct inode *sip;
2726 2727          struct inode *xip;
2727 2728          struct ufsvfs *ufsvfsp;
2728 2729          struct ulockfs *ulp;
2729 2730          struct vnode *vp;
2730 2731          int error;
2731 2732  
2732 2733          /*
2733 2734           * Check flags for type of lookup (regular file or attribute file)
2734 2735           */
2735 2736  
2736 2737          ip = VTOI(dvp);
2737 2738  
2738 2739          if (flags & LOOKUP_XATTR) {
2739 2740  
2740 2741                  /*
2741 2742                   * If not mounted with XATTR support then return EINVAL
2742 2743                   */
2743 2744  
2744 2745                  if (!(ip->i_ufsvfs->vfs_vfs->vfs_flag & VFS_XATTR))
2745 2746                          return (EINVAL);
2746 2747                  /*
2747 2748                   * We don't allow recursive attributes...
2748 2749                   * Maybe someday we will.
2749 2750                   */
2750 2751                  if ((ip->i_cflags & IXATTR)) {
2751 2752                          return (EINVAL);
2752 2753                  }
2753 2754  
2754 2755                  if ((vp = dnlc_lookup(dvp, XATTR_DIR_NAME)) == NULL) {
2755 2756                          error = ufs_xattr_getattrdir(dvp, &sip, flags, cr);
2756 2757                          if (error) {
2757 2758                                  *vpp = NULL;
2758 2759                                  goto out;
2759 2760                          }
2760 2761  
2761 2762                          vp = ITOV(sip);
2762 2763                          dnlc_update(dvp, XATTR_DIR_NAME, vp);
2763 2764                  }
2764 2765  
2765 2766                  /*
2766 2767                   * Check accessibility of directory.
2767 2768                   */
2768 2769                  if (vp == DNLC_NO_VNODE) {
2769 2770                          VN_RELE(vp);
2770 2771                          error = ENOENT;
2771 2772                          goto out;
2772 2773                  }
2773 2774                  if ((error = ufs_iaccess(VTOI(vp), IEXEC, cr, 1)) != 0) {
2774 2775                          VN_RELE(vp);
2775 2776                          goto out;
2776 2777                  }
2777 2778  
2778 2779                  *vpp = vp;
2779 2780                  return (0);
2780 2781          }
2781 2782  
2782 2783          /*
2783 2784           * Check for a null component, which we should treat as
2784 2785           * looking at dvp from within it's parent, so we don't
2785 2786           * need a call to ufs_iaccess(), as it has already been
2786 2787           * done.
2787 2788           */
2788 2789          if (nm[0] == 0) {
2789 2790                  VN_HOLD(dvp);
2790 2791                  error = 0;
2791 2792                  *vpp = dvp;
2792 2793                  goto out;
2793 2794          }
2794 2795  
2795 2796          /*
2796 2797           * Check for "." ie itself. this is a quick check and
2797 2798           * avoids adding "." into the dnlc (which have been seen
2798 2799           * to occupy >10% of the cache).
2799 2800           */
2800 2801          if ((nm[0] == '.') && (nm[1] == 0)) {
2801 2802                  /*
2802 2803                   * Don't return without checking accessibility
2803 2804                   * of the directory. We only need the lock if
2804 2805                   * we are going to return it.
2805 2806                   */
2806 2807                  if ((error = ufs_iaccess(ip, IEXEC, cr, 1)) == 0) {
2807 2808                          VN_HOLD(dvp);
2808 2809                          *vpp = dvp;
2809 2810                  }
2810 2811                  goto out;
2811 2812          }
2812 2813  
2813 2814          /*
2814 2815           * Fast path: Check the directory name lookup cache.
2815 2816           */
2816 2817          if (vp = dnlc_lookup(dvp, nm)) {
2817 2818                  /*
2818 2819                   * Check accessibility of directory.
2819 2820                   */
2820 2821                  if ((error = ufs_iaccess(ip, IEXEC, cr, 1)) != 0) {
2821 2822                          VN_RELE(vp);
2822 2823                          goto out;
2823 2824                  }
2824 2825                  if (vp == DNLC_NO_VNODE) {
2825 2826                          VN_RELE(vp);
2826 2827                          error = ENOENT;
2827 2828                          goto out;
2828 2829                  }
2829 2830                  xip = VTOI(vp);
2830 2831                  ulp = NULL;
2831 2832                  goto fastpath;
2832 2833          }
2833 2834  
2834 2835          /*
2835 2836           * Keep the idle queue from getting too long by
2836 2837           * idling two inodes before attempting to allocate another.
2837 2838           *    This operation must be performed before entering
2838 2839           *    lockfs or a transaction.
2839 2840           */
2840 2841          if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
2841 2842                  if ((curthread->t_flag & T_DONTBLOCK) == 0) {
2842 2843                          ins.in_lidles.value.ul += ufs_lookup_idle_count;
2843 2844                          ufs_idle_some(ufs_lookup_idle_count);
2844 2845                  }
2845 2846  
2846 2847  retry_lookup:
2847 2848          /*
2848 2849           * Check accessibility of directory.
2849 2850           */
2850 2851          if (error = ufs_diraccess(ip, IEXEC, cr))
2851 2852                  goto out;
2852 2853  
2853 2854          ufsvfsp = ip->i_ufsvfs;
2854 2855          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK);
2855 2856          if (error)
2856 2857                  goto out;
2857 2858  
2858 2859          error = ufs_dirlook(ip, nm, &xip, cr, 1, 0);
2859 2860  
2860 2861  fastpath:
2861 2862          if (error == 0) {
2862 2863                  ip = xip;
2863 2864                  *vpp = ITOV(ip);
2864 2865  
2865 2866                  /*
2866 2867                   * If vnode is a device return special vnode instead.
2867 2868                   */
2868 2869                  if (IS_DEVVP(*vpp)) {
2869 2870                          struct vnode *newvp;
2870 2871  
2871 2872                          newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
2872 2873                              cr);
2873 2874                          VN_RELE(*vpp);
2874 2875                          if (newvp == NULL)
2875 2876                                  error = ENOSYS;
2876 2877                          else
2877 2878                                  *vpp = newvp;
2878 2879                  } else if (ip->i_cflags & ICOMPRESS) {
2879 2880                          struct vnode *newvp;
2880 2881  
2881 2882                          /*
2882 2883                           * Compressed file, substitute dcfs vnode
2883 2884                           */
2884 2885                          newvp = decompvp(*vpp, cr, ct);
2885 2886                          VN_RELE(*vpp);
2886 2887                          if (newvp == NULL)
2887 2888                                  error = ENOSYS;
2888 2889                          else
2889 2890                                  *vpp = newvp;
2890 2891                  }
2891 2892          }
2892 2893          if (ulp) {
2893 2894                  ufs_lockfs_end(ulp);
2894 2895          }
2895 2896  
2896 2897          if (error == EAGAIN)
2897 2898                  goto retry_lookup;
2898 2899  
2899 2900  out:
2900 2901          return (error);
2901 2902  }
2902 2903  
2903 2904  /*ARGSUSED*/
2904 2905  static int
2905 2906  ufs_create(struct vnode *dvp, char *name, struct vattr *vap, enum vcexcl excl,
2906 2907      int mode, struct vnode **vpp, struct cred *cr, int flag,
2907 2908      caller_context_t *ct, vsecattr_t *vsecp)
2908 2909  {
2909 2910          struct inode *ip;
2910 2911          struct inode *xip;
2911 2912          struct inode *dip;
2912 2913          struct vnode *xvp;
2913 2914          struct ufsvfs *ufsvfsp;
2914 2915          struct ulockfs *ulp;
2915 2916          int error;
2916 2917          int issync;
2917 2918          int truncflag;
2918 2919          int trans_size;
2919 2920          int noentry;
2920 2921          int defer_dip_seq_update = 0;   /* need to defer update of dip->i_seq */
2921 2922          int retry = 1;
2922 2923          int indeadlock;
2923 2924  
2924 2925  again:
2925 2926          ip = VTOI(dvp);
2926 2927          ufsvfsp = ip->i_ufsvfs;
2927 2928          truncflag = 0;
2928 2929  
2929 2930          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_CREATE_MASK);
2930 2931          if (error)
2931 2932                  goto out;
2932 2933  
2933 2934          if (ulp) {
2934 2935                  trans_size = (int)TOP_CREATE_SIZE(ip);
2935 2936                  TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_CREATE, trans_size);
2936 2937          }
2937 2938  
2938 2939          if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr) != 0)
2939 2940                  vap->va_mode &= ~VSVTX;
2940 2941  
2941 2942          if (*name == '\0') {
2942 2943                  /*
2943 2944                   * Null component name refers to the directory itself.
2944 2945                   */
2945 2946                  VN_HOLD(dvp);
2946 2947                  /*
2947 2948                   * Even though this is an error case, we need to grab the
2948 2949                   * quota lock since the error handling code below is common.
2949 2950                   */
2950 2951                  rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2951 2952                  rw_enter(&ip->i_contents, RW_WRITER);
2952 2953                  error = EEXIST;
2953 2954          } else {
2954 2955                  xip = NULL;
2955 2956                  noentry = 0;
2956 2957                  /*
2957 2958                   * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
2958 2959                   * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
2959 2960                   * possible, retries the operation.
2960 2961                   */
2961 2962                  ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_CREATE,
2962 2963                      retry_dir);
2963 2964                  if (indeadlock)
2964 2965                          goto again;
2965 2966  
2966 2967                  xvp = dnlc_lookup(dvp, name);
2967 2968                  if (xvp == DNLC_NO_VNODE) {
2968 2969                          noentry = 1;
2969 2970                          VN_RELE(xvp);
2970 2971                          xvp = NULL;
2971 2972                  }
2972 2973                  if (xvp) {
2973 2974                          rw_exit(&ip->i_rwlock);
2974 2975                          if (error = ufs_iaccess(ip, IEXEC, cr, 1)) {
2975 2976                                  VN_RELE(xvp);
2976 2977                          } else {
2977 2978                                  error = EEXIST;
2978 2979                                  xip = VTOI(xvp);
2979 2980                          }
2980 2981                  } else {
2981 2982                          /*
2982 2983                           * Suppress file system full message if we will retry
2983 2984                           */
2984 2985                          error = ufs_direnter_cm(ip, name, DE_CREATE,
2985 2986                              vap, &xip, cr, (noentry | (retry ? IQUIET : 0)));
2986 2987                          if (error == EAGAIN) {
2987 2988                                  if (ulp) {
2988 2989                                          TRANS_END_CSYNC(ufsvfsp, error, issync,
2989 2990                                              TOP_CREATE, trans_size);
2990 2991                                          ufs_lockfs_end(ulp);
2991 2992                                  }
2992 2993                                  goto again;
2993 2994                          }
2994 2995                          rw_exit(&ip->i_rwlock);
2995 2996                  }
2996 2997                  ip = xip;
2997 2998                  if (ip != NULL) {
2998 2999                          rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2999 3000                          rw_enter(&ip->i_contents, RW_WRITER);
3000 3001                  }
3001 3002          }
3002 3003  
3003 3004          /*
3004 3005           * If the file already exists and this is a non-exclusive create,
3005 3006           * check permissions and allow access for non-directories.
3006 3007           * Read-only create of an existing directory is also allowed.
3007 3008           * We fail an exclusive create of anything which already exists.
3008 3009           */
3009 3010          if (error == EEXIST) {
3010 3011                  dip = VTOI(dvp);
3011 3012                  if (excl == NONEXCL) {
3012 3013                          if ((((ip->i_mode & IFMT) == IFDIR) ||
3013 3014                              ((ip->i_mode & IFMT) == IFATTRDIR)) &&
3014 3015                              (mode & IWRITE))
3015 3016                                  error = EISDIR;
3016 3017                          else if (mode)
3017 3018                                  error = ufs_iaccess(ip, mode, cr, 0);
3018 3019                          else
3019 3020                                  error = 0;
3020 3021                  }
3021 3022                  if (error) {
3022 3023                          rw_exit(&ip->i_contents);
3023 3024                          rw_exit(&ufsvfsp->vfs_dqrwlock);
3024 3025                          VN_RELE(ITOV(ip));
3025 3026                          goto unlock;
3026 3027                  }
3027 3028                  /*
3028 3029                   * If the error EEXIST was set, then i_seq can not
3029 3030                   * have been updated. The sequence number interface
3030 3031                   * is defined such that a non-error VOP_CREATE must
3031 3032                   * increase the dir va_seq it by at least one. If we
3032 3033                   * have cleared the error, increase i_seq. Note that
3033 3034                   * we are increasing the dir i_seq and in rare cases
3034 3035                   * ip may actually be from the dvp, so we already have
3035 3036                   * the locks and it will not be subject to truncation.
3036 3037                   * In case we have to update i_seq of the parent
3037 3038                   * directory dip, we have to defer it till we have
3038 3039                   * released our locks on ip due to lock ordering requirements.
3039 3040                   */
3040 3041                  if (ip != dip)
3041 3042                          defer_dip_seq_update = 1;
3042 3043                  else
3043 3044                          ip->i_seq++;
3044 3045  
3045 3046                  if (((ip->i_mode & IFMT) == IFREG) &&
3046 3047                      (vap->va_mask & AT_SIZE) && vap->va_size == 0) {
3047 3048                          /*
3048 3049                           * Truncate regular files, if requested by caller.
3049 3050                           * Grab i_rwlock to make sure no one else is
3050 3051                           * currently writing to the file (we promised
3051 3052                           * bmap we would do this).
3052 3053                           * Must get the locks in the correct order.
3053 3054                           */
3054 3055                          if (ip->i_size == 0) {
3055 3056                                  ip->i_flag |= ICHG | IUPD;
3056 3057                                  ip->i_seq++;
3057 3058                                  TRANS_INODE(ufsvfsp, ip);
3058 3059                          } else {
3059 3060                                  /*
3060 3061                                   * Large Files: Why this check here?
3061 3062                                   * Though we do it in vn_create() we really
3062 3063                                   * want to guarantee that we do not destroy
3063 3064                                   * Large file data by atomically checking
3064 3065                                   * the size while holding the contents
3065 3066                                   * lock.
3066 3067                                   */
3067 3068                                  if (flag && !(flag & FOFFMAX) &&
3068 3069                                      ((ip->i_mode & IFMT) == IFREG) &&
3069 3070                                      (ip->i_size > (offset_t)MAXOFF32_T)) {
3070 3071                                          rw_exit(&ip->i_contents);
3071 3072                                          rw_exit(&ufsvfsp->vfs_dqrwlock);
3072 3073                                          error = EOVERFLOW;
3073 3074                                          goto unlock;
3074 3075                                  }
3075 3076                                  if (TRANS_ISTRANS(ufsvfsp))
3076 3077                                          truncflag++;
3077 3078                                  else {
3078 3079                                          rw_exit(&ip->i_contents);
3079 3080                                          rw_exit(&ufsvfsp->vfs_dqrwlock);
3080 3081                                          ufs_tryirwlock_trans(&ip->i_rwlock,
3081 3082                                              RW_WRITER, TOP_CREATE,
3082 3083                                              retry_file);
3083 3084                                          if (indeadlock) {
3084 3085                                                  VN_RELE(ITOV(ip));
3085 3086                                                  goto again;
3086 3087                                          }
3087 3088                                          rw_enter(&ufsvfsp->vfs_dqrwlock,
3088 3089                                              RW_READER);
3089 3090                                          rw_enter(&ip->i_contents, RW_WRITER);
3090 3091                                          (void) ufs_itrunc(ip, (u_offset_t)0, 0,
3091 3092                                              cr);
3092 3093                                          rw_exit(&ip->i_rwlock);
3093 3094                                  }
3094 3095  
3095 3096                          }
3096 3097                          if (error == 0) {
3097 3098                                  vnevent_create(ITOV(ip), ct);
3098 3099                          }
3099 3100                  }
3100 3101          }
3101 3102  
3102 3103          if (error) {
3103 3104                  if (ip != NULL) {
3104 3105                          rw_exit(&ufsvfsp->vfs_dqrwlock);
3105 3106                          rw_exit(&ip->i_contents);
3106 3107                  }
3107 3108                  goto unlock;
3108 3109          }
3109 3110  
3110 3111          *vpp = ITOV(ip);
3111 3112          ITIMES(ip);
3112 3113          rw_exit(&ip->i_contents);
3113 3114          rw_exit(&ufsvfsp->vfs_dqrwlock);
3114 3115  
3115 3116          /*
3116 3117           * If vnode is a device return special vnode instead.
3117 3118           */
3118 3119          if (!error && IS_DEVVP(*vpp)) {
3119 3120                  struct vnode *newvp;
3120 3121  
3121 3122                  newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
3122 3123                  VN_RELE(*vpp);
3123 3124                  if (newvp == NULL) {
3124 3125                          error = ENOSYS;
3125 3126                          goto unlock;
3126 3127                  }
3127 3128                  truncflag = 0;
3128 3129                  *vpp = newvp;
3129 3130          }
3130 3131  unlock:
3131 3132  
3132 3133          /*
3133 3134           * Do the deferred update of the parent directory's sequence
3134 3135           * number now.
3135 3136           */
3136 3137          if (defer_dip_seq_update == 1) {
3137 3138                  rw_enter(&dip->i_contents, RW_READER);
3138 3139                  mutex_enter(&dip->i_tlock);
3139 3140                  dip->i_seq++;
3140 3141                  mutex_exit(&dip->i_tlock);
3141 3142                  rw_exit(&dip->i_contents);
3142 3143          }
3143 3144  
3144 3145          if (ulp) {
3145 3146                  int terr = 0;
3146 3147  
3147 3148                  TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_CREATE,
3148 3149                      trans_size);
3149 3150  
3150 3151                  /*
3151 3152                   * If we haven't had a more interesting failure
3152 3153                   * already, then anything that might've happened
3153 3154                   * here should be reported.
3154 3155                   */
3155 3156                  if (error == 0)
3156 3157                          error = terr;
3157 3158          }
3158 3159  
3159 3160          if (!error && truncflag) {
3160 3161                  ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_trunc);
3161 3162                  if (indeadlock) {
3162 3163                          if (ulp)
3163 3164                                  ufs_lockfs_end(ulp);
3164 3165                          VN_RELE(ITOV(ip));
3165 3166                          goto again;
3166 3167                  }
3167 3168                  (void) TRANS_ITRUNC(ip, (u_offset_t)0, 0, cr);
3168 3169                  rw_exit(&ip->i_rwlock);
3169 3170          }
3170 3171  
3171 3172          if (ulp)
3172 3173                  ufs_lockfs_end(ulp);
3173 3174  
3174 3175          /*
3175 3176           * If no inodes available, try to free one up out of the
3176 3177           * pending delete queue.
3177 3178           */
3178 3179          if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
3179 3180                  ufs_delete_drain_wait(ufsvfsp, 1);
3180 3181                  retry = 0;
3181 3182                  goto again;
3182 3183          }
3183 3184  
3184 3185  out:
3185 3186          return (error);
3186 3187  }
3187 3188  
3188 3189  extern int ufs_idle_max;
3189 3190  /*ARGSUSED*/
3190 3191  static int
3191 3192  ufs_remove(struct vnode *vp, char *nm, struct cred *cr, caller_context_t *ct,
3192 3193      int flags)
3193 3194  {
3194 3195          struct inode *ip = VTOI(vp);
3195 3196          struct ufsvfs *ufsvfsp  = ip->i_ufsvfs;
3196 3197          struct ulockfs *ulp;
3197 3198          vnode_t *rmvp = NULL;   /* Vnode corresponding to name being removed */
3198 3199          int indeadlock;
3199 3200          int error;
3200 3201          int issync;
3201 3202          int trans_size;
3202 3203  
3203 3204          /*
3204 3205           * don't let the delete queue get too long
3205 3206           */
3206 3207          if (ufsvfsp == NULL) {
3207 3208                  error = EIO;
3208 3209                  goto out;
3209 3210          }
3210 3211          if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
3211 3212                  ufs_delete_drain(vp->v_vfsp, 1, 1);
3212 3213  
3213 3214          error = ufs_eventlookup(vp, nm, cr, &rmvp);
3214 3215          if (rmvp != NULL) {
3215 3216                  /* Only send the event if there were no errors */
3216 3217                  if (error == 0)
3217 3218                          vnevent_remove(rmvp, vp, nm, ct);
3218 3219                  VN_RELE(rmvp);
3219 3220          }
3220 3221  
3221 3222  retry_remove:
3222 3223          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_REMOVE_MASK);
3223 3224          if (error)
3224 3225                  goto out;
3225 3226  
3226 3227          if (ulp)
3227 3228                  TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_REMOVE,
3228 3229                      trans_size = (int)TOP_REMOVE_SIZE(VTOI(vp)));
3229 3230  
3230 3231          /*
3231 3232           * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3232 3233           * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3233 3234           * possible, retries the operation.
3234 3235           */
3235 3236          ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_REMOVE, retry);
3236 3237          if (indeadlock)
3237 3238                  goto retry_remove;
3238 3239          error = ufs_dirremove(ip, nm, (struct inode *)0, (struct vnode *)0,
3239 3240              DR_REMOVE, cr);
3240 3241          rw_exit(&ip->i_rwlock);
3241 3242  
3242 3243          if (ulp) {
3243 3244                  TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_REMOVE, trans_size);
3244 3245                  ufs_lockfs_end(ulp);
3245 3246          }
3246 3247  
3247 3248  out:
3248 3249          return (error);
3249 3250  }
3250 3251  
3251 3252  /*
3252 3253   * Link a file or a directory.  Only privileged processes are allowed to
3253 3254   * make links to directories.
3254 3255   */
3255 3256  /*ARGSUSED*/
3256 3257  static int
3257 3258  ufs_link(struct vnode *tdvp, struct vnode *svp, char *tnm, struct cred *cr,
3258 3259      caller_context_t *ct, int flags)
3259 3260  {
3260 3261          struct inode *sip;
3261 3262          struct inode *tdp = VTOI(tdvp);
3262 3263          struct ufsvfs *ufsvfsp = tdp->i_ufsvfs;
3263 3264          struct ulockfs *ulp;
3264 3265          struct vnode *realvp;
3265 3266          int error;
3266 3267          int issync;
3267 3268          int trans_size;
3268 3269          int isdev;
3269 3270          int indeadlock;
3270 3271  
3271 3272  retry_link:
3272 3273          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LINK_MASK);
3273 3274          if (error)
3274 3275                  goto out;
3275 3276  
3276 3277          if (ulp)
3277 3278                  TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_LINK,
3278 3279                      trans_size = (int)TOP_LINK_SIZE(VTOI(tdvp)));
3279 3280  
3280 3281          if (VOP_REALVP(svp, &realvp, ct) == 0)
3281 3282                  svp = realvp;
3282 3283  
3283 3284          /*
3284 3285           * Make sure link for extended attributes is valid
3285 3286           * We only support hard linking of attr in ATTRDIR to ATTRDIR
3286 3287           *
3287 3288           * Make certain we don't attempt to look at a device node as
3288 3289           * a ufs inode.
3289 3290           */
3290 3291  
3291 3292          isdev = IS_DEVVP(svp);
3292 3293          if (((isdev == 0) && ((VTOI(svp)->i_cflags & IXATTR) == 0) &&
3293 3294              ((tdp->i_mode & IFMT) == IFATTRDIR)) ||
3294 3295              ((isdev == 0) && (VTOI(svp)->i_cflags & IXATTR) &&
3295 3296              ((tdp->i_mode & IFMT) == IFDIR))) {
3296 3297                  error = EINVAL;
3297 3298                  goto unlock;
3298 3299          }
3299 3300  
3300 3301          sip = VTOI(svp);
3301 3302          if ((svp->v_type == VDIR &&
3302 3303              secpolicy_fs_linkdir(cr, ufsvfsp->vfs_vfs) != 0) ||
3303 3304              (sip->i_uid != crgetuid(cr) && secpolicy_basic_link(cr) != 0)) {
3304 3305                  error = EPERM;
3305 3306                  goto unlock;
3306 3307          }
3307 3308  
3308 3309          /*
3309 3310           * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3310 3311           * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3311 3312           * possible, retries the operation.
3312 3313           */
3313 3314          ufs_tryirwlock_trans(&tdp->i_rwlock, RW_WRITER, TOP_LINK, retry);
3314 3315          if (indeadlock)
3315 3316                  goto retry_link;
3316 3317          error = ufs_direnter_lr(tdp, tnm, DE_LINK, (struct inode *)0,
3317 3318              sip, cr);
3318 3319          rw_exit(&tdp->i_rwlock);
3319 3320  
3320 3321  unlock:
3321 3322          if (ulp) {
3322 3323                  TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_LINK, trans_size);
3323 3324                  ufs_lockfs_end(ulp);
3324 3325          }
3325 3326  
3326 3327          if (!error) {
3327 3328                  vnevent_link(svp, ct);
3328 3329          }
3329 3330  out:
3330 3331          return (error);
3331 3332  }
3332 3333  
3333 3334  uint64_t ufs_rename_retry_cnt;
3334 3335  uint64_t ufs_rename_upgrade_retry_cnt;
3335 3336  uint64_t ufs_rename_dircheck_retry_cnt;
3336 3337  clock_t  ufs_rename_backoff_delay = 1;
3337 3338  
3338 3339  /*
3339 3340   * Rename a file or directory.
3340 3341   * We are given the vnode and entry string of the source and the
3341 3342   * vnode and entry string of the place we want to move the source
3342 3343   * to (the target). The essential operation is:
3343 3344   *      unlink(target);
3344 3345   *      link(source, target);
3345 3346   *      unlink(source);
3346 3347   * but "atomically".  Can't do full commit without saving state in
3347 3348   * the inode on disk, which isn't feasible at this time.  Best we
3348 3349   * can do is always guarantee that the TARGET exists.
3349 3350   */
3350 3351  
3351 3352  /*ARGSUSED*/
3352 3353  static int
3353 3354  ufs_rename(struct vnode *sdvp, char *snm, struct vnode *tdvp, char *tnm,
3354 3355      struct cred *cr, caller_context_t *ct, int flags)
3355 3356  {
3356 3357          struct inode *sip = NULL;       /* source inode */
3357 3358          struct inode *ip = NULL;        /* check inode */
3358 3359          struct inode *sdp;              /* old (source) parent inode */
3359 3360          struct inode *tdp;              /* new (target) parent inode */
3360 3361          struct vnode *svp = NULL;       /* source vnode */
3361 3362          struct vnode *tvp = NULL;       /* target vnode, if it exists */
3362 3363          struct vnode *realvp;
3363 3364          struct ufsvfs *ufsvfsp;
3364 3365          struct ulockfs *ulp = NULL;
3365 3366          struct ufs_slot slot;
3366 3367          timestruc_t now;
3367 3368          int error;
3368 3369          int issync;
3369 3370          int trans_size;
3370 3371          krwlock_t *first_lock;
3371 3372          krwlock_t *second_lock;
3372 3373          krwlock_t *reverse_lock;
3373 3374          int serr, terr;
3374 3375  
3375 3376          sdp = VTOI(sdvp);
3376 3377          slot.fbp = NULL;
3377 3378          ufsvfsp = sdp->i_ufsvfs;
3378 3379  
3379 3380          if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3380 3381                  tdvp = realvp;
3381 3382  
3382 3383          /* Must do this before taking locks in case of DNLC miss */
3383 3384          terr = ufs_eventlookup(tdvp, tnm, cr, &tvp);
3384 3385          serr = ufs_eventlookup(sdvp, snm, cr, &svp);
3385 3386  
3386 3387          if ((serr == 0) && ((terr == 0) || (terr == ENOENT))) {
3387 3388                  if (tvp != NULL)
3388 3389                          vnevent_pre_rename_dest(tvp, tdvp, tnm, ct);
3389 3390  
3390 3391                  /*
3391 3392                   * Notify the target directory of the rename event
3392 3393                   * if source and target directories are not the same.
3393 3394                   */
3394 3395                  if (sdvp != tdvp)
3395 3396                          vnevent_pre_rename_dest_dir(tdvp, svp, tnm, ct);
3396 3397  
3397 3398                  if (svp != NULL)
3398 3399                          vnevent_pre_rename_src(svp, sdvp, snm, ct);
3399 3400          }
3400 3401  
3401 3402          if (svp != NULL)
3402 3403                  VN_RELE(svp);
3403 3404  
3404 3405  retry_rename:
3405 3406          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RENAME_MASK);
3406 3407          if (error)
3407 3408                  goto unlock;
3408 3409  
3409 3410          if (ulp)
3410 3411                  TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RENAME,
3411 3412                      trans_size = (int)TOP_RENAME_SIZE(sdp));
3412 3413  
3413 3414          if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3414 3415                  tdvp = realvp;
3415 3416  
3416 3417          tdp = VTOI(tdvp);
3417 3418  
3418 3419          /*
3419 3420           * We only allow renaming of attributes from ATTRDIR to ATTRDIR.
3420 3421           */
3421 3422          if ((tdp->i_mode & IFMT) != (sdp->i_mode & IFMT)) {
3422 3423                  error = EINVAL;
3423 3424                  goto unlock;
3424 3425          }
3425 3426  
3426 3427          /*
3427 3428           * Check accessibility of directory.
3428 3429           */
3429 3430          if (error = ufs_diraccess(sdp, IEXEC, cr))
3430 3431                  goto unlock;
3431 3432  
3432 3433          /*
3433 3434           * Look up inode of file we're supposed to rename.
3434 3435           */
3435 3436          gethrestime(&now);
3436 3437          if (error = ufs_dirlook(sdp, snm, &sip, cr, 0, 0)) {
3437 3438                  if (error == EAGAIN) {
3438 3439                          if (ulp) {
3439 3440                                  TRANS_END_CSYNC(ufsvfsp, error, issync,
3440 3441                                      TOP_RENAME, trans_size);
3441 3442                                  ufs_lockfs_end(ulp);
3442 3443                          }
3443 3444                          goto retry_rename;
3444 3445                  }
3445 3446  
3446 3447                  goto unlock;
3447 3448          }
3448 3449  
3449 3450          /*
3450 3451           * Lock both the source and target directories (they may be
3451 3452           * the same) to provide the atomicity semantics that was
3452 3453           * previously provided by the per file system vfs_rename_lock
3453 3454           *
3454 3455           * with vfs_rename_lock removed to allow simultaneous renames
3455 3456           * within a file system, ufs_dircheckpath can deadlock while
3456 3457           * traversing back to ensure that source is not a parent directory
3457 3458           * of target parent directory. This is because we get into
3458 3459           * ufs_dircheckpath with the sdp and tdp locks held as RW_WRITER.
3459 3460           * If the tdp and sdp of the simultaneous renames happen to be
3460 3461           * in the path of each other, it can lead to a deadlock. This
3461 3462           * can be avoided by getting the locks as RW_READER here and then
3462 3463           * upgrading to RW_WRITER after completing the ufs_dircheckpath.
3463 3464           *
3464 3465           * We hold the target directory's i_rwlock after calling
3465 3466           * ufs_lockfs_begin but in many other operations (like ufs_readdir)
3466 3467           * VOP_RWLOCK is explicitly called by the filesystem independent code
3467 3468           * before calling the file system operation. In these cases the order
3468 3469           * is reversed (i.e i_rwlock is taken first and then ufs_lockfs_begin
3469 3470           * is called). This is fine as long as ufs_lockfs_begin acts as a VOP
3470 3471           * counter but with ufs_quiesce setting the SLOCK bit this becomes a
3471 3472           * synchronizing object which might lead to a deadlock. So we use
3472 3473           * rw_tryenter instead of rw_enter. If we fail to get this lock and
3473 3474           * find that SLOCK bit is set, we call ufs_lockfs_end and restart the
3474 3475           * operation.
3475 3476           */
3476 3477  retry:
3477 3478          first_lock = &tdp->i_rwlock;
3478 3479          second_lock = &sdp->i_rwlock;
3479 3480  retry_firstlock:
3480 3481          if (!rw_tryenter(first_lock, RW_READER)) {
3481 3482                  /*
3482 3483                   * We didn't get the lock. Check if the SLOCK is set in the
3483 3484                   * ufsvfs. If yes, we might be in a deadlock. Safer to give up
3484 3485                   * and wait for SLOCK to be cleared.
3485 3486                   */
3486 3487  
3487 3488                  if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
3488 3489                          TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
3489 3490                              trans_size);
3490 3491                          ufs_lockfs_end(ulp);
3491 3492                          goto retry_rename;
3492 3493  
3493 3494                  } else {
3494 3495                          /*
3495 3496                           * SLOCK isn't set so this is a genuine synchronization
3496 3497                           * case. Let's try again after giving them a breather.
3497 3498                           */
3498 3499                          delay(RETRY_LOCK_DELAY);
3499 3500                          goto  retry_firstlock;
3500 3501                  }
3501 3502          }
3502 3503          /*
3503 3504           * Need to check if the tdp and sdp are same !!!
3504 3505           */
3505 3506          if ((tdp != sdp) && (!rw_tryenter(second_lock, RW_READER))) {
3506 3507                  /*
3507 3508                   * We didn't get the lock. Check if the SLOCK is set in the
3508 3509                   * ufsvfs. If yes, we might be in a deadlock. Safer to give up
3509 3510                   * and wait for SLOCK to be cleared.
3510 3511                   */
3511 3512  
3512 3513                  rw_exit(first_lock);
3513 3514                  if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
3514 3515                          TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
3515 3516                              trans_size);
3516 3517                          ufs_lockfs_end(ulp);
3517 3518                          goto retry_rename;
3518 3519  
3519 3520                  } else {
3520 3521                          /*
3521 3522                           * So we couldn't get the second level peer lock *and*
3522 3523                           * the SLOCK bit isn't set. Too bad we can be
3523 3524                           * contentding with someone wanting these locks otherway
3524 3525                           * round. Reverse the locks in case there is a heavy
3525 3526                           * contention for the second level lock.
3526 3527                           */
3527 3528                          reverse_lock = first_lock;
3528 3529                          first_lock = second_lock;
3529 3530                          second_lock = reverse_lock;
3530 3531                          ufs_rename_retry_cnt++;
3531 3532                          goto  retry_firstlock;
3532 3533                  }
3533 3534          }
3534 3535  
3535 3536          if (sip == tdp) {
3536 3537                  error = EINVAL;
3537 3538                  goto errout;
3538 3539          }
3539 3540          /*
3540 3541           * Make sure we can delete the source entry.  This requires
3541 3542           * write permission on the containing directory.
3542 3543           * Check for sticky directories.
3543 3544           */
3544 3545          rw_enter(&sdp->i_contents, RW_READER);
3545 3546          rw_enter(&sip->i_contents, RW_READER);
3546 3547          if ((error = ufs_iaccess(sdp, IWRITE, cr, 0)) != 0 ||
3547 3548              (error = ufs_sticky_remove_access(sdp, sip, cr)) != 0) {
3548 3549                  rw_exit(&sip->i_contents);
3549 3550                  rw_exit(&sdp->i_contents);
3550 3551                  goto errout;
3551 3552          }
3552 3553  
3553 3554          /*
3554 3555           * If this is a rename of a directory and the parent is
3555 3556           * different (".." must be changed), then the source
3556 3557           * directory must not be in the directory hierarchy
3557 3558           * above the target, as this would orphan everything
3558 3559           * below the source directory.  Also the user must have
3559 3560           * write permission in the source so as to be able to
3560 3561           * change "..".
3561 3562           */
3562 3563          if ((((sip->i_mode & IFMT) == IFDIR) ||
3563 3564              ((sip->i_mode & IFMT) == IFATTRDIR)) && sdp != tdp) {
3564 3565                  ino_t   inum;
3565 3566  
3566 3567                  if (error = ufs_iaccess(sip, IWRITE, cr, 0)) {
3567 3568                          rw_exit(&sip->i_contents);
3568 3569                          rw_exit(&sdp->i_contents);
3569 3570                          goto errout;
3570 3571                  }
3571 3572                  inum = sip->i_number;
3572 3573                  rw_exit(&sip->i_contents);
3573 3574                  rw_exit(&sdp->i_contents);
3574 3575                  if ((error = ufs_dircheckpath(inum, tdp, sdp, cr))) {
3575 3576                          /*
3576 3577                           * If we got EAGAIN ufs_dircheckpath detected a
3577 3578                           * potential deadlock and backed out. We need
3578 3579                           * to retry the operation since sdp and tdp have
3579 3580                           * to be released to avoid the deadlock.
3580 3581                           */
3581 3582                          if (error == EAGAIN) {
3582 3583                                  rw_exit(&tdp->i_rwlock);
3583 3584                                  if (tdp != sdp)
3584 3585                                          rw_exit(&sdp->i_rwlock);
3585 3586                                  delay(ufs_rename_backoff_delay);
3586 3587                                  ufs_rename_dircheck_retry_cnt++;
3587 3588                                  goto retry;
3588 3589                          }
3589 3590                          goto errout;
3590 3591                  }
3591 3592          } else {
3592 3593                  rw_exit(&sip->i_contents);
3593 3594                  rw_exit(&sdp->i_contents);
3594 3595          }
3595 3596  
3596 3597  
3597 3598          /*
3598 3599           * Check for renaming '.' or '..' or alias of '.'
3599 3600           */
3600 3601          if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0 || sdp == sip) {
3601 3602                  error = EINVAL;
3602 3603                  goto errout;
3603 3604          }
3604 3605  
3605 3606          /*
3606 3607           * Simultaneous renames can deadlock in ufs_dircheckpath since it
3607 3608           * tries to traverse back the file tree with both tdp and sdp held
3608 3609           * as RW_WRITER. To avoid that we have to hold the tdp and sdp locks
3609 3610           * as RW_READERS  till ufs_dircheckpath is done.
3610 3611           * Now that ufs_dircheckpath is done with, we can upgrade the locks
3611 3612           * to RW_WRITER.
3612 3613           */
3613 3614          if (!rw_tryupgrade(&tdp->i_rwlock)) {
3614 3615                  /*
3615 3616                   * The upgrade failed. We got to give away the lock
3616 3617                   * as to avoid deadlocking with someone else who is
3617 3618                   * waiting for writer lock. With the lock gone, we
3618 3619                   * cannot be sure the checks done above will hold
3619 3620                   * good when we eventually get them back as writer.
3620 3621                   * So if we can't upgrade we drop the locks and retry
3621 3622                   * everything again.
3622 3623                   */
3623 3624                  rw_exit(&tdp->i_rwlock);
3624 3625                  if (tdp != sdp)
3625 3626                          rw_exit(&sdp->i_rwlock);
3626 3627                  delay(ufs_rename_backoff_delay);
3627 3628                  ufs_rename_upgrade_retry_cnt++;
3628 3629                  goto retry;
3629 3630          }
3630 3631          if (tdp != sdp) {
3631 3632                  if (!rw_tryupgrade(&sdp->i_rwlock)) {
3632 3633                          /*
3633 3634                           * The upgrade failed. We got to give away the lock
3634 3635                           * as to avoid deadlocking with someone else who is
3635 3636                           * waiting for writer lock. With the lock gone, we
3636 3637                           * cannot be sure the checks done above will hold
3637 3638                           * good when we eventually get them back as writer.
3638 3639                           * So if we can't upgrade we drop the locks and retry
3639 3640                           * everything again.
3640 3641                           */
3641 3642                          rw_exit(&tdp->i_rwlock);
3642 3643                          rw_exit(&sdp->i_rwlock);
3643 3644                          delay(ufs_rename_backoff_delay);
3644 3645                          ufs_rename_upgrade_retry_cnt++;
3645 3646                          goto retry;
3646 3647                  }
3647 3648          }
3648 3649  
3649 3650          /*
3650 3651           * Now that all the locks are held check to make sure another thread
3651 3652           * didn't slip in and take out the sip.
3652 3653           */
3653 3654          slot.status = NONE;
3654 3655          if ((sip->i_ctime.tv_usec * 1000) > now.tv_nsec ||
3655 3656              sip->i_ctime.tv_sec > now.tv_sec) {
3656 3657                  rw_enter(&sdp->i_ufsvfs->vfs_dqrwlock, RW_READER);
3657 3658                  rw_enter(&sdp->i_contents, RW_WRITER);
3658 3659                  error = ufs_dircheckforname(sdp, snm, strlen(snm), &slot,
3659 3660                      &ip, cr, 0);
3660 3661                  rw_exit(&sdp->i_contents);
3661 3662                  rw_exit(&sdp->i_ufsvfs->vfs_dqrwlock);
3662 3663                  if (error) {
3663 3664                          goto errout;
3664 3665                  }
3665 3666                  if (ip == NULL) {
3666 3667                          error = ENOENT;
3667 3668                          goto errout;
3668 3669                  } else {
3669 3670                          /*
3670 3671                           * If the inode was found need to drop the v_count
3671 3672                           * so as not to keep the filesystem from being
3672 3673                           * unmounted at a later time.
3673 3674                           */
3674 3675                          VN_RELE(ITOV(ip));
3675 3676                  }
3676 3677  
3677 3678                  /*
3678 3679                   * Release the slot.fbp that has the page mapped and
3679 3680                   * locked SE_SHARED, and could be used in in
3680 3681                   * ufs_direnter_lr() which needs to get the SE_EXCL lock
3681 3682                   * on said page.
3682 3683                   */
3683 3684                  if (slot.fbp) {
3684 3685                          fbrelse(slot.fbp, S_OTHER);
3685 3686                          slot.fbp = NULL;
3686 3687                  }
3687 3688          }
3688 3689  
3689 3690          /*
3690 3691           * Link source to the target.
3691 3692           */
3692 3693          if (error = ufs_direnter_lr(tdp, tnm, DE_RENAME, sdp, sip, cr)) {
3693 3694                  /*
3694 3695                   * ESAME isn't really an error; it indicates that the
3695 3696                   * operation should not be done because the source and target
3696 3697                   * are the same file, but that no error should be reported.
3697 3698                   */
3698 3699                  if (error == ESAME)
3699 3700                          error = 0;
3700 3701                  goto errout;
3701 3702          }
3702 3703  
3703 3704          if (error == 0 && tvp != NULL)
3704 3705                  vnevent_rename_dest(tvp, tdvp, tnm, ct);
3705 3706  
3706 3707          /*
3707 3708           * Unlink the source.
3708 3709           * Remove the source entry.  ufs_dirremove() checks that the entry
3709 3710           * still reflects sip, and returns an error if it doesn't.
3710 3711           * If the entry has changed just forget about it.  Release
3711 3712           * the source inode.
3712 3713           */
3713 3714          if ((error = ufs_dirremove(sdp, snm, sip, (struct vnode *)0,
3714 3715              DR_RENAME, cr)) == ENOENT)
3715 3716                  error = 0;
3716 3717  
3717 3718          if (error == 0) {
3718 3719                  vnevent_rename_src(ITOV(sip), sdvp, snm, ct);
3719 3720                  /*
3720 3721                   * Notify the target directory of the rename event
3721 3722                   * if source and target directories are not the same.
3722 3723                   */
3723 3724                  if (sdvp != tdvp)
3724 3725                          vnevent_rename_dest_dir(tdvp, ct);
3725 3726          }
3726 3727  
3727 3728  errout:
3728 3729          if (slot.fbp)
3729 3730                  fbrelse(slot.fbp, S_OTHER);
3730 3731  
3731 3732          rw_exit(&tdp->i_rwlock);
3732 3733          if (sdp != tdp) {
3733 3734                  rw_exit(&sdp->i_rwlock);
3734 3735          }
3735 3736  
3736 3737  unlock:
3737 3738          if (tvp != NULL)
3738 3739                  VN_RELE(tvp);
3739 3740          if (sip != NULL)
3740 3741                  VN_RELE(ITOV(sip));
3741 3742  
3742 3743          if (ulp) {
3743 3744                  TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME, trans_size);
3744 3745                  ufs_lockfs_end(ulp);
3745 3746          }
3746 3747  
3747 3748          return (error);
3748 3749  }
3749 3750  
3750 3751  /*ARGSUSED*/
3751 3752  static int
3752 3753  ufs_mkdir(struct vnode *dvp, char *dirname, struct vattr *vap,
3753 3754      struct vnode **vpp, struct cred *cr, caller_context_t *ct, int flags,
3754 3755      vsecattr_t *vsecp)
3755 3756  {
3756 3757          struct inode *ip;
3757 3758          struct inode *xip;
3758 3759          struct ufsvfs *ufsvfsp;
3759 3760          struct ulockfs *ulp;
3760 3761          int error;
3761 3762          int issync;
3762 3763          int trans_size;
3763 3764          int indeadlock;
3764 3765          int retry = 1;
3765 3766  
3766 3767          ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
3767 3768  
3768 3769          /*
3769 3770           * Can't make directory in attr hidden dir
3770 3771           */
3771 3772          if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
3772 3773                  return (EINVAL);
3773 3774  
3774 3775  again:
3775 3776          ip = VTOI(dvp);
3776 3777          ufsvfsp = ip->i_ufsvfs;
3777 3778          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_MKDIR_MASK);
3778 3779          if (error)
3779 3780                  goto out;
3780 3781          if (ulp)
3781 3782                  TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_MKDIR,
3782 3783                      trans_size = (int)TOP_MKDIR_SIZE(ip));
3783 3784  
3784 3785          /*
3785 3786           * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3786 3787           * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3787 3788           * possible, retries the operation.
3788 3789           */
3789 3790          ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_MKDIR, retry);
3790 3791          if (indeadlock)
3791 3792                  goto again;
3792 3793  
3793 3794          error = ufs_direnter_cm(ip, dirname, DE_MKDIR, vap, &xip, cr,
3794 3795              (retry ? IQUIET : 0));
3795 3796          if (error == EAGAIN) {
3796 3797                  if (ulp) {
3797 3798                          TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_MKDIR,
3798 3799                              trans_size);
3799 3800                          ufs_lockfs_end(ulp);
3800 3801                  }
3801 3802                  goto again;
3802 3803          }
3803 3804  
3804 3805          rw_exit(&ip->i_rwlock);
3805 3806          if (error == 0) {
3806 3807                  ip = xip;
3807 3808                  *vpp = ITOV(ip);
3808 3809          } else if (error == EEXIST)
3809 3810                  VN_RELE(ITOV(xip));
3810 3811  
3811 3812          if (ulp) {
3812 3813                  int terr = 0;
3813 3814                  TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_MKDIR, trans_size);
3814 3815                  ufs_lockfs_end(ulp);
3815 3816                  if (error == 0)
3816 3817                          error = terr;
3817 3818          }
3818 3819  out:
3819 3820          if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
3820 3821                  ufs_delete_drain_wait(ufsvfsp, 1);
3821 3822                  retry = 0;
3822 3823                  goto again;
3823 3824          }
3824 3825  
3825 3826          return (error);
3826 3827  }
3827 3828  
3828 3829  /*ARGSUSED*/
3829 3830  static int
3830 3831  ufs_rmdir(struct vnode *vp, char *nm, struct vnode *cdir, struct cred *cr,
3831 3832      caller_context_t *ct, int flags)
3832 3833  {
3833 3834          struct inode *ip = VTOI(vp);
3834 3835          struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
3835 3836          struct ulockfs *ulp;
3836 3837          vnode_t *rmvp = NULL;   /* Vnode of removed directory */
3837 3838          int error;
3838 3839          int issync;
3839 3840          int trans_size;
3840 3841          int indeadlock;
3841 3842  
3842 3843          /*
3843 3844           * don't let the delete queue get too long
3844 3845           */
3845 3846          if (ufsvfsp == NULL) {
3846 3847                  error = EIO;
3847 3848                  goto out;
3848 3849          }
3849 3850          if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
3850 3851                  ufs_delete_drain(vp->v_vfsp, 1, 1);
3851 3852  
3852 3853          error = ufs_eventlookup(vp, nm, cr, &rmvp);
3853 3854          if (rmvp != NULL) {
3854 3855                  /* Only send the event if there were no errors */
3855 3856                  if (error == 0)
3856 3857                          vnevent_rmdir(rmvp, vp, nm, ct);
3857 3858                  VN_RELE(rmvp);
3858 3859          }
3859 3860  
3860 3861  retry_rmdir:
3861 3862          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RMDIR_MASK);
3862 3863          if (error)
3863 3864                  goto out;
3864 3865  
3865 3866          if (ulp)
3866 3867                  TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RMDIR,
3867 3868                      trans_size = TOP_RMDIR_SIZE);
3868 3869  
3869 3870          /*
3870 3871           * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3871 3872           * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3872 3873           * possible, retries the operation.
3873 3874           */
3874 3875          ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_RMDIR, retry);
3875 3876          if (indeadlock)
3876 3877                  goto retry_rmdir;
3877 3878          error = ufs_dirremove(ip, nm, (struct inode *)0, cdir, DR_RMDIR, cr);
3878 3879  
3879 3880          rw_exit(&ip->i_rwlock);
3880 3881  
3881 3882          if (ulp) {
3882 3883                  TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RMDIR,
3883 3884                      trans_size);
3884 3885                  ufs_lockfs_end(ulp);
3885 3886          }
3886 3887  
3887 3888  out:
3888 3889          return (error);
3889 3890  }
3890 3891  
3891 3892  /* ARGSUSED */
3892 3893  static int
3893 3894  ufs_readdir(struct vnode *vp, struct uio *uiop, struct cred *cr, int *eofp,
3894 3895      caller_context_t *ct, int flags)
3895 3896  {
3896 3897          struct iovec *iovp;
3897 3898          struct inode *ip;
3898 3899          struct direct *idp;
3899 3900          struct dirent64 *odp;
3900 3901          struct fbuf *fbp;
3901 3902          struct ufsvfs *ufsvfsp;
3902 3903          struct ulockfs *ulp;
3903 3904          caddr_t outbuf;
3904 3905          size_t bufsize;
3905 3906          uint_t offset;
3906 3907          uint_t bytes_wanted, total_bytes_wanted;
3907 3908          int incount = 0;
3908 3909          int outcount = 0;
3909 3910          int error;
3910 3911  
3911 3912          ip = VTOI(vp);
3912 3913          ASSERT(RW_READ_HELD(&ip->i_rwlock));
3913 3914  
3914 3915          if (uiop->uio_loffset >= MAXOFF32_T) {
3915 3916                  if (eofp)
3916 3917                          *eofp = 1;
3917 3918                  return (0);
3918 3919          }
3919 3920  
3920 3921          /*
3921 3922           * Check if we have been called with a valid iov_len
3922 3923           * and bail out if not, otherwise we may potentially loop
3923 3924           * forever further down.
3924 3925           */
3925 3926          if (uiop->uio_iov->iov_len <= 0) {
3926 3927                  error = EINVAL;
3927 3928                  goto out;
3928 3929          }
3929 3930  
3930 3931          /*
3931 3932           * Large Files: When we come here we are guaranteed that
3932 3933           * uio_offset can be used safely. The high word is zero.
3933 3934           */
3934 3935  
3935 3936          ufsvfsp = ip->i_ufsvfs;
3936 3937          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READDIR_MASK);
3937 3938          if (error)
3938 3939                  goto out;
3939 3940  
3940 3941          iovp = uiop->uio_iov;
3941 3942          total_bytes_wanted = iovp->iov_len;
3942 3943  
3943 3944          /* Large Files: directory files should not be "large" */
3944 3945  
3945 3946          ASSERT(ip->i_size <= MAXOFF32_T);
3946 3947  
3947 3948          /* Force offset to be valid (to guard against bogus lseek() values) */
3948 3949          offset = (uint_t)uiop->uio_offset & ~(DIRBLKSIZ - 1);
3949 3950  
3950 3951          /* Quit if at end of file or link count of zero (posix) */
3951 3952          if (offset >= (uint_t)ip->i_size || ip->i_nlink <= 0) {
3952 3953                  if (eofp)
3953 3954                          *eofp = 1;
3954 3955                  error = 0;
3955 3956                  goto unlock;
3956 3957          }
3957 3958  
3958 3959          /*
3959 3960           * Get space to change directory entries into fs independent format.
3960 3961           * Do fast alloc for the most commonly used-request size (filesystem
3961 3962           * block size).
3962 3963           */
3963 3964          if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1) {
3964 3965                  bufsize = total_bytes_wanted;
3965 3966                  outbuf = kmem_alloc(bufsize, KM_SLEEP);
3966 3967                  odp = (struct dirent64 *)outbuf;
3967 3968          } else {
3968 3969                  bufsize = total_bytes_wanted;
3969 3970                  odp = (struct dirent64 *)iovp->iov_base;
3970 3971          }
3971 3972  
3972 3973  nextblk:
3973 3974          bytes_wanted = total_bytes_wanted;
3974 3975  
3975 3976          /* Truncate request to file size */
3976 3977          if (offset + bytes_wanted > (int)ip->i_size)
3977 3978                  bytes_wanted = (int)(ip->i_size - offset);
3978 3979  
3979 3980          /* Comply with MAXBSIZE boundary restrictions of fbread() */
3980 3981          if ((offset & MAXBOFFSET) + bytes_wanted > MAXBSIZE)
3981 3982                  bytes_wanted = MAXBSIZE - (offset & MAXBOFFSET);
3982 3983  
3983 3984          /*
3984 3985           * Read in the next chunk.
3985 3986           * We are still holding the i_rwlock.
3986 3987           */
3987 3988          error = fbread(vp, (offset_t)offset, bytes_wanted, S_OTHER, &fbp);
3988 3989  
3989 3990          if (error)
3990 3991                  goto update_inode;
3991 3992          if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (ip->i_fs->fs_ronly == 0) &&
3992 3993              (!ufsvfsp->vfs_noatime)) {
3993 3994                  ip->i_flag |= IACC;
3994 3995          }
3995 3996          incount = 0;
3996 3997          idp = (struct direct *)fbp->fb_addr;
3997 3998          if (idp->d_ino == 0 && idp->d_reclen == 0 && idp->d_namlen == 0) {
3998 3999                  cmn_err(CE_WARN, "ufs_readdir: bad dir, inumber = %llu, "
3999 4000                      "fs = %s\n",
4000 4001                      (u_longlong_t)ip->i_number, ufsvfsp->vfs_fs->fs_fsmnt);
4001 4002                  fbrelse(fbp, S_OTHER);
4002 4003                  error = ENXIO;
4003 4004                  goto update_inode;
4004 4005          }
4005 4006          /* Transform to file-system independent format */
4006 4007          while (incount < bytes_wanted) {
4007 4008                  /*
4008 4009                   * If the current directory entry is mangled, then skip
4009 4010                   * to the next block.  It would be nice to set the FSBAD
4010 4011                   * flag in the super-block so that a fsck is forced on
4011 4012                   * next reboot, but locking is a problem.
4012 4013                   */
4013 4014                  if (idp->d_reclen & 0x3) {
4014 4015                          offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
4015 4016                          break;
4016 4017                  }
4017 4018  
4018 4019                  /* Skip to requested offset and skip empty entries */
4019 4020                  if (idp->d_ino != 0 && offset >= (uint_t)uiop->uio_offset) {
4020 4021                          ushort_t this_reclen =
4021 4022                              DIRENT64_RECLEN(idp->d_namlen);
4022 4023                          /* Buffer too small for any entries */
4023 4024                          if (!outcount && this_reclen > bufsize) {
4024 4025                                  fbrelse(fbp, S_OTHER);
4025 4026                                  error = EINVAL;
4026 4027                                  goto update_inode;
4027 4028                          }
4028 4029                          /* If would overrun the buffer, quit */
4029 4030                          if (outcount + this_reclen > bufsize) {
4030 4031                                  break;
4031 4032                          }
4032 4033                          /* Take this entry */
4033 4034                          odp->d_ino = (ino64_t)idp->d_ino;
4034 4035                          odp->d_reclen = (ushort_t)this_reclen;
4035 4036                          odp->d_off = (offset_t)(offset + idp->d_reclen);
4036 4037  
4037 4038                          /* use strncpy(9f) to zero out uninitialized bytes */
4038 4039  
4039 4040                          ASSERT(strlen(idp->d_name) + 1 <=
4040 4041                              DIRENT64_NAMELEN(this_reclen));
4041 4042                          (void) strncpy(odp->d_name, idp->d_name,
4042 4043                              DIRENT64_NAMELEN(this_reclen));
4043 4044                          outcount += odp->d_reclen;
4044 4045                          odp = (struct dirent64 *)
4045 4046                              ((intptr_t)odp + odp->d_reclen);
4046 4047                          ASSERT(outcount <= bufsize);
4047 4048                  }
4048 4049                  if (idp->d_reclen) {
4049 4050                          incount += idp->d_reclen;
4050 4051                          offset += idp->d_reclen;
4051 4052                          idp = (struct direct *)((intptr_t)idp + idp->d_reclen);
4052 4053                  } else {
4053 4054                          offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
4054 4055                          break;
4055 4056                  }
4056 4057          }
4057 4058          /* Release the chunk */
4058 4059          fbrelse(fbp, S_OTHER);
4059 4060  
4060 4061          /* Read whole block, but got no entries, read another if not eof */
4061 4062  
4062 4063          /*
4063 4064           * Large Files: casting i_size to int here is not a problem
4064 4065           * because directory sizes are always less than MAXOFF32_T.
4065 4066           * See assertion above.
4066 4067           */
4067 4068  
4068 4069          if (offset < (int)ip->i_size && !outcount)
4069 4070                  goto nextblk;
4070 4071  
4071 4072          /* Copy out the entry data */
4072 4073          if (uiop->uio_segflg == UIO_SYSSPACE && uiop->uio_iovcnt == 1) {
4073 4074                  iovp->iov_base += outcount;
4074 4075                  iovp->iov_len -= outcount;
4075 4076                  uiop->uio_resid -= outcount;
4076 4077                  uiop->uio_offset = offset;
4077 4078          } else if ((error = uiomove(outbuf, (long)outcount, UIO_READ,
4078 4079              uiop)) == 0)
4079 4080                  uiop->uio_offset = offset;
4080 4081  update_inode:
4081 4082          ITIMES(ip);
4082 4083          if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1)
4083 4084                  kmem_free(outbuf, bufsize);
4084 4085  
4085 4086          if (eofp && error == 0)
4086 4087                  *eofp = (uiop->uio_offset >= (int)ip->i_size);
4087 4088  unlock:
4088 4089          if (ulp) {
4089 4090                  ufs_lockfs_end(ulp);
4090 4091          }
4091 4092  out:
4092 4093          return (error);
4093 4094  }
4094 4095  
4095 4096  /*ARGSUSED*/
4096 4097  static int
4097 4098  ufs_symlink(struct vnode *dvp, char *linkname, struct vattr *vap, char *target,
4098 4099      struct cred *cr, caller_context_t *ct, int flags)
4099 4100  {
4100 4101          struct inode *ip, *dip = VTOI(dvp);
4101 4102          struct ufsvfs *ufsvfsp = dip->i_ufsvfs;
4102 4103          struct ulockfs *ulp;
4103 4104          int error;
4104 4105          int issync;
4105 4106          int trans_size;
4106 4107          int residual;
4107 4108          int ioflag;
4108 4109          int retry = 1;
4109 4110  
4110 4111          /*
4111 4112           * No symlinks in attrdirs at this time
4112 4113           */
4113 4114          if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
4114 4115                  return (EINVAL);
4115 4116  
4116 4117  again:
4117 4118          ip = (struct inode *)NULL;
4118 4119          vap->va_type = VLNK;
4119 4120          vap->va_rdev = 0;
4120 4121  
4121 4122          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SYMLINK_MASK);
4122 4123          if (error)
4123 4124                  goto out;
4124 4125  
4125 4126          if (ulp)
4126 4127                  TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SYMLINK,
4127 4128                      trans_size = (int)TOP_SYMLINK_SIZE(dip));
4128 4129  
4129 4130          /*
4130 4131           * We must create the inode before the directory entry, to avoid
4131 4132           * racing with readlink().  ufs_dirmakeinode requires that we
4132 4133           * hold the quota lock as reader, and directory locks as writer.
4133 4134           */
4134 4135  
4135 4136          rw_enter(&dip->i_rwlock, RW_WRITER);
4136 4137          rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
4137 4138          rw_enter(&dip->i_contents, RW_WRITER);
4138 4139  
4139 4140          /*
4140 4141           * Suppress any out of inodes messages if we will retry on
4141 4142           * ENOSP
4142 4143           */
4143 4144          if (retry)
4144 4145                  dip->i_flag |= IQUIET;
4145 4146  
4146 4147          error = ufs_dirmakeinode(dip, &ip, vap, DE_SYMLINK, cr);
4147 4148  
4148 4149          dip->i_flag &= ~IQUIET;
4149 4150  
4150 4151          rw_exit(&dip->i_contents);
4151 4152          rw_exit(&ufsvfsp->vfs_dqrwlock);
4152 4153          rw_exit(&dip->i_rwlock);
4153 4154  
4154 4155          if (error)
4155 4156                  goto unlock;
4156 4157  
4157 4158          /*
4158 4159           * OK.  The inode has been created.  Write out the data of the
4159 4160           * symbolic link.  Since symbolic links are metadata, and should
4160 4161           * remain consistent across a system crash, we need to force the
4161 4162           * data out synchronously.
4162 4163           *
4163 4164           * (This is a change from the semantics in earlier releases, which
4164 4165           * only created symbolic links synchronously if the semi-documented
4165 4166           * 'syncdir' option was set, or if we were being invoked by the NFS
4166 4167           * server, which requires symbolic links to be created synchronously.)
4167 4168           *
4168 4169           * We need to pass in a pointer for the residual length; otherwise
4169 4170           * ufs_rdwri() will always return EIO if it can't write the data,
4170 4171           * even if the error was really ENOSPC or EDQUOT.
4171 4172           */
4172 4173  
4173 4174          ioflag = FWRITE | FDSYNC;
4174 4175          residual = 0;
4175 4176  
4176 4177          rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
4177 4178          rw_enter(&ip->i_contents, RW_WRITER);
4178 4179  
4179 4180          /*
4180 4181           * Suppress file system full messages if we will retry
4181 4182           */
4182 4183          if (retry)
4183 4184                  ip->i_flag |= IQUIET;
4184 4185  
4185 4186          error = ufs_rdwri(UIO_WRITE, ioflag, ip, target, strlen(target),
4186 4187              (offset_t)0, UIO_SYSSPACE, &residual, cr);
4187 4188  
4188 4189          ip->i_flag &= ~IQUIET;
4189 4190  
4190 4191          if (error) {
4191 4192                  rw_exit(&ip->i_contents);
4192 4193                  rw_exit(&ufsvfsp->vfs_dqrwlock);
4193 4194                  goto remove;
4194 4195          }
4195 4196  
4196 4197          /*
4197 4198           * If the link's data is small enough, we can cache it in the inode.
4198 4199           * This is a "fast symbolic link".  We don't use the first direct
4199 4200           * block because that's actually used to point at the symbolic link's
4200 4201           * contents on disk; but we know that none of the other direct or
4201 4202           * indirect blocks can be used because symbolic links are restricted
4202 4203           * to be smaller than a file system block.
4203 4204           */
4204 4205  
4205 4206          ASSERT(MAXPATHLEN <= VBSIZE(ITOV(ip)));
4206 4207  
4207 4208          if (ip->i_size > 0 && ip->i_size <= FSL_SIZE) {
4208 4209                  if (kcopy(target, &ip->i_db[1], ip->i_size) == 0) {
4209 4210                          ip->i_flag |= IFASTSYMLNK;
4210 4211                  } else {
4211 4212                          int i;
4212 4213                          /* error, clear garbage left behind */
4213 4214                          for (i = 1; i < NDADDR; i++)
4214 4215                                  ip->i_db[i] = 0;
4215 4216                          for (i = 0; i < NIADDR; i++)
4216 4217                                  ip->i_ib[i] = 0;
4217 4218                  }
4218 4219          }
4219 4220  
4220 4221          rw_exit(&ip->i_contents);
4221 4222          rw_exit(&ufsvfsp->vfs_dqrwlock);
4222 4223  
4223 4224          /*
4224 4225           * OK.  We've successfully created the symbolic link.  All that
4225 4226           * remains is to insert it into the appropriate directory.
4226 4227           */
4227 4228  
4228 4229          rw_enter(&dip->i_rwlock, RW_WRITER);
4229 4230          error = ufs_direnter_lr(dip, linkname, DE_SYMLINK, NULL, ip, cr);
4230 4231          rw_exit(&dip->i_rwlock);
4231 4232  
4232 4233          /*
4233 4234           * Fall through into remove-on-error code.  We're either done, or we
4234 4235           * need to remove the inode (if we couldn't insert it).
4235 4236           */
4236 4237  
4237 4238  remove:
4238 4239          if (error && (ip != NULL)) {
4239 4240                  rw_enter(&ip->i_contents, RW_WRITER);
4240 4241                  ip->i_nlink--;
4241 4242                  ip->i_flag |= ICHG;
4242 4243                  ip->i_seq++;
4243 4244                  ufs_setreclaim(ip);
4244 4245                  rw_exit(&ip->i_contents);
4245 4246          }
4246 4247  
4247 4248  unlock:
4248 4249          if (ip != NULL)
4249 4250                  VN_RELE(ITOV(ip));
4250 4251  
4251 4252          if (ulp) {
4252 4253                  int terr = 0;
4253 4254  
4254 4255                  TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SYMLINK,
4255 4256                      trans_size);
4256 4257                  ufs_lockfs_end(ulp);
4257 4258                  if (error == 0)
4258 4259                          error = terr;
4259 4260          }
4260 4261  
4261 4262          /*
4262 4263           * We may have failed due to lack of an inode or of a block to
4263 4264           * store the target in.  Try flushing the delete queue to free
4264 4265           * logically-available things up and try again.
4265 4266           */
4266 4267          if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
4267 4268                  ufs_delete_drain_wait(ufsvfsp, 1);
4268 4269                  retry = 0;
4269 4270                  goto again;
4270 4271          }
4271 4272  
4272 4273  out:
4273 4274          return (error);
4274 4275  }
4275 4276  
4276 4277  /*
4277 4278   * Ufs specific routine used to do ufs io.
4278 4279   */
4279 4280  int
4280 4281  ufs_rdwri(enum uio_rw rw, int ioflag, struct inode *ip, caddr_t base,
4281 4282      ssize_t len, offset_t offset, enum uio_seg seg, int *aresid,
4282 4283      struct cred *cr)
4283 4284  {
4284 4285          struct uio auio;
4285 4286          struct iovec aiov;
4286 4287          int error;
4287 4288  
4288 4289          ASSERT(RW_LOCK_HELD(&ip->i_contents));
4289 4290  
4290 4291          bzero((caddr_t)&auio, sizeof (uio_t));
4291 4292          bzero((caddr_t)&aiov, sizeof (iovec_t));
4292 4293  
4293 4294          aiov.iov_base = base;
4294 4295          aiov.iov_len = len;
4295 4296          auio.uio_iov = &aiov;
4296 4297          auio.uio_iovcnt = 1;
4297 4298          auio.uio_loffset = offset;
4298 4299          auio.uio_segflg = (short)seg;
4299 4300          auio.uio_resid = len;
4300 4301  
4301 4302          if (rw == UIO_WRITE) {
4302 4303                  auio.uio_fmode = FWRITE;
4303 4304                  auio.uio_extflg = UIO_COPY_DEFAULT;
4304 4305                  auio.uio_llimit = curproc->p_fsz_ctl;
4305 4306                  error = wrip(ip, &auio, ioflag, cr);
4306 4307          } else {
4307 4308                  auio.uio_fmode = FREAD;
4308 4309                  auio.uio_extflg = UIO_COPY_CACHED;
4309 4310                  auio.uio_llimit = MAXOFFSET_T;
4310 4311                  error = rdip(ip, &auio, ioflag, cr);
4311 4312          }
4312 4313  
4313 4314          if (aresid) {
4314 4315                  *aresid = auio.uio_resid;
4315 4316          } else if (auio.uio_resid) {
4316 4317                  error = EIO;
4317 4318          }
4318 4319          return (error);
4319 4320  }
4320 4321  
4321 4322  /*ARGSUSED*/
4322 4323  static int
4323 4324  ufs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
4324 4325  {
4325 4326          struct ufid *ufid;
4326 4327          struct inode *ip = VTOI(vp);
4327 4328  
4328 4329          if (ip->i_ufsvfs == NULL)
4329 4330                  return (EIO);
4330 4331  
4331 4332          if (fidp->fid_len < (sizeof (struct ufid) - sizeof (ushort_t))) {
4332 4333                  fidp->fid_len = sizeof (struct ufid) - sizeof (ushort_t);
4333 4334                  return (ENOSPC);
4334 4335          }
4335 4336  
4336 4337          ufid = (struct ufid *)fidp;
4337 4338          bzero((char *)ufid, sizeof (struct ufid));
4338 4339          ufid->ufid_len = sizeof (struct ufid) - sizeof (ushort_t);
4339 4340          ufid->ufid_ino = ip->i_number;
4340 4341          ufid->ufid_gen = ip->i_gen;
4341 4342  
4342 4343          return (0);
4343 4344  }
4344 4345  
4345 4346  /* ARGSUSED2 */
4346 4347  static int
4347 4348  ufs_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
4348 4349  {
4349 4350          struct inode    *ip = VTOI(vp);
4350 4351          struct ufsvfs   *ufsvfsp;
4351 4352          int             forcedirectio;
4352 4353  
4353 4354          /*
4354 4355           * Read case is easy.
4355 4356           */
4356 4357          if (!write_lock) {
4357 4358                  rw_enter(&ip->i_rwlock, RW_READER);
4358 4359                  return (V_WRITELOCK_FALSE);
4359 4360          }
4360 4361  
4361 4362          /*
4362 4363           * Caller has requested a writer lock, but that inhibits any
4363 4364           * concurrency in the VOPs that follow. Acquire the lock shared
4364 4365           * and defer exclusive access until it is known to be needed in
4365 4366           * other VOP handlers. Some cases can be determined here.
4366 4367           */
4367 4368  
4368 4369          /*
4369 4370           * If directio is not set, there is no chance of concurrency,
4370 4371           * so just acquire the lock exclusive. Beware of a forced
4371 4372           * unmount before looking at the mount option.
4372 4373           */
4373 4374          ufsvfsp = ip->i_ufsvfs;
4374 4375          forcedirectio = ufsvfsp ? ufsvfsp->vfs_forcedirectio : 0;
4375 4376          if (!(ip->i_flag & IDIRECTIO || forcedirectio) ||
4376 4377              !ufs_allow_shared_writes) {
4377 4378                  rw_enter(&ip->i_rwlock, RW_WRITER);
4378 4379                  return (V_WRITELOCK_TRUE);
4379 4380          }
4380 4381  
4381 4382          /*
4382 4383           * Mandatory locking forces acquiring i_rwlock exclusive.
4383 4384           */
4384 4385          if (MANDLOCK(vp, ip->i_mode)) {
4385 4386                  rw_enter(&ip->i_rwlock, RW_WRITER);
4386 4387                  return (V_WRITELOCK_TRUE);
4387 4388          }
4388 4389  
4389 4390          /*
4390 4391           * Acquire the lock shared in case a concurrent write follows.
4391 4392           * Mandatory locking could have become enabled before the lock
4392 4393           * was acquired. Re-check and upgrade if needed.
4393 4394           */
4394 4395          rw_enter(&ip->i_rwlock, RW_READER);
4395 4396          if (MANDLOCK(vp, ip->i_mode)) {
4396 4397                  rw_exit(&ip->i_rwlock);
4397 4398                  rw_enter(&ip->i_rwlock, RW_WRITER);
4398 4399                  return (V_WRITELOCK_TRUE);
4399 4400          }
4400 4401          return (V_WRITELOCK_FALSE);
4401 4402  }
4402 4403  
4403 4404  /*ARGSUSED*/
4404 4405  static void
4405 4406  ufs_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
4406 4407  {
4407 4408          struct inode    *ip = VTOI(vp);
4408 4409  
4409 4410          rw_exit(&ip->i_rwlock);
4410 4411  }
4411 4412  
4412 4413  /* ARGSUSED */
4413 4414  static int
4414 4415  ufs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
4415 4416  {
4416 4417          return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4417 4418  }
4418 4419  
4419 4420  /* ARGSUSED */
4420 4421  static int
4421 4422  ufs_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
4422 4423      offset_t offset, struct flk_callback *flk_cbp, struct cred *cr,
4423 4424      caller_context_t *ct)
4424 4425  {
4425 4426          struct inode *ip = VTOI(vp);
4426 4427  
4427 4428          if (ip->i_ufsvfs == NULL)
4428 4429                  return (EIO);
4429 4430  
4430 4431          /*
4431 4432           * If file is being mapped, disallow frlock.
4432 4433           * XXX I am not holding tlock while checking i_mapcnt because the
4433 4434           * current locking strategy drops all locks before calling fs_frlock.
4434 4435           * So, mapcnt could change before we enter fs_frlock making is
4435 4436           * meaningless to have held tlock in the first place.
4436 4437           */
4437 4438          if (ip->i_mapcnt > 0 && MANDLOCK(vp, ip->i_mode))
4438 4439                  return (EAGAIN);
4439 4440          return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4440 4441  }
4441 4442  
4442 4443  /* ARGSUSED */
4443 4444  static int
4444 4445  ufs_space(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
4445 4446      offset_t offset, cred_t *cr, caller_context_t *ct)
4446 4447  {
4447 4448          struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
4448 4449          struct ulockfs *ulp;
4449 4450          int error;
4450 4451  
4451 4452          if ((error = convoff(vp, bfp, 0, offset)) == 0) {
4452 4453                  if (cmd == F_FREESP) {
4453 4454                          error = ufs_lockfs_begin(ufsvfsp, &ulp,
4454 4455                              ULOCKFS_SPACE_MASK);
4455 4456                          if (error)
4456 4457                                  return (error);
4457 4458                          error = ufs_freesp(vp, bfp, flag, cr);
4458 4459  
4459 4460                          if (error == 0 && bfp->l_start == 0)
4460 4461                                  vnevent_truncate(vp, ct);
4461 4462                  } else if (cmd == F_ALLOCSP) {
4462 4463                          error = ufs_lockfs_begin(ufsvfsp, &ulp,
4463 4464                              ULOCKFS_FALLOCATE_MASK);
4464 4465                          if (error)
4465 4466                                  return (error);
4466 4467                          error = ufs_allocsp(vp, bfp, cr);
4467 4468                  } else
4468 4469                          return (EINVAL); /* Command not handled here */
4469 4470  
4470 4471                  if (ulp)
4471 4472                          ufs_lockfs_end(ulp);
4472 4473  
4473 4474          }
4474 4475          return (error);
4475 4476  }
4476 4477  
4477 4478  /*
4478 4479   * Used to determine if read ahead should be done. Also used to
4479 4480   * to determine when write back occurs.
4480 4481   */
4481 4482  #define CLUSTSZ(ip)             ((ip)->i_ufsvfs->vfs_ioclustsz)
4482 4483  
4483 4484  /*
4484 4485   * A faster version of ufs_getpage.
4485 4486   *
4486 4487   * We optimize by inlining the pvn_getpages iterator, eliminating
4487 4488   * calls to bmap_read if file doesn't have UFS holes, and avoiding
4488 4489   * the overhead of page_exists().
4489 4490   *
4490 4491   * When files has UFS_HOLES and ufs_getpage is called with S_READ,
4491 4492   * we set *protp to PROT_READ to avoid calling bmap_read. This approach
4492 4493   * victimizes performance when a file with UFS holes is faulted
4493 4494   * first in the S_READ mode, and then in the S_WRITE mode. We will get
4494 4495   * two MMU faults in this case.
4495 4496   *
4496 4497   * XXX - the inode fields which control the sequential mode are not
4497 4498   *       protected by any mutex. The read ahead will act wild if
4498 4499   *       multiple processes will access the file concurrently and
4499 4500   *       some of them in sequential mode. One particulary bad case
4500 4501   *       is if another thread will change the value of i_nextrio between
4501 4502   *       the time this thread tests the i_nextrio value and then reads it
4502 4503   *       again to use it as the offset for the read ahead.
4503 4504   */
4504 4505  /*ARGSUSED*/
4505 4506  static int
4506 4507  ufs_getpage(struct vnode *vp, offset_t off, size_t len, uint_t *protp,
4507 4508      page_t *plarr[], size_t plsz, struct seg *seg, caddr_t addr,
4508 4509      enum seg_rw rw, struct cred *cr, caller_context_t *ct)
4509 4510  {
4510 4511          u_offset_t      uoff = (u_offset_t)off; /* type conversion */
4511 4512          u_offset_t      pgoff;
4512 4513          u_offset_t      eoff;
4513 4514          struct inode    *ip = VTOI(vp);
4514 4515          struct ufsvfs   *ufsvfsp = ip->i_ufsvfs;
4515 4516          struct fs       *fs;
4516 4517          struct ulockfs  *ulp;
4517 4518          page_t          **pl;
4518 4519          caddr_t         pgaddr;
4519 4520          krw_t           rwtype;
4520 4521          int             err;
4521 4522          int             has_holes;
4522 4523          int             beyond_eof;
4523 4524          int             seqmode;
4524 4525          int             pgsize = PAGESIZE;
4525 4526          int             dolock;
4526 4527          int             do_qlock;
4527 4528          int             trans_size;
4528 4529  
4529 4530          ASSERT((uoff & PAGEOFFSET) == 0);
4530 4531  
4531 4532          if (protp)
4532 4533                  *protp = PROT_ALL;
4533 4534  
4534 4535          /*
4535 4536           * Obey the lockfs protocol
4536 4537           */
4537 4538          err = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, seg,
4538 4539              rw == S_READ || rw == S_EXEC, protp);
4539 4540          if (err)
4540 4541                  goto out;
4541 4542  
4542 4543          fs = ufsvfsp->vfs_fs;
4543 4544  
4544 4545          if (ulp && (rw == S_CREATE || rw == S_WRITE) &&
4545 4546              !(vp->v_flag & VISSWAP)) {
4546 4547                  /*
4547 4548                   * Try to start a transaction, will return if blocking is
4548 4549                   * expected to occur and the address space is not the
4549 4550                   * kernel address space.
4550 4551                   */
4551 4552                  trans_size = TOP_GETPAGE_SIZE(ip);
4552 4553                  if (seg->s_as != &kas) {
4553 4554                          TRANS_TRY_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE,
4554 4555                              trans_size, err)
4555 4556                          if (err == EWOULDBLOCK) {
4556 4557                                  /*
4557 4558                                   * Use EDEADLK here because the VM code
4558 4559                                   * can normally never see this error.
4559 4560                                   */
4560 4561                                  err = EDEADLK;
4561 4562                                  ufs_lockfs_end(ulp);
4562 4563                                  goto out;
4563 4564                          }
4564 4565                  } else {
4565 4566                          TRANS_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
4566 4567                  }
4567 4568          }
4568 4569  
4569 4570          if (vp->v_flag & VNOMAP) {
4570 4571                  err = ENOSYS;
4571 4572                  goto unlock;
4572 4573          }
4573 4574  
4574 4575          seqmode = ip->i_nextr == uoff && rw != S_CREATE;
4575 4576  
4576 4577          rwtype = RW_READER;             /* start as a reader */
4577 4578          dolock = (rw_owner(&ip->i_contents) != curthread);
4578 4579          /*
4579 4580           * If this thread owns the lock, i.e., this thread grabbed it
4580 4581           * as writer somewhere above, then we don't need to grab the
4581 4582           * lock as reader in this routine.
4582 4583           */
4583 4584          do_qlock = (rw_owner(&ufsvfsp->vfs_dqrwlock) != curthread);
4584 4585  
4585 4586  retrylock:
4586 4587          if (dolock) {
4587 4588                  /*
4588 4589                   * Grab the quota lock if we need to call
4589 4590                   * bmap_write() below (with i_contents as writer).
4590 4591                   */
4591 4592                  if (do_qlock && rwtype == RW_WRITER)
4592 4593                          rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
4593 4594                  rw_enter(&ip->i_contents, rwtype);
4594 4595          }
4595 4596  
4596 4597          /*
4597 4598           * We may be getting called as a side effect of a bmap using
4598 4599           * fbread() when the blocks might be being allocated and the
4599 4600           * size has not yet been up'ed.  In this case we want to be
4600 4601           * able to return zero pages if we get back UFS_HOLE from
4601 4602           * calling bmap for a non write case here.  We also might have
4602 4603           * to read some frags from the disk into a page if we are
4603 4604           * extending the number of frags for a given lbn in bmap().
4604 4605           * Large Files: The read of i_size here is atomic because
4605 4606           * i_contents is held here. If dolock is zero, the lock
4606 4607           * is held in bmap routines.
4607 4608           */
4608 4609          beyond_eof = uoff + len >
4609 4610              P2ROUNDUP_TYPED(ip->i_size, PAGESIZE, u_offset_t);
4610 4611          if (beyond_eof && seg != segkmap) {
4611 4612                  if (dolock) {
4612 4613                          rw_exit(&ip->i_contents);
4613 4614                          if (do_qlock && rwtype == RW_WRITER)
4614 4615                                  rw_exit(&ufsvfsp->vfs_dqrwlock);
4615 4616                  }
4616 4617                  err = EFAULT;
4617 4618                  goto unlock;
4618 4619          }
4619 4620  
4620 4621          /*
4621 4622           * Must hold i_contents lock throughout the call to pvn_getpages
4622 4623           * since locked pages are returned from each call to ufs_getapage.
4623 4624           * Must *not* return locked pages and then try for contents lock
4624 4625           * due to lock ordering requirements (inode > page)
4625 4626           */
4626 4627  
4627 4628          has_holes = bmap_has_holes(ip);
4628 4629  
4629 4630          if ((rw == S_WRITE || rw == S_CREATE) && has_holes && !beyond_eof) {
4630 4631                  int     blk_size;
4631 4632                  u_offset_t offset;
4632 4633  
4633 4634                  /*
4634 4635                   * We must acquire the RW_WRITER lock in order to
4635 4636                   * call bmap_write().
4636 4637                   */
4637 4638                  if (dolock && rwtype == RW_READER) {
4638 4639                          rwtype = RW_WRITER;
4639 4640  
4640 4641                          /*
4641 4642                           * Grab the quota lock before
4642 4643                           * upgrading i_contents, but if we can't grab it
4643 4644                           * don't wait here due to lock order:
4644 4645                           * vfs_dqrwlock > i_contents.
4645 4646                           */
4646 4647                          if (do_qlock &&
4647 4648                              rw_tryenter(&ufsvfsp->vfs_dqrwlock, RW_READER)
4648 4649                              == 0) {
4649 4650                                  rw_exit(&ip->i_contents);
4650 4651                                  goto retrylock;
4651 4652                          }
4652 4653                          if (!rw_tryupgrade(&ip->i_contents)) {
4653 4654                                  rw_exit(&ip->i_contents);
4654 4655                                  if (do_qlock)
4655 4656                                          rw_exit(&ufsvfsp->vfs_dqrwlock);
4656 4657                                  goto retrylock;
4657 4658                          }
4658 4659                  }
4659 4660  
4660 4661                  /*
4661 4662                   * May be allocating disk blocks for holes here as
4662 4663                   * a result of mmap faults. write(2) does the bmap_write
4663 4664                   * in rdip/wrip, not here. We are not dealing with frags
4664 4665                   * in this case.
4665 4666                   */
4666 4667                  /*
4667 4668                   * Large Files: We cast fs_bmask field to offset_t
4668 4669                   * just as we do for MAXBMASK because uoff is a 64-bit
4669 4670                   * data type. fs_bmask will still be a 32-bit type
4670 4671                   * as we cannot change any ondisk data structures.
4671 4672                   */
4672 4673  
4673 4674                  offset = uoff & (offset_t)fs->fs_bmask;
4674 4675                  while (offset < uoff + len) {
4675 4676                          blk_size = (int)blksize(fs, ip, lblkno(fs, offset));
4676 4677                          err = bmap_write(ip, offset, blk_size,
4677 4678                              BI_NORMAL, NULL, cr);
4678 4679                          if (ip->i_flag & (ICHG|IUPD))
4679 4680                                  ip->i_seq++;
4680 4681                          if (err)
4681 4682                                  goto update_inode;
4682 4683                          offset += blk_size; /* XXX - make this contig */
4683 4684                  }
4684 4685          }
4685 4686  
4686 4687          /*
4687 4688           * Can be a reader from now on.
4688 4689           */
4689 4690          if (dolock && rwtype == RW_WRITER) {
4690 4691                  rw_downgrade(&ip->i_contents);
4691 4692                  /*
4692 4693                   * We can release vfs_dqrwlock early so do it, but make
4693 4694                   * sure we don't try to release it again at the bottom.
4694 4695                   */
4695 4696                  if (do_qlock) {
4696 4697                          rw_exit(&ufsvfsp->vfs_dqrwlock);
4697 4698                          do_qlock = 0;
4698 4699                  }
4699 4700          }
4700 4701  
4701 4702          /*
4702 4703           * We remove PROT_WRITE in cases when the file has UFS holes
4703 4704           * because we don't  want to call bmap_read() to check each
4704 4705           * page if it is backed with a disk block.
4705 4706           */
4706 4707          if (protp && has_holes && rw != S_WRITE && rw != S_CREATE)
4707 4708                  *protp &= ~PROT_WRITE;
4708 4709  
4709 4710          err = 0;
4710 4711  
4711 4712          /*
4712 4713           * The loop looks up pages in the range [off, off + len).
4713 4714           * For each page, we first check if we should initiate an asynchronous
4714 4715           * read ahead before we call page_lookup (we may sleep in page_lookup
4715 4716           * for a previously initiated disk read).
4716 4717           */
4717 4718          eoff = (uoff + len);
4718 4719          for (pgoff = uoff, pgaddr = addr, pl = plarr;
4719 4720              pgoff < eoff; /* empty */) {
4720 4721                  page_t  *pp;
4721 4722                  u_offset_t      nextrio;
4722 4723                  se_t    se;
4723 4724                  int retval;
4724 4725  
4725 4726                  se = ((rw == S_CREATE || rw == S_OTHER) ? SE_EXCL : SE_SHARED);
4726 4727  
4727 4728                  /* Handle async getpage (faultahead) */
4728 4729                  if (plarr == NULL) {
4729 4730                          ip->i_nextrio = pgoff;
4730 4731                          (void) ufs_getpage_ra(vp, pgoff, seg, pgaddr);
4731 4732                          pgoff += pgsize;
4732 4733                          pgaddr += pgsize;
4733 4734                          continue;
4734 4735                  }
4735 4736                  /*
4736 4737                   * Check if we should initiate read ahead of next cluster.
4737 4738                   * We call page_exists only when we need to confirm that
4738 4739                   * we have the current page before we initiate the read ahead.
4739 4740                   */
4740 4741                  nextrio = ip->i_nextrio;
4741 4742                  if (seqmode &&
4742 4743                      pgoff + CLUSTSZ(ip) >= nextrio && pgoff <= nextrio &&
4743 4744                      nextrio < ip->i_size && page_exists(vp, pgoff)) {
4744 4745                          retval = ufs_getpage_ra(vp, pgoff, seg, pgaddr);
4745 4746                          /*
4746 4747                           * We always read ahead the next cluster of data
4747 4748                           * starting from i_nextrio. If the page (vp,nextrio)
4748 4749                           * is actually in core at this point, the routine
4749 4750                           * ufs_getpage_ra() will stop pre-fetching data
4750 4751                           * until we read that page in a synchronized manner
4751 4752                           * through ufs_getpage_miss(). So, we should increase
4752 4753                           * i_nextrio if the page (vp, nextrio) exists.
4753 4754                           */
4754 4755                          if ((retval == 0) && page_exists(vp, nextrio)) {
4755 4756                                  ip->i_nextrio = nextrio + pgsize;
4756 4757                          }
4757 4758                  }
4758 4759  
4759 4760                  if ((pp = page_lookup(vp, pgoff, se)) != NULL) {
4760 4761                          /*
4761 4762                           * We found the page in the page cache.
4762 4763                           */
4763 4764                          *pl++ = pp;
4764 4765                          pgoff += pgsize;
4765 4766                          pgaddr += pgsize;
4766 4767                          len -= pgsize;
4767 4768                          plsz -= pgsize;
4768 4769                  } else  {
4769 4770                          /*
4770 4771                           * We have to create the page, or read it from disk.
4771 4772                           */
4772 4773                          if (err = ufs_getpage_miss(vp, pgoff, len, seg, pgaddr,
4773 4774                              pl, plsz, rw, seqmode))
4774 4775                                  goto error;
4775 4776  
4776 4777                          while (*pl != NULL) {
4777 4778                                  pl++;
4778 4779                                  pgoff += pgsize;
4779 4780                                  pgaddr += pgsize;
4780 4781                                  len -= pgsize;
4781 4782                                  plsz -= pgsize;
4782 4783                          }
4783 4784                  }
4784 4785          }
4785 4786  
4786 4787          /*
4787 4788           * Return pages up to plsz if they are in the page cache.
4788 4789           * We cannot return pages if there is a chance that they are
4789 4790           * backed with a UFS hole and rw is S_WRITE or S_CREATE.
4790 4791           */
4791 4792          if (plarr && !(has_holes && (rw == S_WRITE || rw == S_CREATE))) {
4792 4793  
4793 4794                  ASSERT((protp == NULL) ||
4794 4795                      !(has_holes && (*protp & PROT_WRITE)));
4795 4796  
4796 4797                  eoff = pgoff + plsz;
4797 4798                  while (pgoff < eoff) {
4798 4799                          page_t          *pp;
4799 4800  
4800 4801                          if ((pp = page_lookup_nowait(vp, pgoff,
4801 4802                              SE_SHARED)) == NULL)
4802 4803                                  break;
4803 4804  
4804 4805                          *pl++ = pp;
4805 4806                          pgoff += pgsize;
4806 4807                          plsz -= pgsize;
4807 4808                  }
4808 4809          }
4809 4810  
4810 4811          if (plarr)
4811 4812                  *pl = NULL;                     /* Terminate page list */
4812 4813          ip->i_nextr = pgoff;
4813 4814  
4814 4815  error:
4815 4816          if (err && plarr) {
4816 4817                  /*
4817 4818                   * Release any pages we have locked.
4818 4819                   */
4819 4820                  while (pl > &plarr[0])
4820 4821                          page_unlock(*--pl);
4821 4822  
4822 4823                  plarr[0] = NULL;
4823 4824          }
4824 4825  
4825 4826  update_inode:
4826 4827          /*
4827 4828           * If the inode is not already marked for IACC (in rdip() for read)
4828 4829           * and the inode is not marked for no access time update (in wrip()
4829 4830           * for write) then update the inode access time and mod time now.
4830 4831           */
4831 4832          if ((ip->i_flag & (IACC | INOACC)) == 0) {
4832 4833                  if ((rw != S_OTHER) && (ip->i_mode & IFMT) != IFDIR) {
4833 4834                          if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
4834 4835                              (fs->fs_ronly == 0) &&
4835 4836                              (!ufsvfsp->vfs_noatime)) {
4836 4837                                  mutex_enter(&ip->i_tlock);
4837 4838                                  ip->i_flag |= IACC;
4838 4839                                  ITIMES_NOLOCK(ip);
4839 4840                                  mutex_exit(&ip->i_tlock);
4840 4841                          }
4841 4842                  }
4842 4843          }
4843 4844  
4844 4845          if (dolock) {
4845 4846                  rw_exit(&ip->i_contents);
4846 4847                  if (do_qlock && rwtype == RW_WRITER)
4847 4848                          rw_exit(&ufsvfsp->vfs_dqrwlock);
4848 4849          }
4849 4850  
4850 4851  unlock:
4851 4852          if (ulp) {
4852 4853                  if ((rw == S_CREATE || rw == S_WRITE) &&
4853 4854                      !(vp->v_flag & VISSWAP)) {
4854 4855                          TRANS_END_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
4855 4856                  }
4856 4857                  ufs_lockfs_end(ulp);
4857 4858          }
4858 4859  out:
4859 4860          return (err);
4860 4861  }
4861 4862  
4862 4863  /*
4863 4864   * ufs_getpage_miss is called when ufs_getpage missed the page in the page
4864 4865   * cache. The page is either read from the disk, or it's created.
4865 4866   * A page is created (without disk read) if rw == S_CREATE, or if
4866 4867   * the page is not backed with a real disk block (UFS hole).
4867 4868   */
4868 4869  /* ARGSUSED */
4869 4870  static int
4870 4871  ufs_getpage_miss(struct vnode *vp, u_offset_t off, size_t len, struct seg *seg,
4871 4872      caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw, int seq)
4872 4873  {
4873 4874          struct inode    *ip = VTOI(vp);
4874 4875          page_t          *pp;
4875 4876          daddr_t         bn;
4876 4877          size_t          io_len;
4877 4878          int             crpage = 0;
4878 4879          int             err;
4879 4880          int             contig;
4880 4881          int             bsize = ip->i_fs->fs_bsize;
4881 4882  
4882 4883          /*
4883 4884           * Figure out whether the page can be created, or must be
4884 4885           * must be read from the disk.
4885 4886           */
4886 4887          if (rw == S_CREATE)
4887 4888                  crpage = 1;
4888 4889          else {
4889 4890                  contig = 0;
4890 4891                  if (err = bmap_read(ip, off, &bn, &contig))
4891 4892                          return (err);
4892 4893  
4893 4894                  crpage = (bn == UFS_HOLE);
4894 4895  
4895 4896                  /*
4896 4897                   * If its also a fallocated block that hasn't been written to
4897 4898                   * yet, we will treat it just like a UFS_HOLE and create
4898 4899                   * a zero page for it
4899 4900                   */
4900 4901                  if (ISFALLOCBLK(ip, bn))
4901 4902                          crpage = 1;
4902 4903          }
4903 4904  
4904 4905          if (crpage) {
4905 4906                  if ((pp = page_create_va(vp, off, PAGESIZE, PG_WAIT, seg,
4906 4907                      addr)) == NULL) {
4907 4908                          return (ufs_fault(vp,
4908 4909                              "ufs_getpage_miss: page_create == NULL"));
4909 4910                  }
4910 4911  
4911 4912                  if (rw != S_CREATE)
4912 4913                          pagezero(pp, 0, PAGESIZE);
4913 4914  
4914 4915                  io_len = PAGESIZE;
4915 4916          } else {
4916 4917                  u_offset_t      io_off;
4917 4918                  uint_t  xlen;
4918 4919                  struct buf      *bp;
4919 4920                  ufsvfs_t        *ufsvfsp = ip->i_ufsvfs;
4920 4921  
4921 4922                  /*
4922 4923                   * If access is not in sequential order, we read from disk
4923 4924                   * in bsize units.
4924 4925                   *
4925 4926                   * We limit the size of the transfer to bsize if we are reading
4926 4927                   * from the beginning of the file. Note in this situation we
4927 4928                   * will hedge our bets and initiate an async read ahead of
4928 4929                   * the second block.
4929 4930                   */
4930 4931                  if (!seq || off == 0)
4931 4932                          contig = MIN(contig, bsize);
4932 4933  
4933 4934                  pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4934 4935                      &io_len, off, contig, 0);
4935 4936  
4936 4937                  /*
4937 4938                   * Some other thread has entered the page.
4938 4939                   * ufs_getpage will retry page_lookup.
4939 4940                   */
4940 4941                  if (pp == NULL) {
4941 4942                          pl[0] = NULL;
4942 4943                          return (0);
4943 4944                  }
4944 4945  
4945 4946                  /*
4946 4947                   * Zero part of the page which we are not
4947 4948                   * going to read from the disk.
4948 4949                   */
4949 4950                  xlen = io_len & PAGEOFFSET;
4950 4951                  if (xlen != 0)
4951 4952                          pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
4952 4953  
4953 4954                  bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ);
4954 4955                  bp->b_edev = ip->i_dev;
4955 4956                  bp->b_dev = cmpdev(ip->i_dev);
4956 4957                  bp->b_blkno = bn;
4957 4958                  bp->b_un.b_addr = (caddr_t)0;
4958 4959                  bp->b_file = ip->i_vnode;
4959 4960                  bp->b_offset = off;
4960 4961  
4961 4962                  if (ufsvfsp->vfs_log) {
4962 4963                          lufs_read_strategy(ufsvfsp->vfs_log, bp);
4963 4964                  } else if (ufsvfsp->vfs_snapshot) {
4964 4965                          fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
4965 4966                  } else {
4966 4967                          ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
4967 4968                          ub.ub_getpages.value.ul++;
4968 4969                          (void) bdev_strategy(bp);
4969 4970                          lwp_stat_update(LWP_STAT_INBLK, 1);
4970 4971                  }
4971 4972  
4972 4973                  ip->i_nextrio = off + ((io_len + PAGESIZE - 1) & PAGEMASK);
4973 4974  
4974 4975                  /*
4975 4976                   * If the file access is sequential, initiate read ahead
4976 4977                   * of the next cluster.
4977 4978                   */
4978 4979                  if (seq && ip->i_nextrio < ip->i_size)
4979 4980                          (void) ufs_getpage_ra(vp, off, seg, addr);
4980 4981                  err = biowait(bp);
4981 4982                  pageio_done(bp);
4982 4983  
4983 4984                  if (err) {
4984 4985                          pvn_read_done(pp, B_ERROR);
4985 4986                          return (err);
4986 4987                  }
4987 4988          }
4988 4989  
4989 4990          pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4990 4991          return (0);
4991 4992  }
4992 4993  
4993 4994  /*
4994 4995   * Read ahead a cluster from the disk. Returns the length in bytes.
4995 4996   */
4996 4997  static int
4997 4998  ufs_getpage_ra(struct vnode *vp, u_offset_t off, struct seg *seg, caddr_t addr)
4998 4999  {
4999 5000          struct inode    *ip = VTOI(vp);
5000 5001          page_t          *pp;
5001 5002          u_offset_t      io_off = ip->i_nextrio;
5002 5003          ufsvfs_t        *ufsvfsp;
5003 5004          caddr_t         addr2 = addr + (io_off - off);
5004 5005          struct buf      *bp;
5005 5006          daddr_t         bn;
5006 5007          size_t          io_len;
5007 5008          int             err;
5008 5009          int             contig;
5009 5010          int             xlen;
5010 5011          int             bsize = ip->i_fs->fs_bsize;
5011 5012  
5012 5013          /*
5013 5014           * If the directio advisory is in effect on this file,
5014 5015           * then do not do buffered read ahead. Read ahead makes
5015 5016           * it more difficult on threads using directio as they
5016 5017           * will be forced to flush the pages from this vnode.
5017 5018           */
5018 5019          if ((ufsvfsp = ip->i_ufsvfs) == NULL)
5019 5020                  return (0);
5020 5021          if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio)
5021 5022                  return (0);
5022 5023  
5023 5024          /*
5024 5025           * Is this test needed?
5025 5026           */
5026 5027          if (addr2 >= seg->s_base + seg->s_size)
5027 5028                  return (0);
5028 5029  
5029 5030          contig = 0;
5030 5031          err = bmap_read(ip, io_off, &bn, &contig);
5031 5032          /*
5032 5033           * If its a UFS_HOLE or a fallocated block, do not perform
5033 5034           * any read ahead's since there probably is nothing to read ahead
5034 5035           */
5035 5036          if (err || bn == UFS_HOLE || ISFALLOCBLK(ip, bn))
5036 5037                  return (0);
5037 5038  
5038 5039          /*
5039 5040           * Limit the transfer size to bsize if this is the 2nd block.
5040 5041           */
5041 5042          if (io_off == (u_offset_t)bsize)
5042 5043                  contig = MIN(contig, bsize);
5043 5044  
5044 5045          if ((pp = pvn_read_kluster(vp, io_off, seg, addr2, &io_off,
5045 5046              &io_len, io_off, contig, 1)) == NULL)
5046 5047                  return (0);
5047 5048  
5048 5049          /*
5049 5050           * Zero part of page which we are not going to read from disk
5050 5051           */
5051 5052          if ((xlen = (io_len & PAGEOFFSET)) > 0)
5052 5053                  pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
5053 5054  
5054 5055          ip->i_nextrio = (io_off + io_len + PAGESIZE - 1) & PAGEMASK;
5055 5056  
5056 5057          bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ | B_ASYNC);
5057 5058          bp->b_edev = ip->i_dev;
5058 5059          bp->b_dev = cmpdev(ip->i_dev);
5059 5060          bp->b_blkno = bn;
5060 5061          bp->b_un.b_addr = (caddr_t)0;
5061 5062          bp->b_file = ip->i_vnode;
5062 5063          bp->b_offset = off;
5063 5064  
5064 5065          if (ufsvfsp->vfs_log) {
5065 5066                  lufs_read_strategy(ufsvfsp->vfs_log, bp);
5066 5067          } else if (ufsvfsp->vfs_snapshot) {
5067 5068                  fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
5068 5069          } else {
5069 5070                  ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
5070 5071                  ub.ub_getras.value.ul++;
5071 5072                  (void) bdev_strategy(bp);
5072 5073                  lwp_stat_update(LWP_STAT_INBLK, 1);
5073 5074          }
5074 5075  
5075 5076          return (io_len);
5076 5077  }
5077 5078  
5078 5079  int     ufs_delay = 1;
5079 5080  /*
5080 5081   * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE, B_ASYNC}
5081 5082   *
5082 5083   * LMXXX - the inode really ought to contain a pointer to one of these
5083 5084   * async args.  Stuff gunk in there and just hand the whole mess off.
5084 5085   * This would replace i_delaylen, i_delayoff.
5085 5086   */
5086 5087  /*ARGSUSED*/
5087 5088  static int
5088 5089  ufs_putpage(struct vnode *vp, offset_t off, size_t len, int flags,
5089 5090      struct cred *cr, caller_context_t *ct)
5090 5091  {
5091 5092          struct inode *ip = VTOI(vp);
5092 5093          int err = 0;
5093 5094  
5094 5095          if (vp->v_count == 0) {
5095 5096                  return (ufs_fault(vp, "ufs_putpage: bad v_count == 0"));
5096 5097          }
5097 5098  
5098 5099          /*
5099 5100           * XXX - Why should this check be made here?
5100 5101           */
5101 5102          if (vp->v_flag & VNOMAP) {
5102 5103                  err = ENOSYS;
5103 5104                  goto errout;
5104 5105          }
5105 5106  
5106 5107          if (ip->i_ufsvfs == NULL) {
5107 5108                  err = EIO;
5108 5109                  goto errout;
5109 5110          }
5110 5111  
5111 5112          if (flags & B_ASYNC) {
5112 5113                  if (ufs_delay && len &&
5113 5114                      (flags & ~(B_ASYNC|B_DONTNEED|B_FREE)) == 0) {
5114 5115                          mutex_enter(&ip->i_tlock);
5115 5116                          /*
5116 5117                           * If nobody stalled, start a new cluster.
5117 5118                           */
5118 5119                          if (ip->i_delaylen == 0) {
5119 5120                                  ip->i_delayoff = off;
5120 5121                                  ip->i_delaylen = len;
5121 5122                                  mutex_exit(&ip->i_tlock);
5122 5123                                  goto errout;
5123 5124                          }
5124 5125                          /*
5125 5126                           * If we have a full cluster or they are not contig,
5126 5127                           * then push last cluster and start over.
5127 5128                           */
5128 5129                          if (ip->i_delaylen >= CLUSTSZ(ip) ||
5129 5130                              ip->i_delayoff + ip->i_delaylen != off) {
5130 5131                                  u_offset_t doff;
5131 5132                                  size_t dlen;
5132 5133  
5133 5134                                  doff = ip->i_delayoff;
5134 5135                                  dlen = ip->i_delaylen;
5135 5136                                  ip->i_delayoff = off;
5136 5137                                  ip->i_delaylen = len;
5137 5138                                  mutex_exit(&ip->i_tlock);
5138 5139                                  err = ufs_putpages(vp, doff, dlen,
5139 5140                                      flags, cr);
5140 5141                                  /* LMXXX - flags are new val, not old */
5141 5142                                  goto errout;
5142 5143                          }
5143 5144                          /*
5144 5145                           * There is something there, it's not full, and
5145 5146                           * it is contig.
5146 5147                           */
5147 5148                          ip->i_delaylen += len;
5148 5149                          mutex_exit(&ip->i_tlock);
5149 5150                          goto errout;
5150 5151                  }
5151 5152                  /*
5152 5153                   * Must have weird flags or we are not clustering.
5153 5154                   */
5154 5155          }
5155 5156  
5156 5157          err = ufs_putpages(vp, off, len, flags, cr);
5157 5158  
5158 5159  errout:
5159 5160          return (err);
5160 5161  }
5161 5162  
5162 5163  /*
5163 5164   * If len == 0, do from off to EOF.
5164 5165   *
5165 5166   * The normal cases should be len == 0 & off == 0 (entire vp list),
5166 5167   * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
5167 5168   * (from pageout).
5168 5169   */
5169 5170  /*ARGSUSED*/
5170 5171  static int
5171 5172  ufs_putpages(struct vnode *vp, offset_t off, size_t len, int flags,
5172 5173      struct cred *cr)
5173 5174  {
5174 5175          u_offset_t io_off;
5175 5176          u_offset_t eoff;
5176 5177          struct inode *ip = VTOI(vp);
5177 5178          page_t *pp;
5178 5179          size_t io_len;
5179 5180          int err = 0;
5180 5181          int dolock;
5181 5182  
5182 5183          if (vp->v_count == 0)
5183 5184                  return (ufs_fault(vp, "ufs_putpages: v_count == 0"));
5184 5185          /*
5185 5186           * Acquire the readers/write inode lock before locking
5186 5187           * any pages in this inode.
5187 5188           * The inode lock is held during i/o.
5188 5189           */
5189 5190          if (len == 0) {
5190 5191                  mutex_enter(&ip->i_tlock);
5191 5192                  ip->i_delayoff = ip->i_delaylen = 0;
5192 5193                  mutex_exit(&ip->i_tlock);
5193 5194          }
5194 5195          dolock = (rw_owner(&ip->i_contents) != curthread);
5195 5196          if (dolock) {
5196 5197                  /*
5197 5198                   * Must synchronize this thread and any possible thread
5198 5199                   * operating in the window of vulnerability in wrip().
5199 5200                   * It is dangerous to allow both a thread doing a putpage
5200 5201                   * and a thread writing, so serialize them.  The exception
5201 5202                   * is when the thread in wrip() does something which causes
5202 5203                   * a putpage operation.  Then, the thread must be allowed
5203 5204                   * to continue.  It may encounter a bmap_read problem in
5204 5205                   * ufs_putapage, but that is handled in ufs_putapage.
5205 5206                   * Allow async writers to proceed, we don't want to block
5206 5207                   * the pageout daemon.
5207 5208                   */
5208 5209                  if (ip->i_writer == curthread)
5209 5210                          rw_enter(&ip->i_contents, RW_READER);
5210 5211                  else {
5211 5212                          for (;;) {
5212 5213                                  rw_enter(&ip->i_contents, RW_READER);
5213 5214                                  mutex_enter(&ip->i_tlock);
5214 5215                                  /*
5215 5216                                   * If there is no thread in the critical
5216 5217                                   * section of wrip(), then proceed.
5217 5218                                   * Otherwise, wait until there isn't one.
5218 5219                                   */
5219 5220                                  if (ip->i_writer == NULL) {
5220 5221                                          mutex_exit(&ip->i_tlock);
5221 5222                                          break;
5222 5223                                  }
5223 5224                                  rw_exit(&ip->i_contents);
5224 5225                                  /*
5225 5226                                   * Bounce async writers when we have a writer
5226 5227                                   * working on this file so we don't deadlock
5227 5228                                   * the pageout daemon.
5228 5229                                   */
5229 5230                                  if (flags & B_ASYNC) {
5230 5231                                          mutex_exit(&ip->i_tlock);
5231 5232                                          return (0);
5232 5233                                  }
5233 5234                                  cv_wait(&ip->i_wrcv, &ip->i_tlock);
5234 5235                                  mutex_exit(&ip->i_tlock);
5235 5236                          }
5236 5237                  }
5237 5238          }
5238 5239  
5239 5240          if (!vn_has_cached_data(vp)) {
5240 5241                  if (dolock)
5241 5242                          rw_exit(&ip->i_contents);
5242 5243                  return (0);
5243 5244          }
5244 5245  
5245 5246          if (len == 0) {
5246 5247                  /*
5247 5248                   * Search the entire vp list for pages >= off.
5248 5249                   */
5249 5250                  err = pvn_vplist_dirty(vp, (u_offset_t)off, ufs_putapage,
5250 5251                      flags, cr);
5251 5252          } else {
5252 5253                  /*
5253 5254                   * Loop over all offsets in the range looking for
5254 5255                   * pages to deal with.
5255 5256                   */
5256 5257                  if ((eoff = blkroundup(ip->i_fs, ip->i_size)) != 0)
5257 5258                          eoff = MIN(off + len, eoff);
5258 5259                  else
5259 5260                          eoff = off + len;
5260 5261  
5261 5262                  for (io_off = off; io_off < eoff; io_off += io_len) {
5262 5263                          /*
5263 5264                           * If we are not invalidating, synchronously
5264 5265                           * freeing or writing pages, use the routine
5265 5266                           * page_lookup_nowait() to prevent reclaiming
5266 5267                           * them from the free list.
5267 5268                           */
5268 5269                          if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
5269 5270                                  pp = page_lookup(vp, io_off,
5270 5271                                      (flags & (B_INVAL | B_FREE)) ?
5271 5272                                      SE_EXCL : SE_SHARED);
5272 5273                          } else {
5273 5274                                  pp = page_lookup_nowait(vp, io_off,
5274 5275                                      (flags & B_FREE) ? SE_EXCL : SE_SHARED);
5275 5276                          }
5276 5277  
5277 5278                          if (pp == NULL || pvn_getdirty(pp, flags) == 0)
5278 5279                                  io_len = PAGESIZE;
5279 5280                          else {
5280 5281                                  u_offset_t *io_offp = &io_off;
5281 5282  
5282 5283                                  err = ufs_putapage(vp, pp, io_offp, &io_len,
5283 5284                                      flags, cr);
5284 5285                                  if (err != 0)
5285 5286                                          break;
5286 5287                                  /*
5287 5288                                   * "io_off" and "io_len" are returned as
5288 5289                                   * the range of pages we actually wrote.
5289 5290                                   * This allows us to skip ahead more quickly
5290 5291                                   * since several pages may've been dealt
5291 5292                                   * with by this iteration of the loop.
5292 5293                                   */
5293 5294                          }
5294 5295                  }
5295 5296          }
5296 5297          if (err == 0 && off == 0 && (len == 0 || len >= ip->i_size)) {
5297 5298                  /*
5298 5299                   * We have just sync'ed back all the pages on
5299 5300                   * the inode, turn off the IMODTIME flag.
5300 5301                   */
5301 5302                  mutex_enter(&ip->i_tlock);
5302 5303                  ip->i_flag &= ~IMODTIME;
5303 5304                  mutex_exit(&ip->i_tlock);
5304 5305          }
5305 5306          if (dolock)
5306 5307                  rw_exit(&ip->i_contents);
5307 5308          return (err);
5308 5309  }
5309 5310  
5310 5311  static void
5311 5312  ufs_iodone(buf_t *bp)
5312 5313  {
5313 5314          struct inode *ip;
5314 5315  
5315 5316          ASSERT((bp->b_pages->p_vnode != NULL) && !(bp->b_flags & B_READ));
5316 5317  
5317 5318          bp->b_iodone = NULL;
5318 5319  
5319 5320          ip = VTOI(bp->b_pages->p_vnode);
5320 5321  
5321 5322          mutex_enter(&ip->i_tlock);
5322 5323          if (ip->i_writes >= ufs_LW) {
5323 5324                  if ((ip->i_writes -= bp->b_bcount) <= ufs_LW)
5324 5325                          if (ufs_WRITES)
5325 5326                                  cv_broadcast(&ip->i_wrcv); /* wake all up */
5326 5327          } else {
5327 5328                  ip->i_writes -= bp->b_bcount;
5328 5329          }
5329 5330  
5330 5331          mutex_exit(&ip->i_tlock);
5331 5332          iodone(bp);
5332 5333  }
5333 5334  
5334 5335  /*
5335 5336   * Write out a single page, possibly klustering adjacent
5336 5337   * dirty pages.  The inode lock must be held.
5337 5338   *
5338 5339   * LMXXX - bsize < pagesize not done.
5339 5340   */
5340 5341  /*ARGSUSED*/
5341 5342  int
5342 5343  ufs_putapage(struct vnode *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
5343 5344      int flags, struct cred *cr)
5344 5345  {
5345 5346          u_offset_t io_off;
5346 5347          u_offset_t off;
5347 5348          struct inode *ip = VTOI(vp);
5348 5349          struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
5349 5350          struct fs *fs;
5350 5351          struct buf *bp;
5351 5352          size_t io_len;
5352 5353          daddr_t bn;
5353 5354          int err;
5354 5355          int contig;
5355 5356          int dotrans;
5356 5357  
5357 5358          ASSERT(RW_LOCK_HELD(&ip->i_contents));
5358 5359  
5359 5360          if (ufsvfsp == NULL) {
5360 5361                  err = EIO;
5361 5362                  goto out_trace;
5362 5363          }
5363 5364  
5364 5365          fs = ip->i_fs;
5365 5366          ASSERT(fs->fs_ronly == 0);
5366 5367  
5367 5368          /*
5368 5369           * If the modified time on the inode has not already been
5369 5370           * set elsewhere (e.g. for write/setattr) we set the time now.
5370 5371           * This gives us approximate modified times for mmap'ed files
5371 5372           * which are modified via stores in the user address space.
5372 5373           */
5373 5374          if ((ip->i_flag & IMODTIME) == 0) {
5374 5375                  mutex_enter(&ip->i_tlock);
5375 5376                  ip->i_flag |= IUPD;
5376 5377                  ip->i_seq++;
5377 5378                  ITIMES_NOLOCK(ip);
5378 5379                  mutex_exit(&ip->i_tlock);
5379 5380          }
5380 5381  
5381 5382          /*
5382 5383           * Align the request to a block boundry (for old file systems),
5383 5384           * and go ask bmap() how contiguous things are for this file.
5384 5385           */
5385 5386          off = pp->p_offset & (offset_t)fs->fs_bmask;    /* block align it */
5386 5387          contig = 0;
5387 5388          err = bmap_read(ip, off, &bn, &contig);
5388 5389          if (err)
5389 5390                  goto out;
5390 5391          if (bn == UFS_HOLE) {                   /* putpage never allocates */
5391 5392                  /*
5392 5393                   * logging device is in error mode; simply return EIO
5393 5394                   */
5394 5395                  if (TRANS_ISERROR(ufsvfsp)) {
5395 5396                          err = EIO;
5396 5397                          goto out;
5397 5398                  }
5398 5399                  /*
5399 5400                   * Oops, the thread in the window in wrip() did some
5400 5401                   * sort of operation which caused a putpage in the bad
5401 5402                   * range.  In this case, just return an error which will
5402 5403                   * cause the software modified bit on the page to set
5403 5404                   * and the page will get written out again later.
5404 5405                   */
5405 5406                  if (ip->i_writer == curthread) {
5406 5407                          err = EIO;
5407 5408                          goto out;
5408 5409                  }
5409 5410                  /*
5410 5411                   * If the pager is trying to push a page in the bad range
5411 5412                   * just tell it to try again later when things are better.
5412 5413                   */
5413 5414                  if (flags & B_ASYNC) {
5414 5415                          err = EAGAIN;
5415 5416                          goto out;
5416 5417                  }
5417 5418                  err = ufs_fault(ITOV(ip), "ufs_putapage: bn == UFS_HOLE");
5418 5419                  goto out;
5419 5420          }
5420 5421  
5421 5422          /*
5422 5423           * If it is an fallocate'd block, reverse the negativity since
5423 5424           * we are now writing to it
5424 5425           */
5425 5426          if (ISFALLOCBLK(ip, bn)) {
5426 5427                  err = bmap_set_bn(vp, off, dbtofsb(fs, -bn));
5427 5428                  if (err)
5428 5429                          goto out;
5429 5430  
5430 5431                  bn = -bn;
5431 5432          }
5432 5433  
5433 5434          /*
5434 5435           * Take the length (of contiguous bytes) passed back from bmap()
5435 5436           * and _try_ and get a set of pages covering that extent.
5436 5437           */
5437 5438          pp = pvn_write_kluster(vp, pp, &io_off, &io_len, off, contig, flags);
5438 5439  
5439 5440          /*
5440 5441           * May have run out of memory and not clustered backwards.
5441 5442           * off          p_offset
5442 5443           * [  pp - 1  ][   pp   ]
5443 5444           * [    block           ]
5444 5445           * We told bmap off, so we have to adjust the bn accordingly.
5445 5446           */
5446 5447          if (io_off > off) {
5447 5448                  bn += btod(io_off - off);
5448 5449                  contig -= (io_off - off);
5449 5450          }
5450 5451  
5451 5452          /*
5452 5453           * bmap was carefull to tell us the right size so use that.
5453 5454           * There might be unallocated frags at the end.
5454 5455           * LMXXX - bzero the end of the page?  We must be writing after EOF.
5455 5456           */
5456 5457          if (io_len > contig) {
5457 5458                  ASSERT(io_len - contig < fs->fs_bsize);
5458 5459                  io_len -= (io_len - contig);
5459 5460          }
5460 5461  
5461 5462          /*
5462 5463           * Handle the case where we are writing the last page after EOF.
5463 5464           *
5464 5465           * XXX - just a patch for i-mt3.
5465 5466           */
5466 5467          if (io_len == 0) {
5467 5468                  ASSERT(pp->p_offset >=
5468 5469                      (u_offset_t)(roundup(ip->i_size, PAGESIZE)));
5469 5470                  io_len = PAGESIZE;
5470 5471          }
5471 5472  
5472 5473          bp = pageio_setup(pp, io_len, ip->i_devvp, B_WRITE | flags);
5473 5474  
5474 5475          ULOCKFS_SET_MOD(ITOUL(ip));
5475 5476  
5476 5477          bp->b_edev = ip->i_dev;
5477 5478          bp->b_dev = cmpdev(ip->i_dev);
5478 5479          bp->b_blkno = bn;
5479 5480          bp->b_un.b_addr = (caddr_t)0;
5480 5481          bp->b_file = ip->i_vnode;
5481 5482  
5482 5483          /*
5483 5484           * File contents of shadow or quota inodes are metadata, and updates
5484 5485           * to these need to be put into a logging transaction. All direct
5485 5486           * callers in UFS do that, but fsflush can come here _before_ the
5486 5487           * normal codepath. An example would be updating ACL information, for
5487 5488           * which the normal codepath would be:
5488 5489           *      ufs_si_store()
5489 5490           *      ufs_rdwri()
5490 5491           *      wrip()
5491 5492           *      segmap_release()
5492 5493           *      VOP_PUTPAGE()
5493 5494           * Here, fsflush can pick up the dirty page before segmap_release()
5494 5495           * forces it out. If that happens, there's no transaction.
5495 5496           * We therefore need to test whether a transaction exists, and if not
5496 5497           * create one - for fsflush.
5497 5498           */
5498 5499          dotrans =
5499 5500              (((ip->i_mode & IFMT) == IFSHAD || ufsvfsp->vfs_qinod == ip) &&
5500 5501              ((curthread->t_flag & T_DONTBLOCK) == 0) &&
5501 5502              (TRANS_ISTRANS(ufsvfsp)));
5502 5503  
5503 5504          if (dotrans) {
5504 5505                  curthread->t_flag |= T_DONTBLOCK;
5505 5506                  TRANS_BEGIN_ASYNC(ufsvfsp, TOP_PUTPAGE, TOP_PUTPAGE_SIZE(ip));
5506 5507          }
5507 5508          if (TRANS_ISTRANS(ufsvfsp)) {
5508 5509                  if ((ip->i_mode & IFMT) == IFSHAD) {
5509 5510                          TRANS_BUF(ufsvfsp, 0, io_len, bp, DT_SHAD);
5510 5511                  } else if (ufsvfsp->vfs_qinod == ip) {
5511 5512                          TRANS_DELTA(ufsvfsp, ldbtob(bn), bp->b_bcount, DT_QR,
5512 5513                              0, 0);
5513 5514                  }
5514 5515          }
5515 5516          if (dotrans) {
5516 5517                  TRANS_END_ASYNC(ufsvfsp, TOP_PUTPAGE, TOP_PUTPAGE_SIZE(ip));
5517 5518                  curthread->t_flag &= ~T_DONTBLOCK;
5518 5519          }
5519 5520  
5520 5521          /* write throttle */
5521 5522  
5522 5523          ASSERT(bp->b_iodone == NULL);
5523 5524          bp->b_iodone = (int (*)())ufs_iodone;
5524 5525          mutex_enter(&ip->i_tlock);
5525 5526          ip->i_writes += bp->b_bcount;
5526 5527          mutex_exit(&ip->i_tlock);
5527 5528  
5528 5529          if (bp->b_flags & B_ASYNC) {
5529 5530                  if (ufsvfsp->vfs_log) {
5530 5531                          lufs_write_strategy(ufsvfsp->vfs_log, bp);
5531 5532                  } else if (ufsvfsp->vfs_snapshot) {
5532 5533                          fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
5533 5534                  } else {
5534 5535                          ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
5535 5536                          ub.ub_putasyncs.value.ul++;
5536 5537                          (void) bdev_strategy(bp);
5537 5538                          lwp_stat_update(LWP_STAT_OUBLK, 1);
5538 5539                  }
5539 5540          } else {
5540 5541                  if (ufsvfsp->vfs_log) {
5541 5542                          lufs_write_strategy(ufsvfsp->vfs_log, bp);
5542 5543                  } else if (ufsvfsp->vfs_snapshot) {
5543 5544                          fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
5544 5545                  } else {
5545 5546                          ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
5546 5547                          ub.ub_putsyncs.value.ul++;
5547 5548                          (void) bdev_strategy(bp);
5548 5549                          lwp_stat_update(LWP_STAT_OUBLK, 1);
5549 5550                  }
5550 5551                  err = biowait(bp);
5551 5552                  pageio_done(bp);
5552 5553                  pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
5553 5554          }
5554 5555  
5555 5556          pp = NULL;
5556 5557  
5557 5558  out:
5558 5559          if (err != 0 && pp != NULL)
5559 5560                  pvn_write_done(pp, B_ERROR | B_WRITE | flags);
5560 5561  
5561 5562          if (offp)
5562 5563                  *offp = io_off;
5563 5564          if (lenp)
5564 5565                  *lenp = io_len;
5565 5566  out_trace:
5566 5567          return (err);
5567 5568  }
5568 5569  
5569 5570  uint64_t ufs_map_alock_retry_cnt;
5570 5571  uint64_t ufs_map_lockfs_retry_cnt;
5571 5572  
5572 5573  /* ARGSUSED */
5573 5574  static int
5574 5575  ufs_map(struct vnode *vp, offset_t off, struct as *as, caddr_t *addrp,
5575 5576      size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, struct cred *cr,
5576 5577      caller_context_t *ct)
5577 5578  {
5578 5579          struct segvn_crargs vn_a;
5579 5580          struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
5580 5581          struct ulockfs *ulp;
5581 5582          int error, sig;
5582 5583          k_sigset_t smask;
5583 5584          caddr_t hint = *addrp;
5584 5585  
5585 5586          if (vp->v_flag & VNOMAP) {
5586 5587                  error = ENOSYS;
5587 5588                  goto out;
5588 5589          }
5589 5590  
5590 5591          if (off < (offset_t)0 || (offset_t)(off + len) < (offset_t)0) {
5591 5592                  error = ENXIO;
5592 5593                  goto out;
5593 5594          }
5594 5595  
5595 5596          if (vp->v_type != VREG) {
5596 5597                  error = ENODEV;
5597 5598                  goto out;
5598 5599          }
5599 5600  
5600 5601  retry_map:
5601 5602          *addrp = hint;
5602 5603          /*
5603 5604           * If file is being locked, disallow mapping.
5604 5605           */
5605 5606          if (vn_has_mandatory_locks(vp, VTOI(vp)->i_mode)) {
5606 5607                  error = EAGAIN;
5607 5608                  goto out;
5608 5609          }
5609 5610  
5610 5611          as_rangelock(as);
5611 5612          /*
5612 5613           * Note that if we are retrying (because ufs_lockfs_trybegin failed in
5613 5614           * the previous attempt), some other thread could have grabbed
5614 5615           * the same VA range if MAP_FIXED is set. In that case, choose_addr
5615 5616           * would unmap the valid VA range, that is ok.
5616 5617           */
5617 5618          error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5618 5619          if (error != 0) {
5619 5620                  as_rangeunlock(as);
5620 5621                  goto out;
5621 5622          }
5622 5623  
5623 5624          /*
5624 5625           * a_lock has to be acquired before entering the lockfs protocol
5625 5626           * because that is the order in which pagefault works. Also we cannot
5626 5627           * block on a_lock here because this waiting writer will prevent
5627 5628           * further readers like ufs_read from progressing and could cause
5628 5629           * deadlock between ufs_read/ufs_map/pagefault when a quiesce is
5629 5630           * pending.
5630 5631           */
5631 5632          while (!AS_LOCK_TRYENTER(as, RW_WRITER)) {
5632 5633                  ufs_map_alock_retry_cnt++;
5633 5634                  delay(RETRY_LOCK_DELAY);
5634 5635          }
5635 5636  
5636 5637          /*
5637 5638           * We can't hold as->a_lock and wait for lockfs to succeed because
5638 5639           * the proc tools might hang on a_lock, so call ufs_lockfs_trybegin()
5639 5640           * instead.
5640 5641           */
5641 5642          if (error = ufs_lockfs_trybegin(ufsvfsp, &ulp, ULOCKFS_MAP_MASK)) {
5642 5643                  /*
5643 5644                   * ufs_lockfs_trybegin() did not succeed. It is safer to give up
5644 5645                   * as->a_lock and wait for ulp->ul_fs_lock status to change.
5645 5646                   */
5646 5647                  ufs_map_lockfs_retry_cnt++;
5647 5648                  AS_LOCK_EXIT(as);
5648 5649                  as_rangeunlock(as);
5649 5650                  if (error == EIO)
5650 5651                          goto out;
5651 5652  
5652 5653                  mutex_enter(&ulp->ul_lock);
5653 5654                  while (ulp->ul_fs_lock & ULOCKFS_MAP_MASK) {
5654 5655                          if (ULOCKFS_IS_SLOCK(ulp) || ufsvfsp->vfs_nointr) {
5655 5656                                  cv_wait(&ulp->ul_cv, &ulp->ul_lock);
5656 5657                          } else {
5657 5658                                  sigintr(&smask, 1);
5658 5659                                  sig = cv_wait_sig(&ulp->ul_cv, &ulp->ul_lock);
5659 5660                                  sigunintr(&smask);
5660 5661                                  if (((ulp->ul_fs_lock & ULOCKFS_MAP_MASK) &&
5661 5662                                      !sig) || ufsvfsp->vfs_dontblock) {
5662 5663                                          mutex_exit(&ulp->ul_lock);
5663 5664                                          return (EINTR);
5664 5665                                  }
5665 5666                          }
5666 5667                  }
5667 5668                  mutex_exit(&ulp->ul_lock);
5668 5669                  goto retry_map;
5669 5670          }
5670 5671  
5671 5672          vn_a.vp = vp;
5672 5673          vn_a.offset = (u_offset_t)off;
5673 5674          vn_a.type = flags & MAP_TYPE;
5674 5675          vn_a.prot = prot;
5675 5676          vn_a.maxprot = maxprot;
5676 5677          vn_a.cred = cr;
5677 5678          vn_a.amp = NULL;
5678 5679          vn_a.flags = flags & ~MAP_TYPE;
5679 5680          vn_a.szc = 0;
5680 5681          vn_a.lgrp_mem_policy_flags = 0;
5681 5682  
5682 5683          error = as_map_locked(as, *addrp, len, segvn_create, &vn_a);
5683 5684          if (ulp)
5684 5685                  ufs_lockfs_end(ulp);
5685 5686          as_rangeunlock(as);
5686 5687  out:
5687 5688          return (error);
5688 5689  }
5689 5690  
5690 5691  /* ARGSUSED */
5691 5692  static int
5692 5693  ufs_addmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
5693 5694      size_t len, uchar_t  prot, uchar_t  maxprot, uint_t    flags,
5694 5695      struct cred *cr, caller_context_t *ct)
5695 5696  {
5696 5697          struct inode *ip = VTOI(vp);
5697 5698  
5698 5699          if (vp->v_flag & VNOMAP) {
5699 5700                  return (ENOSYS);
5700 5701          }
5701 5702  
5702 5703          mutex_enter(&ip->i_tlock);
5703 5704          ip->i_mapcnt += btopr(len);
5704 5705          mutex_exit(&ip->i_tlock);
5705 5706          return (0);
5706 5707  }
5707 5708  
5708 5709  /*ARGSUSED*/
5709 5710  static int
5710 5711  ufs_delmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
5711 5712      size_t len, uint_t prot,  uint_t maxprot,  uint_t flags, struct cred *cr,
5712 5713      caller_context_t *ct)
5713 5714  {
5714 5715          struct inode *ip = VTOI(vp);
5715 5716  
5716 5717          if (vp->v_flag & VNOMAP) {
5717 5718                  return (ENOSYS);
5718 5719          }
5719 5720  
5720 5721          mutex_enter(&ip->i_tlock);
5721 5722          ip->i_mapcnt -= btopr(len);     /* Count released mappings */
5722 5723          ASSERT(ip->i_mapcnt >= 0);
5723 5724          mutex_exit(&ip->i_tlock);
5724 5725          return (0);
5725 5726  }
5726 5727  /*
5727 5728   * Return the answer requested to poll() for non-device files
5728 5729   */
5729 5730  struct pollhead ufs_pollhd;
5730 5731  
5731 5732  /* ARGSUSED */
5732 5733  int
5733 5734  ufs_poll(vnode_t *vp, short ev, int any, short *revp, struct pollhead **phpp,
5734 5735      caller_context_t *ct)
5735 5736  {
5736 5737          struct ufsvfs   *ufsvfsp;
5737 5738  
5738 5739          /*
5739 5740           * Regular files reject edge-triggered pollers.
5740 5741           * See the comment in fs_poll() for a more detailed explanation.
5741 5742           */
5742 5743          if (ev & POLLET) {
5743 5744                  return (EPERM);
5744 5745          }
5745 5746  
5746 5747          *revp = 0;
5747 5748          ufsvfsp = VTOI(vp)->i_ufsvfs;
5748 5749  
5749 5750          if (!ufsvfsp) {
5750 5751                  *revp = POLLHUP;
5751 5752                  goto out;
5752 5753          }
5753 5754  
5754 5755          if (ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs) ||
5755 5756              ULOCKFS_IS_ELOCK(&ufsvfsp->vfs_ulockfs)) {
5756 5757                  *revp |= POLLERR;
5757 5758  
5758 5759          } else {
5759 5760                  if ((ev & POLLOUT) && !ufsvfsp->vfs_fs->fs_ronly &&
5760 5761                      !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
5761 5762                          *revp |= POLLOUT;
5762 5763  
5763 5764                  if ((ev & POLLWRBAND) && !ufsvfsp->vfs_fs->fs_ronly &&
5764 5765                      !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
5765 5766                          *revp |= POLLWRBAND;
5766 5767  
5767 5768                  if (ev & POLLIN)
5768 5769                          *revp |= POLLIN;
5769 5770  
5770 5771                  if (ev & POLLRDNORM)
5771 5772                          *revp |= POLLRDNORM;
5772 5773  
5773 5774                  if (ev & POLLRDBAND)
5774 5775                          *revp |= POLLRDBAND;
5775 5776          }
5776 5777  
5777 5778          if ((ev & POLLPRI) && (*revp & (POLLERR|POLLHUP)))
5778 5779                  *revp |= POLLPRI;
5779 5780  out:
5780 5781          if (*revp == 0 && ! any) {
5781 5782                  *phpp = &ufs_pollhd;
5782 5783          }
5783 5784  
5784 5785          return (0);
5785 5786  }
5786 5787  
5787 5788  /* ARGSUSED */
5788 5789  static int
5789 5790  ufs_l_pathconf(struct vnode *vp, int cmd, ulong_t *valp, struct cred *cr,
5790 5791      caller_context_t *ct)
5791 5792  {
5792 5793          struct ufsvfs   *ufsvfsp = VTOI(vp)->i_ufsvfs;
5793 5794          struct ulockfs  *ulp = NULL;
5794 5795          struct inode    *sip = NULL;
5795 5796          int             error;
5796 5797          struct inode    *ip = VTOI(vp);
5797 5798          int             issync;
5798 5799  
5799 5800          error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_PATHCONF_MASK);
5800 5801          if (error)
5801 5802                  return (error);
5802 5803  
5803 5804          switch (cmd) {
5804 5805                  /*
5805 5806                   * Have to handle _PC_NAME_MAX here, because the normal way
5806 5807                   * [fs_pathconf() -> VOP_STATVFS() -> ufs_statvfs()]
5807 5808                   * results in a lock ordering reversal between
5808 5809                   * ufs_lockfs_{begin,end}() and
5809 5810                   * ufs_thread_{suspend,continue}().
5810 5811                   *
5811 5812                   * Keep in sync with ufs_statvfs().
5812 5813                   */
5813 5814          case _PC_NAME_MAX:
5814 5815                  *valp = MAXNAMLEN;
5815 5816                  break;
5816 5817  
5817 5818          case _PC_FILESIZEBITS:
5818 5819                  if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
5819 5820                          *valp = UFS_FILESIZE_BITS;
5820 5821                  else
5821 5822                          *valp = 32;
5822 5823                  break;
5823 5824  
5824 5825          case _PC_XATTR_EXISTS:
5825 5826                  if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
5826 5827  
5827 5828                          error =
5828 5829                              ufs_xattr_getattrdir(vp, &sip, LOOKUP_XATTR, cr);
5829 5830                          if (error ==  0 && sip != NULL) {
5830 5831                                  /* Start transaction */
5831 5832                                  if (ulp) {
5832 5833                                          TRANS_BEGIN_CSYNC(ufsvfsp, issync,
5833 5834                                              TOP_RMDIR, TOP_RMDIR_SIZE);
5834 5835                                  }
5835 5836                                  /*
5836 5837                                   * Is directory empty
5837 5838                                   */
5838 5839                                  rw_enter(&sip->i_rwlock, RW_WRITER);
5839 5840                                  rw_enter(&sip->i_contents, RW_WRITER);
5840 5841                                  if (ufs_xattrdirempty(sip,
5841 5842                                      sip->i_number, CRED())) {
5842 5843                                          rw_enter(&ip->i_contents, RW_WRITER);
5843 5844                                          ufs_unhook_shadow(ip, sip);
5844 5845                                          rw_exit(&ip->i_contents);
5845 5846  
5846 5847                                          *valp = 0;
5847 5848  
5848 5849                                  } else
5849 5850                                          *valp = 1;
5850 5851                                  rw_exit(&sip->i_contents);
5851 5852                                  rw_exit(&sip->i_rwlock);
5852 5853                                  if (ulp) {
5853 5854                                          TRANS_END_CSYNC(ufsvfsp, error, issync,
5854 5855                                              TOP_RMDIR, TOP_RMDIR_SIZE);
5855 5856                                  }
5856 5857                                  VN_RELE(ITOV(sip));
5857 5858                          } else if (error == ENOENT) {
5858 5859                                  *valp = 0;
5859 5860                                  error = 0;
5860 5861                          }
5861 5862                  } else {
5862 5863                          error = fs_pathconf(vp, cmd, valp, cr, ct);
5863 5864                  }
5864 5865                  break;
5865 5866  
5866 5867          case _PC_ACL_ENABLED:
5867 5868                  *valp = _ACL_ACLENT_ENABLED;
5868 5869                  break;
5869 5870  
5870 5871          case _PC_MIN_HOLE_SIZE:
5871 5872                  *valp = (ulong_t)ip->i_fs->fs_bsize;
5872 5873                  break;
5873 5874  
5874 5875          case _PC_SATTR_ENABLED:
5875 5876          case _PC_SATTR_EXISTS:
5876 5877                  *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5877 5878                      (vp->v_type == VREG || vp->v_type == VDIR);
5878 5879                  break;
5879 5880  
5880 5881          case _PC_TIMESTAMP_RESOLUTION:
5881 5882                  /*
5882 5883                   * UFS keeps only microsecond timestamp resolution.
5883 5884                   * This is historical and will probably never change.
5884 5885                   */
5885 5886                  *valp = 1000L;
5886 5887                  break;
5887 5888  
5888 5889          default:
5889 5890                  error = fs_pathconf(vp, cmd, valp, cr, ct);
5890 5891                  break;
5891 5892          }
5892 5893  
5893 5894          if (ulp != NULL) {
5894 5895                  ufs_lockfs_end(ulp);
5895 5896          }
5896 5897          return (error);
5897 5898  }
5898 5899  
5899 5900  int ufs_pageio_writes, ufs_pageio_reads;
5900 5901  
5901 5902  /*ARGSUSED*/
5902 5903  static int
5903 5904  ufs_pageio(struct vnode *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5904 5905      int flags, struct cred *cr, caller_context_t *ct)
5905 5906  {
5906 5907          struct inode *ip = VTOI(vp);
5907 5908          struct ufsvfs *ufsvfsp;
5908 5909          page_t *npp = NULL, *opp = NULL, *cpp = pp;
5909 5910          struct buf *bp;
5910 5911          daddr_t bn;
5911 5912          size_t done_len = 0, cur_len = 0;
5912 5913          int err = 0;
5913 5914          int contig = 0;
5914 5915          int dolock;
5915 5916          int vmpss = 0;
5916 5917          struct ulockfs *ulp;
5917 5918  
5918 5919          if ((flags & B_READ) && pp != NULL && pp->p_vnode == vp &&
5919 5920              vp->v_mpssdata != NULL) {
5920 5921                  vmpss = 1;
5921 5922          }
5922 5923  
5923 5924          dolock = (rw_owner(&ip->i_contents) != curthread);
5924 5925          /*
5925 5926           * We need a better check.  Ideally, we would use another
5926 5927           * vnodeops so that hlocked and forcibly unmounted file
5927 5928           * systems would return EIO where appropriate and w/o the
5928 5929           * need for these checks.
5929 5930           */
5930 5931          if ((ufsvfsp = ip->i_ufsvfs) == NULL)
5931 5932                  return (EIO);
5932 5933  
5933 5934          /*
5934 5935           * For vmpss (pp can be NULL) case respect the quiesce protocol.
5935 5936           * ul_lock must be taken before locking pages so we can't use it here
5936 5937           * if pp is non NULL because segvn already locked pages
5937 5938           * SE_EXCL. Instead we rely on the fact that a forced umount or
5938 5939           * applying a filesystem lock via ufs_fiolfs() will block in the
5939 5940           * implicit call to ufs_flush() until we unlock the pages after the
5940 5941           * return to segvn. Other ufs_quiesce() callers keep ufs_quiesce_pend
5941 5942           * above 0 until they are done. We have to be careful not to increment
5942 5943           * ul_vnops_cnt here after forceful unmount hlocks the file system.
5943 5944           *
5944 5945           * If pp is NULL use ul_lock to make sure we don't increment
5945 5946           * ul_vnops_cnt after forceful unmount hlocks the file system.
5946 5947           */
5947 5948          if (vmpss || pp == NULL) {
5948 5949                  ulp = &ufsvfsp->vfs_ulockfs;
5949 5950                  if (pp == NULL)
5950 5951                          mutex_enter(&ulp->ul_lock);
5951 5952                  if (ulp->ul_fs_lock & ULOCKFS_GETREAD_MASK) {
5952 5953                          if (pp == NULL) {
5953 5954                                  mutex_exit(&ulp->ul_lock);
5954 5955                          }
5955 5956                          return (vmpss ? EIO : EINVAL);
5956 5957                  }
5957 5958                  atomic_inc_ulong(&ulp->ul_vnops_cnt);
5958 5959                  if (pp == NULL)
5959 5960                          mutex_exit(&ulp->ul_lock);
5960 5961                  if (ufs_quiesce_pend) {
5961 5962                          if (!atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
5962 5963                                  cv_broadcast(&ulp->ul_cv);
5963 5964                          return (vmpss ? EIO : EINVAL);
5964 5965                  }
5965 5966          }
5966 5967  
5967 5968          if (dolock) {
5968 5969                  /*
5969 5970                   * segvn may call VOP_PAGEIO() instead of VOP_GETPAGE() to
5970 5971                   * handle a fault against a segment that maps vnode pages with
5971 5972                   * large mappings.  Segvn creates pages and holds them locked
5972 5973                   * SE_EXCL during VOP_PAGEIO() call. In this case we have to
5973 5974                   * use rw_tryenter() to avoid a potential deadlock since in
5974 5975                   * lock order i_contents needs to be taken first.
5975 5976                   * Segvn will retry via VOP_GETPAGE() if VOP_PAGEIO() fails.
5976 5977                   */
5977 5978                  if (!vmpss) {
5978 5979                          rw_enter(&ip->i_contents, RW_READER);
5979 5980                  } else if (!rw_tryenter(&ip->i_contents, RW_READER)) {
5980 5981                          if (!atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
5981 5982                                  cv_broadcast(&ulp->ul_cv);
5982 5983                          return (EDEADLK);
5983 5984                  }
5984 5985          }
5985 5986  
5986 5987          /*
5987 5988           * Return an error to segvn because the pagefault request is beyond
5988 5989           * PAGESIZE rounded EOF.
5989 5990           */
5990 5991          if (vmpss && btopr(io_off + io_len) > btopr(ip->i_size)) {
5991 5992                  if (dolock)
5992 5993                          rw_exit(&ip->i_contents);
5993 5994                  if (!atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
5994 5995                          cv_broadcast(&ulp->ul_cv);
5995 5996                  return (EFAULT);
5996 5997          }
5997 5998  
5998 5999          if (pp == NULL) {
5999 6000                  if (bmap_has_holes(ip)) {
6000 6001                          err = ENOSYS;
6001 6002                  } else {
6002 6003                          err = EINVAL;
6003 6004                  }
6004 6005                  if (dolock)
6005 6006                          rw_exit(&ip->i_contents);
6006 6007                  if (!atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
6007 6008                          cv_broadcast(&ulp->ul_cv);
6008 6009                  return (err);
6009 6010          }
6010 6011  
6011 6012          /*
6012 6013           * Break the io request into chunks, one for each contiguous
6013 6014           * stretch of disk blocks in the target file.
6014 6015           */
6015 6016          while (done_len < io_len) {
6016 6017                  ASSERT(cpp);
6017 6018                  contig = 0;
6018 6019                  if (err = bmap_read(ip, (u_offset_t)(io_off + done_len),
6019 6020                      &bn, &contig))
6020 6021                          break;
6021 6022  
6022 6023                  if (bn == UFS_HOLE) {   /* No holey swapfiles */
6023 6024                          if (vmpss) {
6024 6025                                  err = EFAULT;
6025 6026                                  break;
6026 6027                          }
6027 6028                          err = ufs_fault(ITOV(ip), "ufs_pageio: bn == UFS_HOLE");
6028 6029                          break;
6029 6030                  }
6030 6031  
6031 6032                  cur_len = MIN(io_len - done_len, contig);
6032 6033                  /*
6033 6034                   * Zero out a page beyond EOF, when the last block of
6034 6035                   * a file is a UFS fragment so that ufs_pageio() can be used
6035 6036                   * instead of ufs_getpage() to handle faults against
6036 6037                   * segvn segments that use large pages.
6037 6038                   */
6038 6039                  page_list_break(&cpp, &npp, btopr(cur_len));
6039 6040                  if ((flags & B_READ) && (cur_len & PAGEOFFSET)) {
6040 6041                          size_t xlen = cur_len & PAGEOFFSET;
6041 6042                          pagezero(cpp->p_prev, xlen, PAGESIZE - xlen);
6042 6043                  }
6043 6044  
6044 6045                  bp = pageio_setup(cpp, cur_len, ip->i_devvp, flags);
6045 6046                  ASSERT(bp != NULL);
6046 6047  
6047 6048                  bp->b_edev = ip->i_dev;
6048 6049                  bp->b_dev = cmpdev(ip->i_dev);
6049 6050                  bp->b_blkno = bn;
6050 6051                  bp->b_un.b_addr = (caddr_t)0;
6051 6052                  bp->b_file = ip->i_vnode;
6052 6053  
6053 6054                  ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
6054 6055                  ub.ub_pageios.value.ul++;
6055 6056                  if (ufsvfsp->vfs_snapshot)
6056 6057                          fssnap_strategy(&(ufsvfsp->vfs_snapshot), bp);
6057 6058                  else
6058 6059                          (void) bdev_strategy(bp);
6059 6060  
6060 6061                  if (flags & B_READ)
6061 6062                          ufs_pageio_reads++;
6062 6063                  else
6063 6064                          ufs_pageio_writes++;
6064 6065                  if (flags & B_READ)
6065 6066                          lwp_stat_update(LWP_STAT_INBLK, 1);
6066 6067                  else
6067 6068                          lwp_stat_update(LWP_STAT_OUBLK, 1);
6068 6069                  /*
6069 6070                   * If the request is not B_ASYNC, wait for i/o to complete
6070 6071                   * and re-assemble the page list to return to the caller.
6071 6072                   * If it is B_ASYNC we leave the page list in pieces and
6072 6073                   * cleanup() will dispose of them.
6073 6074                   */
6074 6075                  if ((flags & B_ASYNC) == 0) {
6075 6076                          err = biowait(bp);
6076 6077                          pageio_done(bp);
6077 6078                          if (err)
6078 6079                                  break;
6079 6080                          page_list_concat(&opp, &cpp);
6080 6081                  }
6081 6082                  cpp = npp;
6082 6083                  npp = NULL;
6083 6084                  if (flags & B_READ)
6084 6085                          cur_len = P2ROUNDUP_TYPED(cur_len, PAGESIZE, size_t);
6085 6086                  done_len += cur_len;
6086 6087          }
6087 6088          ASSERT(err || (cpp == NULL && npp == NULL && done_len == io_len));
6088 6089          if (err) {
6089 6090                  if (flags & B_ASYNC) {
6090 6091                          /* Cleanup unprocessed parts of list */
6091 6092                          page_list_concat(&cpp, &npp);
6092 6093                          if (flags & B_READ)
6093 6094                                  pvn_read_done(cpp, B_ERROR);
6094 6095                          else
6095 6096                                  pvn_write_done(cpp, B_ERROR);
6096 6097                  } else {
6097 6098                          /* Re-assemble list and let caller clean up */
6098 6099                          page_list_concat(&opp, &cpp);
6099 6100                          page_list_concat(&opp, &npp);
6100 6101                  }
6101 6102          }
6102 6103  
6103 6104          if (vmpss && !(ip->i_flag & IACC) && !ULOCKFS_IS_NOIACC(ulp) &&
6104 6105              ufsvfsp->vfs_fs->fs_ronly == 0 && !ufsvfsp->vfs_noatime) {
6105 6106                  mutex_enter(&ip->i_tlock);
6106 6107                  ip->i_flag |= IACC;
6107 6108                  ITIMES_NOLOCK(ip);
6108 6109                  mutex_exit(&ip->i_tlock);
6109 6110          }
6110 6111  
6111 6112          if (dolock)
6112 6113                  rw_exit(&ip->i_contents);
6113 6114          if (vmpss && !atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
6114 6115                  cv_broadcast(&ulp->ul_cv);
6115 6116          return (err);
6116 6117  }
6117 6118  
6118 6119  /*
6119 6120   * Called when the kernel is in a frozen state to dump data
6120 6121   * directly to the device. It uses a private dump data structure,
6121 6122   * set up by dump_ctl, to locate the correct disk block to which to dump.
6122 6123   */
6123 6124  /*ARGSUSED*/
6124 6125  static int
6125 6126  ufs_dump(vnode_t *vp, caddr_t addr, offset_t ldbn, offset_t dblks,
6126 6127      caller_context_t *ct)
6127 6128  {
6128 6129          u_offset_t      file_size;
6129 6130          struct inode    *ip = VTOI(vp);
6130 6131          struct fs       *fs = ip->i_fs;
6131 6132          daddr_t         dbn, lfsbn;
6132 6133          int             disk_blks = fs->fs_bsize >> DEV_BSHIFT;
6133 6134          int             error = 0;
6134 6135          int             ndbs, nfsbs;
6135 6136  
6136 6137          /*
6137 6138           * forced unmount case
6138 6139           */
6139 6140          if (ip->i_ufsvfs == NULL)
6140 6141                  return (EIO);
6141 6142          /*
6142 6143           * Validate the inode that it has not been modified since
6143 6144           * the dump structure is allocated.
6144 6145           */
6145 6146          mutex_enter(&ip->i_tlock);
6146 6147          if ((dump_info == NULL) ||
6147 6148              (dump_info->ip != ip) ||
6148 6149              (dump_info->time.tv_sec != ip->i_mtime.tv_sec) ||
6149 6150              (dump_info->time.tv_usec != ip->i_mtime.tv_usec)) {
6150 6151                  mutex_exit(&ip->i_tlock);
6151 6152                  return (-1);
6152 6153          }
6153 6154          mutex_exit(&ip->i_tlock);
6154 6155  
6155 6156          /*
6156 6157           * See that the file has room for this write
6157 6158           */
6158 6159          UFS_GET_ISIZE(&file_size, ip);
6159 6160  
6160 6161          if (ldbtob(ldbn + dblks) > file_size)
6161 6162                  return (ENOSPC);
6162 6163  
6163 6164          /*
6164 6165           * Find the physical disk block numbers from the dump
6165 6166           * private data structure directly and write out the data
6166 6167           * in contiguous block lumps
6167 6168           */
6168 6169          while (dblks > 0 && !error) {
6169 6170                  lfsbn = (daddr_t)lblkno(fs, ldbtob(ldbn));
6170 6171                  dbn = fsbtodb(fs, dump_info->dblk[lfsbn]) + ldbn % disk_blks;
6171 6172                  nfsbs = 1;
6172 6173                  ndbs = disk_blks - ldbn % disk_blks;
6173 6174                  while (ndbs < dblks && fsbtodb(fs, dump_info->dblk[lfsbn +
6174 6175                      nfsbs]) == dbn + ndbs) {
6175 6176                          nfsbs++;
6176 6177                          ndbs += disk_blks;
6177 6178                  }
6178 6179                  if (ndbs > dblks)
6179 6180                          ndbs = dblks;
6180 6181                  error = bdev_dump(ip->i_dev, addr, dbn, ndbs);
6181 6182                  addr += ldbtob((offset_t)ndbs);
6182 6183                  dblks -= ndbs;
6183 6184                  ldbn += ndbs;
6184 6185          }
6185 6186          return (error);
6186 6187  
6187 6188  }
6188 6189  
6189 6190  /*
6190 6191   * Prepare the file system before and after the dump operation.
6191 6192   *
6192 6193   * action = DUMP_ALLOC:
6193 6194   * Preparation before dump, allocate dump private data structure
6194 6195   * to hold all the direct and indirect block info for dump.
6195 6196   *
6196 6197   * action = DUMP_FREE:
6197 6198   * Clean up after dump, deallocate the dump private data structure.
6198 6199   *
6199 6200   * action = DUMP_SCAN:
6200 6201   * Scan dump_info for *blkp DEV_BSIZE blocks of contig fs space;
6201 6202   * if found, the starting file-relative DEV_BSIZE lbn is written
6202 6203   * to *bklp; that lbn is intended for use with VOP_DUMP()
6203 6204   */
6204 6205  /*ARGSUSED*/
6205 6206  static int
6206 6207  ufs_dumpctl(vnode_t *vp, int action, offset_t *blkp, caller_context_t *ct)
6207 6208  {
6208 6209          struct inode    *ip = VTOI(vp);
6209 6210          ufsvfs_t        *ufsvfsp = ip->i_ufsvfs;
6210 6211          struct fs       *fs;
6211 6212          daddr32_t       *dblk, *storeblk;
6212 6213          daddr32_t       *nextblk, *endblk;
6213 6214          struct buf      *bp;
6214 6215          int             i, entry, entries;
6215 6216          int             n, ncontig;
6216 6217  
6217 6218          /*
6218 6219           * check for forced unmount
6219 6220           */
6220 6221          if (ufsvfsp == NULL)
6221 6222                  return (EIO);
6222 6223  
6223 6224          if (action == DUMP_ALLOC) {
6224 6225                  /*
6225 6226                   * alloc and record dump_info
6226 6227                   */
6227 6228                  if (dump_info != NULL)
6228 6229                          return (EINVAL);
6229 6230  
6230 6231                  ASSERT(vp->v_type == VREG);
6231 6232                  fs = ufsvfsp->vfs_fs;
6232 6233  
6233 6234                  rw_enter(&ip->i_contents, RW_READER);
6234 6235  
6235 6236                  if (bmap_has_holes(ip)) {
6236 6237                          rw_exit(&ip->i_contents);
6237 6238                          return (EFAULT);
6238 6239                  }
6239 6240  
6240 6241                  /*
6241 6242                   * calculate and allocate space needed according to i_size
6242 6243                   */
6243 6244                  entries = (int)lblkno(fs, blkroundup(fs, ip->i_size));
6244 6245                  dump_info = kmem_alloc(sizeof (struct dump) +
6245 6246                      (entries - 1) * sizeof (daddr32_t), KM_NOSLEEP);
6246 6247                  if (dump_info == NULL) {
6247 6248                          rw_exit(&ip->i_contents);
6248 6249                          return (ENOMEM);
6249 6250                  }
6250 6251  
6251 6252                  /* Start saving the info */
6252 6253                  dump_info->fsbs = entries;
6253 6254                  dump_info->ip = ip;
6254 6255                  storeblk = &dump_info->dblk[0];
6255 6256  
6256 6257                  /* Direct Blocks */
6257 6258                  for (entry = 0; entry < NDADDR && entry < entries; entry++)
6258 6259                          *storeblk++ = ip->i_db[entry];
6259 6260  
6260 6261                  /* Indirect Blocks */
6261 6262                  for (i = 0; i < NIADDR; i++) {
6262 6263                          int error = 0;
6263 6264  
6264 6265                          bp = UFS_BREAD(ufsvfsp,
6265 6266                              ip->i_dev, fsbtodb(fs, ip->i_ib[i]), fs->fs_bsize);
6266 6267                          if (bp->b_flags & B_ERROR)
6267 6268                                  error = EIO;
6268 6269                          else {
6269 6270                                  dblk = bp->b_un.b_daddr;
6270 6271                                  if ((storeblk = save_dblks(ip, ufsvfsp,
6271 6272                                      storeblk, dblk, i, entries)) == NULL)
6272 6273                                          error = EIO;
6273 6274                          }
6274 6275  
6275 6276                          brelse(bp);
6276 6277  
6277 6278                          if (error != 0) {
6278 6279                                  kmem_free(dump_info, sizeof (struct dump) +
6279 6280                                      (entries - 1) * sizeof (daddr32_t));
6280 6281                                  rw_exit(&ip->i_contents);
6281 6282                                  dump_info = NULL;
6282 6283                                  return (error);
6283 6284                          }
6284 6285                  }
6285 6286                  /* and time stamp the information */
6286 6287                  mutex_enter(&ip->i_tlock);
6287 6288                  dump_info->time = ip->i_mtime;
6288 6289                  mutex_exit(&ip->i_tlock);
6289 6290  
6290 6291                  rw_exit(&ip->i_contents);
6291 6292          } else if (action == DUMP_FREE) {
6292 6293                  /*
6293 6294                   * free dump_info
6294 6295                   */
6295 6296                  if (dump_info == NULL)
6296 6297                          return (EINVAL);
6297 6298                  entries = dump_info->fsbs - 1;
6298 6299                  kmem_free(dump_info, sizeof (struct dump) +
6299 6300                      entries * sizeof (daddr32_t));
6300 6301                  dump_info = NULL;
6301 6302          } else if (action == DUMP_SCAN) {
6302 6303                  /*
6303 6304                   * scan dump_info
6304 6305                   */
6305 6306                  if (dump_info == NULL)
6306 6307                          return (EINVAL);
6307 6308  
6308 6309                  dblk = dump_info->dblk;
6309 6310                  nextblk = dblk + 1;
6310 6311                  endblk = dblk + dump_info->fsbs - 1;
6311 6312                  fs = ufsvfsp->vfs_fs;
6312 6313                  ncontig = *blkp >> (fs->fs_bshift - DEV_BSHIFT);
6313 6314  
6314 6315                  /*
6315 6316                   * scan dblk[] entries; contig fs space is found when:
6316 6317                   * ((current blkno + frags per block) == next blkno)
6317 6318                   */
6318 6319                  n = 0;
6319 6320                  while (n < ncontig && dblk < endblk) {
6320 6321                          if ((*dblk + fs->fs_frag) == *nextblk)
6321 6322                                  n++;
6322 6323                          else
6323 6324                                  n = 0;
6324 6325                          dblk++;
6325 6326                          nextblk++;
6326 6327                  }
6327 6328  
6328 6329                  /*
6329 6330                   * index is where size bytes of contig space begins;
6330 6331                   * conversion from index to the file's DEV_BSIZE lbn
6331 6332                   * is equivalent to:  (index * fs_bsize) / DEV_BSIZE
6332 6333                   */
6333 6334                  if (n == ncontig) {
6334 6335                          i = (dblk - dump_info->dblk) - ncontig;
6335 6336                          *blkp = i << (fs->fs_bshift - DEV_BSHIFT);
6336 6337                  } else
6337 6338                          return (EFAULT);
6338 6339          }
6339 6340          return (0);
6340 6341  }
6341 6342  
6342 6343  /*
6343 6344   * Recursive helper function for ufs_dumpctl().  It follows the indirect file
6344 6345   * system  blocks until it reaches the the disk block addresses, which are
6345 6346   * then stored into the given buffer, storeblk.
6346 6347   */
6347 6348  static daddr32_t *
6348 6349  save_dblks(struct inode *ip, struct ufsvfs *ufsvfsp,  daddr32_t *storeblk,
6349 6350      daddr32_t *dblk, int level, int entries)
6350 6351  {
6351 6352          struct fs       *fs = ufsvfsp->vfs_fs;
6352 6353          struct buf      *bp;
6353 6354          int             i;
6354 6355  
6355 6356          if (level == 0) {
6356 6357                  for (i = 0; i < NINDIR(fs); i++) {
6357 6358                          if (storeblk - dump_info->dblk >= entries)
6358 6359                                  break;
6359 6360                          *storeblk++ = dblk[i];
6360 6361                  }
6361 6362                  return (storeblk);
6362 6363          }
6363 6364          for (i = 0; i < NINDIR(fs); i++) {
6364 6365                  if (storeblk - dump_info->dblk >= entries)
6365 6366                          break;
6366 6367                  bp = UFS_BREAD(ufsvfsp,
6367 6368                      ip->i_dev, fsbtodb(fs, dblk[i]), fs->fs_bsize);
6368 6369                  if (bp->b_flags & B_ERROR) {
6369 6370                          brelse(bp);
6370 6371                          return (NULL);
6371 6372                  }
6372 6373                  storeblk = save_dblks(ip, ufsvfsp, storeblk, bp->b_un.b_daddr,
6373 6374                      level - 1, entries);
6374 6375                  brelse(bp);
6375 6376  
6376 6377                  if (storeblk == NULL)
6377 6378                          return (NULL);
6378 6379          }
6379 6380          return (storeblk);
6380 6381  }
6381 6382  
6382 6383  /* ARGSUSED */
6383 6384  static int
6384 6385  ufs_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag,
6385 6386      struct cred *cr, caller_context_t *ct)
6386 6387  {
6387 6388          struct inode    *ip = VTOI(vp);
6388 6389          struct ulockfs  *ulp;
6389 6390          struct ufsvfs   *ufsvfsp = ip->i_ufsvfs;
6390 6391          ulong_t         vsa_mask = vsap->vsa_mask;
6391 6392          int             err = EINVAL;
6392 6393  
6393 6394          vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
6394 6395  
6395 6396          /*
6396 6397           * Only grab locks if needed - they're not needed to check vsa_mask
6397 6398           * or if the mask contains no acl flags.
6398 6399           */
6399 6400          if (vsa_mask != 0) {
6400 6401                  if (err = ufs_lockfs_begin(ufsvfsp, &ulp,
6401 6402                      ULOCKFS_GETATTR_MASK))
6402 6403                          return (err);
6403 6404  
6404 6405                  rw_enter(&ip->i_contents, RW_READER);
6405 6406                  err = ufs_acl_get(ip, vsap, flag, cr);
6406 6407                  rw_exit(&ip->i_contents);
6407 6408  
6408 6409                  if (ulp)
6409 6410                          ufs_lockfs_end(ulp);
6410 6411          }
6411 6412          return (err);
6412 6413  }
6413 6414  
6414 6415  /* ARGSUSED */
6415 6416  static int
6416 6417  ufs_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr,
6417 6418      caller_context_t *ct)
6418 6419  {
6419 6420          struct inode    *ip = VTOI(vp);
6420 6421          struct ulockfs  *ulp = NULL;
6421 6422          struct ufsvfs   *ufsvfsp = VTOI(vp)->i_ufsvfs;
6422 6423          ulong_t         vsa_mask = vsap->vsa_mask;
6423 6424          int             err;
6424 6425          int             haverwlock = 1;
6425 6426          int             trans_size;
6426 6427          int             donetrans = 0;
6427 6428          int             retry = 1;
6428 6429  
6429 6430          ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
6430 6431  
6431 6432          /* Abort now if the request is either empty or invalid. */
6432 6433          vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
6433 6434          if ((vsa_mask == 0) ||
6434 6435              ((vsap->vsa_aclentp == NULL) &&
6435 6436              (vsap->vsa_dfaclentp == NULL))) {
6436 6437                  err = EINVAL;
6437 6438                  goto out;
6438 6439          }
6439 6440  
6440 6441          /*
6441 6442           * Following convention, if this is a directory then we acquire the
6442 6443           * inode's i_rwlock after starting a UFS logging transaction;
6443 6444           * otherwise, we acquire it beforehand. Since we were called (and
6444 6445           * must therefore return) with the lock held, we will have to drop it,
6445 6446           * and later reacquire it, if operating on a directory.
6446 6447           */
6447 6448          if (vp->v_type == VDIR) {
6448 6449                  rw_exit(&ip->i_rwlock);
6449 6450                  haverwlock = 0;
6450 6451          } else {
6451 6452                  /* Upgrade the lock if required. */
6452 6453                  if (!rw_write_held(&ip->i_rwlock)) {
6453 6454                          rw_exit(&ip->i_rwlock);
6454 6455                          rw_enter(&ip->i_rwlock, RW_WRITER);
6455 6456                  }
6456 6457          }
6457 6458  
6458 6459  again:
6459 6460          ASSERT(!(vp->v_type == VDIR && haverwlock));
6460 6461          if (err = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK)) {
6461 6462                  ulp = NULL;
6462 6463                  retry = 0;
6463 6464                  goto out;
6464 6465          }
6465 6466  
6466 6467          /*
6467 6468           * Check that the file system supports this operation. Note that
6468 6469           * ufs_lockfs_begin() will have checked that the file system had
6469 6470           * not been forcibly unmounted.
6470 6471           */
6471 6472          if (ufsvfsp->vfs_fs->fs_ronly) {
6472 6473                  err = EROFS;
6473 6474                  goto out;
6474 6475          }
6475 6476          if (ufsvfsp->vfs_nosetsec) {
6476 6477                  err = ENOSYS;
6477 6478                  goto out;
6478 6479          }
6479 6480  
6480 6481          if (ulp) {
6481 6482                  TRANS_BEGIN_ASYNC(ufsvfsp, TOP_SETSECATTR,
6482 6483                      trans_size = TOP_SETSECATTR_SIZE(VTOI(vp)));
6483 6484                  donetrans = 1;
6484 6485          }
6485 6486  
6486 6487          if (vp->v_type == VDIR) {
6487 6488                  rw_enter(&ip->i_rwlock, RW_WRITER);
6488 6489                  haverwlock = 1;
6489 6490          }
6490 6491  
6491 6492          ASSERT(haverwlock);
6492 6493  
6493 6494          /* Do the actual work. */
6494 6495          rw_enter(&ip->i_contents, RW_WRITER);
6495 6496          /*
6496 6497           * Suppress out of inodes messages if we will retry.
6497 6498           */
6498 6499          if (retry)
6499 6500                  ip->i_flag |= IQUIET;
6500 6501          err = ufs_acl_set(ip, vsap, flag, cr);
6501 6502          ip->i_flag &= ~IQUIET;
6502 6503          rw_exit(&ip->i_contents);
6503 6504  
6504 6505  out:
6505 6506          if (ulp) {
6506 6507                  if (donetrans) {
6507 6508                          /*
6508 6509                           * top_end_async() can eventually call
6509 6510                           * top_end_sync(), which can block. We must
6510 6511                           * therefore observe the lock-ordering protocol
6511 6512                           * here as well.
6512 6513                           */
6513 6514                          if (vp->v_type == VDIR) {
6514 6515                                  rw_exit(&ip->i_rwlock);
6515 6516                                  haverwlock = 0;
6516 6517                          }
6517 6518                          TRANS_END_ASYNC(ufsvfsp, TOP_SETSECATTR, trans_size);
6518 6519                  }
6519 6520                  ufs_lockfs_end(ulp);
6520 6521          }
6521 6522          /*
6522 6523           * If no inodes available, try scaring a logically-
6523 6524           * free one out of the delete queue to someplace
6524 6525           * that we can find it.
6525 6526           */
6526 6527          if ((err == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
6527 6528                  ufs_delete_drain_wait(ufsvfsp, 1);
6528 6529                  retry = 0;
6529 6530                  if (vp->v_type == VDIR && haverwlock) {
6530 6531                          rw_exit(&ip->i_rwlock);
6531 6532                          haverwlock = 0;
6532 6533                  }
6533 6534                  goto again;
6534 6535          }
6535 6536          /*
6536 6537           * If we need to reacquire the lock then it is safe to do so
6537 6538           * as a reader. This is because ufs_rwunlock(), which will be
6538 6539           * called by our caller after we return, does not differentiate
6539 6540           * between shared and exclusive locks.
6540 6541           */
6541 6542          if (!haverwlock) {
6542 6543                  ASSERT(vp->v_type == VDIR);
6543 6544                  rw_enter(&ip->i_rwlock, RW_READER);
6544 6545          }
6545 6546  
6546 6547          return (err);
6547 6548  }
6548 6549  
6549 6550  /*
6550 6551   * Locate the vnode to be used for an event notification. As this will
6551 6552   * be called prior to the name space change perform basic verification
6552 6553   * that the change will be allowed.
6553 6554   */
6554 6555  
6555 6556  static int
6556 6557  ufs_eventlookup(struct vnode *dvp, char *nm, struct cred *cr,
6557 6558      struct vnode **vpp)
6558 6559  {
6559 6560          int     namlen;
6560 6561          int     error;
6561 6562          struct vnode    *vp;
6562 6563          struct inode    *ip;
6563 6564          struct inode    *xip;
6564 6565          struct ufsvfs   *ufsvfsp;
6565 6566          struct ulockfs  *ulp;
6566 6567  
6567 6568          ip = VTOI(dvp);
6568 6569          *vpp = NULL;
6569 6570  
6570 6571          if ((namlen = strlen(nm)) == 0)
6571 6572                  return (EINVAL);
6572 6573  
6573 6574          if (nm[0] == '.') {
6574 6575                  if (namlen == 1)
6575 6576                          return (EINVAL);
6576 6577                  else if ((namlen == 2) && nm[1] == '.') {
6577 6578                          return (EEXIST);
6578 6579                  }
6579 6580          }
6580 6581  
6581 6582          /*
6582 6583           * Check accessibility and write access of parent directory as we
6583 6584           * only want to post the event if we're able to make a change.
6584 6585           */
6585 6586          if (error = ufs_diraccess(ip, IEXEC|IWRITE, cr))
6586 6587                  return (error);
6587 6588  
6588 6589          if (vp = dnlc_lookup(dvp, nm)) {
6589 6590                  if (vp == DNLC_NO_VNODE) {
6590 6591                          VN_RELE(vp);
6591 6592                          return (ENOENT);
6592 6593                  }
6593 6594  
6594 6595                  *vpp = vp;
6595 6596                  return (0);
6596 6597          }
6597 6598  
6598 6599          /*
6599 6600           * Keep the idle queue from getting too long by idling two
6600 6601           * inodes before attempting to allocate another.
6601 6602           * This operation must be performed before entering lockfs
6602 6603           * or a transaction.
6603 6604           */
6604 6605          if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
6605 6606                  if ((curthread->t_flag & T_DONTBLOCK) == 0) {
6606 6607                          ins.in_lidles.value.ul += ufs_lookup_idle_count;
6607 6608                          ufs_idle_some(ufs_lookup_idle_count);
6608 6609                  }
6609 6610  
6610 6611          ufsvfsp = ip->i_ufsvfs;
6611 6612  
6612 6613  retry_lookup:
6613 6614          if (error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK))
6614 6615                  return (error);
6615 6616  
6616 6617          if ((error = ufs_dirlook(ip, nm, &xip, cr, 1, 1)) == 0) {
6617 6618                  vp = ITOV(xip);
6618 6619                  *vpp = vp;
6619 6620          }
6620 6621  
6621 6622          if (ulp) {
6622 6623                  ufs_lockfs_end(ulp);
6623 6624          }
6624 6625  
6625 6626          if (error == EAGAIN)
6626 6627                  goto retry_lookup;
6627 6628  
6628 6629          return (error);
6629 6630  }
  
    | 
      ↓ open down ↓ | 
    4722 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX