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