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
36 #include <assert.h>
37 #include <fcntl.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/mount.h>
46 #include <unistd.h>
47 #include <uuid.h>
48
49 #include "libhammer.h"
50
51 char *
libhammer_find_pfs_mount(uuid_t * unique_uuid)52 libhammer_find_pfs_mount(uuid_t *unique_uuid)
53 {
54 struct hammer_ioc_pseudofs_rw pfs;
55 struct hammer_pseudofs_data pfsd;
56 struct statfs *mntbuf;
57 int mntsize;
58 int curmount;
59 int fd;
60 size_t mntbufsize;
61 uuid_t uuid;
62 char *retval;
63
64 retval = NULL;
65
66 /* Do not continue if there are no mounted filesystems */
67 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
68 if (mntsize <= 0)
69 return retval;
70
71 mntbufsize = mntsize * sizeof(struct statfs);
72 mntbuf = _libhammer_malloc(mntbufsize);
73
74 mntsize = getfsstat(mntbuf, (long)mntbufsize, MNT_NOWAIT);
75 curmount = mntsize - 1;
76
77 /*
78 * Iterate all the mounted points looking for the PFS passed to
79 * this function.
80 */
81 while(curmount >= 0) {
82 struct statfs *mnt = &mntbuf[curmount];
83 /*
84 * Discard any non null(5) or hammer(5) filesystems as synthetic
85 * filesystems like procfs(5) could accept ioctl calls and thus
86 * produce bogus results.
87 */
88 if ((strcmp("hammer", mnt->f_fstypename) != 0) &&
89 (strcmp("null", mnt->f_fstypename) != 0)) {
90 curmount--;
91 continue;
92 }
93 bzero(&pfs, sizeof(pfs));
94 bzero(&pfsd, sizeof(pfsd));
95 pfs.pfs_id = -1;
96 pfs.ondisk = &pfsd;
97 pfs.bytes = sizeof(struct hammer_pseudofs_data);
98 fd = open(mnt->f_mntonname, O_RDONLY);
99 if (fd < 0 || (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0)) {
100 close(fd);
101 curmount--;
102 continue;
103 }
104
105 memcpy(&uuid, &pfs.ondisk->unique_uuid, sizeof(uuid));
106 if (uuid_compare(unique_uuid, &uuid, NULL) == 0) {
107 retval = strdup(mnt->f_mntonname);
108 close(fd);
109 break;
110 }
111
112 curmount--;
113 close(fd);
114 }
115 free(mntbuf);
116
117 return retval;
118 }
119
120 /*
121 * Find out the path that can be used to open(2) a PFS
122 * when it is not mounted. It allocates *path so the
123 * caller is in charge of freeing it up.
124 */
125 void
libhammer_pfs_canonical_path(char * mtpt,libhammer_pfsinfo_t pip,char ** path)126 libhammer_pfs_canonical_path(char * mtpt, libhammer_pfsinfo_t pip, char **path)
127 {
128 struct statfs st;
129
130 assert(pip != NULL);
131 assert(mtpt != NULL);
132
133 if ((statfs(mtpt, &st) < 0) ||
134 ((strcmp("hammer", st.f_fstypename) != 0) &&
135 (strcmp("null", st.f_fstypename) != 0))) {
136 *path = NULL;
137 return;
138 }
139
140 if (pip->ismaster)
141 asprintf(path, "%s/@@-1:%.5d", mtpt,
142 pip->pfs_id);
143 else
144 asprintf(path, "%s/@@0x%016jx:%.5d", mtpt,
145 pip->end_tid, pip->pfs_id);
146 }
147
148
149 /*
150 * Allocate len bytes of memory and return the pointer.
151 * It'll exit in the case no memory could be allocated.
152 *
153 * To be used only by the library itself.
154 */
155 void *
_libhammer_malloc(size_t len)156 _libhammer_malloc(size_t len)
157 {
158 void *m;
159
160 m = calloc(len, sizeof(char));
161 if (m == NULL)
162 errx(1, "Failed to allocate %zd bytes", len);
163
164 return (m);
165 }
166