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