xref: /dragonfly/lib/libhammer/info.c (revision 7dc46daa)
1cb7575e6SAntonio Huete Jimenez /*
2cb7575e6SAntonio Huete Jimenez  * Copyright (c) 2011 The DragonFly Project.  All rights reserved.
3cb7575e6SAntonio Huete Jimenez  *
4cb7575e6SAntonio Huete Jimenez  * This code is derived from software contributed to The DragonFly Project
5cb7575e6SAntonio Huete Jimenez  * by Antonio Huete <tuxillo@quantumachine.net>
6cb7575e6SAntonio Huete Jimenez  *
7cb7575e6SAntonio Huete Jimenez  * Redistribution and use in source and binary forms, with or without
8cb7575e6SAntonio Huete Jimenez  * modification, are permitted provided that the following conditions
9cb7575e6SAntonio Huete Jimenez  * are met:
10cb7575e6SAntonio Huete Jimenez  *
11cb7575e6SAntonio Huete Jimenez  * 1. Redistributions of source code must retain the above copyright
12cb7575e6SAntonio Huete Jimenez  *    notice, this list of conditions and the following disclaimer.
13cb7575e6SAntonio Huete Jimenez  * 2. Redistributions in binary form must reproduce the above copyright
14cb7575e6SAntonio Huete Jimenez  *    notice, this list of conditions and the following disclaimer in
15cb7575e6SAntonio Huete Jimenez  *    the documentation and/or other materials provided with the
16cb7575e6SAntonio Huete Jimenez  *    distribution.
17cb7575e6SAntonio Huete Jimenez  * 3. Neither the name of The DragonFly Project nor the names of its
18cb7575e6SAntonio Huete Jimenez  *    contributors may be used to endorse or promote products derived
19cb7575e6SAntonio Huete Jimenez  *    from this software without specific, prior written permission.
20cb7575e6SAntonio Huete Jimenez  *
21cb7575e6SAntonio Huete Jimenez  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22cb7575e6SAntonio Huete Jimenez  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23cb7575e6SAntonio Huete Jimenez  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24cb7575e6SAntonio Huete Jimenez  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25cb7575e6SAntonio Huete Jimenez  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26cb7575e6SAntonio Huete Jimenez  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27cb7575e6SAntonio Huete Jimenez  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28cb7575e6SAntonio Huete Jimenez  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29cb7575e6SAntonio Huete Jimenez  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30cb7575e6SAntonio Huete Jimenez  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31cb7575e6SAntonio Huete Jimenez  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32cb7575e6SAntonio Huete Jimenez  * SUCH DAMAGE.
33cb7575e6SAntonio Huete Jimenez  */
34c8fec8afSAntonio Huete Jimenez 
35cb7575e6SAntonio Huete Jimenez #include <dirent.h>
36cb7575e6SAntonio Huete Jimenez #include <err.h>
37cb7575e6SAntonio Huete Jimenez #include <errno.h>
38cb7575e6SAntonio Huete Jimenez #include <fcntl.h>
39cb7575e6SAntonio Huete Jimenez #include <stdio.h>
40cb7575e6SAntonio Huete Jimenez #include <stdlib.h>
41cb7575e6SAntonio Huete Jimenez #include <string.h>
42cb7575e6SAntonio Huete Jimenez #include <sys/param.h>
43cb7575e6SAntonio Huete Jimenez #include <sys/stat.h>
44cb7575e6SAntonio Huete Jimenez #include <unistd.h>
45cb7575e6SAntonio Huete Jimenez 
463729b417SAntonio Huete Jimenez #include "libhammer.h"
47cb7575e6SAntonio Huete Jimenez 
48a77d0377SAntonio Huete Jimenez libhammer_fsinfo_t
libhammer_get_fsinfo(const char * path)49a77d0377SAntonio Huete Jimenez libhammer_get_fsinfo(const char *path)
50cb7575e6SAntonio Huete Jimenez {
51cb7575e6SAntonio Huete Jimenez 	struct hammer_pseudofs_data *pfs_od;
52e0f42079STomohiro Kusumi 	struct hammer_ioc_pseudofs_rw pfs;
53cb7575e6SAntonio Huete Jimenez 	struct hammer_ioc_info info;
54a77d0377SAntonio Huete Jimenez 	libhammer_pfsinfo_t pip;
55a77d0377SAntonio Huete Jimenez 	libhammer_fsinfo_t fip;
56431893caSAntonio Huete Jimenez 	int error = 0;
57cb7575e6SAntonio Huete Jimenez 	int fd;
58cb7575e6SAntonio Huete Jimenez 
59cb7575e6SAntonio Huete Jimenez 	if ((fd = open(path, O_RDONLY)) < 0)
60c8fec8afSAntonio Huete Jimenez 		return NULL;
61cb7575e6SAntonio Huete Jimenez 
62a77d0377SAntonio Huete Jimenez 	fip = _libhammer_malloc(sizeof(*fip));
63a77d0377SAntonio Huete Jimenez 	TAILQ_INIT(&fip->list_pseudo);
64c8fec8afSAntonio Huete Jimenez 	if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
65a77d0377SAntonio Huete Jimenez 		libhammer_free_fsinfo(fip);
66c8fec8afSAntonio Huete Jimenez 		close(fd);
67c8fec8afSAntonio Huete Jimenez 		return NULL;
68c8fec8afSAntonio Huete Jimenez 	}
69cb7575e6SAntonio Huete Jimenez 
70a77d0377SAntonio Huete Jimenez 	/* Fill filesystem information */
716c39d27aSTomohiro Kusumi 	snprintf(fip->vol_name, TXTLEN, "%s", info.vol_label);
72a77d0377SAntonio Huete Jimenez 	fip->vol_fsid = info.vol_fsid;
73a77d0377SAntonio Huete Jimenez 	fip->version = info.version;
74a77d0377SAntonio Huete Jimenez 	fip->nvolumes = info.nvolumes;
757aee7a8bSTomohiro Kusumi 	fip->rootvol = info.rootvol;
76a77d0377SAntonio Huete Jimenez 	fip->inodes = info.inodes;
77a77d0377SAntonio Huete Jimenez 	fip->bigblocks = info.bigblocks;
78a77d0377SAntonio Huete Jimenez 	fip->freebigblocks = info.freebigblocks;
79a77d0377SAntonio Huete Jimenez 	fip->rsvbigblocks = info.rsvbigblocks;
80cb7575e6SAntonio Huete Jimenez 
81e0f42079STomohiro Kusumi 	bzero(&pfs, sizeof(pfs));
82e0f42079STomohiro Kusumi 	pfs.pfs_id = 0;	/* start off with root PFS */
83e0f42079STomohiro Kusumi 	pfs.ondisk = _libhammer_malloc(sizeof(*pfs_od));
84e0f42079STomohiro Kusumi 	pfs.bytes = sizeof(struct hammer_pseudofs_data);
85e0f42079STomohiro Kusumi 
86431893caSAntonio Huete Jimenez 	while(error == 0) {
87*7dc46daaSTomohiro Kusumi 		error = ioctl(fd, HAMMERIOC_SCAN_PSEUDOFS, &pfs);
88e0f42079STomohiro Kusumi 		if (error == 0 && !hammer_is_pfs_deleted(pfs.ondisk)) {
89a77d0377SAntonio Huete Jimenez 			pip = _libhammer_malloc(sizeof(*pip));
90e0f42079STomohiro Kusumi 			pfs_od = pfs.ondisk;
912bacc088STomohiro Kusumi 			pip->ismaster = hammer_is_pfs_master(pfs_od);
92cb7575e6SAntonio Huete Jimenez 
93cb7575e6SAntonio Huete Jimenez 			/*
94cb7575e6SAntonio Huete Jimenez 			 * Fill in structs used in the library. We don't rely on
95a77d0377SAntonio Huete Jimenez 			 * HAMMER structs but we do fill our own.
96cb7575e6SAntonio Huete Jimenez 			 */
97a77d0377SAntonio Huete Jimenez 			pip->version = fip->version;
98e0f42079STomohiro Kusumi 			pip->pfs_id = pfs.pfs_id;
99a77d0377SAntonio Huete Jimenez 			pip->mirror_flags = pfs_od->mirror_flags;
100a77d0377SAntonio Huete Jimenez 			pip->beg_tid = pfs_od->sync_beg_tid;
101a77d0377SAntonio Huete Jimenez 			pip->end_tid = pfs_od->sync_end_tid;
102a77d0377SAntonio Huete Jimenez 			pip->mountedon =
103d428efb7SAntonio Huete Jimenez 			    libhammer_find_pfs_mount(&pfs_od->unique_uuid);
104a77d0377SAntonio Huete Jimenez 			if (fip->version < 3) {
105a77d0377SAntonio Huete Jimenez 				libhammer_compat_old_snapcount(pip);
106a77d0377SAntonio Huete Jimenez 			} else {
107a77d0377SAntonio Huete Jimenez 				TAILQ_INIT(&pip->list_snap);
108a77d0377SAntonio Huete Jimenez 				if (libhammer_pfs_get_snapshots(fip, pip) < 0)
109a77d0377SAntonio Huete Jimenez 					pip->snapcount = 0;
110a77d0377SAntonio Huete Jimenez 			}
111cb7575e6SAntonio Huete Jimenez 
112a77d0377SAntonio Huete Jimenez 			/*
113a77d0377SAntonio Huete Jimenez 			 * PFS retrieval goes 0..n so inserting in the tail
114a77d0377SAntonio Huete Jimenez 			 * leaves the TAILQ sorted by pfs_id.
115a77d0377SAntonio Huete Jimenez 			 * This is important since quickly getting PFS #0 is
116a77d0377SAntonio Huete Jimenez 			 * required for some functions so *DO NOT CHANGE IT*.
117a77d0377SAntonio Huete Jimenez 			 */
118a77d0377SAntonio Huete Jimenez 			TAILQ_INSERT_TAIL(&fip->list_pseudo, pip, entries);
119a77d0377SAntonio Huete Jimenez 
120cb7575e6SAntonio Huete Jimenez 		}
121e0f42079STomohiro Kusumi 		pfs.pfs_id++;
122cb7575e6SAntonio Huete Jimenez 	}
123e0f42079STomohiro Kusumi 	free(pfs.ondisk);
124431893caSAntonio Huete Jimenez 
125cb7575e6SAntonio Huete Jimenez 	close (fd);
126cb7575e6SAntonio Huete Jimenez 
127a77d0377SAntonio Huete Jimenez 	return (fip);
128cb7575e6SAntonio Huete Jimenez }
129cb7575e6SAntonio Huete Jimenez 
130cb7575e6SAntonio Huete Jimenez void
libhammer_free_fsinfo(libhammer_fsinfo_t fip)131a77d0377SAntonio Huete Jimenez libhammer_free_fsinfo(libhammer_fsinfo_t fip)
132cb7575e6SAntonio Huete Jimenez {
133cb7575e6SAntonio Huete Jimenez 	struct libhammer_pfsinfo *pfstmp;
134cb7575e6SAntonio Huete Jimenez 
135a77d0377SAntonio Huete Jimenez 	while(!TAILQ_EMPTY(&fip->list_pseudo)) {
136a77d0377SAntonio Huete Jimenez 		pfstmp = TAILQ_FIRST(&fip->list_pseudo);
137cc96d2feSAntonio Huete Jimenez 		libhammer_pfs_free_snapshots(pfstmp);
138a77d0377SAntonio Huete Jimenez 		if (pfstmp->mountedon)
139cb7575e6SAntonio Huete Jimenez 			free(pfstmp->mountedon);
140a77d0377SAntonio Huete Jimenez 		TAILQ_REMOVE(&fip->list_pseudo, pfstmp, entries);
141cb7575e6SAntonio Huete Jimenez 		free(pfstmp);
142cb7575e6SAntonio Huete Jimenez 	}
143a77d0377SAntonio Huete Jimenez 	free(fip);
144cb7575e6SAntonio Huete Jimenez }
145