xref: /dflybsd-src/lib/libhammer/misc.c (revision 848b370cf5dbae7a7b0f22ec95396b1cdb8c9262)
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 
70 	mntsize = getfsstat(mntbuf, (long)mntbufsize, MNT_NOWAIT);
71 	curmount = mntsize - 1;
72 
73 	asprintf(&trailstr, ":%05d", pfsid);
74 
75 	/*
76 	 * Iterate all the mounted points looking for the PFS passed to
77 	 * this function.
78 	 */
79 	while(curmount >= 0) {
80 		struct statfs *mnt = &mntbuf[curmount];
81 		/*
82 		 * We need to avoid that PFS belonging to other HAMMER
83 		 * filesystems are showed as mounted, so we compare
84 		 * against the FSID, which is presumable to be unique.
85 		 */
86 		bzero(&hi, sizeof(hi));
87 		if ((fd = open(mnt->f_mntfromname, O_RDONLY)) < 0) {
88 			curmount--;
89 			continue;
90 		}
91 
92 		if (ioctl(fd, HAMMERIOC_GET_INFO, &hi) < 0) {
93 			close(fd);
94 			curmount--;
95 			continue;
96 		}
97 
98 		if (strstr(mnt->f_mntfromname, trailstr) != NULL &&
99 		    (uuid_compare(&hi.vol_fsid, &parentuuid, NULL)) == 0) {
100 			if (ismaster) {
101 				if (strstr(mnt->f_mntfromname,
102 				    "@@-1") != NULL) {
103 					retval = strdup(mnt->f_mntonname);
104 					break;
105 				}
106 			} else {
107 				if (strstr(mnt->f_mntfromname,
108 				    "@@0x") != NULL ) {
109 					retval = strdup(mnt->f_mntonname);
110 					break;
111 				}
112 			}
113 		}
114 		curmount--;
115 		close(fd);
116 	}
117 	free(trailstr);
118 	free(mntbuf);
119 
120 	return retval;
121 }
122 
123 /*
124  * Allocate len bytes of memory and return the pointer.
125  * It'll exit in the case no memory could be allocated.
126  *
127  * To be used only by the library itself.
128  */
129 void *
130 _libhammer_malloc(size_t len)
131 {
132 	void *m;
133 
134 	m = calloc(len, sizeof(char));
135 	if (m == NULL)
136 		errx(1, "Failed to allocate %zd bytes", len);
137 
138 	return (m);
139 }
140