1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <string.h>
30*7c478bd9Sstevel@tonic-gate #include <errno.h>
31*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
32*7c478bd9Sstevel@tonic-gate #include <kvm.h>
33*7c478bd9Sstevel@tonic-gate #include <kstat.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
37*7c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
38*7c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
40*7c478bd9Sstevel@tonic-gate #include <inttypes.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include "libfsmgt.h"
45*7c478bd9Sstevel@tonic-gate #include "replica.h"
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #define	IGNORE	0
48*7c478bd9Sstevel@tonic-gate #define	DEV	1
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * Private variables
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate static char *mntopts[] = { MNTOPT_IGNORE, MNTOPT_DEV, NULL };
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * Private method declarations
58*7c478bd9Sstevel@tonic-gate  */
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate static int ignore(char *);
61*7c478bd9Sstevel@tonic-gate static int get_kstat_info(nfs_mntlist_t *, int *);
62*7c478bd9Sstevel@tonic-gate static nfs_mntlist_t *get_mount_data(fs_mntlist_t *, int *);
63*7c478bd9Sstevel@tonic-gate static nfs_mntlist_t *get_nfs_info(fs_mntlist_t *, int *);
64*7c478bd9Sstevel@tonic-gate static nfs_mntlist_t *kstat_mount(nfs_mntlist_t *, kstat_t *);
65*7c478bd9Sstevel@tonic-gate static int load_kstat_data(kstat_ctl_t *, nfs_mntlist_t *, kstat_t *, int *);
66*7c478bd9Sstevel@tonic-gate static kid_t safe_kstat_read(kstat_ctl_t *, kstat_t *, void *, int *);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * Public methods
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate void
nfs_free_mntinfo_list(nfs_mntlist_t * list)73*7c478bd9Sstevel@tonic-gate nfs_free_mntinfo_list(nfs_mntlist_t *list)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t *tmp;
76*7c478bd9Sstevel@tonic-gate 	int i;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	while (list != NULL) {
79*7c478bd9Sstevel@tonic-gate 		free(list->nml_resource);
80*7c478bd9Sstevel@tonic-gate 		free(list->nml_mountp);
81*7c478bd9Sstevel@tonic-gate 		free(list->nml_fstype);
82*7c478bd9Sstevel@tonic-gate 		free(list->nml_mntopts);
83*7c478bd9Sstevel@tonic-gate 		free(list->nml_time);
84*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < list->nml_failovercount; i++) {
85*7c478bd9Sstevel@tonic-gate 			if (list->nml_failoverlist[i] != NULL)
86*7c478bd9Sstevel@tonic-gate 				free(list->nml_failoverlist[i]);
87*7c478bd9Sstevel@tonic-gate 		}
88*7c478bd9Sstevel@tonic-gate 		free(list->nml_failoverlist);
89*7c478bd9Sstevel@tonic-gate 		free(list->nml_securitymode);
90*7c478bd9Sstevel@tonic-gate 		tmp = list->next;
91*7c478bd9Sstevel@tonic-gate 		free(list);
92*7c478bd9Sstevel@tonic-gate 		list = tmp;
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate } /* nfs_free_mntinfo_list */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate nfs_mntlist_t *
nfs_get_filtered_mount_list(char * resource,char * mountp,char * mntopts,char * time,boolean_t find_overlays,int * errp)97*7c478bd9Sstevel@tonic-gate nfs_get_filtered_mount_list(char *resource, char *mountp, char *mntopts,
98*7c478bd9Sstevel@tonic-gate 	char *time, boolean_t find_overlays, int *errp) {
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	fs_mntlist_t	*fs_mount_list;
101*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t	*nfs_mount_list;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	fs_mount_list = fs_get_filtered_mount_list(resource, mountp,
104*7c478bd9Sstevel@tonic-gate 		MNTTYPE_NFS, mntopts, time, find_overlays, errp);
105*7c478bd9Sstevel@tonic-gate 	if (fs_mount_list == NULL) {
106*7c478bd9Sstevel@tonic-gate 		return (NULL);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if ((nfs_mount_list = get_nfs_info(fs_mount_list, errp)) == NULL) {
110*7c478bd9Sstevel@tonic-gate 		fs_free_mount_list(fs_mount_list);
111*7c478bd9Sstevel@tonic-gate 		return (NULL);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	fs_free_mount_list(fs_mount_list);
115*7c478bd9Sstevel@tonic-gate 	return (nfs_mount_list);
116*7c478bd9Sstevel@tonic-gate } /* nfs_get_filtered_mount_list */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate nfs_mntlist_t *
nfs_get_mounts_by_mntopt(char * mntopt,boolean_t find_overlays,int * errp)119*7c478bd9Sstevel@tonic-gate nfs_get_mounts_by_mntopt(char *mntopt, boolean_t find_overlays, int *errp)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate 	fs_mntlist_t	*fs_mount_list;
122*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t	*nfs_mount_list;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	fs_mount_list = fs_get_mounts_by_mntopt(mntopt, find_overlays, errp);
125*7c478bd9Sstevel@tonic-gate 	if (fs_mount_list == NULL) {
126*7c478bd9Sstevel@tonic-gate 		return (NULL);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	if ((nfs_mount_list = get_nfs_info(fs_mount_list, errp)) == NULL) {
130*7c478bd9Sstevel@tonic-gate 		fs_free_mount_list(fs_mount_list);
131*7c478bd9Sstevel@tonic-gate 		return (NULL);
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	fs_free_mount_list(fs_mount_list);
135*7c478bd9Sstevel@tonic-gate 	return (nfs_mount_list);
136*7c478bd9Sstevel@tonic-gate } /* nfs_get_mount_by_mntopt */
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate nfs_mntlist_t *
nfs_get_mount_list(int * errp)139*7c478bd9Sstevel@tonic-gate nfs_get_mount_list(int *errp)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	fs_mntlist_t *fs_mount_list;
142*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t *nfs_mount_list;
143*7c478bd9Sstevel@tonic-gate 	boolean_t find_overlays = B_TRUE;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	if ((fs_mount_list = fs_get_mount_list(find_overlays, errp)) == NULL) {
146*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "nfs_mntinfo: Can't access mnttab. %s\n",
147*7c478bd9Sstevel@tonic-gate 		    strerror(*errp));
148*7c478bd9Sstevel@tonic-gate 		return (NULL);
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if ((nfs_mount_list = get_nfs_info(fs_mount_list, errp)) == NULL) {
152*7c478bd9Sstevel@tonic-gate 		fs_free_mount_list(fs_mount_list);
153*7c478bd9Sstevel@tonic-gate 		return (NULL);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	fs_free_mount_list(fs_mount_list);
157*7c478bd9Sstevel@tonic-gate 	return (nfs_mount_list);
158*7c478bd9Sstevel@tonic-gate } /* nfs_get_mount_list */
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate /*
161*7c478bd9Sstevel@tonic-gate  * Private methods
162*7c478bd9Sstevel@tonic-gate  */
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate static int
get_kstat_info(nfs_mntlist_t * nfs_mntinfo,int * errp)165*7c478bd9Sstevel@tonic-gate get_kstat_info(nfs_mntlist_t *nfs_mntinfo, int *errp)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	kstat_ctl_t *libkstat_cookie = NULL;
168*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t *mrp;
169*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	if ((libkstat_cookie = kstat_open()) == NULL) {
172*7c478bd9Sstevel@tonic-gate 		*errp = errno;
173*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
174*7c478bd9Sstevel@tonic-gate 		    "nfs_mntinfo: kstat_open(): can't open /dev/kstat.\n");
175*7c478bd9Sstevel@tonic-gate 		return (-1);
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 	/*
178*7c478bd9Sstevel@tonic-gate 	 * Each kstat consists of header and data sections that are
179*7c478bd9Sstevel@tonic-gate 	 * connected as a "chain" or linked list of kstat stuctures.
180*7c478bd9Sstevel@tonic-gate 	 * The kc_chain used here is the pointer to the global kstat
181*7c478bd9Sstevel@tonic-gate 	 * chain (or the head of the chain of kstat's).
182*7c478bd9Sstevel@tonic-gate 	 */
183*7c478bd9Sstevel@tonic-gate 	for (ksp = libkstat_cookie->kc_chain; ksp; ksp = ksp->ks_next) {
184*7c478bd9Sstevel@tonic-gate 		if ((ksp->ks_type == KSTAT_TYPE_RAW) &&
185*7c478bd9Sstevel@tonic-gate 		    (strcmp(ksp->ks_module, "nfs") == 0) &&
186*7c478bd9Sstevel@tonic-gate 		    (strcmp(ksp->ks_name, "mntinfo") == 0) &&
187*7c478bd9Sstevel@tonic-gate 		    ((mrp = kstat_mount(nfs_mntinfo, ksp)) != NULL)) {
188*7c478bd9Sstevel@tonic-gate 			if (load_kstat_data(libkstat_cookie, mrp, ksp, errp)
189*7c478bd9Sstevel@tonic-gate 			    == -1) {
190*7c478bd9Sstevel@tonic-gate 				nfs_free_mntinfo_list(mrp);
191*7c478bd9Sstevel@tonic-gate 				return (-1);
192*7c478bd9Sstevel@tonic-gate 			}
193*7c478bd9Sstevel@tonic-gate 		}
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 	return (0);
196*7c478bd9Sstevel@tonic-gate } /* get_kstat_info */
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate static int
load_kstat_data(kstat_ctl_t * libkstat_cookie,nfs_mntlist_t * mrp,kstat_t * ksp,int * errp)199*7c478bd9Sstevel@tonic-gate load_kstat_data(kstat_ctl_t *libkstat_cookie, nfs_mntlist_t *mrp,
200*7c478bd9Sstevel@tonic-gate     kstat_t *ksp, int *errp)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	struct mntinfo_kstat mik;
203*7c478bd9Sstevel@tonic-gate 	seconfig_t nfs_sec;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	if (mrp == 0) {
206*7c478bd9Sstevel@tonic-gate 		return (0);
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	if (safe_kstat_read(libkstat_cookie, ksp, &mik, errp) == -1) {
210*7c478bd9Sstevel@tonic-gate 		return (-1);
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	if (strlcpy(mrp->nml_proto, mik.mik_proto, KNC_STRSIZE)
214*7c478bd9Sstevel@tonic-gate 	    >= KNC_STRSIZE) {
215*7c478bd9Sstevel@tonic-gate 		*errp = errno;
216*7c478bd9Sstevel@tonic-gate 		return (-1);
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate 	if (strlcpy(mrp->nml_curserver, mik.mik_curserver, SYS_NMLN)
219*7c478bd9Sstevel@tonic-gate 	    >= SYS_NMLN) {
220*7c478bd9Sstevel@tonic-gate 		*errp = errno;
221*7c478bd9Sstevel@tonic-gate 		return (-1);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 	mrp->nml_vers = mik.mik_vers;
224*7c478bd9Sstevel@tonic-gate 	/*
225*7c478bd9Sstevel@tonic-gate 	 *  get the secmode name from /etc/nfssec.conf.
226*7c478bd9Sstevel@tonic-gate 	 */
227*7c478bd9Sstevel@tonic-gate 	if (!nfs_getseconfig_bynumber(mik.mik_secmod, &nfs_sec)) {
228*7c478bd9Sstevel@tonic-gate 		mrp->nml_securitymode = strdup(nfs_sec.sc_name);
229*7c478bd9Sstevel@tonic-gate 	} else {
230*7c478bd9Sstevel@tonic-gate 		mrp->nml_securitymode = NULL;
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 	mrp->nml_curread = mik.mik_curread;
233*7c478bd9Sstevel@tonic-gate 	mrp->nml_curwrite = mik.mik_curwrite;
234*7c478bd9Sstevel@tonic-gate 	mrp->nml_timeo = mik.mik_timeo;
235*7c478bd9Sstevel@tonic-gate 	mrp->nml_retrans = mik.mik_retrans;
236*7c478bd9Sstevel@tonic-gate 	mrp->nml_acregmin = mik.mik_acregmin;
237*7c478bd9Sstevel@tonic-gate 	mrp->nml_acregmax = mik.mik_acregmax;
238*7c478bd9Sstevel@tonic-gate 	mrp->nml_acdirmin = mik.mik_acdirmin;
239*7c478bd9Sstevel@tonic-gate 	mrp->nml_acdirmax = mik.mik_acdirmax;
240*7c478bd9Sstevel@tonic-gate 	mrp->nml_hard =
241*7c478bd9Sstevel@tonic-gate 	    ((mik.mik_flags & MI_HARD) ? B_TRUE : B_FALSE);
242*7c478bd9Sstevel@tonic-gate 	mrp->nml_intr =
243*7c478bd9Sstevel@tonic-gate 	    ((mik.mik_flags & MI_INT) ? B_TRUE : B_FALSE);
244*7c478bd9Sstevel@tonic-gate 	mrp->nml_noac =
245*7c478bd9Sstevel@tonic-gate 	    ((mik.mik_flags & MI_NOAC) ? B_TRUE : B_FALSE);
246*7c478bd9Sstevel@tonic-gate 	mrp->nml_nocto =
247*7c478bd9Sstevel@tonic-gate 	    ((mik.mik_flags & MI_NOCTO) ? B_TRUE : B_FALSE);
248*7c478bd9Sstevel@tonic-gate 	mrp->nml_grpid =
249*7c478bd9Sstevel@tonic-gate 	    ((mik.mik_flags & MI_GRPID) ? B_TRUE : B_FALSE);
250*7c478bd9Sstevel@tonic-gate 	mrp->nml_directio =
251*7c478bd9Sstevel@tonic-gate 	    ((mik.mik_flags & MI_DIRECTIO) ? B_TRUE : B_FALSE);
252*7c478bd9Sstevel@tonic-gate 	mrp->nml_xattr =
253*7c478bd9Sstevel@tonic-gate 	    ((mik.mik_flags & MI_EXTATTR) ? B_TRUE : B_FALSE);
254*7c478bd9Sstevel@tonic-gate 	return (0);
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate nfs_mntlist_t *
kstat_mount(nfs_mntlist_t * nfs_mntinfo,kstat_t * ksp)258*7c478bd9Sstevel@tonic-gate kstat_mount(nfs_mntlist_t *nfs_mntinfo, kstat_t *ksp) {
259*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t *mrp;
260*7c478bd9Sstevel@tonic-gate 	/*
261*7c478bd9Sstevel@tonic-gate 	 * MAXMIN is used to retrieve the minor number
262*7c478bd9Sstevel@tonic-gate 	 * which is compared to the kstat instance.
263*7c478bd9Sstevel@tonic-gate 	 * If they are the same then this is an instance
264*7c478bd9Sstevel@tonic-gate 	 * for which mount information is needed.
265*7c478bd9Sstevel@tonic-gate 	 * MAXMIN is the maximum minor number and is
266*7c478bd9Sstevel@tonic-gate 	 * defined in mkdev.h.
267*7c478bd9Sstevel@tonic-gate 	 */
268*7c478bd9Sstevel@tonic-gate 	mrp = nfs_mntinfo;
269*7c478bd9Sstevel@tonic-gate 	while ((mrp != NULL) &&
270*7c478bd9Sstevel@tonic-gate 	    ((mrp->nml_fsid & MAXMIN) != ksp->ks_instance)) {
271*7c478bd9Sstevel@tonic-gate 		mrp = mrp->next;
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 	return (mrp);
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate static nfs_mntlist_t *
get_nfs_info(fs_mntlist_t * fslist,int * errp)277*7c478bd9Sstevel@tonic-gate get_nfs_info(fs_mntlist_t *fslist, int *errp) {
278*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t *mrp = NULL;
279*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t *headptr = NULL;
280*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t *tailptr = NULL;
281*7c478bd9Sstevel@tonic-gate 	fs_mntlist_t *fsmnt_list;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	for (fsmnt_list = fslist; fsmnt_list; fsmnt_list = fsmnt_list->next) {
284*7c478bd9Sstevel@tonic-gate 		/* ignore non "nfs" and the "ignore" entries */
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 		if ((strcmp(fsmnt_list->fstype, MNTTYPE_NFS) != 0) ||
287*7c478bd9Sstevel@tonic-gate 		    (ignore(fsmnt_list->mntopts))) {
288*7c478bd9Sstevel@tonic-gate 			continue;
289*7c478bd9Sstevel@tonic-gate 		}
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 		if ((mrp = get_mount_data(fsmnt_list, errp)) == NULL) {
292*7c478bd9Sstevel@tonic-gate 			nfs_free_mntinfo_list(headptr);
293*7c478bd9Sstevel@tonic-gate 			return (NULL);
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 		if (tailptr == NULL) {
296*7c478bd9Sstevel@tonic-gate 			headptr = mrp;
297*7c478bd9Sstevel@tonic-gate 			tailptr = mrp;
298*7c478bd9Sstevel@tonic-gate 			tailptr->next = NULL;
299*7c478bd9Sstevel@tonic-gate 		} else {
300*7c478bd9Sstevel@tonic-gate 			tailptr->next = mrp;
301*7c478bd9Sstevel@tonic-gate 			tailptr = mrp;
302*7c478bd9Sstevel@tonic-gate 			tailptr->next = NULL;
303*7c478bd9Sstevel@tonic-gate 		}
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	if (get_kstat_info(headptr, errp) == -1) {
307*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
308*7c478bd9Sstevel@tonic-gate 		return (NULL);
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 	return (headptr);
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate } /* get_nfs_info */
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate static nfs_mntlist_t *
get_mount_data(fs_mntlist_t * fsmnt_list,int * errp)316*7c478bd9Sstevel@tonic-gate get_mount_data(fs_mntlist_t *fsmnt_list, int *errp) {
317*7c478bd9Sstevel@tonic-gate 	struct replica *rep_list; /* defined in replica.h */
318*7c478bd9Sstevel@tonic-gate 	nfs_mntlist_t *mrp;
319*7c478bd9Sstevel@tonic-gate 	int i, server_count = 0;
320*7c478bd9Sstevel@tonic-gate 	struct stat	stat_buf;
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	if ((mrp = malloc(sizeof (nfs_mntlist_t))) == 0) {
323*7c478bd9Sstevel@tonic-gate 		*errp = errno;
324*7c478bd9Sstevel@tonic-gate 		return (NULL);
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	if ((stat(fsmnt_list->mountp, &stat_buf) == 0)) {
328*7c478bd9Sstevel@tonic-gate 		mrp->nml_fsid = stat_buf.st_dev;
329*7c478bd9Sstevel@tonic-gate 	} else {
330*7c478bd9Sstevel@tonic-gate 		*errp = errno;
331*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
332*7c478bd9Sstevel@tonic-gate 		return (NULL);
333*7c478bd9Sstevel@tonic-gate 	}
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if ((mrp->nml_resource = strdup(fsmnt_list->resource))
336*7c478bd9Sstevel@tonic-gate 	    == NULL) {
337*7c478bd9Sstevel@tonic-gate 		*errp = errno;
338*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
339*7c478bd9Sstevel@tonic-gate 		return (NULL);
340*7c478bd9Sstevel@tonic-gate 	}
341*7c478bd9Sstevel@tonic-gate 	if ((rep_list =
342*7c478bd9Sstevel@tonic-gate 	    parse_replica(mrp->nml_resource, &server_count)) == NULL) {
343*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
344*7c478bd9Sstevel@tonic-gate 		return (NULL);
345*7c478bd9Sstevel@tonic-gate 	}
346*7c478bd9Sstevel@tonic-gate 	if ((mrp->nml_failoverlist =
347*7c478bd9Sstevel@tonic-gate 	    calloc(server_count, sizeof (char *))) == NULL) {
348*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
349*7c478bd9Sstevel@tonic-gate 		return (NULL);
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < server_count; i++) {
352*7c478bd9Sstevel@tonic-gate 		mrp->nml_failoverlist[i] =
353*7c478bd9Sstevel@tonic-gate 		    malloc(strlen(rep_list[i].host) + strlen(":") +
354*7c478bd9Sstevel@tonic-gate 		    strlen(rep_list[i].path) + 2);
355*7c478bd9Sstevel@tonic-gate 		if (!mrp->nml_failoverlist[i]) {
356*7c478bd9Sstevel@tonic-gate 			nfs_free_mntinfo_list(mrp);
357*7c478bd9Sstevel@tonic-gate 			return (NULL);
358*7c478bd9Sstevel@tonic-gate 		}
359*7c478bd9Sstevel@tonic-gate 		sprintf(mrp->nml_failoverlist[i], "%s%s%s",
360*7c478bd9Sstevel@tonic-gate 		    rep_list[i].host, ":", rep_list[i].path);
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 	/*
363*7c478bd9Sstevel@tonic-gate 	 * If the number of servers is not 1 then resource is
364*7c478bd9Sstevel@tonic-gate 	 * either a failover list or there is an error. In either
365*7c478bd9Sstevel@tonic-gate 	 * case the path can't be determined and curpath is set to
366*7c478bd9Sstevel@tonic-gate 	 * unknown".
367*7c478bd9Sstevel@tonic-gate 	 */
368*7c478bd9Sstevel@tonic-gate 	if (server_count == 1) {
369*7c478bd9Sstevel@tonic-gate 		if (strcmp(rep_list[0].host, "nfs") == 0) {
370*7c478bd9Sstevel@tonic-gate 			char *path;
371*7c478bd9Sstevel@tonic-gate 			char *last;
372*7c478bd9Sstevel@tonic-gate 			path = strdup(rep_list[0].path);
373*7c478bd9Sstevel@tonic-gate 			if ((path = (char *)strtok_r(path, "//",
374*7c478bd9Sstevel@tonic-gate 			    &last)) != NULL) {
375*7c478bd9Sstevel@tonic-gate 				strcpy(mrp->nml_curpath,
376*7c478bd9Sstevel@tonic-gate 				    strcat("/", last));
377*7c478bd9Sstevel@tonic-gate 			} else {
378*7c478bd9Sstevel@tonic-gate 				/*
379*7c478bd9Sstevel@tonic-gate 				 * If NULL is returned this is an
380*7c478bd9Sstevel@tonic-gate 				 * invalid path entry. no path can
381*7c478bd9Sstevel@tonic-gate 				 * be determined.
382*7c478bd9Sstevel@tonic-gate 				 */
383*7c478bd9Sstevel@tonic-gate 				strcpy(mrp->nml_curpath, "unknown");
384*7c478bd9Sstevel@tonic-gate 			}
385*7c478bd9Sstevel@tonic-gate 		} else {
386*7c478bd9Sstevel@tonic-gate 			strcpy(mrp->nml_curpath,
387*7c478bd9Sstevel@tonic-gate 			    (strchr(mrp->nml_failoverlist[0],
388*7c478bd9Sstevel@tonic-gate 			    ':') + 1));
389*7c478bd9Sstevel@tonic-gate 		}
390*7c478bd9Sstevel@tonic-gate 	} else {
391*7c478bd9Sstevel@tonic-gate 		/*
392*7c478bd9Sstevel@tonic-gate 		 * more than one server in the failover list
393*7c478bd9Sstevel@tonic-gate 		 * path can't be determined.
394*7c478bd9Sstevel@tonic-gate 		 */
395*7c478bd9Sstevel@tonic-gate 		strcpy(mrp->nml_curpath, "unknown");
396*7c478bd9Sstevel@tonic-gate 	}
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	mrp->nml_failovercount = server_count;
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < server_count; i++) {
401*7c478bd9Sstevel@tonic-gate 		if (rep_list[i].host) {
402*7c478bd9Sstevel@tonic-gate 			free(rep_list[i].host);
403*7c478bd9Sstevel@tonic-gate 		}
404*7c478bd9Sstevel@tonic-gate 		if (rep_list[i].path) {
405*7c478bd9Sstevel@tonic-gate 			free(rep_list[i].path);
406*7c478bd9Sstevel@tonic-gate 		}
407*7c478bd9Sstevel@tonic-gate 	}
408*7c478bd9Sstevel@tonic-gate 	free(rep_list);
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	if ((mrp->nml_mountp = strdup(fsmnt_list->mountp)) == NULL) {
411*7c478bd9Sstevel@tonic-gate 		*errp = errno;
412*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
413*7c478bd9Sstevel@tonic-gate 		return (NULL);
414*7c478bd9Sstevel@tonic-gate 	}
415*7c478bd9Sstevel@tonic-gate 	if ((mrp->nml_fstype = strdup(fsmnt_list->fstype)) == NULL) {
416*7c478bd9Sstevel@tonic-gate 		*errp = errno;
417*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
418*7c478bd9Sstevel@tonic-gate 		return (NULL);
419*7c478bd9Sstevel@tonic-gate 	}
420*7c478bd9Sstevel@tonic-gate 	if ((mrp->nml_mntopts = strdup(fsmnt_list->mntopts)) == NULL) {
421*7c478bd9Sstevel@tonic-gate 		*errp = errno;
422*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
423*7c478bd9Sstevel@tonic-gate 		return (NULL);
424*7c478bd9Sstevel@tonic-gate 	}
425*7c478bd9Sstevel@tonic-gate 	if ((mrp->nml_time = strdup(fsmnt_list->time)) == NULL) {
426*7c478bd9Sstevel@tonic-gate 		*errp = errno;
427*7c478bd9Sstevel@tonic-gate 		nfs_free_mntinfo_list(mrp);
428*7c478bd9Sstevel@tonic-gate 		return (NULL);
429*7c478bd9Sstevel@tonic-gate 	}
430*7c478bd9Sstevel@tonic-gate 	if (fsmnt_list->overlayed) {
431*7c478bd9Sstevel@tonic-gate 		mrp->nml_overlayed = B_TRUE;
432*7c478bd9Sstevel@tonic-gate 	} else {
433*7c478bd9Sstevel@tonic-gate 		mrp->nml_overlayed = B_FALSE;
434*7c478bd9Sstevel@tonic-gate 	}
435*7c478bd9Sstevel@tonic-gate 	return (mrp);
436*7c478bd9Sstevel@tonic-gate } /* get_mount_data */
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate kid_t
safe_kstat_read(kstat_ctl_t * libkstat_cookie,kstat_t * ksp,void * data,int * errp)439*7c478bd9Sstevel@tonic-gate safe_kstat_read(
440*7c478bd9Sstevel@tonic-gate 	kstat_ctl_t *libkstat_cookie,
441*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp,
442*7c478bd9Sstevel@tonic-gate 	void *data,
443*7c478bd9Sstevel@tonic-gate 	int *errp)
444*7c478bd9Sstevel@tonic-gate {
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(libkstat_cookie, ksp, data);
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	if (kstat_chain_id == -1) {
449*7c478bd9Sstevel@tonic-gate 		*errp = errno;
450*7c478bd9Sstevel@tonic-gate 		return (-1);
451*7c478bd9Sstevel@tonic-gate 	}
452*7c478bd9Sstevel@tonic-gate 	return (kstat_chain_id);
453*7c478bd9Sstevel@tonic-gate } /* safe_kstat_read */
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate /*
457*7c478bd9Sstevel@tonic-gate  * ignore - Checks for the ignore mount option in the mount opts string.
458*7c478bd9Sstevel@tonic-gate  * Returns 1 if the ignore option is found and 0 if not.
459*7c478bd9Sstevel@tonic-gate  */
460*7c478bd9Sstevel@tonic-gate static int
ignore(char * opts)461*7c478bd9Sstevel@tonic-gate ignore(char *opts)
462*7c478bd9Sstevel@tonic-gate {
463*7c478bd9Sstevel@tonic-gate 	char *value;
464*7c478bd9Sstevel@tonic-gate 	char *s;
465*7c478bd9Sstevel@tonic-gate 	char *tmp;
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	if (opts == NULL)
468*7c478bd9Sstevel@tonic-gate 		return (0);
469*7c478bd9Sstevel@tonic-gate 	s = strdup(opts);
470*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
471*7c478bd9Sstevel@tonic-gate 		return (0);
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	tmp = s;
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate 	while (*s != '\0') {
476*7c478bd9Sstevel@tonic-gate 		if (getsubopt(&s, mntopts, &value) == IGNORE) {
477*7c478bd9Sstevel@tonic-gate 			free(tmp);
478*7c478bd9Sstevel@tonic-gate 			return (1);
479*7c478bd9Sstevel@tonic-gate 		}
480*7c478bd9Sstevel@tonic-gate 	}
481*7c478bd9Sstevel@tonic-gate 	free(tmp);
482*7c478bd9Sstevel@tonic-gate 	return (0);
483*7c478bd9Sstevel@tonic-gate } /* ignore */
484