1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
  24  */
  25 
  26 #include <sys/zfs_context.h>
  27 #include <sys/spa.h>
  28 #include <sys/spa_impl.h>
  29 #include <sys/vdev_file.h>
  30 #include <sys/vdev_impl.h>
  31 #include <sys/zio.h>
  32 #include <sys/fs/zfs.h>
  33 #include <sys/fm/fs/zfs.h>
  34 #include <sys/abd.h>
  35 
  36 /*
  37  * Virtual device vector for files.
  38  */
  39 
  40 static void
  41 vdev_file_hold(vdev_t *vd)
  42 {
  43         ASSERT(vd->vdev_path != NULL);
  44 }
  45 
  46 static void
  47 vdev_file_rele(vdev_t *vd)
  48 {
  49         ASSERT(vd->vdev_path != NULL);
  50 }
  51 
  52 static int
  53 vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
  54     uint64_t *ashift)
  55 {
  56         vdev_file_t *vf;
  57         vnode_t *vp;
  58         vattr_t vattr;
  59         int error;
  60 
  61         /*
  62          * We must have a pathname, and it must be absolute.
  63          */
  64         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
  65                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
  66                 return (SET_ERROR(EINVAL));
  67         }
  68 
  69         /*
  70          * Reopen the device if it's not currently open.  Otherwise,
  71          * just update the physical size of the device.
  72          */
  73         if (vd->vdev_tsd != NULL) {
  74                 ASSERT(vd->vdev_reopening);
  75                 vf = vd->vdev_tsd;
  76                 goto skip_open;
  77         }
  78 
  79         vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
  80 
  81         /*
  82          * We always open the files from the root of the global zone, even if
  83          * we're in a local zone.  If the user has gotten to this point, the
  84          * administrator has already decided that the pool should be available
  85          * to local zone users, so the underlying devices should be as well.
  86          */
  87         ASSERT(vd->vdev_path != NULL && vd->vdev_path[0] == '/');
  88         error = vn_openat(vd->vdev_path + 1, UIO_SYSSPACE,
  89             spa_mode(vd->vdev_spa) | FOFFMAX, 0, &vp, 0, 0, rootdir, -1);
  90 
  91         if (error) {
  92                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
  93                 return (error);
  94         }
  95 
  96         vf->vf_vnode = vp;
  97 
  98 #ifdef _KERNEL
  99         /*
 100          * Make sure it's a regular file.
 101          */
 102         if (vp->v_type != VREG) {
 103                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 104                 return (SET_ERROR(ENODEV));
 105         }
 106 #endif
 107 
 108 skip_open:
 109         /*
 110          * Determine the physical size of the file.
 111          */
 112         vattr.va_mask = AT_SIZE;
 113         error = VOP_GETATTR(vf->vf_vnode, &vattr, 0, kcred, NULL);
 114         if (error) {
 115                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
 116                 return (error);
 117         }
 118 
 119         *max_psize = *psize = vattr.va_size;
 120         *ashift = SPA_MINBLOCKSHIFT;
 121 
 122         return (0);
 123 }
 124 
 125 static void
 126 vdev_file_close(vdev_t *vd)
 127 {
 128         vdev_file_t *vf = vd->vdev_tsd;
 129 
 130         if (vd->vdev_reopening || vf == NULL)
 131                 return;
 132 
 133         if (vf->vf_vnode != NULL) {
 134                 (void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred, NULL);
 135                 (void) VOP_CLOSE(vf->vf_vnode, spa_mode(vd->vdev_spa), 1, 0,
 136                     kcred, NULL);
 137                 VN_RELE(vf->vf_vnode);
 138         }
 139 
 140         vd->vdev_delayed_close = B_FALSE;
 141         kmem_free(vf, sizeof (vdev_file_t));
 142         vd->vdev_tsd = NULL;
 143 }
 144 
 145 /*
 146  * Implements the interrupt side for file vdev types. This routine will be
 147  * called when the I/O completes allowing us to transfer the I/O to the
 148  * interrupt taskqs. For consistency, the code structure mimics disk vdev
 149  * types.
 150  */
 151 static void
 152 vdev_file_io_intr(buf_t *bp)
 153 {
 154         vdev_buf_t *vb = (vdev_buf_t *)bp;
 155         zio_t *zio = vb->vb_io;
 156 
 157         zio->io_error = (geterror(bp) != 0 ? EIO : 0);
 158         if (zio->io_error == 0 && bp->b_resid != 0)
 159                 zio->io_error = SET_ERROR(ENOSPC);
 160 
 161         if (zio->io_type == ZIO_TYPE_READ) {
 162                 abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size);
 163         } else {
 164                 abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size);
 165         }
 166 
 167         kmem_free(vb, sizeof (vdev_buf_t));
 168         zio_delay_interrupt(zio);
 169 }
 170 
 171 static void
 172 vdev_file_io_strategy(void *arg)
 173 {
 174         buf_t *bp = arg;
 175         vnode_t *vp = bp->b_private;
 176         ssize_t resid;
 177         int error;
 178 
 179         error = vn_rdwr((bp->b_flags & B_READ) ? UIO_READ : UIO_WRITE,
 180             vp, bp->b_un.b_addr, bp->b_bcount, ldbtob(bp->b_lblkno),
 181             UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
 182 
 183         if (error == 0) {
 184                 bp->b_resid = resid;
 185                 biodone(bp);
 186         } else {
 187                 bioerror(bp, error);
 188                 biodone(bp);
 189         }
 190 }
 191 
 192 static void
 193 vdev_file_io_start(zio_t *zio)
 194 {
 195         vdev_t *vd = zio->io_vd;
 196         vdev_file_t *vf = vd->vdev_tsd;
 197         vdev_buf_t *vb;
 198         buf_t *bp;
 199 
 200         if (zio->io_type == ZIO_TYPE_IOCTL) {
 201                 /* XXPOLICY */
 202                 if (!vdev_readable(vd)) {
 203                         zio->io_error = SET_ERROR(ENXIO);
 204                         zio_interrupt(zio);
 205                         return;
 206                 }
 207 
 208                 switch (zio->io_cmd) {
 209                 case DKIOCFLUSHWRITECACHE:
 210                         zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
 211                             kcred, NULL);
 212                         break;
 213                 default:
 214                         zio->io_error = SET_ERROR(ENOTSUP);
 215                 }
 216 
 217                 zio_execute(zio);
 218                 return;
 219         }
 220 
 221         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
 222         zio->io_target_timestamp = zio_handle_io_delay(zio);
 223 
 224         vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
 225 
 226         vb->vb_io = zio;
 227         bp = &vb->vb_buf;
 228 
 229         bioinit(bp);
 230         bp->b_flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
 231         bp->b_bcount = zio->io_size;
 232 
 233         if (zio->io_type == ZIO_TYPE_READ) {
 234                 bp->b_un.b_addr =
 235                     abd_borrow_buf(zio->io_abd, zio->io_size);
 236         } else {
 237                 bp->b_un.b_addr =
 238                     abd_borrow_buf_copy(zio->io_abd, zio->io_size);
 239         }
 240 
 241         bp->b_lblkno = lbtodb(zio->io_offset);
 242         bp->b_bufsize = zio->io_size;
 243         bp->b_private = vf->vf_vnode;
 244         bp->b_iodone = (int (*)())vdev_file_io_intr;
 245 
 246         VERIFY3U(taskq_dispatch(system_taskq, vdev_file_io_strategy, bp,
 247             TQ_SLEEP), !=, 0);
 248 }
 249 
 250 /* ARGSUSED */
 251 static void
 252 vdev_file_io_done(zio_t *zio)
 253 {
 254 }
 255 
 256 vdev_ops_t vdev_file_ops = {
 257         vdev_file_open,
 258         vdev_file_close,
 259         vdev_default_asize,
 260         vdev_file_io_start,
 261         vdev_file_io_done,
 262         NULL,
 263         vdev_file_hold,
 264         vdev_file_rele,
 265         VDEV_TYPE_FILE,         /* name of this vdev type */
 266         B_TRUE                  /* leaf vdev */
 267 };
 268 
 269 /*
 270  * From userland we access disks just like files.
 271  */
 272 #ifndef _KERNEL
 273 
 274 vdev_ops_t vdev_disk_ops = {
 275         vdev_file_open,
 276         vdev_file_close,
 277         vdev_default_asize,
 278         vdev_file_io_start,
 279         vdev_file_io_done,
 280         NULL,
 281         vdev_file_hold,
 282         vdev_file_rele,
 283         VDEV_TYPE_DISK,         /* name of this vdev type */
 284         B_TRUE                  /* leaf vdev */
 285 };
 286 
 287 #endif