1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy 22eda14cbcSMatt Macy #include <sys/zfs_context.h> 23eda14cbcSMatt Macy #include <sys/spa.h> 24eda14cbcSMatt Macy #include <sys/spa_impl.h> 25eda14cbcSMatt Macy #include <sys/dmu.h> 26eda14cbcSMatt Macy #include <sys/zap.h> 27eda14cbcSMatt Macy #include <sys/vdev.h> 28eda14cbcSMatt Macy #include <sys/vdev_os.h> 29eda14cbcSMatt Macy #include <sys/vdev_impl.h> 30eda14cbcSMatt Macy #include <sys/uberblock_impl.h> 31eda14cbcSMatt Macy #include <sys/metaslab.h> 32eda14cbcSMatt Macy #include <sys/metaslab_impl.h> 33eda14cbcSMatt Macy #include <sys/zio.h> 34eda14cbcSMatt Macy #include <sys/dsl_scan.h> 35eda14cbcSMatt Macy #include <sys/abd.h> 36eda14cbcSMatt Macy #include <sys/fs/zfs.h> 37eda14cbcSMatt Macy 38eda14cbcSMatt Macy int 39eda14cbcSMatt Macy vdev_label_write_pad2(vdev_t *vd, const char *buf, size_t size) 40eda14cbcSMatt Macy { 41eda14cbcSMatt Macy spa_t *spa = vd->vdev_spa; 42eda14cbcSMatt Macy zio_t *zio; 43eda14cbcSMatt Macy abd_t *pad2; 44eda14cbcSMatt Macy int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL; 45eda14cbcSMatt Macy int error; 46eda14cbcSMatt Macy 47eda14cbcSMatt Macy if (size > VDEV_PAD_SIZE) 48eda14cbcSMatt Macy return (EINVAL); 49eda14cbcSMatt Macy 50eda14cbcSMatt Macy if (!vd->vdev_ops->vdev_op_leaf) 51eda14cbcSMatt Macy return (ENODEV); 52eda14cbcSMatt Macy if (vdev_is_dead(vd)) 53eda14cbcSMatt Macy return (ENXIO); 54eda14cbcSMatt Macy 5516038816SMartin Matuska ASSERT3U(spa_config_held(spa, SCL_ALL, RW_WRITER), ==, SCL_ALL); 56eda14cbcSMatt Macy 57eda14cbcSMatt Macy pad2 = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE); 58eda14cbcSMatt Macy abd_copy_from_buf(pad2, buf, size); 59a2b560ccSMartin Matuska abd_zero_off(pad2, size, VDEV_PAD_SIZE - size); 60eda14cbcSMatt Macy 61eda14cbcSMatt Macy retry: 62eda14cbcSMatt Macy zio = zio_root(spa, NULL, NULL, flags); 63eda14cbcSMatt Macy vdev_label_write(zio, vd, 0, pad2, 64eda14cbcSMatt Macy offsetof(vdev_label_t, vl_be), 65eda14cbcSMatt Macy VDEV_PAD_SIZE, NULL, NULL, flags); 66eda14cbcSMatt Macy error = zio_wait(zio); 67eda14cbcSMatt Macy if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) { 68eda14cbcSMatt Macy flags |= ZIO_FLAG_TRYHARD; 69eda14cbcSMatt Macy goto retry; 70eda14cbcSMatt Macy } 71eda14cbcSMatt Macy 72eda14cbcSMatt Macy abd_free(pad2); 73eda14cbcSMatt Macy return (error); 74eda14cbcSMatt Macy } 75e716630dSMartin Matuska 76e716630dSMartin Matuska static void 77e716630dSMartin Matuska vdev_child_done(zio_t *zio) 78e716630dSMartin Matuska { 79e716630dSMartin Matuska zio_t *pio = zio->io_private; 80e716630dSMartin Matuska 81e716630dSMartin Matuska mutex_enter(&pio->io_lock); 82e716630dSMartin Matuska pio->io_error = zio_worst_error(pio->io_error, zio->io_error); 83e716630dSMartin Matuska mutex_exit(&pio->io_lock); 84e716630dSMartin Matuska } 85e716630dSMartin Matuska 86e716630dSMartin Matuska /* 87e716630dSMartin Matuska * Check if the reserved boot area is in-use. 88e716630dSMartin Matuska * 89e716630dSMartin Matuska * When booting FreeBSD with an MBR partition with ZFS, the zfsboot file 90e716630dSMartin Matuska * (which understands the ZFS file system) is written to the ZFS BOOT 91e716630dSMartin Matuska * reserve area (at offset 512K). We check for that here before attaching 92e716630dSMartin Matuska * a disk to raidz which would then corrupt this boot data. 93e716630dSMartin Matuska */ 94e716630dSMartin Matuska int 95e716630dSMartin Matuska vdev_check_boot_reserve(spa_t *spa, vdev_t *childvd) 96e716630dSMartin Matuska { 97e716630dSMartin Matuska ASSERT(childvd->vdev_ops->vdev_op_leaf); 98e716630dSMartin Matuska 99*9d6043f9SMartin Matuska size_t size = 1ULL << childvd->vdev_top->vdev_ashift; 100e716630dSMartin Matuska abd_t *abd = abd_alloc_linear(size, B_FALSE); 101e716630dSMartin Matuska 102e716630dSMartin Matuska zio_t *pio = zio_root(spa, NULL, NULL, 0); 103e716630dSMartin Matuska /* 104e716630dSMartin Matuska * Note: zio_vdev_child_io() adds VDEV_LABEL_START_SIZE to the offset 105e716630dSMartin Matuska * to calculate the physical offset to write to. Passing in a negative 106e716630dSMartin Matuska * offset lets us access the boot area. 107e716630dSMartin Matuska */ 108e716630dSMartin Matuska zio_nowait(zio_vdev_child_io(pio, NULL, childvd, 109e716630dSMartin Matuska VDEV_BOOT_OFFSET - VDEV_LABEL_START_SIZE, abd, size, ZIO_TYPE_READ, 110e716630dSMartin Matuska ZIO_PRIORITY_ASYNC_READ, 0, vdev_child_done, pio)); 111e716630dSMartin Matuska zio_wait(pio); 112e716630dSMartin Matuska 113e716630dSMartin Matuska unsigned char *buf = abd_to_buf(abd); 114e716630dSMartin Matuska 115e716630dSMartin Matuska /* 116e716630dSMartin Matuska * The BTX server has a special header at the begining. 117e716630dSMartin Matuska * 118e716630dSMartin Matuska * btx_hdr: .byte 0xeb # Machine ID 119e716630dSMartin Matuska * .byte 0xe # Header size 120e716630dSMartin Matuska * .ascii "BTX" # Magic 121e716630dSMartin Matuska * .byte 0x1 # Major version 122e716630dSMartin Matuska * .byte 0x2 # Minor version 123e716630dSMartin Matuska * .byte BTX_FLAGS # Flags 124e716630dSMartin Matuska */ 125e716630dSMartin Matuska if (buf[0] == 0xeb && buf[1] == 0x0e && 126e716630dSMartin Matuska buf[2] == 'B' && buf[3] == 'T' && buf[4] == 'X') { 127e716630dSMartin Matuska abd_free(abd); 128e716630dSMartin Matuska return (EBUSY); 129e716630dSMartin Matuska } 130e716630dSMartin Matuska 131e716630dSMartin Matuska abd_free(abd); 132e716630dSMartin Matuska return (0); 133e716630dSMartin Matuska } 134