Print this page
    
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/pcfs/pc_vnops.c
          +++ new/usr/src/uts/common/fs/pcfs/pc_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
  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 2009 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   */
  26   26  
  27   27  /*
  28   28   * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  29   29   * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
  30   30   */
  31   31  
  32   32  #include <sys/param.h>
  33   33  #include <sys/t_lock.h>
  34   34  #include <sys/systm.h>
  35   35  #include <sys/sysmacros.h>
  36   36  #include <sys/user.h>
  37   37  #include <sys/buf.h>
  38   38  #include <sys/stat.h>
  39   39  #include <sys/vfs.h>
  40   40  #include <sys/vfs_opreg.h>
  41   41  #include <sys/dirent.h>
  42   42  #include <sys/vnode.h>
  43   43  #include <sys/proc.h>
  44   44  #include <sys/file.h>
  45   45  #include <sys/fcntl.h>
  46   46  #include <sys/uio.h>
  47   47  #include <sys/fs/pc_label.h>
  48   48  #include <sys/fs/pc_fs.h>
  49   49  #include <sys/fs/pc_dir.h>
  50   50  #include <sys/fs/pc_node.h>
  51   51  #include <sys/mman.h>
  52   52  #include <sys/pathname.h>
  53   53  #include <sys/vmsystm.h>
  54   54  #include <sys/cmn_err.h>
  55   55  #include <sys/debug.h>
  56   56  #include <sys/statvfs.h>
  57   57  #include <sys/unistd.h>
  58   58  #include <sys/kmem.h>
  59   59  #include <sys/conf.h>
  60   60  #include <sys/flock.h>
  61   61  #include <sys/policy.h>
  62   62  #include <sys/sdt.h>
  63   63  #include <sys/sunddi.h>
  64   64  #include <sys/types.h>
  65   65  #include <sys/errno.h>
  66   66  
  67   67  #include <vm/seg.h>
  68   68  #include <vm/page.h>
  69   69  #include <vm/pvn.h>
  70   70  #include <vm/seg_map.h>
  71   71  #include <vm/seg_vn.h>
  72   72  #include <vm/hat.h>
  73   73  #include <vm/as.h>
  74   74  #include <vm/seg_kmem.h>
  75   75  
  76   76  #include <fs/fs_subr.h>
  77   77  
  78   78  static int pcfs_open(struct vnode **, int, struct cred *, caller_context_t *ct);
  79   79  static int pcfs_close(struct vnode *, int, int, offset_t, struct cred *,
  80   80          caller_context_t *ct);
  81   81  static int pcfs_read(struct vnode *, struct uio *, int, struct cred *,
  82   82          caller_context_t *);
  83   83  static int pcfs_write(struct vnode *, struct uio *, int, struct cred *,
  84   84          caller_context_t *);
  85   85  static int pcfs_getattr(struct vnode *, struct vattr *, int, struct cred *,
  86   86          caller_context_t *ct);
  87   87  static int pcfs_setattr(struct vnode *, struct vattr *, int, struct cred *,
  88   88          caller_context_t *);
  89   89  static int pcfs_access(struct vnode *, int, int, struct cred *,
  90   90          caller_context_t *ct);
  91   91  static int pcfs_lookup(struct vnode *, char *, struct vnode **,
  92   92          struct pathname *, int, struct vnode *, struct cred *,
  93   93          caller_context_t *, int *, pathname_t *);
  94   94  static int pcfs_create(struct vnode *, char *, struct vattr *,
  95   95          enum vcexcl, int mode, struct vnode **, struct cred *, int,
  96   96          caller_context_t *, vsecattr_t *);
  97   97  static int pcfs_remove(struct vnode *, char *, struct cred *,
  98   98          caller_context_t *, int);
  99   99  static int pcfs_rename(struct vnode *, char *, struct vnode *, char *,
 100  100          struct cred *, caller_context_t *, int);
 101  101  static int pcfs_mkdir(struct vnode *, char *, struct vattr *, struct vnode **,
 102  102          struct cred *, caller_context_t *, int, vsecattr_t *);
 103  103  static int pcfs_rmdir(struct vnode *, char *, struct vnode *, struct cred *,
 104  104          caller_context_t *, int);
 105  105  static int pcfs_readdir(struct vnode *, struct uio *, struct cred *, int *,
 106  106          caller_context_t *, int);
 107  107  static int pcfs_fsync(struct vnode *, int, struct cred *, caller_context_t *);
 108  108  static void pcfs_inactive(struct vnode *, struct cred *, caller_context_t *);
 109  109  static int pcfs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *);
 110  110  static int pcfs_space(struct vnode *, int, struct flock64 *, int,
 111  111          offset_t, cred_t *, caller_context_t *);
 112  112  static int pcfs_getpage(struct vnode *, offset_t, size_t, uint_t *, page_t *[],
 113  113          size_t, struct seg *, caddr_t, enum seg_rw, struct cred *,
 114  114          caller_context_t *);
 115  115  static int pcfs_getapage(struct vnode *, u_offset_t, size_t, uint_t *,
 116  116          page_t *[], size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
 117  117  static int pcfs_putpage(struct vnode *, offset_t, size_t, int, struct cred *,
 118  118          caller_context_t *);
 119  119  static int pcfs_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
 120  120          uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
 121  121  static int pcfs_addmap(struct vnode *, offset_t, struct as *, caddr_t,
 122  122          size_t, uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
 123  123  static int pcfs_delmap(struct vnode *, offset_t, struct as *, caddr_t,
 124  124          size_t, uint_t, uint_t, uint_t, struct cred *, caller_context_t *);
 125  125  static int pcfs_seek(struct vnode *, offset_t, offset_t *,
 126  126          caller_context_t *);
 127  127  static int pcfs_pathconf(struct vnode *, int, ulong_t *, struct cred *,
 128  128          caller_context_t *);
 129  129  
 130  130  int pcfs_putapage(struct vnode *, page_t *, u_offset_t *, size_t *, int,
 131  131          struct cred *);
 132  132  static int rwpcp(struct pcnode *, struct uio *, enum uio_rw, int);
 133  133  static int get_long_fn_chunk(struct pcdir_lfn *ep, char *buf);
 134  134  
 135  135  extern krwlock_t pcnodes_lock;
 136  136  
 137  137  #define lround(r)       (((r)+sizeof (long long)-1)&(~(sizeof (long long)-1)))
 138  138  
 139  139  /*
 140  140   * vnode op vectors for files and directories.
 141  141   */
 142  142  struct vnodeops *pcfs_fvnodeops;
 143  143  struct vnodeops *pcfs_dvnodeops;
 144  144  
 145  145  const fs_operation_def_t pcfs_fvnodeops_template[] = {
 146  146          VOPNAME_OPEN,           { .vop_open = pcfs_open },
 147  147          VOPNAME_CLOSE,          { .vop_close = pcfs_close },
 148  148          VOPNAME_READ,           { .vop_read = pcfs_read },
 149  149          VOPNAME_WRITE,          { .vop_write = pcfs_write },
 150  150          VOPNAME_GETATTR,        { .vop_getattr = pcfs_getattr },
 151  151          VOPNAME_SETATTR,        { .vop_setattr = pcfs_setattr },
 152  152          VOPNAME_ACCESS,         { .vop_access = pcfs_access },
 153  153          VOPNAME_FSYNC,          { .vop_fsync = pcfs_fsync },
 154  154          VOPNAME_INACTIVE,       { .vop_inactive = pcfs_inactive },
 155  155          VOPNAME_FID,            { .vop_fid = pcfs_fid },
 156  156          VOPNAME_SEEK,           { .vop_seek = pcfs_seek },
 157  157          VOPNAME_SPACE,          { .vop_space = pcfs_space },
 158  158          VOPNAME_GETPAGE,        { .vop_getpage = pcfs_getpage },
 159  159          VOPNAME_PUTPAGE,        { .vop_putpage = pcfs_putpage },
 160  160          VOPNAME_MAP,            { .vop_map = pcfs_map },
 161  161          VOPNAME_ADDMAP,         { .vop_addmap = pcfs_addmap },
 162  162          VOPNAME_DELMAP,         { .vop_delmap = pcfs_delmap },
 163  163          VOPNAME_PATHCONF,       { .vop_pathconf = pcfs_pathconf },
 164  164          VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
 165  165          NULL,                   NULL
 166  166  };
 167  167  
 168  168  const fs_operation_def_t pcfs_dvnodeops_template[] = {
 169  169          VOPNAME_OPEN,           { .vop_open = pcfs_open },
 170  170          VOPNAME_CLOSE,          { .vop_close = pcfs_close },
 171  171          VOPNAME_GETATTR,        { .vop_getattr = pcfs_getattr },
 172  172          VOPNAME_SETATTR,        { .vop_setattr = pcfs_setattr },
 173  173          VOPNAME_ACCESS,         { .vop_access = pcfs_access },
 174  174          VOPNAME_LOOKUP,         { .vop_lookup = pcfs_lookup },
 175  175          VOPNAME_CREATE,         { .vop_create = pcfs_create },
 176  176          VOPNAME_REMOVE,         { .vop_remove = pcfs_remove },
 177  177          VOPNAME_RENAME,         { .vop_rename = pcfs_rename },
 178  178          VOPNAME_MKDIR,          { .vop_mkdir = pcfs_mkdir },
 179  179          VOPNAME_RMDIR,          { .vop_rmdir = pcfs_rmdir },
 180  180          VOPNAME_READDIR,        { .vop_readdir = pcfs_readdir },
 181  181          VOPNAME_FSYNC,          { .vop_fsync = pcfs_fsync },
 182  182          VOPNAME_INACTIVE,       { .vop_inactive = pcfs_inactive },
 183  183          VOPNAME_FID,            { .vop_fid = pcfs_fid },
 184  184          VOPNAME_SEEK,           { .vop_seek = pcfs_seek },
 185  185          VOPNAME_PATHCONF,       { .vop_pathconf = pcfs_pathconf },
 186  186          VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
 187  187          NULL,                   NULL
 188  188  };
 189  189  
 190  190  
 191  191  /*ARGSUSED*/
 192  192  static int
 193  193  pcfs_open(
 194  194          struct vnode **vpp,
 195  195          int flag,
 196  196          struct cred *cr,
 197  197          caller_context_t *ct)
 198  198  {
 199  199          return (0);
 200  200  }
 201  201  
 202  202  /*
 203  203   * files are sync'ed on close to keep floppy up to date
 204  204   */
 205  205  
 206  206  /*ARGSUSED*/
 207  207  static int
 208  208  pcfs_close(
 209  209          struct vnode *vp,
 210  210          int flag,
 211  211          int count,
 212  212          offset_t offset,
 213  213          struct cred *cr,
 214  214          caller_context_t *ct)
 215  215  {
 216  216          return (0);
 217  217  }
 218  218  
 219  219  /*ARGSUSED*/
 220  220  static int
 221  221  pcfs_read(
 222  222          struct vnode *vp,
 223  223          struct uio *uiop,
 224  224          int ioflag,
 225  225          struct cred *cr,
 226  226          struct caller_context *ct)
 227  227  {
 228  228          struct pcfs *fsp;
 229  229          struct pcnode *pcp;
 230  230          int error;
 231  231  
 232  232          fsp = VFSTOPCFS(vp->v_vfsp);
 233  233          if (error = pc_verify(fsp))
 234  234                  return (error);
 235  235          error = pc_lockfs(fsp, 0, 0);
 236  236          if (error)
 237  237                  return (error);
 238  238          if ((pcp = VTOPC(vp)) == NULL || pcp->pc_flags & PC_INVAL) {
 239  239                  pc_unlockfs(fsp);
 240  240                  return (EIO);
 241  241          }
 242  242          error = rwpcp(pcp, uiop, UIO_READ, ioflag);
 243  243          if ((fsp->pcfs_vfs->vfs_flag & VFS_RDONLY) == 0) {
 244  244                  pc_mark_acc(fsp, pcp);
 245  245          }
 246  246          pc_unlockfs(fsp);
 247  247          if (error) {
 248  248                  PC_DPRINTF1(1, "pcfs_read: io error = %d\n", error);
 249  249          }
 250  250          return (error);
 251  251  }
 252  252  
 253  253  /*ARGSUSED*/
 254  254  static int
 255  255  pcfs_write(
 256  256          struct vnode *vp,
 257  257          struct uio *uiop,
 258  258          int ioflag,
 259  259          struct cred *cr,
 260  260          struct caller_context *ct)
 261  261  {
 262  262          struct pcfs *fsp;
 263  263          struct pcnode *pcp;
 264  264          int error;
 265  265  
 266  266          fsp = VFSTOPCFS(vp->v_vfsp);
 267  267          if (error = pc_verify(fsp))
 268  268                  return (error);
 269  269          error = pc_lockfs(fsp, 0, 0);
 270  270          if (error)
 271  271                  return (error);
 272  272          if ((pcp = VTOPC(vp)) == NULL || pcp->pc_flags & PC_INVAL) {
 273  273                  pc_unlockfs(fsp);
 274  274                  return (EIO);
 275  275          }
 276  276          if (ioflag & FAPPEND) {
 277  277                  /*
 278  278                   * in append mode start at end of file.
 279  279                   */
 280  280                  uiop->uio_loffset = pcp->pc_size;
 281  281          }
 282  282          error = rwpcp(pcp, uiop, UIO_WRITE, ioflag);
 283  283          pcp->pc_flags |= PC_MOD;
 284  284          pc_mark_mod(fsp, pcp);
 285  285          if (ioflag & (FSYNC|FDSYNC))
 286  286                  (void) pc_nodeupdate(pcp);
 287  287  
 288  288          pc_unlockfs(fsp);
 289  289          if (error) {
 290  290                  PC_DPRINTF1(1, "pcfs_write: io error = %d\n", error);
 291  291          }
 292  292          return (error);
 293  293  }
 294  294  
 295  295  /*
 296  296   * read or write a vnode
 297  297   */
 298  298  static int
 299  299  rwpcp(
 300  300          struct pcnode *pcp,
 301  301          struct uio *uio,
 302  302          enum uio_rw rw,
 303  303          int ioflag)
 304  304  {
 305  305          struct vnode *vp = PCTOV(pcp);
 306  306          struct pcfs *fsp;
 307  307          daddr_t bn;                     /* phys block number */
 308  308          int n;
 309  309          offset_t off;
 310  310          caddr_t base;
 311  311          int mapon, pagecreate;
 312  312          int newpage;
 313  313          int error = 0;
 314  314          rlim64_t limit = uio->uio_llimit;
 315  315          int oresid = uio->uio_resid;
 316  316  
 317  317          /*
 318  318           * If the filesystem was umounted by force, return immediately.
 319  319           */
 320  320          if (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
 321  321                  return (EIO);
 322  322  
 323  323          PC_DPRINTF4(5, "rwpcp pcp=%p off=%lld resid=%ld size=%u\n", (void *)pcp,
 324  324              uio->uio_loffset, uio->uio_resid, pcp->pc_size);
 325  325  
 326  326          ASSERT(rw == UIO_READ || rw == UIO_WRITE);
 327  327          ASSERT(vp->v_type == VREG);
 328  328  
 329  329          if (uio->uio_loffset >= UINT32_MAX && rw == UIO_READ) {
 330  330                  return (0);
 331  331          }
 332  332  
 333  333          if (uio->uio_loffset < 0)
 334  334                  return (EINVAL);
 335  335  
 336  336          if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
 337  337                  limit = MAXOFFSET_T;
 338  338  
 339  339          if (uio->uio_loffset >= limit && rw == UIO_WRITE) {
 340  340                  proc_t *p = ttoproc(curthread);
 341  341  
 342  342                  mutex_enter(&p->p_lock);
 343  343                  (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
 344  344                      p, RCA_UNSAFE_SIGINFO);
 345  345                  mutex_exit(&p->p_lock);
 346  346                  return (EFBIG);
 347  347          }
 348  348  
 349  349          /* the following condition will occur only for write */
 350  350  
 351  351          if (uio->uio_loffset >= UINT32_MAX)
 352  352                  return (EFBIG);
 353  353  
 354  354          if (uio->uio_resid == 0)
 355  355                  return (0);
 356  356  
 357  357          if (limit > UINT32_MAX)
 358  358                  limit = UINT32_MAX;
 359  359  
 360  360          fsp = VFSTOPCFS(vp->v_vfsp);
 361  361          if (fsp->pcfs_flags & PCFS_IRRECOV)
 362  362                  return (EIO);
 363  363  
 364  364          do {
 365  365                  /*
 366  366                   * Assignments to "n" in this block may appear
 367  367                   * to overflow in some cases.  However, after careful
 368  368                   * analysis it was determined that all assignments to
 369  369                   * "n" serve only to make "n" smaller.  Since "n"
 370  370                   * starts out as no larger than MAXBSIZE, "int" is
 371  371                   * safe.
 372  372                   */
 373  373                  off = uio->uio_loffset & MAXBMASK;
 374  374                  mapon = (int)(uio->uio_loffset & MAXBOFFSET);
 375  375                  n = MIN(MAXBSIZE - mapon, uio->uio_resid);
 376  376                  if (rw == UIO_READ) {
 377  377                          offset_t diff;
 378  378  
 379  379                          diff = pcp->pc_size - uio->uio_loffset;
 380  380                          if (diff <= 0)
 381  381                                  return (0);
 382  382                          if (diff < n)
 383  383                                  n = (int)diff;
 384  384                  }
 385  385                  /*
 386  386                   * Compare limit with the actual offset + n, not the
 387  387                   * rounded down offset "off" or we will overflow
 388  388                   * the maximum file size after all.
 389  389                   */
 390  390                  if (rw == UIO_WRITE && uio->uio_loffset + n >= limit) {
 391  391                          if (uio->uio_loffset >= limit) {
 392  392                                  error = EFBIG;
 393  393                                  break;
 394  394                          }
 395  395                          n = (int)(limit - uio->uio_loffset);
 396  396                  }
 397  397  
 398  398                  /*
 399  399                   * Touch the page and fault it in if it is not in
 400  400                   * core before segmap_getmapflt can lock it. This
 401  401                   * is to avoid the deadlock if the buffer is mapped
 402  402                   * to the same file through mmap which we want to
 403  403                   * write to.
 404  404                   */
 405  405                  uio_prefaultpages((long)n, uio);
 406  406  
 407  407                  base = segmap_getmap(segkmap, vp, (u_offset_t)off);
 408  408                  pagecreate = 0;
 409  409                  newpage = 0;
 410  410                  if (rw == UIO_WRITE) {
 411  411                          /*
 412  412                           * If PAGESIZE < MAXBSIZE, perhaps we ought to deal
 413  413                           * with one page at a time, instead of one MAXBSIZE
 414  414                           * at a time, so we can fully explore pagecreate
 415  415                           * optimization??
 416  416                           */
 417  417                          if (uio->uio_loffset + n > pcp->pc_size) {
 418  418                                  uint_t ncl, lcn;
 419  419  
 420  420                                  ncl = (uint_t)howmany((offset_t)pcp->pc_size,
 421  421                                      fsp->pcfs_clsize);
 422  422                                  if (uio->uio_loffset > pcp->pc_size &&
 423  423                                      ncl < (uint_t)howmany(uio->uio_loffset,
 424  424                                      fsp->pcfs_clsize)) {
 425  425                                          /*
 426  426                                           * Allocate and zerofill skipped
 427  427                                           * clusters. This may not be worth the
 428  428                                           * effort since a small lseek beyond
 429  429                                           * eof but still within the cluster
 430  430                                           * will not be zeroed out.
 431  431                                           */
 432  432                                          lcn = pc_lblkno(fsp, uio->uio_loffset);
 433  433                                          error = pc_balloc(pcp, (daddr_t)lcn,
 434  434                                              1, &bn);
 435  435                                          ncl = lcn + 1;
 436  436                                  }
 437  437                                  if (!error &&
 438  438                                      ncl < (uint_t)howmany(uio->uio_loffset + n,
 439  439                                      fsp->pcfs_clsize))
 440  440                                          /*
 441  441                                           * allocate clusters w/o zerofill
 442  442                                           */
 443  443                                          error = pc_balloc(pcp,
 444  444                                              (daddr_t)pc_lblkno(fsp,
 445  445                                              uio->uio_loffset + n - 1),
 446  446                                              0, &bn);
 447  447  
 448  448                                  pcp->pc_flags |= PC_CHG;
 449  449  
 450  450                                  if (error) {
 451  451                                          pc_cluster32_t ncl;
 452  452                                          int nerror;
 453  453  
 454  454                                          /*
 455  455                                           * figure out new file size from
 456  456                                           * cluster chain length. If this
 457  457                                           * is detected to loop, the chain
 458  458                                           * is corrupted and we'd better
 459  459                                           * keep our fingers off that file.
 460  460                                           */
 461  461                                          nerror = pc_fileclsize(fsp,
 462  462                                              pcp->pc_scluster, &ncl);
 463  463                                          if (nerror) {
 464  464                                                  PC_DPRINTF1(2,
 465  465                                                      "cluster chain "
 466  466                                                      "corruption, "
 467  467                                                      "scluster=%d\n",
 468  468                                                      pcp->pc_scluster);
 469  469                                                  pcp->pc_size = 0;
 470  470                                                  pcp->pc_flags |= PC_INVAL;
 471  471                                                  error = nerror;
 472  472                                                  (void) segmap_release(segkmap,
 473  473                                                      base, 0);
 474  474                                                  break;
 475  475                                          }
 476  476                                          pcp->pc_size = fsp->pcfs_clsize * ncl;
 477  477  
 478  478                                          if (error == ENOSPC &&
 479  479                                              (pcp->pc_size - uio->uio_loffset)
 480  480                                              > 0) {
 481  481                                                  PC_DPRINTF3(2, "rwpcp ENOSPC "
 482  482                                                      "off=%lld n=%d size=%d\n",
 483  483                                                      uio->uio_loffset,
 484  484                                                      n, pcp->pc_size);
 485  485                                                  n = (int)(pcp->pc_size -
 486  486                                                      uio->uio_loffset);
 487  487                                          } else {
 488  488                                                  PC_DPRINTF1(1,
 489  489                                                      "rwpcp error1=%d\n", error);
 490  490                                                  (void) segmap_release(segkmap,
 491  491                                                      base, 0);
 492  492                                                  break;
 493  493                                          }
 494  494                                  } else {
 495  495                                          pcp->pc_size =
 496  496                                              (uint_t)(uio->uio_loffset + n);
 497  497                                  }
 498  498                                  if (mapon == 0) {
 499  499                                          newpage = segmap_pagecreate(segkmap,
 500  500                                              base, (size_t)n, 0);
 501  501                                          pagecreate = 1;
 502  502                                  }
 503  503                          } else if (n == MAXBSIZE) {
 504  504                                  newpage = segmap_pagecreate(segkmap, base,
 505  505                                      (size_t)n, 0);
 506  506                                  pagecreate = 1;
 507  507                          }
 508  508                  }
 509  509                  error = uiomove(base + mapon, (size_t)n, rw, uio);
 510  510  
 511  511                  if (pagecreate && uio->uio_loffset <
 512  512                      roundup(off + mapon + n, PAGESIZE)) {
 513  513                          offset_t nzero, nmoved;
 514  514  
 515  515                          nmoved = uio->uio_loffset - (off + mapon);
 516  516                          nzero = roundup(mapon + n, PAGESIZE) - nmoved;
 517  517                          (void) kzero(base + mapon + nmoved, (size_t)nzero);
 518  518                  }
 519  519  
 520  520                  /*
 521  521                   * Unlock the pages which have been allocated by
 522  522                   * page_create_va() in segmap_pagecreate().
 523  523                   */
 524  524                  if (newpage) {
 525  525                          segmap_pageunlock(segkmap, base, (size_t)n,
 526  526                              rw == UIO_WRITE ? S_WRITE : S_READ);
 527  527                  }
 528  528  
 529  529                  if (error) {
 530  530                          PC_DPRINTF1(1, "rwpcp error2=%d\n", error);
 531  531                          /*
 532  532                           * If we failed on a write, we may have already
 533  533                           * allocated file blocks as well as pages.  It's hard
 534  534                           * to undo the block allocation, but we must be sure
 535  535                           * to invalidate any pages that may have been
 536  536                           * allocated.
 537  537                           */
 538  538                          if (rw == UIO_WRITE)
 539  539                                  (void) segmap_release(segkmap, base, SM_INVAL);
 540  540                          else
 541  541                                  (void) segmap_release(segkmap, base, 0);
 542  542                  } else {
 543  543                          uint_t flags = 0;
 544  544  
 545  545                          if (rw == UIO_READ) {
 546  546                                  if (n + mapon == MAXBSIZE ||
 547  547                                      uio->uio_loffset == pcp->pc_size)
 548  548                                          flags = SM_DONTNEED;
 549  549                          } else if (ioflag & (FSYNC|FDSYNC)) {
 550  550                                  flags = SM_WRITE;
 551  551                          } else if (n + mapon == MAXBSIZE) {
 552  552                                  flags = SM_WRITE|SM_ASYNC|SM_DONTNEED;
 553  553                          }
 554  554                          error = segmap_release(segkmap, base, flags);
 555  555                  }
 556  556  
 557  557          } while (error == 0 && uio->uio_resid > 0 && n != 0);
 558  558  
 559  559          if (oresid != uio->uio_resid)
 560  560                  error = 0;
 561  561          return (error);
 562  562  }
 563  563  
 564  564  /*ARGSUSED*/
 565  565  static int
 566  566  pcfs_getattr(
 567  567          struct vnode *vp,
 568  568          struct vattr *vap,
 569  569          int flags,
 570  570          struct cred *cr,
 571  571          caller_context_t *ct)
 572  572  {
 573  573          struct pcnode *pcp;
 574  574          struct pcfs *fsp;
 575  575          int error;
 576  576          char attr;
 577  577          struct pctime atime;
 578  578          int64_t unixtime;
 579  579  
 580  580          PC_DPRINTF1(8, "pcfs_getattr: vp=%p\n", (void *)vp);
 581  581  
 582  582          fsp = VFSTOPCFS(vp->v_vfsp);
 583  583          error = pc_lockfs(fsp, 0, 0);
 584  584          if (error)
 585  585                  return (error);
 586  586  
 587  587          /*
 588  588           * Note that we don't check for "invalid node" (PC_INVAL) here
 589  589           * only in order to make stat() succeed. We allow no I/O on such
 590  590           * a node, but do allow to check for its existence.
 591  591           */
 592  592          if ((pcp = VTOPC(vp)) == NULL) {
 593  593                  pc_unlockfs(fsp);
 594  594                  return (EIO);
 595  595          }
 596  596          /*
 597  597           * Copy from pcnode.
 598  598           */
 599  599          vap->va_type = vp->v_type;
 600  600          attr = pcp->pc_entry.pcd_attr;
 601  601          if (PCA_IS_HIDDEN(fsp, attr))
 602  602                  vap->va_mode = 0;
 603  603          else if (attr & PCA_LABEL)
 604  604                  vap->va_mode = 0444;
 605  605          else if (attr & PCA_RDONLY)
 606  606                  vap->va_mode = 0555;
 607  607          else if (fsp->pcfs_flags & PCFS_BOOTPART) {
 608  608                  vap->va_mode = 0755;
 609  609          } else {
 610  610                  vap->va_mode = 0777;
 611  611          }
 612  612  
 613  613          if (attr & PCA_DIR)
 614  614                  vap->va_mode |= S_IFDIR;
 615  615          else
 616  616                  vap->va_mode |= S_IFREG;
 617  617          if (fsp->pcfs_flags & PCFS_BOOTPART) {
 618  618                  vap->va_uid = 0;
 619  619                  vap->va_gid = 0;
 620  620          } else {
 621  621                  vap->va_uid = crgetuid(cr);
 622  622                  vap->va_gid = crgetgid(cr);
 623  623          }
 624  624          vap->va_fsid = vp->v_vfsp->vfs_dev;
 625  625          vap->va_nodeid = (ino64_t)pc_makenodeid(pcp->pc_eblkno,
 626  626              pcp->pc_eoffset, pcp->pc_entry.pcd_attr,
 627  627              pc_getstartcluster(fsp, &pcp->pc_entry), pc_direntpersec(fsp));
 628  628          vap->va_nlink = 1;
 629  629          vap->va_size = (u_offset_t)pcp->pc_size;
 630  630          vap->va_rdev = 0;
 631  631          vap->va_nblocks =
 632  632              (fsblkcnt64_t)howmany((offset_t)pcp->pc_size, DEV_BSIZE);
 633  633          vap->va_blksize = fsp->pcfs_clsize;
 634  634  
 635  635          /*
 636  636           * FAT root directories have no timestamps. In order not to return
 637  637           * "time zero" (1/1/1970), we record the time of the mount and give
 638  638           * that. This breaks less expectations.
 639  639           */
 640  640          if (vp->v_flag & VROOT) {
 641  641                  vap->va_mtime = fsp->pcfs_mounttime;
 642  642                  vap->va_atime = fsp->pcfs_mounttime;
 643  643                  vap->va_ctime = fsp->pcfs_mounttime;
 644  644                  pc_unlockfs(fsp);
 645  645                  return (0);
 646  646          }
 647  647  
 648  648          pc_pcttotv(&pcp->pc_entry.pcd_mtime, &unixtime);
 649  649          if ((fsp->pcfs_flags & PCFS_NOCLAMPTIME) == 0) {
 650  650                  if (unixtime > INT32_MAX)
 651  651                          DTRACE_PROBE1(pcfs__mtimeclamped, int64_t, unixtime);
 652  652                  unixtime = MIN(unixtime, INT32_MAX);
 653  653          } else if (unixtime > INT32_MAX &&
 654  654              get_udatamodel() == DATAMODEL_ILP32) {
 655  655                  pc_unlockfs(fsp);
 656  656                  DTRACE_PROBE1(pcfs__mtimeoverflowed, int64_t, unixtime);
 657  657                  return (EOVERFLOW);
 658  658          }
 659  659  
 660  660          vap->va_mtime.tv_sec = (time_t)unixtime;
 661  661          vap->va_mtime.tv_nsec = 0;
 662  662  
 663  663          /*
 664  664           * FAT doesn't know about POSIX ctime.
 665  665           * Best approximation is to always set it to mtime.
 666  666           */
 667  667          vap->va_ctime = vap->va_mtime;
 668  668  
 669  669          /*
 670  670           * FAT only stores "last access date". If that's the
 671  671           * same as the date of last modification then the time
 672  672           * of last access is known. Otherwise, use midnight.
 673  673           */
 674  674          atime.pct_date = pcp->pc_entry.pcd_ladate;
 675  675          if (atime.pct_date == pcp->pc_entry.pcd_mtime.pct_date)
 676  676                  atime.pct_time = pcp->pc_entry.pcd_mtime.pct_time;
 677  677          else
 678  678                  atime.pct_time = 0;
 679  679          pc_pcttotv(&atime, &unixtime);
 680  680          if ((fsp->pcfs_flags & PCFS_NOCLAMPTIME) == 0) {
 681  681                  if (unixtime > INT32_MAX)
 682  682                          DTRACE_PROBE1(pcfs__atimeclamped, int64_t, unixtime);
 683  683                  unixtime = MIN(unixtime, INT32_MAX);
 684  684          } else if (unixtime > INT32_MAX &&
 685  685              get_udatamodel() == DATAMODEL_ILP32) {
 686  686                  pc_unlockfs(fsp);
 687  687                  DTRACE_PROBE1(pcfs__atimeoverflowed, int64_t, unixtime);
 688  688                  return (EOVERFLOW);
 689  689          }
 690  690  
 691  691          vap->va_atime.tv_sec = (time_t)unixtime;
 692  692          vap->va_atime.tv_nsec = 0;
 693  693  
 694  694          pc_unlockfs(fsp);
 695  695          return (0);
 696  696  }
 697  697  
 698  698  
 699  699  /*ARGSUSED*/
 700  700  static int
 701  701  pcfs_setattr(
 702  702          struct vnode *vp,
 703  703          struct vattr *vap,
 704  704          int flags,
 705  705          struct cred *cr,
 706  706          caller_context_t *ct)
 707  707  {
 708  708          struct pcnode *pcp;
 709  709          mode_t mask = vap->va_mask;
 710  710          int error;
 711  711          struct pcfs *fsp;
 712  712          timestruc_t now, *timep;
 713  713  
 714  714          PC_DPRINTF2(6, "pcfs_setattr: vp=%p mask=%x\n", (void *)vp, (int)mask);
 715  715          /*
 716  716           * cannot set these attributes
 717  717           */
 718  718          if (mask & (AT_NOSET | AT_UID | AT_GID)) {
 719  719                  return (EINVAL);
 720  720          }
 721  721          /*
 722  722           * pcfs_setattr is now allowed on directories to avoid silly warnings
 723  723           * from 'tar' when it tries to set times on a directory, and console
 724  724           * printf's on the NFS server when it gets EINVAL back on such a
 725  725           * request. One possible problem with that since a directory entry
 726  726           * identifies a file, '.' and all the '..' entries in subdirectories
 727  727           * may get out of sync when the directory is updated since they're
 728  728           * treated like separate files. We could fix that by looking for
 729  729           * '.' and giving it the same attributes, and then looking for
 730  730           * all the subdirectories and updating '..', but that's pretty
 731  731           * expensive for something that doesn't seem likely to matter.
 732  732           */
 733  733          /* can't do some ops on directories anyway */
 734  734          if ((vp->v_type == VDIR) &&
 735  735              (mask & AT_SIZE)) {
 736  736                  return (EINVAL);
 737  737          }
 738  738  
 739  739          fsp = VFSTOPCFS(vp->v_vfsp);
 740  740          error = pc_lockfs(fsp, 0, 0);
 741  741          if (error)
 742  742                  return (error);
 743  743          if ((pcp = VTOPC(vp)) == NULL || pcp->pc_flags & PC_INVAL) {
 744  744                  pc_unlockfs(fsp);
 745  745                  return (EIO);
 746  746          }
 747  747  
 748  748          if (fsp->pcfs_flags & PCFS_BOOTPART) {
 749  749                  if (secpolicy_pcfs_modify_bootpartition(cr) != 0) {
 750  750                          pc_unlockfs(fsp);
 751  751                          return (EACCES);
 752  752                  }
 753  753          }
 754  754  
 755  755          /*
 756  756           * Change file access modes.
 757  757           * If nobody has write permission, file is marked readonly.
 758  758           * Otherwise file is writable by anyone.
 759  759           */
 760  760          if ((mask & AT_MODE) && (vap->va_mode != (mode_t)-1)) {
 761  761                  if ((vap->va_mode & 0222) == 0)
 762  762                          pcp->pc_entry.pcd_attr |= PCA_RDONLY;
 763  763                  else
 764  764                          pcp->pc_entry.pcd_attr &= ~PCA_RDONLY;
 765  765                  pcp->pc_flags |= PC_CHG;
 766  766          }
 767  767          /*
 768  768           * Truncate file. Must have write permission.
 769  769           */
 770  770          if ((mask & AT_SIZE) && (vap->va_size != (u_offset_t)-1)) {
 771  771                  if (pcp->pc_entry.pcd_attr & PCA_RDONLY) {
 772  772                          error = EACCES;
 773  773                          goto out;
 774  774                  }
 775  775                  if (vap->va_size > UINT32_MAX) {
 776  776                          error = EFBIG;
 777  777                          goto out;
 778  778                  }
 779  779                  error = pc_truncate(pcp, (uint_t)vap->va_size);
 780  780  
 781  781                  if (error)
 782  782                          goto out;
 783  783  
 784  784                  if (vap->va_size == 0) {
 785  785                          vnevent_truncate(vp, ct);
 786  786                  } else {
 787  787                          vnevent_resize(vp, ct);
 788  788                  }
 789  789          }
 790  790          /*
 791  791           * Change file modified times.
 792  792           */
 793  793          if (mask & (AT_MTIME | AT_CTIME)) {
 794  794                  /*
 795  795                   * If SysV-compatible option to set access and
 796  796                   * modified times if privileged, owner, or write access,
 797  797                   * use current time rather than va_mtime.
 798  798                   *
 799  799                   * XXX - va_mtime.tv_sec == -1 flags this.
 800  800                   */
 801  801                  timep = &vap->va_mtime;
 802  802                  if (vap->va_mtime.tv_sec == -1) {
 803  803                          gethrestime(&now);
 804  804                          timep = &now;
 805  805                  }
 806  806                  if ((fsp->pcfs_flags & PCFS_NOCLAMPTIME) == 0 &&
 807  807                      timep->tv_sec > INT32_MAX) {
 808  808                          error = EOVERFLOW;
 809  809                          goto out;
 810  810                  }
 811  811                  error = pc_tvtopct(timep, &pcp->pc_entry.pcd_mtime);
 812  812                  if (error)
 813  813                          goto out;
 814  814                  pcp->pc_flags |= PC_CHG;
 815  815          }
 816  816          /*
 817  817           * Change file access times.
 818  818           */
 819  819          if (mask & AT_ATIME) {
 820  820                  /*
 821  821                   * If SysV-compatible option to set access and
 822  822                   * modified times if privileged, owner, or write access,
 823  823                   * use current time rather than va_mtime.
 824  824                   *
 825  825                   * XXX - va_atime.tv_sec == -1 flags this.
 826  826                   */
 827  827                  struct pctime   atime;
 828  828  
 829  829                  timep = &vap->va_atime;
 830  830                  if (vap->va_atime.tv_sec == -1) {
 831  831                          gethrestime(&now);
 832  832                          timep = &now;
 833  833                  }
 834  834                  if ((fsp->pcfs_flags & PCFS_NOCLAMPTIME) == 0 &&
 835  835                      timep->tv_sec > INT32_MAX) {
 836  836                          error = EOVERFLOW;
 837  837                          goto out;
 838  838                  }
 839  839                  error = pc_tvtopct(timep, &atime);
 840  840                  if (error)
 841  841                          goto out;
 842  842                  pcp->pc_entry.pcd_ladate = atime.pct_date;
 843  843                  pcp->pc_flags |= PC_CHG;
 844  844          }
 845  845  out:
 846  846          pc_unlockfs(fsp);
 847  847          return (error);
 848  848  }
 849  849  
 850  850  
 851  851  /*ARGSUSED*/
 852  852  static int
 853  853  pcfs_access(
 854  854          struct vnode *vp,
 855  855          int mode,
 856  856          int flags,
 857  857          struct cred *cr,
 858  858          caller_context_t *ct)
 859  859  {
 860  860          struct pcnode *pcp;
 861  861          struct pcfs *fsp;
 862  862  
 863  863  
 864  864          fsp = VFSTOPCFS(vp->v_vfsp);
 865  865  
 866  866          if ((pcp = VTOPC(vp)) == NULL || pcp->pc_flags & PC_INVAL)
 867  867                  return (EIO);
 868  868          if ((mode & VWRITE) && (pcp->pc_entry.pcd_attr & PCA_RDONLY))
 869  869                  return (EACCES);
 870  870  
 871  871          /*
 872  872           * If this is a boot partition, privileged users have full access while
 873  873           * others have read-only access.
 874  874           */
 875  875          if (fsp->pcfs_flags & PCFS_BOOTPART) {
 876  876                  if ((mode & VWRITE) &&
 877  877                      secpolicy_pcfs_modify_bootpartition(cr) != 0)
 878  878                          return (EACCES);
 879  879          }
 880  880          return (0);
 881  881  }
 882  882  
 883  883  
 884  884  /*ARGSUSED*/
 885  885  static int
 886  886  pcfs_fsync(
 887  887          struct vnode *vp,
 888  888          int syncflag,
 889  889          struct cred *cr,
 890  890          caller_context_t *ct)
 891  891  {
 892  892          struct pcfs *fsp;
 893  893          struct pcnode *pcp;
 894  894          int error;
 895  895  
 896  896          fsp = VFSTOPCFS(vp->v_vfsp);
 897  897          if (error = pc_verify(fsp))
 898  898                  return (error);
 899  899          error = pc_lockfs(fsp, 0, 0);
 900  900          if (error)
 901  901                  return (error);
 902  902          if ((pcp = VTOPC(vp)) == NULL || pcp->pc_flags & PC_INVAL) {
 903  903                  pc_unlockfs(fsp);
 904  904                  return (EIO);
 905  905          }
 906  906          rw_enter(&pcnodes_lock, RW_WRITER);
 907  907          error = pc_nodesync(pcp);
 908  908          rw_exit(&pcnodes_lock);
 909  909          pc_unlockfs(fsp);
 910  910          return (error);
 911  911  }
 912  912  
 913  913  
 914  914  /*ARGSUSED*/
 915  915  static void
 916  916  pcfs_inactive(
 917  917          struct vnode *vp,
 918  918          struct cred *cr,
 919  919          caller_context_t *ct)
 920  920  {
 921  921          struct pcnode *pcp;
 922  922          struct pcfs *fsp;
 923  923          int error;
 924  924  
 925  925          fsp = VFSTOPCFS(vp->v_vfsp);
 926  926          error = pc_lockfs(fsp, 0, 1);
 927  927  
 928  928          /*
 929  929           * If the filesystem was umounted by force, all dirty
 930  930           * pages associated with this vnode are invalidated
 931  931           * and then the vnode will be freed.
 932  932           */
 933  933          if (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED) {
 934  934                  pcp = VTOPC(vp);
 935  935                  if (vn_has_cached_data(vp)) {
 936  936                          (void) pvn_vplist_dirty(vp, (u_offset_t)0,
 937  937                              pcfs_putapage, B_INVAL, (struct cred *)NULL);
 938  938                  }
 939  939                  remque(pcp);
 940  940                  if (error == 0)
 941  941                          pc_unlockfs(fsp);
 942  942                  vn_free(vp);
 943  943                  kmem_free(pcp, sizeof (struct pcnode));
 944  944                  VFS_RELE(PCFSTOVFS(fsp));
 945  945                  return;
 946  946          }
 947  947  
 948  948          mutex_enter(&vp->v_lock);
 949  949          ASSERT(vp->v_count >= 1);
 950  950          if (vp->v_count > 1) {
 951  951                  vp->v_count--;  /* release our hold from vn_rele */
 952  952                  mutex_exit(&vp->v_lock);
 953  953                  pc_unlockfs(fsp);
 954  954                  return;
 955  955          }
 956  956          mutex_exit(&vp->v_lock);
 957  957  
 958  958          /*
 959  959           * Check again to confirm that no intervening I/O error
 960  960           * with a subsequent pc_diskchanged() call has released
 961  961           * the pcnode. If it has then release the vnode as above.
 962  962           */
 963  963          pcp = VTOPC(vp);
 964  964          if (pcp == NULL || pcp->pc_flags & PC_INVAL) {
 965  965                  if (vn_has_cached_data(vp))
 966  966                          (void) pvn_vplist_dirty(vp, (u_offset_t)0,
 967  967                              pcfs_putapage, B_INVAL | B_TRUNC,
 968  968                              (struct cred *)NULL);
 969  969          }
 970  970  
 971  971          if (pcp == NULL) {
 972  972                  vn_free(vp);
 973  973          } else {
 974  974                  pc_rele(pcp);
 975  975          }
 976  976  
 977  977          if (!error)
 978  978                  pc_unlockfs(fsp);
 979  979  }
 980  980  
 981  981  /*ARGSUSED*/
 982  982  static int
 983  983  pcfs_lookup(
 984  984          struct vnode *dvp,
 985  985          char *nm,
 986  986          struct vnode **vpp,
 987  987          struct pathname *pnp,
 988  988          int flags,
 989  989          struct vnode *rdir,
 990  990          struct cred *cr,
 991  991          caller_context_t *ct,
 992  992          int *direntflags,
 993  993          pathname_t *realpnp)
 994  994  {
 995  995          struct pcfs *fsp;
 996  996          struct pcnode *pcp;
 997  997          int error;
 998  998  
 999  999          /*
1000 1000           * If the filesystem was umounted by force, return immediately.
1001 1001           */
1002 1002          if (dvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
1003 1003                  return (EIO);
1004 1004  
1005 1005          /*
1006 1006           * verify that the dvp is still valid on the disk
1007 1007           */
1008 1008          fsp = VFSTOPCFS(dvp->v_vfsp);
1009 1009          if (error = pc_verify(fsp))
1010 1010                  return (error);
1011 1011          error = pc_lockfs(fsp, 0, 0);
1012 1012          if (error)
1013 1013                  return (error);
1014 1014          if (VTOPC(dvp) == NULL || VTOPC(dvp)->pc_flags & PC_INVAL) {
1015 1015                  pc_unlockfs(fsp);
1016 1016                  return (EIO);
1017 1017          }
1018 1018          /*
1019 1019           * Null component name is a synonym for directory being searched.
1020 1020           */
1021 1021          if (*nm == '\0') {
1022 1022                  VN_HOLD(dvp);
1023 1023                  *vpp = dvp;
1024 1024                  pc_unlockfs(fsp);
1025 1025                  return (0);
1026 1026          }
1027 1027  
1028 1028          error = pc_dirlook(VTOPC(dvp), nm, &pcp);
1029 1029          if (!error) {
1030 1030                  *vpp = PCTOV(pcp);
1031 1031                  pcp->pc_flags |= PC_EXTERNAL;
1032 1032          }
1033 1033          pc_unlockfs(fsp);
1034 1034          return (error);
1035 1035  }
1036 1036  
1037 1037  
1038 1038  /*ARGSUSED*/
1039 1039  static int
1040 1040  pcfs_create(
1041 1041          struct vnode *dvp,
1042 1042          char *nm,
1043 1043          struct vattr *vap,
1044 1044          enum vcexcl exclusive,
1045 1045          int mode,
1046 1046          struct vnode **vpp,
1047 1047          struct cred *cr,
1048 1048          int flag,
1049 1049          caller_context_t *ct,
1050 1050          vsecattr_t *vsecp)
1051 1051  {
1052 1052          int error;
1053 1053          struct pcnode *pcp;
1054 1054          struct vnode *vp;
1055 1055          struct pcfs *fsp;
1056 1056  
1057 1057          /*
1058 1058           * can't create directories. use pcfs_mkdir.
1059 1059           * can't create anything other than files.
1060 1060           */
1061 1061          if (vap->va_type == VDIR)
1062 1062                  return (EISDIR);
1063 1063          else if (vap->va_type != VREG)
1064 1064                  return (EINVAL);
1065 1065  
1066 1066          pcp = NULL;
1067 1067          fsp = VFSTOPCFS(dvp->v_vfsp);
1068 1068          error = pc_lockfs(fsp, 0, 0);
1069 1069          if (error)
1070 1070                  return (error);
1071 1071          if (VTOPC(dvp) == NULL || VTOPC(dvp)->pc_flags & PC_INVAL) {
1072 1072                  pc_unlockfs(fsp);
1073 1073                  return (EIO);
1074 1074          }
1075 1075  
1076 1076          if (fsp->pcfs_flags & PCFS_BOOTPART) {
1077 1077                  if (secpolicy_pcfs_modify_bootpartition(cr) != 0) {
1078 1078                          pc_unlockfs(fsp);
1079 1079                          return (EACCES);
1080 1080                  }
1081 1081          }
1082 1082  
1083 1083          if (*nm == '\0') {
1084 1084                  /*
1085 1085                   * Null component name refers to the directory itself.
1086 1086                   */
1087 1087                  VN_HOLD(dvp);
1088 1088                  pcp = VTOPC(dvp);
1089 1089                  error = EEXIST;
1090 1090          } else {
1091 1091                  error = pc_direnter(VTOPC(dvp), nm, vap, &pcp);
1092 1092          }
1093 1093          /*
1094 1094           * if file exists and this is a nonexclusive create,
1095 1095           * check for access permissions
1096 1096           */
1097 1097          if (error == EEXIST) {
1098 1098                  vp = PCTOV(pcp);
1099 1099                  if (exclusive == NONEXCL) {
1100 1100                          if (vp->v_type == VDIR) {
1101 1101                                  error = EISDIR;
1102 1102                          } else if (mode) {
1103 1103                                  error = pcfs_access(PCTOV(pcp), mode, 0,
1104 1104                                      cr, ct);
1105 1105                          } else {
1106 1106                                  error = 0;
1107 1107                          }
1108 1108                  }
1109 1109                  if (error) {
1110 1110                          VN_RELE(PCTOV(pcp));
1111 1111                  } else if ((vp->v_type == VREG) && (vap->va_mask & AT_SIZE) &&
1112 1112                      (vap->va_size == 0)) {
1113 1113                          error = pc_truncate(pcp, 0L);
1114 1114                          if (error) {
1115 1115                                  VN_RELE(PCTOV(pcp));
1116 1116                          } else {
1117 1117                                  vnevent_create(PCTOV(pcp), ct);
1118 1118                          }
1119 1119                  }
1120 1120          }
1121 1121          if (error) {
1122 1122                  pc_unlockfs(fsp);
1123 1123                  return (error);
1124 1124          }
1125 1125          *vpp = PCTOV(pcp);
1126 1126          pcp->pc_flags |= PC_EXTERNAL;
1127 1127          pc_unlockfs(fsp);
1128 1128          return (error);
1129 1129  }
1130 1130  
1131 1131  /*ARGSUSED*/
1132 1132  static int
1133 1133  pcfs_remove(
1134 1134          struct vnode *vp,
1135 1135          char *nm,
1136 1136          struct cred *cr,
1137 1137          caller_context_t *ct,
1138 1138          int flags)
1139 1139  {
1140 1140          struct pcfs *fsp;
1141 1141          struct pcnode *pcp;
1142 1142          int error;
1143 1143  
1144 1144          fsp = VFSTOPCFS(vp->v_vfsp);
1145 1145          if (error = pc_verify(fsp))
1146 1146                  return (error);
1147 1147          error = pc_lockfs(fsp, 0, 0);
1148 1148          if (error)
1149 1149                  return (error);
1150 1150          if ((pcp = VTOPC(vp)) == NULL || pcp->pc_flags & PC_INVAL) {
1151 1151                  pc_unlockfs(fsp);
1152 1152                  return (EIO);
1153 1153          }
1154 1154          if (fsp->pcfs_flags & PCFS_BOOTPART) {
1155 1155                  if (secpolicy_pcfs_modify_bootpartition(cr) != 0) {
1156 1156                          pc_unlockfs(fsp);
1157 1157                          return (EACCES);
1158 1158                  }
1159 1159          }
1160 1160          error = pc_dirremove(pcp, nm, (struct vnode *)0, VREG, ct);
1161 1161          pc_unlockfs(fsp);
1162 1162          return (error);
1163 1163  }
1164 1164  
1165 1165  /*
1166 1166   * Rename a file or directory
1167 1167   * This rename is restricted to only rename files within a directory.
1168 1168   * XX should make rename more general
1169 1169   */
1170 1170  /*ARGSUSED*/
1171 1171  static int
1172 1172  pcfs_rename(
1173 1173          struct vnode *sdvp,             /* old (source) parent vnode */
1174 1174          char *snm,                      /* old (source) entry name */
1175 1175          struct vnode *tdvp,             /* new (target) parent vnode */
1176 1176          char *tnm,                      /* new (target) entry name */
1177 1177          struct cred *cr,
1178 1178          caller_context_t *ct,
1179 1179          int flags)
1180 1180  {
1181 1181          struct pcfs *fsp;
1182 1182          struct pcnode *dp;      /* parent pcnode */
1183 1183          struct pcnode *tdp;
1184 1184          int error;
1185 1185  
1186 1186          fsp = VFSTOPCFS(sdvp->v_vfsp);
1187 1187          if (error = pc_verify(fsp))
1188 1188                  return (error);
1189 1189  
1190 1190          /*
1191 1191           * make sure we can muck with this directory.
1192 1192           */
1193 1193          error = pcfs_access(sdvp, VWRITE, 0, cr, ct);
1194 1194          if (error) {
1195 1195                  return (error);
1196 1196          }
1197 1197          error = pc_lockfs(fsp, 0, 0);
1198 1198          if (error)
1199 1199                  return (error);
1200 1200          if (((dp = VTOPC(sdvp)) == NULL) || ((tdp = VTOPC(tdvp)) == NULL) ||
1201 1201              (dp->pc_flags & PC_INVAL) || (tdp->pc_flags & PC_INVAL)) {
1202 1202                  pc_unlockfs(fsp);
1203 1203                  return (EIO);
1204 1204          }
1205 1205          error = pc_rename(dp, tdp, snm, tnm, ct);
1206 1206          pc_unlockfs(fsp);
1207 1207          return (error);
1208 1208  }
1209 1209  
1210 1210  /*ARGSUSED*/
1211 1211  static int
1212 1212  pcfs_mkdir(
1213 1213          struct vnode *dvp,
1214 1214          char *nm,
1215 1215          struct vattr *vap,
1216 1216          struct vnode **vpp,
1217 1217          struct cred *cr,
1218 1218          caller_context_t *ct,
1219 1219          int flags,
1220 1220          vsecattr_t *vsecp)
1221 1221  {
1222 1222          struct pcfs *fsp;
1223 1223          struct pcnode *pcp;
1224 1224          int error;
1225 1225  
1226 1226          fsp = VFSTOPCFS(dvp->v_vfsp);
1227 1227          if (error = pc_verify(fsp))
1228 1228                  return (error);
1229 1229          error = pc_lockfs(fsp, 0, 0);
1230 1230          if (error)
1231 1231                  return (error);
1232 1232          if (VTOPC(dvp) == NULL || VTOPC(dvp)->pc_flags & PC_INVAL) {
1233 1233                  pc_unlockfs(fsp);
1234 1234                  return (EIO);
1235 1235          }
1236 1236  
1237 1237          if (fsp->pcfs_flags & PCFS_BOOTPART) {
1238 1238                  if (secpolicy_pcfs_modify_bootpartition(cr) != 0) {
1239 1239                          pc_unlockfs(fsp);
1240 1240                          return (EACCES);
1241 1241                  }
1242 1242          }
1243 1243  
1244 1244          error = pc_direnter(VTOPC(dvp), nm, vap, &pcp);
1245 1245  
1246 1246          if (!error) {
1247 1247                  pcp -> pc_flags |= PC_EXTERNAL;
1248 1248                  *vpp = PCTOV(pcp);
1249 1249          } else if (error == EEXIST) {
1250 1250                  VN_RELE(PCTOV(pcp));
1251 1251          }
1252 1252          pc_unlockfs(fsp);
1253 1253          return (error);
1254 1254  }
1255 1255  
1256 1256  /*ARGSUSED*/
1257 1257  static int
1258 1258  pcfs_rmdir(
1259 1259          struct vnode *dvp,
1260 1260          char *nm,
1261 1261          struct vnode *cdir,
1262 1262          struct cred *cr,
1263 1263          caller_context_t *ct,
1264 1264          int flags)
1265 1265  {
1266 1266          struct pcfs *fsp;
1267 1267          struct pcnode *pcp;
1268 1268          int error;
1269 1269  
1270 1270          fsp = VFSTOPCFS(dvp -> v_vfsp);
1271 1271          if (error = pc_verify(fsp))
1272 1272                  return (error);
1273 1273          if (error = pc_lockfs(fsp, 0, 0))
1274 1274                  return (error);
1275 1275  
1276 1276          if ((pcp = VTOPC(dvp)) == NULL || pcp->pc_flags & PC_INVAL) {
1277 1277                  pc_unlockfs(fsp);
1278 1278                  return (EIO);
1279 1279          }
1280 1280  
1281 1281          if (fsp->pcfs_flags & PCFS_BOOTPART) {
1282 1282                  if (secpolicy_pcfs_modify_bootpartition(cr) != 0) {
1283 1283                          pc_unlockfs(fsp);
1284 1284                          return (EACCES);
1285 1285                  }
1286 1286          }
1287 1287  
1288 1288          error = pc_dirremove(pcp, nm, cdir, VDIR, ct);
1289 1289          pc_unlockfs(fsp);
1290 1290          return (error);
1291 1291  }
1292 1292  
1293 1293  /*
1294 1294   * read entries in a directory.
1295 1295   * we must convert pc format to unix format
1296 1296   */
1297 1297  
1298 1298  /*ARGSUSED*/
1299 1299  static int
1300 1300  pcfs_readdir(
1301 1301          struct vnode *dvp,
1302 1302          struct uio *uiop,
1303 1303          struct cred *cr,
1304 1304          int *eofp,
1305 1305          caller_context_t *ct,
1306 1306          int flags)
1307 1307  {
1308 1308          struct pcnode *pcp;
1309 1309          struct pcfs *fsp;
1310 1310          struct pcdir *ep;
1311 1311          struct buf *bp = NULL;
1312 1312          offset_t offset;
1313 1313          int boff;
1314 1314          struct pc_dirent lbp;
1315 1315          struct pc_dirent *ld = &lbp;
1316 1316          int error;
1317 1317  
1318 1318          /*
1319 1319           * If the filesystem was umounted by force, return immediately.
1320 1320           */
1321 1321          if (dvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
1322 1322                  return (EIO);
1323 1323  
1324 1324          if ((uiop->uio_iovcnt != 1) ||
1325 1325              (uiop->uio_loffset % sizeof (struct pcdir)) != 0) {
1326 1326                  return (EINVAL);
1327 1327          }
1328 1328          fsp = VFSTOPCFS(dvp->v_vfsp);
1329 1329          /*
1330 1330           * verify that the dp is still valid on the disk
1331 1331           */
1332 1332          if (error = pc_verify(fsp)) {
1333 1333                  return (error);
1334 1334          }
1335 1335          error = pc_lockfs(fsp, 0, 0);
1336 1336          if (error)
1337 1337                  return (error);
1338 1338          if ((pcp = VTOPC(dvp)) == NULL || pcp->pc_flags & PC_INVAL) {
1339 1339                  pc_unlockfs(fsp);
1340 1340                  return (EIO);
1341 1341          }
1342 1342  
1343 1343          bzero(ld, sizeof (*ld));
1344 1344  
1345 1345          if (eofp != NULL)
1346 1346                  *eofp = 0;
1347 1347          offset = uiop->uio_loffset;
1348 1348  
1349 1349          if (dvp->v_flag & VROOT) {
1350 1350                  /*
1351 1351                   * kludge up entries for "." and ".." in the root.
1352 1352                   */
1353 1353                  if (offset == 0) {
1354 1354                          (void) strcpy(ld->d_name, ".");
1355 1355                          ld->d_reclen = DIRENT64_RECLEN(1);
1356 1356                          ld->d_off = (off64_t)sizeof (struct pcdir);
1357 1357                          ld->d_ino = (ino64_t)UINT_MAX;
1358 1358                          if (ld->d_reclen > uiop->uio_resid) {
1359 1359                                  pc_unlockfs(fsp);
1360 1360                                  return (ENOSPC);
1361 1361                          }
1362 1362                          (void) uiomove(ld, ld->d_reclen, UIO_READ, uiop);
1363 1363                          uiop->uio_loffset = ld->d_off;
1364 1364                          offset = uiop->uio_loffset;
1365 1365                  }
1366 1366                  if (offset == sizeof (struct pcdir)) {
1367 1367                          (void) strcpy(ld->d_name, "..");
1368 1368                          ld->d_reclen = DIRENT64_RECLEN(2);
1369 1369                          if (ld->d_reclen > uiop->uio_resid) {
1370 1370                                  pc_unlockfs(fsp);
1371 1371                                  return (ENOSPC);
1372 1372                          }
1373 1373                          ld->d_off = (off64_t)(uiop->uio_loffset +
1374 1374                              sizeof (struct pcdir));
1375 1375                          ld->d_ino = (ino64_t)UINT_MAX;
1376 1376                          (void) uiomove(ld, ld->d_reclen, UIO_READ, uiop);
1377 1377                          uiop->uio_loffset = ld->d_off;
1378 1378                          offset = uiop->uio_loffset;
1379 1379                  }
1380 1380                  offset -= 2 * sizeof (struct pcdir);
1381 1381                  /* offset now has the real offset value into directory file */
1382 1382          }
1383 1383  
1384 1384          for (;;) {
1385 1385                  boff = pc_blkoff(fsp, offset);
1386 1386                  if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
1387 1387                          if (bp != NULL) {
1388 1388                                  brelse(bp);
1389 1389                                  bp = NULL;
1390 1390                          }
1391 1391                          error = pc_blkatoff(pcp, offset, &bp, &ep);
1392 1392                          if (error) {
1393 1393                                  if (error == ENOENT) {
1394 1394                                          error = 0;
1395 1395                                          if (eofp)
1396 1396                                                  *eofp = 1;
1397 1397                                  }
1398 1398                                  break;
1399 1399                          }
1400 1400                  }
1401 1401                  if (ep->pcd_filename[0] == PCD_UNUSED) {
1402 1402                          if (eofp)
1403 1403                                  *eofp = 1;
1404 1404                          break;
1405 1405                  }
1406 1406                  /*
1407 1407                   * Don't display label because it may contain funny characters.
1408 1408                   */
1409 1409                  if (ep->pcd_filename[0] == PCD_ERASED) {
1410 1410                          uiop->uio_loffset += sizeof (struct pcdir);
1411 1411                          offset += sizeof (struct pcdir);
1412 1412                          ep++;
1413 1413                          continue;
1414 1414                  }
1415 1415                  if (PCDL_IS_LFN(ep)) {
1416 1416                          if (pc_read_long_fn(dvp, uiop, ld, &ep, &offset, &bp) !=
1417 1417                              0)
1418 1418                                  break;
1419 1419                          continue;
1420 1420                  }
1421 1421  
1422 1422                  if (pc_read_short_fn(dvp, uiop, ld, &ep, &offset, &bp) != 0)
1423 1423                          break;
1424 1424          }
1425 1425          if (bp)
1426 1426                  brelse(bp);
1427 1427          pc_unlockfs(fsp);
1428 1428          return (error);
1429 1429  }
1430 1430  
1431 1431  
1432 1432  /*
1433 1433   * Called from pvn_getpages to get a particular page.  When we are called
1434 1434   * the pcfs is already locked.
1435 1435   */
1436 1436  /*ARGSUSED*/
1437 1437  static int
1438 1438  pcfs_getapage(
1439 1439          struct vnode *vp,
1440 1440          u_offset_t off,
1441 1441          size_t len,
1442 1442          uint_t *protp,
1443 1443          page_t *pl[],           /* NULL if async IO is requested */
1444 1444          size_t plsz,
1445 1445          struct seg *seg,
1446 1446          caddr_t addr,
1447 1447          enum seg_rw rw,
1448 1448          struct cred *cr)
1449 1449  {
1450 1450          struct pcnode *pcp;
1451 1451          struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
1452 1452          struct vnode *devvp;
1453 1453          page_t *pp;
1454 1454          page_t *pagefound;
1455 1455          int err;
1456 1456  
1457 1457          /*
1458 1458           * If the filesystem was umounted by force, return immediately.
1459 1459           */
1460 1460          if (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
1461 1461                  return (EIO);
1462 1462  
1463 1463          PC_DPRINTF3(5, "pcfs_getapage: vp=%p off=%lld len=%lu\n",
1464 1464              (void *)vp, off, len);
1465 1465  
1466 1466          if ((pcp = VTOPC(vp)) == NULL || pcp->pc_flags & PC_INVAL)
1467 1467                  return (EIO);
1468 1468          devvp = fsp->pcfs_devvp;
1469 1469  
1470 1470          /* pcfs doesn't do readaheads */
1471 1471          if (pl == NULL)
1472 1472                  return (0);
1473 1473  
1474 1474          pl[0] = NULL;
1475 1475          err = 0;
1476 1476          /*
1477 1477           * If the accessed time on the pcnode has not already been
1478 1478           * set elsewhere (e.g. for read/setattr) we set the time now.
1479 1479           * This gives us approximate modified times for mmap'ed files
1480 1480           * which are accessed via loads in the user address space.
1481 1481           */
1482 1482          if ((pcp->pc_flags & PC_ACC) == 0 &&
1483 1483              ((fsp->pcfs_vfs->vfs_flag & VFS_RDONLY) == 0)) {
1484 1484                  pc_mark_acc(fsp, pcp);
1485 1485          }
1486 1486  reread:
1487 1487          if ((pagefound = page_exists(vp, off)) == NULL) {
1488 1488                  /*
1489 1489                   * Need to really do disk IO to get the page(s).
1490 1490                   */
1491 1491                  struct buf *bp;
1492 1492                  daddr_t lbn, bn;
1493 1493                  u_offset_t io_off;
1494 1494                  size_t io_len;
1495 1495                  u_offset_t lbnoff, xferoffset;
1496 1496                  u_offset_t pgoff;
1497 1497                  uint_t  xfersize;
1498 1498                  int err1;
1499 1499  
1500 1500                  lbn = pc_lblkno(fsp, off);
1501 1501                  lbnoff = off & ~(fsp->pcfs_clsize - 1);
1502 1502                  xferoffset = off & ~(fsp->pcfs_secsize - 1);
1503 1503  
1504 1504                  pp = pvn_read_kluster(vp, off, seg, addr, &io_off, &io_len,
1505 1505                      off, (size_t)MIN(pc_blksize(fsp, pcp, off), PAGESIZE), 0);
1506 1506                  if (pp == NULL)
1507 1507                          /*
1508 1508                           * XXX - If pcfs is made MT-hot, this should go
1509 1509                           * back to reread.
1510 1510                           */
1511 1511                          panic("pcfs_getapage pvn_read_kluster");
1512 1512  
1513 1513                  for (pgoff = 0; pgoff < PAGESIZE && xferoffset < pcp->pc_size;
1514 1514                      pgoff += xfersize,
1515 1515                      lbn +=  howmany(xfersize, fsp->pcfs_clsize),
1516 1516                      lbnoff += xfersize, xferoffset += xfersize) {
1517 1517                          /*
1518 1518                           * read as many contiguous blocks as possible to
1519 1519                           * fill this page
1520 1520                           */
1521 1521                          xfersize = PAGESIZE - pgoff;
1522 1522                          err1 = pc_bmap(pcp, lbn, &bn, &xfersize);
1523 1523                          if (err1) {
1524 1524                                  PC_DPRINTF1(1, "pc_getapage err=%d", err1);
1525 1525                                  err = err1;
1526 1526                                  goto out;
1527 1527                          }
1528 1528                          bp = pageio_setup(pp, xfersize, devvp, B_READ);
1529 1529                          bp->b_edev = devvp->v_rdev;
1530 1530                          bp->b_dev = cmpdev(devvp->v_rdev);
1531 1531                          bp->b_blkno = bn + btodt(xferoffset - lbnoff);
1532 1532                          bp->b_un.b_addr = (caddr_t)(uintptr_t)pgoff;
1533 1533                          bp->b_file = vp;
1534 1534                          bp->b_offset = (offset_t)(off + pgoff);
1535 1535  
1536 1536                          (void) bdev_strategy(bp);
1537 1537  
1538 1538                          lwp_stat_update(LWP_STAT_INBLK, 1);
1539 1539  
1540 1540                          if (err == 0)
1541 1541                                  err = biowait(bp);
1542 1542                          else
1543 1543                                  (void) biowait(bp);
1544 1544                          pageio_done(bp);
1545 1545                          if (err)
1546 1546                                  goto out;
1547 1547                  }
1548 1548                  if (pgoff < PAGESIZE) {
1549 1549                          pagezero(pp->p_prev, pgoff, PAGESIZE - pgoff);
1550 1550                  }
1551 1551                  pvn_plist_init(pp, pl, plsz, off, io_len, rw);
1552 1552          }
1553 1553  out:
1554 1554          if (err) {
1555 1555                  if (pp != NULL)
1556 1556                          pvn_read_done(pp, B_ERROR);
1557 1557                  return (err);
1558 1558          }
1559 1559  
1560 1560          if (pagefound) {
1561 1561                  /*
1562 1562                   * Page exists in the cache, acquire the "shared"
1563 1563                   * lock.  If this fails, go back to reread.
1564 1564                   */
1565 1565                  if ((pp = page_lookup(vp, off, SE_SHARED)) == NULL) {
1566 1566                          goto reread;
1567 1567                  }
1568 1568                  pl[0] = pp;
1569 1569                  pl[1] = NULL;
1570 1570          }
1571 1571          return (err);
1572 1572  }
1573 1573  
1574 1574  /*
1575 1575   * Return all the pages from [off..off+len] in given file
1576 1576   */
1577 1577  /* ARGSUSED */
1578 1578  static int
1579 1579  pcfs_getpage(
1580 1580          struct vnode *vp,
1581 1581          offset_t off,
1582 1582          size_t len,
1583 1583          uint_t *protp,
1584 1584          page_t *pl[],
1585 1585          size_t plsz,
1586 1586          struct seg *seg,
1587 1587          caddr_t addr,
1588 1588          enum seg_rw rw,
1589 1589          struct cred *cr,
1590 1590          caller_context_t *ct)
1591 1591  {
1592 1592          struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
1593 1593          int err;
1594 1594  
1595 1595          PC_DPRINTF0(6, "pcfs_getpage\n");
1596 1596          if (err = pc_verify(fsp))
1597 1597                  return (err);
1598 1598          if (vp->v_flag & VNOMAP)
1599 1599                  return (ENOSYS);
1600 1600          ASSERT(off <= UINT32_MAX);
1601 1601          err = pc_lockfs(fsp, 0, 0);
1602 1602          if (err)
1603 1603                  return (err);
1604 1604          if (protp != NULL)
1605 1605                  *protp = PROT_ALL;
1606 1606  
1607 1607          ASSERT((off & PAGEOFFSET) == 0);
1608 1608          err = pvn_getpages(pcfs_getapage, vp, off, len, protp, pl, plsz,
1609 1609              seg, addr, rw, cr);
1610 1610  
1611 1611          pc_unlockfs(fsp);
1612 1612          return (err);
1613 1613  }
1614 1614  
1615 1615  
1616 1616  /*
1617 1617   * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
1618 1618   * If len == 0, do from off to EOF.
1619 1619   *
1620 1620   * The normal cases should be len == 0 & off == 0 (entire vp list),
1621 1621   * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
1622 1622   * (from pageout).
1623 1623   *
1624 1624   */
1625 1625  /*ARGSUSED*/
1626 1626  static int
1627 1627  pcfs_putpage(
1628 1628          struct vnode *vp,
1629 1629          offset_t off,
1630 1630          size_t len,
1631 1631          int flags,
1632 1632          struct cred *cr,
1633 1633          caller_context_t *ct)
1634 1634  {
1635 1635          struct pcnode *pcp;
1636 1636          page_t *pp;
1637 1637          struct pcfs *fsp;
1638 1638          u_offset_t io_off;
1639 1639          size_t io_len;
1640 1640          offset_t eoff;
1641 1641          int err;
1642 1642  
1643 1643          /*
1644 1644           * If the filesystem was umounted by force, return immediately.
1645 1645           */
1646 1646          if (vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
1647 1647                  return (EIO);
1648 1648  
1649 1649          PC_DPRINTF1(6, "pcfs_putpage vp=0x%p\n", (void *)vp);
1650 1650          if (vp->v_flag & VNOMAP)
1651 1651                  return (ENOSYS);
1652 1652  
1653 1653          fsp = VFSTOPCFS(vp->v_vfsp);
1654 1654  
1655 1655          if (err = pc_verify(fsp))
1656 1656                  return (err);
1657 1657          if ((pcp = VTOPC(vp)) == NULL) {
1658 1658                  PC_DPRINTF1(3, "pcfs_putpage NULL vp=0x%p\n", (void *)vp);
1659 1659                  return (EIO);
1660 1660          }
1661 1661          if (pcp->pc_flags & PC_INVAL)
1662 1662                  return (EIO);
1663 1663  
1664 1664          if (curproc == proc_pageout) {
1665 1665                  /*
1666 1666                   * XXX - This is a quick hack to avoid blocking
1667 1667                   * pageout. Also to avoid pcfs_getapage deadlocking
1668 1668                   * with putpage when memory is running out,
1669 1669                   * since we only have one global lock and we don't
1670 1670                   * support async putpage.
1671 1671                   * It should be fixed someday.
1672 1672                   *
1673 1673                   * Interestingly, this used to be a test of NOMEMWAIT().
1674 1674                   * We only ever got here once pcfs started supporting
1675 1675                   * NFS sharing, and then only because the NFS server
1676 1676                   * threads seem to do writes in sched's process context.
1677 1677                   * Since everyone else seems to just care about pageout,
1678 1678                   * the test was changed to look for pageout directly.
1679 1679                   */
1680 1680                  return (ENOMEM);
1681 1681          }
1682 1682  
1683 1683          ASSERT(off <= UINT32_MAX);
1684 1684  
1685 1685          flags &= ~B_ASYNC;      /* XXX should fix this later */
1686 1686  
1687 1687          err = pc_lockfs(fsp, 0, 0);
1688 1688          if (err)
1689 1689                  return (err);
1690 1690          if (!vn_has_cached_data(vp) || off >= pcp->pc_size) {
1691 1691                  pc_unlockfs(fsp);
1692 1692                  return (0);
1693 1693          }
1694 1694  
1695 1695          if (len == 0) {
1696 1696                  /*
1697 1697                   * Search the entire vp list for pages >= off
1698 1698                   */
1699 1699                  err = pvn_vplist_dirty(vp, off,
1700 1700                      pcfs_putapage, flags, cr);
1701 1701          } else {
1702 1702                  eoff = off + len;
1703 1703  
1704 1704                  for (io_off = off; io_off < eoff &&
1705 1705                      io_off < pcp->pc_size; io_off += io_len) {
1706 1706                          /*
1707 1707                           * If we are not invalidating, synchronously
1708 1708                           * freeing or writing pages use the routine
1709 1709                           * page_lookup_nowait() to prevent reclaiming
1710 1710                           * them from the free list.
1711 1711                           */
1712 1712                          if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
1713 1713                                  pp = page_lookup(vp, io_off,
1714 1714                                      (flags & (B_INVAL | B_FREE)) ?
1715 1715                                      SE_EXCL : SE_SHARED);
1716 1716                          } else {
1717 1717                                  pp = page_lookup_nowait(vp, io_off,
1718 1718                                      (flags & B_FREE) ? SE_EXCL : SE_SHARED);
1719 1719                          }
1720 1720  
1721 1721                          if (pp == NULL || pvn_getdirty(pp, flags) == 0)
1722 1722                                  io_len = PAGESIZE;
1723 1723                          else {
1724 1724                                  err = pcfs_putapage(vp, pp, &io_off, &io_len,
1725 1725                                      flags, cr);
1726 1726                                  if (err != 0)
1727 1727                                          break;
1728 1728                                  /*
1729 1729                                   * "io_off" and "io_len" are returned as
1730 1730                                   * the range of pages we actually wrote.
1731 1731                                   * This allows us to skip ahead more quickly
1732 1732                                   * since several pages may've been dealt
1733 1733                                   * with by this iteration of the loop.
1734 1734                                   */
1735 1735                          }
1736 1736                  }
1737 1737          }
1738 1738          if (err == 0 && (flags & B_INVAL) &&
1739 1739              off == 0 && len == 0 && vn_has_cached_data(vp)) {
1740 1740                  /*
1741 1741                   * If doing "invalidation", make sure that
1742 1742                   * all pages on the vnode list are actually
1743 1743                   * gone.
1744 1744                   */
1745 1745                  cmn_err(CE_PANIC,
1746 1746                      "pcfs_putpage: B_INVAL, pages not gone");
1747 1747          } else if (err) {
1748 1748                  PC_DPRINTF1(1, "pcfs_putpage err=%d\n", err);
1749 1749          }
1750 1750          pc_unlockfs(fsp);
1751 1751          return (err);
1752 1752  }
1753 1753  
1754 1754  /*
1755 1755   * Write out a single page, possibly klustering adjacent dirty pages.
1756 1756   */
1757 1757  /*ARGSUSED*/
1758 1758  int
1759 1759  pcfs_putapage(
1760 1760          struct vnode *vp,
1761 1761          page_t *pp,
1762 1762          u_offset_t *offp,
1763 1763          size_t *lenp,
1764 1764          int flags,
1765 1765          struct cred *cr)
1766 1766  {
1767 1767          struct pcnode *pcp;
1768 1768          struct pcfs *fsp;
1769 1769          struct vnode *devvp;
1770 1770          size_t io_len;
1771 1771          daddr_t bn;
1772 1772          u_offset_t lbn, lbnoff, xferoffset;
1773 1773          uint_t pgoff, xfersize;
1774 1774          int err = 0;
1775 1775          u_offset_t io_off;
1776 1776  
1777 1777          pcp = VTOPC(vp);
1778 1778          fsp = VFSTOPCFS(vp->v_vfsp);
1779 1779          devvp = fsp->pcfs_devvp;
1780 1780  
1781 1781          /*
1782 1782           * If the modified time on the inode has not already been
1783 1783           * set elsewhere (e.g. for write/setattr) and this is not
1784 1784           * a call from msync (B_FORCE) we set the time now.
1785 1785           * This gives us approximate modified times for mmap'ed files
1786 1786           * which are modified via stores in the user address space.
1787 1787           */
1788 1788          if ((pcp->pc_flags & PC_MOD) == 0 || (flags & B_FORCE)) {
1789 1789                  pcp->pc_flags |= PC_MOD;
1790 1790                  pc_mark_mod(fsp, pcp);
1791 1791          }
1792 1792          pp = pvn_write_kluster(vp, pp, &io_off, &io_len, pp->p_offset,
1793 1793              PAGESIZE, flags);
1794 1794  
1795 1795          if (fsp->pcfs_flags & PCFS_IRRECOV) {
1796 1796                  goto out;
1797 1797          }
1798 1798  
1799 1799          PC_DPRINTF1(7, "pc_putpage writing dirty page off=%llu\n", io_off);
1800 1800  
1801 1801          lbn = pc_lblkno(fsp, io_off);
1802 1802          lbnoff = io_off & ~(fsp->pcfs_clsize - 1);
1803 1803          xferoffset = io_off & ~(fsp->pcfs_secsize - 1);
1804 1804  
1805 1805          for (pgoff = 0; pgoff < io_len && xferoffset < pcp->pc_size;
1806 1806              pgoff += xfersize,
1807 1807              lbn += howmany(xfersize, fsp->pcfs_clsize),
1808 1808              lbnoff += xfersize, xferoffset += xfersize) {
1809 1809  
1810 1810                  struct buf *bp;
1811 1811                  int err1;
1812 1812  
1813 1813                  /*
1814 1814                   * write as many contiguous blocks as possible from this page
1815 1815                   */
1816 1816                  xfersize = io_len - pgoff;
1817 1817                  err1 = pc_bmap(pcp, (daddr_t)lbn, &bn, &xfersize);
1818 1818                  if (err1) {
1819 1819                          err = err1;
1820 1820                          goto out;
1821 1821                  }
1822 1822                  bp = pageio_setup(pp, xfersize, devvp, B_WRITE | flags);
1823 1823                  bp->b_edev = devvp->v_rdev;
1824 1824                  bp->b_dev = cmpdev(devvp->v_rdev);
1825 1825                  bp->b_blkno = bn + btodt(xferoffset - lbnoff);
1826 1826                  bp->b_un.b_addr = (caddr_t)(uintptr_t)pgoff;
1827 1827                  bp->b_file = vp;
1828 1828                  bp->b_offset = (offset_t)(io_off + pgoff);
1829 1829  
1830 1830                  (void) bdev_strategy(bp);
1831 1831  
1832 1832                  lwp_stat_update(LWP_STAT_OUBLK, 1);
1833 1833  
1834 1834                  if (err == 0)
1835 1835                          err = biowait(bp);
1836 1836                  else
1837 1837                          (void) biowait(bp);
1838 1838                  pageio_done(bp);
1839 1839          }
1840 1840          pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
1841 1841          pp = NULL;
1842 1842  
1843 1843  out:
1844 1844          if ((fsp->pcfs_flags & PCFS_IRRECOV) && pp != NULL) {
1845 1845                  pvn_write_done(pp, B_WRITE | flags);
1846 1846          } else if (err != 0 && pp != NULL) {
1847 1847                  pvn_write_done(pp, B_ERROR | B_WRITE | flags);
1848 1848          }
1849 1849  
1850 1850          if (offp)
1851 1851                  *offp = io_off;
1852 1852          if (lenp)
1853 1853                  *lenp = io_len;
1854 1854                  PC_DPRINTF4(4, "pcfs_putapage: vp=%p pp=%p off=%lld len=%lu\n",
1855 1855                      (void *)vp, (void *)pp, io_off, io_len);
1856 1856          if (err) {
1857 1857                  PC_DPRINTF1(1, "pcfs_putapage err=%d", err);
1858 1858          }
1859 1859          return (err);
1860 1860  }
1861 1861  
1862 1862  /*ARGSUSED*/
1863 1863  static int
1864 1864  pcfs_map(
1865 1865          struct vnode *vp,
1866 1866          offset_t off,
1867 1867          struct as *as,
1868 1868          caddr_t *addrp,
1869 1869          size_t len,
1870 1870          uchar_t prot,
1871 1871          uchar_t maxprot,
1872 1872          uint_t flags,
1873 1873          struct cred *cr,
1874 1874          caller_context_t *ct)
1875 1875  {
1876 1876          struct segvn_crargs vn_a;
1877 1877          int error;
1878 1878  
1879 1879          PC_DPRINTF0(6, "pcfs_map\n");
1880 1880          if (vp->v_flag & VNOMAP)
1881 1881                  return (ENOSYS);
1882 1882  
1883 1883          if (off > UINT32_MAX || off + len > UINT32_MAX)
1884 1884                  return (ENXIO);
1885 1885  
1886 1886          as_rangelock(as);
1887 1887          error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
1888 1888          if (error != 0) {
1889 1889                  as_rangeunlock(as);
1890 1890                  return (error);
1891 1891          }
1892 1892  
1893 1893          vn_a.vp = vp;
1894 1894          vn_a.offset = off;
1895 1895          vn_a.type = flags & MAP_TYPE;
1896 1896          vn_a.prot = prot;
1897 1897          vn_a.maxprot = maxprot;
1898 1898          vn_a.flags = flags & ~MAP_TYPE;
1899 1899          vn_a.cred = cr;
1900 1900          vn_a.amp = NULL;
1901 1901          vn_a.szc = 0;
1902 1902          vn_a.lgrp_mem_policy_flags = 0;
1903 1903  
1904 1904          error = as_map(as, *addrp, len, segvn_create, &vn_a);
1905 1905          as_rangeunlock(as);
1906 1906          return (error);
1907 1907  }
1908 1908  
1909 1909  /* ARGSUSED */
1910 1910  static int
1911 1911  pcfs_seek(
1912 1912          struct vnode *vp,
1913 1913          offset_t ooff,
1914 1914          offset_t *noffp,
1915 1915          caller_context_t *ct)
1916 1916  {
1917 1917          if (*noffp < 0)
1918 1918                  return (EINVAL);
1919 1919          else if (*noffp > MAXOFFSET_T)
1920 1920                  return (EINVAL);
1921 1921          else
1922 1922                  return (0);
1923 1923  }
1924 1924  
1925 1925  /* ARGSUSED */
1926 1926  static int
1927 1927  pcfs_addmap(
1928 1928          struct vnode *vp,
1929 1929          offset_t off,
1930 1930          struct as *as,
1931 1931          caddr_t addr,
1932 1932          size_t len,
1933 1933          uchar_t prot,
1934 1934          uchar_t maxprot,
1935 1935          uint_t flags,
1936 1936          struct cred *cr,
1937 1937          caller_context_t *ct)
1938 1938  {
1939 1939          if (vp->v_flag & VNOMAP)
1940 1940                  return (ENOSYS);
1941 1941          return (0);
1942 1942  }
1943 1943  
1944 1944  /*ARGSUSED*/
1945 1945  static int
1946 1946  pcfs_delmap(
1947 1947          struct vnode *vp,
1948 1948          offset_t off,
1949 1949          struct as *as,
1950 1950          caddr_t addr,
1951 1951          size_t len,
1952 1952          uint_t prot,
1953 1953          uint_t maxprot,
1954 1954          uint_t flags,
1955 1955          struct cred *cr,
1956 1956          caller_context_t *ct)
1957 1957  {
1958 1958          if (vp->v_flag & VNOMAP)
1959 1959                  return (ENOSYS);
1960 1960          return (0);
1961 1961  }
1962 1962  
1963 1963  /*
1964 1964   * POSIX pathconf() support.
1965 1965   */
1966 1966  /* ARGSUSED */
1967 1967  static int
1968 1968  pcfs_pathconf(
1969 1969          struct vnode *vp,
1970 1970          int cmd,
1971 1971          ulong_t *valp,
1972 1972          struct cred *cr,
1973 1973          caller_context_t *ct)
1974 1974  {
1975 1975          struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
1976 1976  
1977 1977          switch (cmd) {
1978 1978          case _PC_LINK_MAX:
1979 1979                  *valp = 1;
1980 1980                  return (0);
1981 1981  
1982 1982          case _PC_CASE_BEHAVIOR:
1983 1983                  return (EINVAL);
1984 1984  
1985 1985          case _PC_FILESIZEBITS:
1986 1986                  /*
1987 1987                   * Both FAT16 and FAT32 support 4GB - 1 byte for file size.
1988 1988                   * FAT12 can only go up to the maximum filesystem capacity
1989 1989                   * which is ~509MB.
1990 1990                   */
1991 1991                  *valp = IS_FAT12(fsp) ? 30 : 33;
1992 1992                  return (0);
1993 1993  
1994 1994          case _PC_TIMESTAMP_RESOLUTION:
1995 1995                  /*
1996 1996                   * PCFS keeps track of modification times, it its own
1997 1997                   * internal format, to a resolution of 2 seconds.
1998 1998                   * Since 2000 million is representable in an int32_t
1999 1999                   * without overflow (or becoming negative), we allow
2000 2000                   * this value to be returned.
2001 2001                   */
2002 2002                  *valp = 2000000000L;
2003 2003                  return (0);
2004 2004  
2005 2005          default:
2006 2006                  return (fs_pathconf(vp, cmd, valp, cr, ct));
2007 2007          }
2008 2008  
2009 2009  }
2010 2010  
2011 2011  /* ARGSUSED */
2012 2012  static int
2013 2013  pcfs_space(
2014 2014          struct vnode *vp,
2015 2015          int cmd,
2016 2016          struct flock64 *bfp,
2017 2017          int flag,
2018 2018          offset_t offset,
2019 2019          cred_t *cr,
2020 2020          caller_context_t *ct)
2021 2021  {
2022 2022          struct vattr vattr;
2023 2023          int error;
2024 2024  
2025 2025          if (cmd != F_FREESP)
2026 2026                  return (EINVAL);
2027 2027  
2028 2028          if ((error = convoff(vp, bfp, 0, offset)) == 0) {
2029 2029                  if ((bfp->l_start > UINT32_MAX) || (bfp->l_len > UINT32_MAX))
2030 2030                          return (EFBIG);
2031 2031                  /*
2032 2032                   * we only support the special case of l_len == 0,
2033 2033                   * meaning free to end of file at this moment.
2034 2034                   */
2035 2035                  if (bfp->l_len != 0)
2036 2036                          return (EINVAL);
2037 2037                  vattr.va_mask = AT_SIZE;
2038 2038                  vattr.va_size = bfp->l_start;
2039 2039                  error = VOP_SETATTR(vp, (vattr_t *)&vattr, 0, cr, ct);
2040 2040          }
2041 2041          return (error);
2042 2042  }
2043 2043  
2044 2044  /*
2045 2045   * Break up 'len' chars from 'buf' into a long file name chunk.
2046 2046   * Pad with '0xff' to make Norton Disk Doctor and Microsoft ScanDisk happy.
2047 2047   */
2048 2048  void
2049 2049  set_long_fn_chunk(struct pcdir_lfn *ep, char *buf, int len)
2050 2050  {
2051 2051          int     i;
2052 2052  
2053 2053          ASSERT(buf != NULL);
2054 2054  
2055 2055          for (i = 0; i < PCLF_FIRSTNAMESIZE; i += 2) {
2056 2056                  if (len > 0) {
2057 2057                          ep->pcdl_firstfilename[i] = *buf++;
2058 2058                          ep->pcdl_firstfilename[i + 1] = *buf++;
2059 2059                          len -= 2;
2060 2060                  } else {
2061 2061                          ep->pcdl_firstfilename[i] = (uchar_t)0xff;
2062 2062                          ep->pcdl_firstfilename[i + 1] = (uchar_t)0xff;
2063 2063                  }
2064 2064          }
2065 2065  
2066 2066          for (i = 0; i < PCLF_SECONDNAMESIZE; i += 2) {
2067 2067                  if (len > 0) {
2068 2068                          ep->pcdl_secondfilename[i] = *buf++;
2069 2069                          ep->pcdl_secondfilename[i + 1] = *buf++;
2070 2070                          len -= 2;
2071 2071                  } else {
2072 2072                          ep->pcdl_secondfilename[i] = (uchar_t)0xff;
2073 2073                          ep->pcdl_secondfilename[i + 1] = (uchar_t)0xff;
2074 2074                  }
2075 2075          }
2076 2076          for (i = 0; i < PCLF_THIRDNAMESIZE; i += 2) {
2077 2077                  if (len > 0) {
2078 2078                          ep->pcdl_thirdfilename[i] = *buf++;
2079 2079                          ep->pcdl_thirdfilename[i + 1] = *buf++;
2080 2080                          len -= 2;
2081 2081                  } else {
2082 2082                          ep->pcdl_thirdfilename[i] = (uchar_t)0xff;
2083 2083                          ep->pcdl_thirdfilename[i + 1] = (uchar_t)0xff;
2084 2084                  }
2085 2085          }
2086 2086  }
2087 2087  
2088 2088  /*
2089 2089   * Extract the characters from the long filename chunk into 'buf'.
2090 2090   * Return the number of characters extracted.
2091 2091   */
2092 2092  static int
2093 2093  get_long_fn_chunk(struct pcdir_lfn *ep, char *buf)
2094 2094  {
2095 2095          char    *tmp = buf;
2096 2096          int     i;
2097 2097  
2098 2098          /* Copy all the names, no filtering now */
2099 2099  
2100 2100          for (i = 0; i < PCLF_FIRSTNAMESIZE; i += 2, tmp += 2) {
2101 2101                  *tmp = ep->pcdl_firstfilename[i];
2102 2102                  *(tmp + 1) = ep->pcdl_firstfilename[i + 1];
2103 2103  
2104 2104                  if ((*tmp == '\0') && (*(tmp+1) == '\0'))
2105 2105                          return (tmp - buf);
2106 2106          }
2107 2107          for (i = 0; i < PCLF_SECONDNAMESIZE; i += 2, tmp += 2) {
2108 2108                  *tmp = ep->pcdl_secondfilename[i];
2109 2109                  *(tmp + 1) = ep->pcdl_secondfilename[i + 1];
2110 2110  
2111 2111                  if ((*tmp == '\0') && (*(tmp+1) == '\0'))
2112 2112                          return (tmp - buf);
2113 2113          }
2114 2114          for (i = 0; i < PCLF_THIRDNAMESIZE; i += 2, tmp += 2) {
2115 2115                  *tmp = ep->pcdl_thirdfilename[i];
2116 2116                  *(tmp + 1) = ep->pcdl_thirdfilename[i + 1];
2117 2117  
2118 2118                  if ((*tmp == '\0') && (*(tmp+1) == '\0'))
2119 2119                          return (tmp - buf);
2120 2120          }
2121 2121          return (tmp - buf);
2122 2122  }
2123 2123  
2124 2124  
2125 2125  /*
2126 2126   * Checksum the passed in short filename.
2127 2127   * This is used to validate each component of the long name to make
2128 2128   * sure the long name is valid (it hasn't been "detached" from the
2129 2129   * short filename). This algorithm was found in FreeBSD.
2130 2130   * (sys/fs/msdosfs/msdosfs_conv.c:winChksum(), Wolfgang Solfrank)
2131 2131   */
2132 2132  
2133 2133  uchar_t
2134 2134  pc_checksum_long_fn(char *name, char *ext)
2135 2135  {
2136 2136          uchar_t c;
2137 2137          char    b[11];
2138 2138  
2139 2139          bcopy(name, b, 8);
2140 2140          bcopy(ext, b+8, 3);
2141 2141  
2142 2142          c = b[0];
2143 2143          c = ((c << 7) | (c >> 1)) + b[1];
2144 2144          c = ((c << 7) | (c >> 1)) + b[2];
2145 2145          c = ((c << 7) | (c >> 1)) + b[3];
2146 2146          c = ((c << 7) | (c >> 1)) + b[4];
2147 2147          c = ((c << 7) | (c >> 1)) + b[5];
2148 2148          c = ((c << 7) | (c >> 1)) + b[6];
2149 2149          c = ((c << 7) | (c >> 1)) + b[7];
2150 2150          c = ((c << 7) | (c >> 1)) + b[8];
2151 2151          c = ((c << 7) | (c >> 1)) + b[9];
2152 2152          c = ((c << 7) | (c >> 1)) + b[10];
2153 2153  
2154 2154          return (c);
2155 2155  }
2156 2156  
2157 2157  /*
2158 2158   * Read a chunk of long filename entries into 'namep'.
2159 2159   * Return with offset pointing to short entry (on success), or next
2160 2160   * entry to read (if this wasn't a valid lfn really).
2161 2161   * Uses the passed-in buffer if it can, otherwise kmem_allocs() room for
2162 2162   * a long filename.
2163 2163   *
2164 2164   * Can also be called with a NULL namep, in which case it just returns
2165 2165   * whether this was really a valid long filename and consumes it
2166 2166   * (used by pc_dirempty()).
2167 2167   */
2168 2168  int
2169 2169  pc_extract_long_fn(struct pcnode *pcp, char *namep,
2170 2170      struct pcdir **epp, offset_t *offset, struct buf **bp)
2171 2171  {
2172 2172          struct pcdir *ep = *epp;
2173 2173          struct pcdir_lfn *lep = (struct pcdir_lfn *)ep;
2174 2174          struct vnode *dvp = PCTOV(pcp);
2175 2175          struct pcfs *fsp = VFSTOPCFS(dvp->v_vfsp);
2176 2176          char    *lfn;
2177 2177          char    *lfn_base;
2178 2178          int     boff;
2179 2179          int     i, cs;
2180 2180          char    *buf;
2181 2181          uchar_t cksum;
2182 2182          int     detached = 0;
2183 2183          int     error = 0;
2184 2184          int     foldcase;
2185 2185          int     count = 0;
2186 2186          size_t  u16l = 0, u8l = 0;
2187 2187          char    *outbuf;
2188 2188          size_t  ret, inlen, outlen;
2189 2189  
2190 2190          foldcase = (fsp->pcfs_flags & PCFS_FOLDCASE);
2191 2191          lfn_base = kmem_alloc(PCMAXNAM_UTF16, KM_SLEEP);
2192 2192          lfn = lfn_base + PCMAXNAM_UTF16 - sizeof (uint16_t);
2193 2193          *lfn = '\0';
2194 2194          *(lfn + 1) = '\0';
2195 2195          cksum = lep->pcdl_checksum;
2196 2196  
2197 2197          buf = kmem_alloc(PCMAXNAM_UTF16, KM_SLEEP);
2198 2198          for (i = (lep->pcdl_ordinal & ~0xc0); i > 0; i--) {
2199 2199                  /* read next block if necessary */
2200 2200                  boff = pc_blkoff(fsp, *offset);
2201 2201                  if (boff == 0 || *bp == NULL || boff >= (*bp)->b_bcount) {
2202 2202                          if (*bp != NULL) {
2203 2203                                  brelse(*bp);
2204 2204                                  *bp = NULL;
2205 2205                          }
2206 2206                          error = pc_blkatoff(pcp, *offset, bp, &ep);
2207 2207                          if (error) {
2208 2208                                  kmem_free(lfn_base, PCMAXNAM_UTF16);
2209 2209                                  kmem_free(buf, PCMAXNAM_UTF16);
2210 2210                                  return (error);
2211 2211                          }
2212 2212                          lep = (struct pcdir_lfn *)ep;
2213 2213                  }
2214 2214                  /* can this happen? Bad fs? */
2215 2215                  if (!PCDL_IS_LFN((struct pcdir *)lep)) {
2216 2216                          detached = 1;
2217 2217                          break;
2218 2218                  }
2219 2219                  if (cksum != lep->pcdl_checksum)
2220 2220                          detached = 1;
2221 2221                  /* process current entry */
2222 2222                  cs = get_long_fn_chunk(lep, buf);
2223 2223                  count += cs;
2224 2224                  for (; cs > 0; cs--) {
2225 2225                          /* see if we underflow */
2226 2226                          if (lfn >= lfn_base)
2227 2227                                  *--lfn = buf[cs - 1];
2228 2228                          else
2229 2229                                  detached = 1;
2230 2230                  }
2231 2231                  lep++;
2232 2232                  *offset += sizeof (struct pcdir);
2233 2233          }
2234 2234          kmem_free(buf, PCMAXNAM_UTF16);
2235 2235          /* read next block if necessary */
2236 2236          boff = pc_blkoff(fsp, *offset);
2237 2237          ep = (struct pcdir *)lep;
2238 2238          if (boff == 0 || *bp == NULL || boff >= (*bp)->b_bcount) {
2239 2239                  if (*bp != NULL) {
2240 2240                          brelse(*bp);
2241 2241                          *bp = NULL;
2242 2242                  }
2243 2243                  error = pc_blkatoff(pcp, *offset, bp, &ep);
2244 2244                  if (error) {
2245 2245                          kmem_free(lfn_base, PCMAXNAM_UTF16);
2246 2246                          return (error);
2247 2247                  }
2248 2248          }
2249 2249          /* should be on the short one */
2250 2250          if (PCDL_IS_LFN(ep) || ((ep->pcd_filename[0] == PCD_UNUSED) ||
2251 2251              (ep->pcd_filename[0] == PCD_ERASED))) {
2252 2252                  detached = 1;
2253 2253          }
2254 2254          if (detached ||
2255 2255              (cksum != pc_checksum_long_fn(ep->pcd_filename, ep->pcd_ext)) ||
2256 2256              !pc_valid_long_fn(lfn, 0)) {
2257 2257                  /*
2258 2258                   * process current entry again. This may end up another lfn
2259 2259                   * or a short name.
2260 2260                   */
2261 2261                  *epp = ep;
2262 2262                  kmem_free(lfn_base, PCMAXNAM_UTF16);
2263 2263                  return (EINVAL);
2264 2264          }
2265 2265          if (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) {
2266 2266                  /*
2267 2267                   * Don't display label because it may contain
2268 2268                   * funny characters.
2269 2269                   */
2270 2270                  *offset += sizeof (struct pcdir);
2271 2271                  ep++;
2272 2272                  *epp = ep;
2273 2273                  kmem_free(lfn_base, PCMAXNAM_UTF16);
2274 2274                  return (EINVAL);
2275 2275          }
2276 2276          if (namep) {
2277 2277                  u16l = count / 2;
2278 2278                  u8l = PCMAXNAMLEN;
2279 2279                  error = uconv_u16tou8((const uint16_t *)lfn, &u16l,
2280 2280                      (uchar_t *)namep, &u8l, UCONV_IN_LITTLE_ENDIAN);
2281 2281                  /*
2282 2282                   * uconv_u16tou8() will catch conversion errors including
2283 2283                   * the case where there is not enough room to write the
2284 2284                   * converted result and the u8l will never go over the given
2285 2285                   * PCMAXNAMLEN.
2286 2286                   */
2287 2287                  if (error != 0) {
2288 2288                          kmem_free(lfn_base, PCMAXNAM_UTF16);
2289 2289                          return (EINVAL);
2290 2290                  }
2291 2291                  namep[u8l] = '\0';
2292 2292                  if (foldcase) {
2293 2293                          inlen = strlen(namep);
2294 2294                          outlen = PCMAXNAMLEN;
2295 2295                          outbuf = kmem_alloc(PCMAXNAMLEN + 1, KM_SLEEP);
2296 2296                          ret = u8_textprep_str(namep, &inlen, outbuf,
2297 2297                              &outlen, U8_TEXTPREP_TOLOWER, U8_UNICODE_LATEST,
2298 2298                              &error);
2299 2299                          if (ret == -1) {
2300 2300                                  kmem_free(outbuf, PCMAXNAMLEN + 1);
2301 2301                                  kmem_free(lfn_base, PCMAXNAM_UTF16);
2302 2302                                  return (EINVAL);
2303 2303                          }
2304 2304                          outbuf[PCMAXNAMLEN - outlen] = '\0';
2305 2305                          (void) strncpy(namep, outbuf, PCMAXNAMLEN + 1);
2306 2306                          kmem_free(outbuf, PCMAXNAMLEN + 1);
2307 2307                  }
2308 2308          }
2309 2309          kmem_free(lfn_base, PCMAXNAM_UTF16);
2310 2310          *epp = ep;
2311 2311          return (0);
2312 2312  }
2313 2313  /*
2314 2314   * Read a long filename into the pc_dirent structure and copy it out.
2315 2315   */
2316 2316  int
2317 2317  pc_read_long_fn(struct vnode *dvp, struct uio *uiop, struct pc_dirent *ld,
2318 2318      struct pcdir **epp, offset_t *offset, struct buf **bp)
2319 2319  {
2320 2320          struct pcdir *ep;
2321 2321          struct pcnode *pcp = VTOPC(dvp);
2322 2322          struct pcfs *fsp = VFSTOPCFS(dvp->v_vfsp);
2323 2323          offset_t uiooffset = uiop->uio_loffset;
2324 2324          int     error = 0;
2325 2325          offset_t oldoffset;
2326 2326  
2327 2327          oldoffset = *offset;
2328 2328          error = pc_extract_long_fn(pcp, ld->d_name, epp, offset, bp);
2329 2329          if (error) {
2330 2330                  if (error == EINVAL) {
2331 2331                          uiop->uio_loffset += *offset - oldoffset;
2332 2332                          return (0);
2333 2333                  } else
2334 2334                          return (error);
2335 2335          }
2336 2336  
2337 2337          ep = *epp;
2338 2338          uiop->uio_loffset += *offset - oldoffset;
2339 2339          ld->d_reclen = DIRENT64_RECLEN(strlen(ld->d_name));
2340 2340          if (ld->d_reclen > uiop->uio_resid) {
2341 2341                  uiop->uio_loffset = uiooffset;
2342 2342                  return (ENOSPC);
2343 2343          }
2344 2344          ld->d_off = uiop->uio_loffset + sizeof (struct pcdir);
2345 2345          ld->d_ino = pc_makenodeid(pc_daddrdb(fsp, (*bp)->b_blkno),
2346 2346              pc_blkoff(fsp, *offset), ep->pcd_attr,
2347 2347              pc_getstartcluster(fsp, ep), pc_direntpersec(fsp));
2348 2348          (void) uiomove((caddr_t)ld, ld->d_reclen, UIO_READ, uiop);
2349 2349          uiop->uio_loffset = ld->d_off;
2350 2350          *offset += sizeof (struct pcdir);
2351 2351          ep++;
2352 2352          *epp = ep;
2353 2353          return (0);
2354 2354  }
2355 2355  
2356 2356  /*
2357 2357   * Read a short filename into the pc_dirent structure and copy it out.
2358 2358   */
2359 2359  int
2360 2360  pc_read_short_fn(struct vnode *dvp, struct uio *uiop, struct pc_dirent *ld,
2361 2361      struct pcdir **epp, offset_t *offset, struct buf **bp)
2362 2362  {
2363 2363          struct pcfs *fsp = VFSTOPCFS(dvp->v_vfsp);
2364 2364          int     boff = pc_blkoff(fsp, *offset);
2365 2365          struct pcdir *ep = *epp;
2366 2366          offset_t        oldoffset = uiop->uio_loffset;
2367 2367          int     error;
2368 2368          int     foldcase;
2369 2369  
2370 2370          if (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) {
2371 2371                  uiop->uio_loffset += sizeof (struct pcdir);
2372 2372                  *offset += sizeof (struct pcdir);
2373 2373                  ep++;
2374 2374                  *epp = ep;
2375 2375                  return (0);
2376 2376          }
2377 2377          ld->d_ino = (ino64_t)pc_makenodeid(pc_daddrdb(fsp, (*bp)->b_blkno),
2378 2378              boff, ep->pcd_attr, pc_getstartcluster(fsp, ep),
2379 2379              pc_direntpersec(fsp));
2380 2380          foldcase = (fsp->pcfs_flags & PCFS_FOLDCASE);
2381 2381          error = pc_fname_ext_to_name(&ld->d_name[0], &ep->pcd_filename[0],
2382 2382              &ep->pcd_ext[0], foldcase);
2383 2383          if (error == 0) {
2384 2384                  ld->d_reclen = DIRENT64_RECLEN(strlen(ld->d_name));
2385 2385                  if (ld->d_reclen > uiop->uio_resid) {
2386 2386                          uiop->uio_loffset = oldoffset;
2387 2387                          return (ENOSPC);
2388 2388                  }
2389 2389                  ld->d_off = (off64_t)(uiop->uio_loffset +
2390 2390                      sizeof (struct pcdir));
2391 2391                  (void) uiomove((caddr_t)ld,
2392 2392                      ld->d_reclen, UIO_READ, uiop);
2393 2393                  uiop->uio_loffset = ld->d_off;
2394 2394          } else {
2395 2395                  uiop->uio_loffset += sizeof (struct pcdir);
2396 2396          }
2397 2397          *offset += sizeof (struct pcdir);
2398 2398          ep++;
2399 2399          *epp = ep;
2400 2400          return (0);
2401 2401  }
2402 2402  
2403 2403  /* ARGSUSED */
2404 2404  static int
2405 2405  pcfs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
2406 2406  {
2407 2407          struct pc_fid *pcfid;
2408 2408          struct pcnode *pcp;
2409 2409          struct pcfs     *fsp;
2410 2410          int     error;
2411 2411  
2412 2412          fsp = VFSTOPCFS(vp->v_vfsp);
2413 2413          if (fsp == NULL)
2414 2414                  return (EIO);
2415 2415          error = pc_lockfs(fsp, 0, 0);
2416 2416          if (error)
2417 2417                  return (error);
2418 2418          if ((pcp = VTOPC(vp)) == NULL || pcp->pc_flags & PC_INVAL) {
2419 2419                  pc_unlockfs(fsp);
2420 2420                  return (EIO);
2421 2421          }
2422 2422          if (fidp->fid_len < (sizeof (struct pc_fid) - sizeof (ushort_t))) {
2423 2423                  fidp->fid_len = sizeof (struct pc_fid) - sizeof (ushort_t);
2424 2424                  pc_unlockfs(fsp);
2425 2425                  return (ENOSPC);
2426 2426          }
2427 2427  
2428 2428          pcfid = (struct pc_fid *)fidp;
2429 2429          bzero(pcfid, sizeof (struct pc_fid));
2430 2430          pcfid->pcfid_len = sizeof (struct pc_fid) - sizeof (ushort_t);
2431 2431          if (vp->v_flag & VROOT) {
2432 2432                  pcfid->pcfid_block = 0;
2433 2433                  pcfid->pcfid_offset = 0;
2434 2434                  pcfid->pcfid_ctime = 0;
2435 2435          } else {
2436 2436                  pcfid->pcfid_block = pcp->pc_eblkno;
2437 2437                  pcfid->pcfid_offset = pcp->pc_eoffset;
2438 2438                  pcfid->pcfid_ctime = pcp->pc_entry.pcd_crtime.pct_time;
2439 2439          }
2440 2440          pc_unlockfs(fsp);
2441 2441          return (0);
2442 2442  }
  
    | 
      ↓ open down ↓ | 
    2442 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX