2 * Copyright (c) 2011 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Antonio Huete <tuxillo@quantumachine.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
42 #include <sys/param.h>
46 #include "libhammer.h"
48 static u_int32_t count_snapshots(u_int32_t, char *, char *, int *);
51 libhammer_get_volinfo(const char *path)
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;
61 if ((fd = open(path, O_RDONLY)) < 0)
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);
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;
82 bzero(&pi, sizeof(pi));
83 pi.ondisk = _libhammer_malloc(sizeof(*pfs_od));
85 error = ioctl(fd, HAMMERIOC_PFS_ITERATE, &pi);
87 ((pi.head.flags & HAMMER_PFSD_DELETED) == 0)) {
89 * XXX - In the case the path passed is on PFS#0 but it
90 * is not the mountpoint itself, it could produce a
93 pfstmp = _libhammer_malloc(sizeof(*pfstmp));
96 (pfs_od->mirror_flags & HAMMER_PFSD_SLAVE) ? 0 : 1;
99 * Fill in structs used in the library. We don't rely on
100 * HAMMER own struct but we do fill our own.
102 pfstmp->version = hvi->version;
103 pfstmp->pfs_id = pi.pos;
104 pfstmp->mirror_flags = pfs_od->mirror_flags;
105 pfstmp->beg_tid = pfs_od->sync_beg_tid;
106 pfstmp->end_tid = pfs_od->sync_end_tid;
108 libhammer_find_pfs_mount(&pfs_od->unique_uuid);
109 pfstmp->snapcount = count_snapshots(hvi->version,
110 pfstmp->snapshots, pfstmp->mountedon,
111 &pfstmp->head.error);
113 TAILQ_INSERT_TAIL(&hvi->list_pseudo, pfstmp, entries);
125 libhammer_free_volinfo(libhammer_volinfo_t volinfo)
127 struct libhammer_pfsinfo *pfstmp;
129 while(!TAILQ_EMPTY(&volinfo->list_pseudo)) {
130 pfstmp = TAILQ_FIRST(&volinfo->list_pseudo);
131 free(pfstmp->mountedon);
132 TAILQ_REMOVE(&volinfo->list_pseudo, pfstmp, entries);
139 count_snapshots(u_int32_t version, char *pfs_snapshots, char *mountedon,
142 struct hammer_ioc_snapshot snapinfo;
143 char *snapshots_path, *fpath;
147 u_int32_t snapshot_count;
153 bzero(&snapinfo, sizeof(struct hammer_ioc_snapshot));
155 fd = open(mountedon, O_RDONLY);
163 * old style: count the number of softlinks in the snapshots dir
165 if (pfs_snapshots[0]) {
166 snapshots_path = pfs_snapshots;
169 asprintf(&snapshots_path, "%s/snapshots", mountedon);
172 if ((dir = opendir(snapshots_path)) != NULL) {
173 while ((den = readdir(dir)) != NULL) {
174 if (den->d_name[0] == '.')
176 asprintf(&fpath, "%s/%s", snapshots_path,
178 if (lstat(fpath, &st) == 0 &&
186 free(snapshots_path);
189 * new style: file system meta-data
192 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapinfo) < 0) {
197 snapshot_count += snapinfo.count;
198 } while (snapinfo.head.error == 0 && snapinfo.count);
204 return snapshot_count;