xref: /dragonfly/lib/libhammer/info.c (revision 279dd846)
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 libhammer_fsinfo_t
49 libhammer_get_fsinfo(const char *path)
50 {
51 	struct hammer_pseudofs_data *pfs_od;
52 	struct hammer_ioc_pfs_iterate pi;
53 	struct hammer_ioc_info info;
54 	libhammer_pfsinfo_t pip;
55 	libhammer_fsinfo_t fip;
56 	int error = 0;
57 	int fd;
58 
59 	if ((fd = open(path, O_RDONLY)) < 0)
60 		return NULL;
61 
62 	fip = _libhammer_malloc(sizeof(*fip));
63 	TAILQ_INIT(&fip->list_pseudo);
64 	if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
65 		libhammer_free_fsinfo(fip);
66 		close(fd);
67 		return NULL;
68 	}
69 
70 	/* Fill filesystem information */
71 	snprintf(fip->vol_name, TXTLEN, "%s", info.vol_name);
72 	fip->vol_fsid = info.vol_fsid;
73 	fip->version = info.version;
74 	fip->nvolumes = info.nvolumes;
75 	fip->inodes = info.inodes;
76 	fip->bigblocks = info.bigblocks;
77 	fip->freebigblocks = info.freebigblocks;
78 	fip->rsvbigblocks = info.rsvbigblocks;
79 
80 	bzero(&pi, sizeof(pi));
81 	pi.ondisk = _libhammer_malloc(sizeof(*pfs_od));
82 	while(error == 0) {
83 		error = ioctl(fd, HAMMERIOC_PFS_ITERATE, &pi);
84 		if (error == 0 &&
85 		    (pi.ondisk->mirror_flags & HAMMER_PFSD_DELETED) == 0) {
86 			pip = _libhammer_malloc(sizeof(*pip));
87 			pfs_od = pi.ondisk;
88 			pip->ismaster =
89 			    (pfs_od->mirror_flags & HAMMER_PFSD_SLAVE) ? 0 : 1;
90 
91 			/*
92 			 * Fill in structs used in the library. We don't rely on
93 			 * HAMMER structs but we do fill our own.
94 			 */
95 			pip->version = fip->version;
96 			pip->pfs_id = pi.pos;
97 			pip->mirror_flags = pfs_od->mirror_flags;
98 			pip->beg_tid = pfs_od->sync_beg_tid;
99 			pip->end_tid = pfs_od->sync_end_tid;
100 			pip->mountedon =
101 			    libhammer_find_pfs_mount(&pfs_od->unique_uuid);
102 			if (fip->version < 3) {
103 				libhammer_compat_old_snapcount(pip);
104 			} else {
105 				TAILQ_INIT(&pip->list_snap);
106 				if (libhammer_pfs_get_snapshots(fip, pip) < 0)
107 					pip->snapcount = 0;
108 			}
109 
110 			/*
111 			 * PFS retrieval goes 0..n so inserting in the tail
112 			 * leaves the TAILQ sorted by pfs_id.
113 			 * This is important since quickly getting PFS #0 is
114 			 * required for some functions so *DO NOT CHANGE IT*.
115 			 */
116 			TAILQ_INSERT_TAIL(&fip->list_pseudo, pip, entries);
117 
118 		}
119 		pi.pos++;
120 	}
121 	free(pi.ondisk);
122 
123 	close (fd);
124 
125 	return (fip);
126 }
127 
128 void
129 libhammer_free_fsinfo(libhammer_fsinfo_t fip)
130 {
131 	struct libhammer_pfsinfo *pfstmp;
132 
133 	while(!TAILQ_EMPTY(&fip->list_pseudo)) {
134 		pfstmp = TAILQ_FIRST(&fip->list_pseudo);
135 		libhammer_pfs_free_snapshots(pfstmp);
136 		if (pfstmp->mountedon)
137 			free(pfstmp->mountedon);
138 		TAILQ_REMOVE(&fip->list_pseudo, pfstmp, entries);
139 		free(pfstmp);
140 	}
141 	free(fip);
142 }
143