Merge branch 'vendor/TNFTP'
[dragonfly.git] / lib / libhammer / info.c
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 Antonio Huete <tuxillo@quantumachine.net>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include <dirent.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44
45 #include <libhammer.h>
46
47 static u_int32_t count_snapshots(u_int32_t, char *, char *, int *);
48
49 libhammer_volinfo_t
50 libhammer_get_volinfo(const char *path)
51 {
52         struct hammer_ioc_pseudofs_rw pseudofs;
53         struct hammer_pseudofs_data *pfs_od;
54         struct hammer_ioc_info info;
55         libhammer_pfsinfo_t pfstmp;
56         libhammer_volinfo_t hvi;
57         int pfs_id;
58         int fd;
59
60         hvi = _libhammer_malloc(sizeof(*hvi));
61
62         TAILQ_INIT(&hvi->list_pseudo);
63
64         if ((fd = open(path, O_RDONLY)) < 0)
65                 goto error1;
66
67         if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0)
68                 goto error1;
69
70         /* Fill volume information */
71         snprintf(hvi->vol_name, TXTLEN, "%s", info.vol_name);
72         hvi->vol_fsid = info.vol_fsid;
73         hvi->version = info.version;
74         hvi->nvolumes = info.nvolumes;
75         hvi->inodes = info.inodes;
76         hvi->bigblocks = info.bigblocks;
77         hvi->freebigblocks = info.freebigblocks;
78         hvi->rsvbigblocks = info.rsvbigblocks;
79
80         /*
81          * XXX - By now we iterate the whole range of +65000 possible
82          * pseudofs to discover the ones that are in use. Of course
83          * this is suboptimal. Best way would be to extract the
84          * pseudofs information from the control records in the
85          * B-Tree, that's in kernel land.
86          */
87         pfs_od = _libhammer_malloc(sizeof(*pfs_od));
88
89         for(pfs_id = 0; pfs_id < HAMMER_MAX_PFS; pfs_id++) {
90                 /* Clear the struct to be passed to the ioctl. */
91                 bzero(&pseudofs, sizeof(pseudofs));
92
93                 pfstmp = _libhammer_malloc(sizeof(*pfstmp));
94
95                 pseudofs.pfs_id = pfs_id;
96                 pseudofs.ondisk = pfs_od;
97                 pseudofs.bytes = sizeof(struct hammer_pseudofs_data);
98                 pseudofs.version = HAMMER_IOC_PSEUDOFS_VERSION;
99                 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pseudofs) != -1) {
100                         /* XXX
101                          * It may be completely wrong in the case that we pass a path
102                          * that is in PFS 0 but isn't the mount path of it. There's no
103                          * easy way to do this.
104                          */
105                         pfstmp->ismaster = (pfs_od->mirror_flags & HAMMER_PFSD_SLAVE)
106                             ? 0 : 1;
107
108                         if (pfs_id == 0)
109                                 pfstmp->mountedon = strdup(path);
110                         else
111                                 pfstmp->mountedon = libhammer_find_pfs_mount(pfs_id,
112                                     hvi->vol_fsid, pfstmp->ismaster);
113                         /*
114                          * Fill in structs used in the library. We don't rely on
115                          * HAMMER own struct but we do fill our own.
116                          */
117                         pfstmp->version = pseudofs.version;
118                         pfstmp->pfs_id = pseudofs.pfs_id;
119                         pfstmp->mirror_flags = pfs_od->mirror_flags;
120                         pfstmp->snapcount = count_snapshots(hvi->version,
121                             pfstmp->snapshots, pfstmp->mountedon, &pfstmp->head.error);
122
123                         TAILQ_INSERT_TAIL(&hvi->list_pseudo, pfstmp, entries);
124                 }
125         }
126
127         goto end;
128
129 error1:
130         libhammer_free_volinfo(hvi);
131         if (fd != -1)
132                 close(fd);
133
134 end:
135         return (hvi);
136 }
137
138 void
139 libhammer_free_volinfo(libhammer_volinfo_t volinfo)
140 {
141         struct libhammer_pfsinfo *pfstmp;
142
143         while(!TAILQ_EMPTY(&volinfo->list_pseudo)) {
144                 pfstmp = TAILQ_FIRST(&volinfo->list_pseudo);
145                 free(pfstmp->mountedon);
146                 TAILQ_REMOVE(&volinfo->list_pseudo, pfstmp, entries);
147                 free(pfstmp);
148         }
149         free(volinfo);
150 }
151
152 static u_int32_t
153 count_snapshots(u_int32_t version, char *pfs_snapshots, char *mountedon, int *errorp)
154 {
155         struct hammer_ioc_snapshot snapinfo;
156         char *snapshots_path, *fpath;
157         struct dirent *den;
158         struct stat st;
159         DIR *dir;
160         u_int32_t snapshot_count;
161         int fd;
162
163         snapshot_count = 0;
164
165         bzero(&snapinfo, sizeof(struct hammer_ioc_snapshot));
166
167         fd = open(mountedon, O_RDONLY);
168         if (fd < 0) {
169                 *errorp = errno;
170                 return 0;
171         }
172
173         if (version < 3) {
174                 /*
175                  * old style: count the number of softlinks in the snapshots dir
176                  */
177                 if (pfs_snapshots[0])
178                         snapshots_path = pfs_snapshots;
179                 else
180                         asprintf(&snapshots_path, "%s/snapshots", mountedon);
181                 if ((dir = opendir(snapshots_path)) != NULL) {
182                         while ((den = readdir(dir)) != NULL) {
183                                 if (den->d_name[0] == '.')
184                                         continue;
185                                 asprintf(&fpath, "%s/%s", snapshots_path,
186                                     den->d_name);
187                                 if (lstat(fpath, &st) == 0 &&
188                                     S_ISLNK(st.st_mode))
189                                         snapshot_count++;
190                                 free(fpath);
191                         }
192                         closedir(dir);
193                 }
194         } else {
195                 /*
196                  * new style: file system meta-data
197                  */
198                 do {
199                         if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapinfo) < 0) {
200                                 *errorp = errno;
201                                 goto out;
202                         }
203
204                         snapshot_count += snapinfo.count;
205                 } while (snapinfo.head.error == 0 && snapinfo.count);
206         }
207
208 out:
209         if (fd != -1)
210                 close(fd);
211         return snapshot_count;
212 }