xref: /illumos-gate/usr/src/cmd/fs.d/zfs/fstyp/fstyp.c (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * libfstyp module for zfs
30  */
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <libintl.h>
37 #include <locale.h>
38 #include <string.h>
39 #include <libnvpair.h>
40 #include <libzfs.h>
41 #include <libfstyp_module.h>
42 #include <errno.h>
43 
44 struct fstyp_zfs {
45 	int		fd;
46 	nvlist_t	*config;
47 };
48 
49 int	fstyp_mod_init(int fd, off_t offset, fstyp_mod_handle_t *handle);
50 void	fstyp_mod_fini(fstyp_mod_handle_t handle);
51 int	fstyp_mod_ident(fstyp_mod_handle_t handle);
52 int	fstyp_mod_get_attr(fstyp_mod_handle_t handle, nvlist_t **attrp);
53 
54 int
55 fstyp_mod_init(int fd, off_t offset, fstyp_mod_handle_t *handle)
56 {
57 	struct fstyp_zfs *h;
58 
59 	if (offset != 0) {
60 		return (FSTYP_ERR_OFFSET);
61 	}
62 
63 	if ((h = calloc(1, sizeof (struct fstyp_zfs))) == NULL) {
64 		return (FSTYP_ERR_NOMEM);
65 	}
66 	h->fd = fd;
67 
68 	*handle = (fstyp_mod_handle_t)h;
69 	return (0);
70 }
71 
72 void
73 fstyp_mod_fini(fstyp_mod_handle_t handle)
74 {
75 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
76 
77 	if (h->config != NULL) {
78 		nvlist_free(h->config);
79 	}
80 	free(h);
81 }
82 
83 int
84 fstyp_mod_ident(fstyp_mod_handle_t handle)
85 {
86 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
87 	uint64_t state;
88 	char	*str;
89 	uint64_t u64;
90 	char	buf[64];
91 
92 	if (zpool_read_label(h->fd, &h->config) != 0 ||
93 	    h->config == NULL) {
94 		return (FSTYP_ERR_NO_MATCH);
95 	}
96 
97 	if (nvlist_lookup_uint64(h->config, ZPOOL_CONFIG_POOL_STATE,
98 	    &state) != 0 || state == POOL_STATE_DESTROYED) {
99 		nvlist_free(h->config);
100 		h->config = NULL;
101 		return (FSTYP_ERR_NO_MATCH);
102 	}
103 
104 	/* add generic attributes */
105 	(void) nvlist_add_boolean_value(h->config, "gen_clean", B_TRUE);
106 	if (nvlist_lookup_uint64(h->config, "guid", &u64) == 0) {
107 		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)u64);
108 		(void) nvlist_add_string(h->config, "gen_guid", buf);
109 	}
110 	if (nvlist_lookup_uint64(h->config, "version", &u64) == 0) {
111 		(void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)u64);
112 		(void) nvlist_add_string(h->config, "gen_version", buf);
113 	}
114 	if (nvlist_lookup_string(h->config, "name", &str) == 0) {
115 		(void) nvlist_add_string(h->config, "gen_volume_label", str);
116 	}
117 
118 	return (0);
119 }
120 
121 int
122 fstyp_mod_get_attr(fstyp_mod_handle_t handle, nvlist_t **attrp)
123 {
124 	struct fstyp_zfs *h = (struct fstyp_zfs *)handle;
125 
126 	*attrp = h->config;
127 	return (0);
128 }
129