1 /*- 2 * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions, and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * substantially similar to the "NO WARRANTY" disclaimer below 13 * ("Disclaimer") and any redistribution must be conditioned upon 14 * including a substantially similar Disclaimer requirement for further 15 * binary redistribution. 16 * 17 * NO WARRANTY 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGES. 29 * 30 * Authors: Justin T. Gibbs (Spectra Logic Corporation) 31 * 32 * $FreeBSD$ 33 */ 34 35 /** 36 * \file vdev_iterator.cc 37 * 38 * Implementation of the VdevIterator class. 39 */ 40 #include <sys/cdefs.h> 41 #include <sys/fs/zfs.h> 42 43 #include <stdint.h> 44 #include <syslog.h> 45 46 #include <libzfs.h> 47 48 #include <list> 49 #include <string> 50 51 #include <devdctl/exception.h> 52 #include <devdctl/guid.h> 53 54 #include "vdev.h" 55 #include "vdev_iterator.h" 56 #include "zfsd_exception.h" 57 58 /*============================ Namespace Control =============================*/ 59 using DevdCtl::Guid; 60 61 /*=========================== Class Implementations ==========================*/ 62 /*------------------------------- VdevIterator -------------------------------*/ 63 VdevIterator::VdevIterator(zpool_handle_t *pool) 64 : m_poolConfig(zpool_get_config(pool, NULL)) 65 { 66 Reset(); 67 } 68 69 VdevIterator::VdevIterator(nvlist_t *poolConfig) 70 : m_poolConfig(poolConfig) 71 { 72 Reset(); 73 } 74 75 void 76 VdevIterator::Reset() 77 { 78 nvlist_t *rootVdev; 79 int result; 80 81 result = nvlist_lookup_nvlist(m_poolConfig, 82 ZPOOL_CONFIG_VDEV_TREE, 83 &rootVdev); 84 if (result != 0) 85 throw ZfsdException(m_poolConfig, "Unable to extract " 86 "ZPOOL_CONFIG_VDEV_TREE from pool."); 87 m_vdevQueue.assign(1, rootVdev); 88 } 89 90 nvlist_t * 91 VdevIterator::Next() 92 { 93 nvlist_t *vdevConfig; 94 95 if (m_vdevQueue.empty()) 96 return (NULL); 97 98 for (;;) { 99 nvlist_t **vdevChildren; 100 int result; 101 u_int numChildren; 102 103 vdevConfig = m_vdevQueue.front(); 104 m_vdevQueue.pop_front(); 105 106 /* Expand non-leaf vdevs. */ 107 result = nvlist_lookup_nvlist_array(vdevConfig, 108 ZPOOL_CONFIG_CHILDREN, 109 &vdevChildren, &numChildren); 110 if (result != 0) { 111 /* leaf vdev */ 112 break; 113 } 114 115 /* 116 * Insert children at the head of the queue to effect a 117 * depth first traversal of the tree. 118 */ 119 m_vdevQueue.insert(m_vdevQueue.begin(), vdevChildren, 120 vdevChildren + numChildren); 121 } 122 123 return (vdevConfig); 124 } 125 126 void 127 VdevIterator::Each(VdevCallback_t *callBack, void *callBackArg) 128 { 129 nvlist_t *vdevConfig; 130 131 Reset(); 132 while ((vdevConfig = Next()) != NULL) { 133 Vdev vdev(m_poolConfig, vdevConfig); 134 135 if (callBack(vdev, callBackArg)) 136 break; 137 } 138 } 139 140 nvlist_t * 141 VdevIterator::Find(Guid vdevGUID) 142 { 143 nvlist_t *vdevConfig; 144 145 Reset(); 146 while ((vdevConfig = Next()) != NULL) { 147 Vdev vdev(m_poolConfig, vdevConfig); 148 149 if (vdev.GUID() == vdevGUID) 150 return (vdevConfig); 151 } 152 return (NULL); 153 } 154