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 https://opensource.org/licenses/CDDL-1.0.
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 #include <sys/zfs_context.h>
23 #include <sys/spa.h>
24 #include <sys/spa_impl.h>
25 #include <sys/dmu.h>
26 #include <sys/zap.h>
27 #include <sys/vdev.h>
28 #include <sys/vdev_os.h>
29 #include <sys/vdev_impl.h>
30 #include <sys/uberblock_impl.h>
31 #include <sys/metaslab.h>
32 #include <sys/metaslab_impl.h>
33 #include <sys/zio.h>
34 #include <sys/dsl_scan.h>
35 #include <sys/abd.h>
36 #include <sys/fs/zfs.h>
37 
38 int
39 vdev_label_write_pad2(vdev_t *vd, const char *buf, size_t size)
40 {
41 	spa_t *spa = vd->vdev_spa;
42 	zio_t *zio;
43 	abd_t *pad2;
44 	int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
45 	int error;
46 
47 	if (size > VDEV_PAD_SIZE)
48 		return (EINVAL);
49 
50 	if (!vd->vdev_ops->vdev_op_leaf)
51 		return (ENODEV);
52 	if (vdev_is_dead(vd))
53 		return (ENXIO);
54 
55 	ASSERT3U(spa_config_held(spa, SCL_ALL, RW_WRITER), ==, SCL_ALL);
56 
57 	pad2 = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
58 	abd_zero(pad2, VDEV_PAD_SIZE);
59 	abd_copy_from_buf(pad2, buf, size);
60 
61 retry:
62 	zio = zio_root(spa, NULL, NULL, flags);
63 	vdev_label_write(zio, vd, 0, pad2,
64 	    offsetof(vdev_label_t, vl_be),
65 	    VDEV_PAD_SIZE, NULL, NULL, flags);
66 	error = zio_wait(zio);
67 	if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
68 		flags |= ZIO_FLAG_TRYHARD;
69 		goto retry;
70 	}
71 
72 	abd_free(pad2);
73 	return (error);
74 }
75