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
41 #include <sys/param.h>
45 #include <libhammer.h>
47 static u_int32_t
count_snapshots(u_int32_t
, char *, char *, int *);
50 libhammer_get_volinfo(const char *path
)
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
;
60 hvi
= _libhammer_malloc(sizeof(*hvi
));
62 TAILQ_INIT(&hvi
->list_pseudo
);
64 if ((fd
= open(path
, O_RDONLY
)) < 0)
67 if ((ioctl(fd
, HAMMERIOC_GET_INFO
, &info
)) < 0)
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
;
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.
87 pfs_od
= _libhammer_malloc(sizeof(*pfs_od
));
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
));
93 pfstmp
= _libhammer_malloc(sizeof(*pfstmp
));
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) {
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.
105 pfstmp
->ismaster
= (pfs_od
->mirror_flags
& HAMMER_PFSD_SLAVE
)
109 pfstmp
->mountedon
= strdup(path
);
111 pfstmp
->mountedon
= libhammer_find_pfs_mount(pfs_id
,
112 hvi
->vol_fsid
, pfstmp
->ismaster
);
114 * Fill in structs used in the library. We don't rely on
115 * HAMMER own struct but we do fill our own.
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
);
123 TAILQ_INSERT_TAIL(&hvi
->list_pseudo
, pfstmp
, entries
);
130 libhammer_free_volinfo(hvi
);
139 libhammer_free_volinfo(libhammer_volinfo_t volinfo
)
141 struct libhammer_pfsinfo
*pfstmp
;
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
);
153 count_snapshots(u_int32_t version
, char *pfs_snapshots
, char *mountedon
, int *errorp
)
155 struct hammer_ioc_snapshot snapinfo
;
156 char *snapshots_path
, *fpath
;
160 u_int32_t snapshot_count
;
165 bzero(&snapinfo
, sizeof(struct hammer_ioc_snapshot
));
167 fd
= open(mountedon
, O_RDONLY
);
175 * old style: count the number of softlinks in the snapshots dir
177 if (pfs_snapshots
[0])
178 snapshots_path
= pfs_snapshots
;
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] == '.')
185 asprintf(&fpath
, "%s/%s", snapshots_path
,
187 if (lstat(fpath
, &st
) == 0 &&
196 * new style: file system meta-data
199 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapinfo
) < 0) {
204 snapshot_count
+= snapinfo
.count
;
205 } while (snapinfo
.head
.error
== 0 && snapinfo
.count
);
211 return snapshot_count
;