xref: /dragonfly/lib/libhammer/misc.c (revision d22a69a4)
1 /*
2  * Copyright (c) 2011 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * by Antonio Huete <tuxillo@quantumachine.net>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #include <fcntl.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <uuid.h>
45 
46 #include <libhammer.h>
47 
48 char *
49 libhammer_find_pfs_mount(int pfsid, uuid_t parentuuid, int ismaster)
50 {
51 	struct hammer_ioc_info hi;
52 	struct statfs *mntbuf;
53 	int mntsize;
54 	int curmount;
55 	int fd;
56 	size_t	mntbufsize;
57 	char *trailstr;
58 	char *retval;
59 
60 	retval = NULL;
61 
62 	/* Do not continue if there are no mounted filesystems */
63 	mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
64 	if (mntsize <= 0)
65 		return retval;
66 
67 	mntbufsize = (mntsize) * sizeof(struct statfs);
68 	mntbuf = _libhammer_malloc(mntbufsize);
69 	if (mntbuf == NULL) {
70 		perror("show_info");
71 		exit(EXIT_FAILURE);
72 	}
73 
74 	mntsize = getfsstat(mntbuf, (long)mntbufsize, MNT_NOWAIT);
75 	curmount = mntsize - 1;
76 
77 	asprintf(&trailstr, ":%05d", pfsid);
78 
79 	/*
80 	 * Iterate all the mounted points looking for the PFS passed to
81 	 * this function.
82 	 */
83 	while(curmount >= 0) {
84 		/*
85 		 * We need to avoid that PFS belonging to other HAMMER
86 		 * filesystems are showed as mounted, so we compare
87 		 * against the FSID, which is presumable to be unique.
88 		 */
89 		bzero(&hi, sizeof(hi));
90 		if ((fd = open(mntbuf[curmount].f_mntfromname, O_RDONLY)) < 0) {
91 			curmount--;
92 			continue;
93 		}
94 
95 		if ((ioctl(fd, HAMMERIOC_GET_INFO, &hi)) < 0) {
96 			curmount--;
97 			continue;
98 		}
99 
100 		if (strstr(mntbuf[curmount].f_mntfromname, trailstr) != NULL &&
101 		    (uuid_compare(&hi.vol_fsid, &parentuuid, NULL)) == 0) {
102 			if (ismaster) {
103 				if (strstr(mntbuf[curmount].f_mntfromname,
104 				    "@@-1") != NULL) {
105 					retval =
106 					    strdup(mntbuf[curmount].f_mntonname);
107 					break;
108 				}
109 			} else {
110 				if (strstr(mntbuf[curmount].f_mntfromname,
111 				    "@@0x") != NULL ) {
112 					retval =
113 					    strdup(mntbuf[curmount].f_mntonname);
114 					break;
115 				}
116 			}
117 		}
118 		curmount--;
119 		close(fd);
120 	}
121 	free(trailstr);
122 
123 	return retval;
124 }
125 
126 /*
127  * Allocate len bytes of memory and return the pointer.
128  * It'll exit in the case no memory could be allocated.
129  *
130  * To be used only by the library itself.
131  */
132 void *
133 _libhammer_malloc(size_t len)
134 {
135 	void *m;
136 
137 	m = calloc(len, sizeof(char));
138 	if (m == NULL)
139 		errx(1, "Failed to allocate %zd bytes", len);
140 
141 	return (m);
142 }
143