1300906Sasomers/*-
2300906Sasomers * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
3300906Sasomers * All rights reserved.
4300906Sasomers *
5300906Sasomers * Redistribution and use in source and binary forms, with or without
6300906Sasomers * modification, are permitted provided that the following conditions
7300906Sasomers * are met:
8300906Sasomers * 1. Redistributions of source code must retain the above copyright
9300906Sasomers *    notice, this list of conditions, and the following disclaimer,
10300906Sasomers *    without modification.
11300906Sasomers * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12300906Sasomers *    substantially similar to the "NO WARRANTY" disclaimer below
13300906Sasomers *    ("Disclaimer") and any redistribution must be conditioned upon
14300906Sasomers *    including a substantially similar Disclaimer requirement for further
15300906Sasomers *    binary redistribution.
16300906Sasomers *
17300906Sasomers * NO WARRANTY
18300906Sasomers * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19300906Sasomers * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20300906Sasomers * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21300906Sasomers * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22300906Sasomers * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23300906Sasomers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24300906Sasomers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25300906Sasomers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26300906Sasomers * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27300906Sasomers * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28300906Sasomers * POSSIBILITY OF SUCH DAMAGES.
29300906Sasomers *
30300906Sasomers * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31300906Sasomers *
32300906Sasomers * $FreeBSD$
33300906Sasomers */
34300906Sasomers
35300906Sasomers/**
36300906Sasomers * \file zpool_list.cc
37300906Sasomers *
38300906Sasomers * Implementation of the ZpoolList class.
39300906Sasomers */
40300906Sasomers#include <sys/cdefs.h>
41300906Sasomers#include <sys/fs/zfs.h>
42300906Sasomers
43300906Sasomers#include <stdint.h>
44300906Sasomers
45300906Sasomers#include <libzfs.h>
46300906Sasomers
47300906Sasomers#include <list>
48300906Sasomers#include <map>
49300906Sasomers#include <string>
50300906Sasomers
51300906Sasomers#include <devdctl/guid.h>
52300906Sasomers#include <devdctl/event.h>
53300906Sasomers#include <devdctl/event_factory.h>
54300906Sasomers#include <devdctl/exception.h>
55300906Sasomers#include <devdctl/consumer.h>
56300906Sasomers
57300906Sasomers#include "vdev.h"
58300906Sasomers#include "vdev_iterator.h"
59300906Sasomers#include "zpool_list.h"
60300906Sasomers#include "zfsd.h"
61300906Sasomers
62300906Sasomers/*============================ Namespace Control =============================*/
63300906Sasomersusing DevdCtl::Guid;
64300906Sasomers
65300906Sasomers/*=========================== Class Implementations ==========================*/
66300906Sasomers/*--------------------------------- ZpoolList --------------------------------*/
67300906Sasomersbool
68300906SasomersZpoolList::ZpoolAll(zpool_handle_t *pool, nvlist_t *poolConfig, void *cbArg)
69300906Sasomers{
70300906Sasomers	return (true);
71300906Sasomers}
72300906Sasomers
73300906Sasomersbool
74300906SasomersZpoolList::ZpoolByGUID(zpool_handle_t *pool, nvlist_t *poolConfig,
75300906Sasomers			   void *cbArg)
76300906Sasomers{
77300906Sasomers	Guid *desiredPoolGUID(static_cast<Guid *>(cbArg));
78300906Sasomers	uint64_t poolGUID;
79300906Sasomers
80300906Sasomers	/* We are only intested in the pool that matches our pool GUID. */
81300906Sasomers	return (nvlist_lookup_uint64(poolConfig, ZPOOL_CONFIG_POOL_GUID,
82300906Sasomers				     &poolGUID) == 0
83300906Sasomers	     && poolGUID == (uint64_t)*desiredPoolGUID);
84300906Sasomers}
85300906Sasomers
86300906Sasomersbool
87300906SasomersZpoolList::ZpoolByName(zpool_handle_t *pool, nvlist_t *poolConfig, void *cbArg)
88300906Sasomers{
89300906Sasomers	const string &desiredPoolName(*static_cast<const string *>(cbArg));
90300906Sasomers
91300906Sasomers	/* We are only intested in the pool that matches our pool GUID. */
92300906Sasomers	return (desiredPoolName == zpool_get_name(pool));
93300906Sasomers}
94300906Sasomers
95300906Sasomersint
96300906SasomersZpoolList::LoadIterator(zpool_handle_t *pool, void *data)
97300906Sasomers{
98300906Sasomers	ZpoolList *zpl(reinterpret_cast<ZpoolList *>(data));
99300906Sasomers	nvlist_t  *poolConfig(zpool_get_config(pool, NULL));
100300906Sasomers
101300906Sasomers	if (zpl->m_filter(pool, poolConfig, zpl->m_filterArg))
102300906Sasomers		zpl->push_back(pool);
103300906Sasomers	else
104300906Sasomers		zpool_close(pool);
105300906Sasomers	return (0);
106300906Sasomers}
107300906Sasomers
108300906SasomersZpoolList::ZpoolList(PoolFilter_t *filter, void * filterArg)
109300906Sasomers : m_filter(filter),
110300906Sasomers   m_filterArg(filterArg)
111300906Sasomers{
112300906Sasomers	zpool_iter(g_zfsHandle, LoadIterator, this);
113300906Sasomers}
114300906Sasomers
115300906SasomersZpoolList::~ZpoolList()
116300906Sasomers{
117300906Sasomers	for (iterator it(begin()); it != end(); it++)
118300906Sasomers		zpool_close(*it);
119300906Sasomers
120300906Sasomers	clear();
121300906Sasomers}
122