Merge branch 'openssh'
[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
35 #include <dirent.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45
46 #include "libhammer.h"
47
48 static u_int32_t count_snapshots(u_int32_t, char *, char *, int *);
49
50 libhammer_volinfo_t
51 libhammer_get_volinfo(const char *path)
52 {
53         struct hammer_pseudofs_data *pfs_od;
54         struct hammer_ioc_pfs_iterate pi;
55         struct hammer_ioc_info info;
56         libhammer_pfsinfo_t pfstmp;
57         libhammer_volinfo_t hvi;
58         int error = 0;
59         int fd;
60
61         if ((fd = open(path, O_RDONLY)) < 0)
62                 return NULL;
63
64         hvi = _libhammer_malloc(sizeof(*hvi));
65         TAILQ_INIT(&hvi->list_pseudo);
66         if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
67                 libhammer_free_volinfo(hvi);
68                 close(fd);
69                 return NULL;
70         }
71
72         /* Fill volume information */
73         snprintf(hvi->vol_name, TXTLEN, "%s", info.vol_name);
74         hvi->vol_fsid = info.vol_fsid;
75         hvi->version = info.version;
76         hvi->nvolumes = info.nvolumes;
77         hvi->inodes = info.inodes;
78         hvi->bigblocks = info.bigblocks;
79         hvi->freebigblocks = info.freebigblocks;
80         hvi->rsvbigblocks = info.rsvbigblocks;
81
82         bzero(&pi, sizeof(pi));
83         pi.ondisk = _libhammer_malloc(sizeof(*pfs_od));
84         while(error == 0) {
85                 error = ioctl(fd, HAMMERIOC_PFS_ITERATE, &pi);
86                 if (error == 0 &&
87                     ((pi.head.flags & HAMMER_PFSD_DELETED) == 0)) {
88                         /*
89                          * XXX - In the case the path passed is on PFS#0 but it
90                          * is not the mountpoint itself, it could produce a
91                          * wrong type of PFS.
92                          */
93                         pfstmp = _libhammer_malloc(sizeof(*pfstmp));
94                         pfs_od = pi.ondisk;
95                         pfstmp->ismaster =
96                             (pfs_od->mirror_flags & HAMMER_PFSD_SLAVE) ? 0 : 1;
97
98                         if (pi.pos == 0)
99                                 pfstmp->mountedon = strdup(path);
100                         else
101                                 pfstmp->mountedon =
102                                     libhammer_find_pfs_mount(pi.pos,
103                                         hvi->vol_fsid, pfstmp->ismaster);
104                         /*
105                          * Fill in structs used in the library. We don't rely on
106                          * HAMMER own struct but we do fill our own.
107                          */
108                         pfstmp->version = hvi->version;
109                         pfstmp->pfs_id = pi.pos;
110                         pfstmp->mirror_flags = pfs_od->mirror_flags;
111                         pfstmp->beg_tid = pfs_od->sync_beg_tid;
112                         pfstmp->end_tid = pfs_od->sync_end_tid;
113                         pfstmp->snapcount = count_snapshots(hvi->version,
114                             pfstmp->snapshots, pfstmp->mountedon,
115                             &pfstmp->head.error);
116
117                         TAILQ_INSERT_TAIL(&hvi->list_pseudo, pfstmp, entries);
118                 }
119                 pi.pos++;
120         }
121         free(pi.ondisk);
122
123         close (fd);
124
125         return (hvi);
126 }
127
128 void
129 libhammer_free_volinfo(libhammer_volinfo_t volinfo)
130 {
131         struct libhammer_pfsinfo *pfstmp;
132
133         while(!TAILQ_EMPTY(&volinfo->list_pseudo)) {
134                 pfstmp = TAILQ_FIRST(&volinfo->list_pseudo);
135                 free(pfstmp->mountedon);
136                 TAILQ_REMOVE(&volinfo->list_pseudo, pfstmp, entries);
137                 free(pfstmp);
138         }
139         free(volinfo);
140 }
141
142 static u_int32_t
143 count_snapshots(u_int32_t version, char *pfs_snapshots, char *mountedon,
144     int *errorp)
145 {
146         struct hammer_ioc_snapshot snapinfo;
147         char *snapshots_path, *fpath;
148         struct dirent *den;
149         struct stat st;
150         DIR *dir;
151         u_int32_t snapshot_count;
152         int fd;
153         int spallocated;
154
155         snapshot_count = 0;
156
157         bzero(&snapinfo, sizeof(struct hammer_ioc_snapshot));
158
159         fd = open(mountedon, O_RDONLY);
160         if (fd < 0) {
161                 *errorp = errno;
162                 return 0;
163         }
164
165         if (version < 3) {
166                 /*
167                  * old style: count the number of softlinks in the snapshots dir
168                  */
169                 if (pfs_snapshots[0]) {
170                         snapshots_path = pfs_snapshots;
171                         spallocated = 0;
172                 } else {
173                         asprintf(&snapshots_path, "%s/snapshots", mountedon);
174                         spallocated = 1;
175                 }
176                 if ((dir = opendir(snapshots_path)) != NULL) {
177                         while ((den = readdir(dir)) != NULL) {
178                                 if (den->d_name[0] == '.')
179                                         continue;
180                                 asprintf(&fpath, "%s/%s", snapshots_path,
181                                     den->d_name);
182                                 if (lstat(fpath, &st) == 0 &&
183                                     S_ISLNK(st.st_mode))
184                                         snapshot_count++;
185                                 free(fpath);
186                         }
187                         closedir(dir);
188                 }
189                 if (spallocated)
190                         free(snapshots_path);
191         } else {
192                 /*
193                  * new style: file system meta-data
194                  */
195                 do {
196                         if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapinfo) < 0) {
197                                 *errorp = errno;
198                                 goto out;
199                         }
200
201                         snapshot_count += snapinfo.count;
202                 } while (snapinfo.head.error == 0 && snapinfo.count);
203         }
204
205 out:
206         if (fd != -1)
207                 close(fd);
208         return snapshot_count;
209 }