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