2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 2N/A * Pool import support functions. 2N/A * To import a pool, we rely on reading the configuration information from the 2N/A * ZFS label of each device. If we successfully read the label, then we 2N/A * organize the configuration information in the following hierarchy: 2N/A * pool guid -> toplevel vdev guid -> label txg 2N/A * Duplicate entries matching this same tuple will be discarded. Once we have 2N/A * examined every device, we pick the best label txg config for each toplevel 2N/A * vdev. We then arrange these toplevel vdevs into a complete pool config, and 2N/A * update any paths that have changed. Finally, we attempt to import the pool 2N/A * using our derived config, and record the results. 2N/A * Intermediate structures used to gather configuration information. 2N/A * Go through and fix up any path and/or devid information for the given vdev 2N/A * This is a leaf (file or disk) vdev. In either case, go through 2N/A * the name list and see if we find a matching guid. If so, replace 2N/A * the path and see if we can calculate a new devid. 2N/A * There may be multiple names associated with a particular guid, in 2N/A * which case we have overlapping slices or multiple paths to the same 2N/A * disk. If this is the case, then we want to pick the path that is 2N/A * the most similar to the original, where "most similar" is the number 2N/A * of matching characters starting from the end of the path. This will 2N/A * preserve slice numbers even if the disks have been reorganized, and 2N/A * will also catch preferred disk names if multiple paths exist. 2N/A * At this point, 'count' is the number of characters 2N/A * matched from the end. 2N/A * Add the given configuration to the list of known devices. 2N/A * If this is a hot spare not currently in use or level 2 cache 2N/A * device, add it to the list of names to translate, but don't do 2N/A * If we have a valid config but cannot read any of these fields, then 2N/A * it means we have a half-initialized label. In vdev_label_init() 2N/A * we write a label with txg == 0 so that we can identify the device 2N/A * in case the user refers to the same disk later on. If we fail to 2N/A * create the pool, we'll be left with a label in this state 2N/A * which should not be considered part of a valid pool. 2N/A * First, see if we know about this pool. If not, then add it to the 2N/A * list of known pools. 2N/A * Second, see if we know about this toplevel vdev. Add it if its 2N/A * Third, see if we have a config with a matching transaction group. If 2N/A * so, then we do nothing. Otherwise, add it to the list of known 2N/A * At this point we've successfully added our config to the list of 2N/A * known configs. The last thing to do is add the vdev guid -> path 2N/A * mappings so that we can fix up the configuration as necessary before 2N/A * Returns true if the named pool matches the given GUID. 2N/A * Determine if the vdev id is a hole in the namespace. 2N/A /* Top-level is a hole */ 2N/A * Convert our list of pools into the definitive set of configurations. We 2N/A * start by picking the best config for each toplevel vdev. Once that's done, 2N/A * we assemble the toplevel vdevs into a full config for the pool. We make a 2N/A * pass to fix up any incorrect paths, and then add it to the main list to 2N/A * return to the user. 2N/A * Iterate over all toplevel vdevs. Grab the pool configuration 2N/A * from the first one we find, and then go through the rest and 2N/A * add them as necessary to the 'vdevs' member of the config. 2N/A * Determine the best configuration for this vdev by 2N/A * selecting the config with the latest transaction 2N/A * We rely on the fact that the max txg for the 2N/A * pool will contain the most up-to-date information 2N/A * about the valid top-levels in the vdev namespace. 2N/A * Copy the relevant pieces of data to the pool 2N/A * hostid (if available) 2N/A * hostname (if available) 2N/A * Add this top-level vdev to the child array. 2N/A * If we have information about all the top-levels then 2N/A * clean up the nvlist which we've constructed. This 2N/A * means removing any extraneous devices that are 2N/A * beyond the valid range or adding devices to the end 2N/A * of our array which appear to be missing. 2N/A * The vdev namespace may contain holes as a result of 2N/A * device removal. We must add them back into the vdev 2N/A * tree before we process any missing devices. 2N/A * Holes in the namespace are treated as 2N/A * "hole" top-level vdevs and have a 2N/A * special flag set on them. 2N/A * Look for any missing top-level vdevs. If this is the case, 2N/A * create a faked up 'missing' vdev as a placeholder. We cannot 2N/A * simply compress the child array, because the kernel performs 2N/A * certain checks to make sure the vdev IDs match their location 2N/A * in the configuration. 2N/A * Put all of this pool's top-level vdevs into a root vdev. 2N/A * Go through and fix up any paths and/or devids based on our 2N/A * known list of vdev GUID -> path mappings. 2N/A * Add the root vdev to this pool's configuration. 2N/A * zdb uses this path to report on active pools that were 2N/A * imported or created using -R. 2N/A * Determine if this pool is currently active, in which case we 2N/A * can't actually import it. 2N/A * Go through and update the paths for spares, now that we have 2N/A * Update the paths for l2cache devices. 2N/A * Restore the original information read from the actual label. 2N/A * Add this pool to the list of configs. 2N/A * Return the offset of the given label. 2N/A * Given a file descriptor, read the label information and return an nvlist 2N/A * describing the configuration, if there is one. 2N/A char *
rn_path;
/* always used in the AVLs compare function */ 2N/A * slices zero and two are the most likely to provide results, 2N/A * so put those first 2N/A return (
rv > 0 ?
1 : -
1);
2N/A * protect against division by zero for disk labels that 2N/A * contain a bogus sector size 2N/A /* too small to contain a zpool? */ 2N/A * on x86 we'll still have leftover links that point 2N/A * to slices s[9-15], so use NDKMAP instead 2N/A /* nodes p[1-4] are never used with EFI labels */ 2N/A * Using raw devices instead of block devices when we're reading 2N/A * labels skips a bunch of slow operations during close(2). 2N/A /* symlink to a device that's no longer there */ 2N/A * Ignore failed stats. We only want regular 2N/A * files, character devs and block devs. 2N/A /* this file is too small to hold a zpool */ 2N/A * Try to read the disk label first so we don't have to 2N/A * open a bunch of minor nodes that can't have a zpool. 2N/A * Given a file descriptor, clear (zero) the label information. This function 2N/A * is currently only used in the appliance stack as part of the ZFS sysevent 2N/A * Normalize input path, remove any superfluous slashes. 2N/A * If we deal with a directory we can use realpath(3C) instantly 2N/A * to normalize our path. 2N/A * We can not use realpath(3C) directly for files as it resolves 2N/A * symbolic links delivering device paths into /devices which are 2N/A * of no use for us later. 2N/A * Returns malloc'ed memory on success, NULL on failure. 2N/A * If we are dealing with a directory we're almost 2N/A * done. Copy at most MAXPATHLEN - 2 to always 2N/A * leave room for the trailing slash and NUL 2N/A /* Caution, dirname(3C) below modifies its input */ 2N/A /* Caution, basename(3C) below modifies its input */ 2N/A * Produce a raw and non-raw version of the input path name 2N/A * for every object living in the /dev/ namespace 2N/A * that we recognize and return it. 2N/A * Returns malloc'ed memory on success, NULL on failure. 2N/A * If we are in the /dev/ namespace, produce a raw and 2N/A * non-raw version of the input path. 2N/A * Assume already got a raw name, copy it, 2N/A * unraw path if needed and return. 2N/A * Assume already got a block name, copy it, 2N/A * setup raw path if needed and return. 2N/A * We are left with either a file or directory path 2N/A * outside /dev/, just copy it. 2N/A * Given a list of paths to search, find all pools stored on disk. 2N/A * This includes partial pools which are not available to import. 2N/A * If no args are given (iarg->path is 0), then the default directory 2N/A * (/dev/rdsk) is searched. poolname or guid (but not both) are 2N/A * provided by the caller when trying to import a specific pool. 2N/A * Go through and read the label configuration information from every 2N/A * possible device, organizing the information according to pool GUID 2N/A * and toplevel GUID. 2N/A * Using raw devices instead of block devices when we're 2N/A * reading the labels skips a bunch of slow operations during 2N/A * Determine the raw and non-raw version of the input path. 2N/A * If opening the rawpath fails, we fall back to using 2N/A * the normalized user supplied input path and ignore 2N/A * this optimization. 2N/A * Revert back to normalized input path. 2N/A * Treat this as a regular file. 2N/A * This is not MT-safe, but we have no MT consumers 2N/A * Create a thread pool to do all of this in parallel; 2N/A * rn_nozpool is not protected, so this is racy in that 2N/A * multiple tasks could decide that the same slice can 2N/A * not hold a zpool, which is benign. Also choose 2N/A * double the number of processors; we hold a lot of 2N/A * locks in the kernel, so going beyond this doesn't 2N/A /* Use non-raw path for the config */ 2N/A "failed to read cache file contents"));
2N/A "invalid or corrupt cache file contents"));
2N/A * Given a cache file, return the contents as a list of importable pools. 2N/A * poolname or guid (but not both) are provided by the caller when trying 2N/A * to import a specific pool. If 'trusted' is specified, then we don't 2N/A * attempt a TRYIMPORT and treat the cachefile as the absolute truth. When 2N/A * using this option, the resulting config won't be complete in the traditional 2N/A * sense, as there will be no vdev statistics or state information. 2N/A * Go through and get the current state of the pools and refresh their 2N/A * Contracted interface for cluster to find the pool(s) configuration 2N/A * Determines if the pool is in use. If so, it returns true and the state of 2N/A * the pool as well as the name of the pool. Both strings are allocated and 2N/A * must be freed by the caller. 2N/A * A pool with an exported state may in fact be imported 2N/A * read-only, so check the in-core state to see if it's 2N/A * active and imported read-only. If it is, set 2N/A * its state to active. 2N/A * For an active pool, we have to determine if it's really part 2N/A * of a currently active pool (in which case the pool will exist 2N/A * and the guid will be the same), or whether it's part of an 2N/A * active pool that was disconnected without being explicitly 2N/A * Because the device may have been removed while 2N/A * offlined, we only report it as active if the vdev is 2N/A * still present in the config. Otherwise, pretend like 2N/A * If this is an active spare within another pool, we 2N/A * treat it like an unused hot spare. This allows the 2N/A * user to create a pool with a hot spare that currently 2N/A * in use within another pool. Since we return B_TRUE, 2N/A * libdiskmgt will continue to prevent generic consumers 2N/A * from using the device. 2N/A * For a hot spare, it can be either definitively in use, or 2N/A * potentially active. To determine if it's in use, we iterate 2N/A * over all pools in the system and search for one with a spare 2N/A * with a matching guid. 2N/A * Due to the shared nature of spares, we don't actually report 2N/A * the potentially active case as in use. This means the user 2N/A * can freely create pools on the hot spares of exported pools, 2N/A * but to do otherwise makes the resulting code complicated, and 2N/A * we end up having to deal with this case anyway. 2N/A * Check if any pool is currently using this l2cache device.