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.
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.
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]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2018 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
42 #include <sys/efi_partition.h>
43 #include <sys/systeminfo.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/vdev_disk.h>
50 #include "zfs_namecheck.h"
52 #include "libzfs_impl.h"
53 #include "zfs_comutil.h"
54 #include "zfeature_common.h"
57 * If the device has being dynamically expanded then we need to relabel
58 * the disk to use the new unallocated space.
61 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
65 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
66 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
67 "relabel '%s': unable to open device: %d"), path, errno);
68 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
72 * It's possible that we might encounter an error if the device
73 * does not have any unallocated space left. If so, we simply
74 * ignore that error and continue on.
76 error = efi_use_whole_disk(fd);
78 /* Flush the buffers to disk and invalidate the page cache. */
80 (void) ioctl(fd, BLKFLSBUF);
83 if (error && error != VT_ENOSPC) {
84 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
85 "relabel '%s': unable to read disk capacity"), path);
86 return (zfs_error(hdl, EZFS_NOCAP, msg));
92 * Read the EFI label from the config, if a label does not exist then
93 * pass back the error to the caller. If the caller has passed a non-NULL
94 * diskaddr argument then we set it to the starting address of the EFI
98 read_efi_label(nvlist_t *config, diskaddr_t *sb)
102 char diskname[MAXPATHLEN];
105 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
108 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
110 if ((fd = open(diskname, O_RDONLY|O_DIRECT)) >= 0) {
113 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
115 *sb = vtoc->efi_parts[0].p_start;
124 * determine where a partition starts on a disk in the current
128 find_start_block(nvlist_t *config)
132 diskaddr_t sb = MAXOFFSET_T;
135 if (nvlist_lookup_nvlist_array(config,
136 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
137 if (nvlist_lookup_uint64(config,
138 ZPOOL_CONFIG_WHOLE_DISK,
139 &wholedisk) != 0 || !wholedisk) {
140 return (MAXOFFSET_T);
142 if (read_efi_label(config, &sb) < 0)
147 for (c = 0; c < children; c++) {
148 sb = find_start_block(child[c]);
149 if (sb != MAXOFFSET_T) {
153 return (MAXOFFSET_T);
157 zpool_label_disk_check(char *path)
162 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
165 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
170 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
182 * Generate a unique partition name for the ZFS member. Partitions must
183 * have unique names to ensure udev will be able to create symlinks under
184 * /dev/disk/by-partlabel/ for all pool members. The partition names are
185 * of the form <pool>-<unique-id>.
188 zpool_label_name(char *label_name, int label_size)
193 fd = open("/dev/urandom", O_RDONLY);
195 if (read(fd, &id, sizeof (id)) != sizeof (id))
202 id = (((uint64_t)rand()) << 32) | (uint64_t)rand();
204 snprintf(label_name, label_size, "zfs-%016llx", (u_longlong_t)id);
208 * Label an individual disk. The name provided is the short name,
209 * stripped of any leading /dev path.
212 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
214 char path[MAXPATHLEN];
217 size_t resv = EFI_MIN_RESV_SIZE;
219 diskaddr_t start_block;
222 /* prepare an error message just in case */
223 (void) snprintf(errbuf, sizeof (errbuf),
224 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
229 verify(nvlist_lookup_nvlist(zhp->zpool_config,
230 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
232 if (zhp->zpool_start_block == 0)
233 start_block = find_start_block(nvroot);
235 start_block = zhp->zpool_start_block;
236 zhp->zpool_start_block = start_block;
239 start_block = NEW_START_BLOCK;
242 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
244 if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL)) < 0) {
246 * This shouldn't happen. We've long since verified that this
249 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
250 "label '%s': unable to open device: %d"), path, errno);
251 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
254 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
256 * The only way this can fail is if we run out of memory, or we
257 * were unable to read the disk's capacity
260 (void) no_memory(hdl);
263 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
264 "label '%s': unable to read disk capacity"), path);
266 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
269 slice_size = vtoc->efi_last_u_lba + 1;
270 slice_size -= EFI_MIN_RESV_SIZE;
271 if (start_block == MAXOFFSET_T)
272 start_block = NEW_START_BLOCK;
273 slice_size -= start_block;
274 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
276 vtoc->efi_parts[0].p_start = start_block;
277 vtoc->efi_parts[0].p_size = slice_size;
280 * Why we use V_USR: V_BACKUP confuses users, and is considered
281 * disposable by some EFI utilities (since EFI doesn't have a backup
282 * slice). V_UNASSIGNED is supposed to be used only for zero size
283 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
284 * etc. were all pretty specific. V_USR is as close to reality as we
285 * can get, in the absence of V_OTHER.
287 vtoc->efi_parts[0].p_tag = V_USR;
288 zpool_label_name(vtoc->efi_parts[0].p_name, EFI_PART_NAME_LEN);
290 vtoc->efi_parts[8].p_start = slice_size + start_block;
291 vtoc->efi_parts[8].p_size = resv;
292 vtoc->efi_parts[8].p_tag = V_RESERVED;
294 rval = efi_write(fd, vtoc);
296 /* Flush the buffers to disk and invalidate the page cache. */
298 (void) ioctl(fd, BLKFLSBUF);
301 rval = efi_rescan(fd);
304 * Some block drivers (like pcata) may not support EFI GPT labels.
305 * Print out a helpful error message directing the user to manually
306 * label the disk and give a specific slice.
312 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
313 "parted(8) and then provide a specific slice: %d"), rval);
314 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
320 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
321 (void) zfs_append_partition(path, MAXPATHLEN);
323 /* Wait to udev to signal use the device has settled. */
324 rval = zpool_label_disk_wait(path, DISK_LABEL_WAIT);
326 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
327 "detect device partitions on '%s': %d"), path, rval);
328 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
331 /* We can't be to paranoid. Read the label back and verify it. */
332 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
333 rval = zpool_label_disk_check(path);
335 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
336 "EFI label on '%s' is damaged. Ensure\nthis device "
337 "is not in use, and is functioning properly: %d"),
339 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));