xref: /freebsd/cddl/usr.sbin/zfsd/zpool_list.cc (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
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 
33 /**
34  * \file zpool_list.cc
35  *
36  * Implementation of the ZpoolList class.
37  */
38 #include <sys/cdefs.h>
39 #include <sys/byteorder.h>
40 #include <sys/fs/zfs.h>
41 
42 #include <stdint.h>
43 
44 #include <libzfs.h>
45 
46 #include <list>
47 #include <map>
48 #include <string>
49 
50 #include <devdctl/guid.h>
51 #include <devdctl/event.h>
52 #include <devdctl/event_factory.h>
53 #include <devdctl/exception.h>
54 #include <devdctl/consumer.h>
55 
56 #include "vdev.h"
57 #include "vdev_iterator.h"
58 #include "zpool_list.h"
59 #include "zfsd.h"
60 
61 /*============================ Namespace Control =============================*/
62 using DevdCtl::Guid;
63 
64 /*=========================== Class Implementations ==========================*/
65 /*--------------------------------- ZpoolList --------------------------------*/
66 bool
67 ZpoolList::ZpoolAll(zpool_handle_t *pool, nvlist_t *poolConfig, void *cbArg)
68 {
69 	return (true);
70 }
71 
72 bool
73 ZpoolList::ZpoolByGUID(zpool_handle_t *pool, nvlist_t *poolConfig,
74 			   void *cbArg)
75 {
76 	Guid *desiredPoolGUID(static_cast<Guid *>(cbArg));
77 	uint64_t poolGUID;
78 
79 	/* We are only intested in the pool that matches our pool GUID. */
80 	return (nvlist_lookup_uint64(poolConfig, ZPOOL_CONFIG_POOL_GUID,
81 				     &poolGUID) == 0
82 	     && poolGUID == (uint64_t)*desiredPoolGUID);
83 }
84 
85 bool
86 ZpoolList::ZpoolByName(zpool_handle_t *pool, nvlist_t *poolConfig, void *cbArg)
87 {
88 	const string &desiredPoolName(*static_cast<const string *>(cbArg));
89 
90 	/* We are only intested in the pool that matches our pool GUID. */
91 	return (desiredPoolName == zpool_get_name(pool));
92 }
93 
94 int
95 ZpoolList::LoadIterator(zpool_handle_t *pool, void *data)
96 {
97 	ZpoolList *zpl(reinterpret_cast<ZpoolList *>(data));
98 	nvlist_t  *poolConfig(zpool_get_config(pool, NULL));
99 
100 	if (zpl->m_filter(pool, poolConfig, zpl->m_filterArg))
101 		zpl->push_back(pool);
102 	else
103 		zpool_close(pool);
104 	return (0);
105 }
106 
107 ZpoolList::ZpoolList(PoolFilter_t *filter, void * filterArg)
108  : m_filter(filter),
109    m_filterArg(filterArg)
110 {
111 	zpool_iter(g_zfsHandle, LoadIterator, this);
112 }
113 
114 ZpoolList::~ZpoolList()
115 {
116 	for (iterator it(begin()); it != end(); it++)
117 		zpool_close(*it);
118 
119 	clear();
120 }
121