rc.d: Introduce 'dhcp_client' to wrap over dhclient and dhcpcd
[dragonfly.git] / lib / libhammer / info.c
CommitLineData
cb7575e6
AHJ
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 */
c8fec8af 34
cb7575e6
AHJ
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
3729b417 46#include "libhammer.h"
cb7575e6 47
a77d0377
AHJ
48libhammer_fsinfo_t
49libhammer_get_fsinfo(const char *path)
cb7575e6 50{
cb7575e6 51 struct hammer_pseudofs_data *pfs_od;
e0f42079 52 struct hammer_ioc_pseudofs_rw pfs;
cb7575e6 53 struct hammer_ioc_info info;
a77d0377
AHJ
54 libhammer_pfsinfo_t pip;
55 libhammer_fsinfo_t fip;
431893ca 56 int error = 0;
cb7575e6
AHJ
57 int fd;
58
cb7575e6 59 if ((fd = open(path, O_RDONLY)) < 0)
c8fec8af 60 return NULL;
cb7575e6 61
a77d0377
AHJ
62 fip = _libhammer_malloc(sizeof(*fip));
63 TAILQ_INIT(&fip->list_pseudo);
c8fec8af 64 if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
a77d0377 65 libhammer_free_fsinfo(fip);
c8fec8af
AHJ
66 close(fd);
67 return NULL;
68 }
cb7575e6 69
a77d0377 70 /* Fill filesystem information */
6c39d27a 71 snprintf(fip->vol_name, TXTLEN, "%s", info.vol_label);
a77d0377
AHJ
72 fip->vol_fsid = info.vol_fsid;
73 fip->version = info.version;
74 fip->nvolumes = info.nvolumes;
7aee7a8b 75 fip->rootvol = info.rootvol;
a77d0377
AHJ
76 fip->inodes = info.inodes;
77 fip->bigblocks = info.bigblocks;
78 fip->freebigblocks = info.freebigblocks;
79 fip->rsvbigblocks = info.rsvbigblocks;
cb7575e6 80
e0f42079
TK
81 bzero(&pfs, sizeof(pfs));
82 pfs.pfs_id = 0; /* start off with root PFS */
83 pfs.ondisk = _libhammer_malloc(sizeof(*pfs_od));
84 pfs.bytes = sizeof(struct hammer_pseudofs_data);
85
431893ca 86 while(error == 0) {
7dc46daa 87 error = ioctl(fd, HAMMERIOC_SCAN_PSEUDOFS, &pfs);
e0f42079 88 if (error == 0 && !hammer_is_pfs_deleted(pfs.ondisk)) {
a77d0377 89 pip = _libhammer_malloc(sizeof(*pip));
e0f42079 90 pfs_od = pfs.ondisk;
2bacc088 91 pip->ismaster = hammer_is_pfs_master(pfs_od);
cb7575e6 92
cb7575e6
AHJ
93 /*
94 * Fill in structs used in the library. We don't rely on
a77d0377 95 * HAMMER structs but we do fill our own.
cb7575e6 96 */
a77d0377 97 pip->version = fip->version;
e0f42079 98 pip->pfs_id = pfs.pfs_id;
a77d0377
AHJ
99 pip->mirror_flags = pfs_od->mirror_flags;
100 pip->beg_tid = pfs_od->sync_beg_tid;
101 pip->end_tid = pfs_od->sync_end_tid;
102 pip->mountedon =
d428efb7 103 libhammer_find_pfs_mount(&pfs_od->unique_uuid);
a77d0377
AHJ
104 if (fip->version < 3) {
105 libhammer_compat_old_snapcount(pip);
106 } else {
107 TAILQ_INIT(&pip->list_snap);
108 if (libhammer_pfs_get_snapshots(fip, pip) < 0)
109 pip->snapcount = 0;
110 }
111
112 /*
113 * PFS retrieval goes 0..n so inserting in the tail
114 * leaves the TAILQ sorted by pfs_id.
115 * This is important since quickly getting PFS #0 is
116 * required for some functions so *DO NOT CHANGE IT*.
117 */
118 TAILQ_INSERT_TAIL(&fip->list_pseudo, pip, entries);
cb7575e6 119
cb7575e6 120 }
e0f42079 121 pfs.pfs_id++;
cb7575e6 122 }
e0f42079 123 free(pfs.ondisk);
431893ca 124
c8fec8af 125 close (fd);
cb7575e6 126
a77d0377 127 return (fip);
cb7575e6
AHJ
128}
129
130void
a77d0377 131libhammer_free_fsinfo(libhammer_fsinfo_t fip)
cb7575e6
AHJ
132{
133 struct libhammer_pfsinfo *pfstmp;
134
a77d0377
AHJ
135 while(!TAILQ_EMPTY(&fip->list_pseudo)) {
136 pfstmp = TAILQ_FIRST(&fip->list_pseudo);
cc96d2fe 137 libhammer_pfs_free_snapshots(pfstmp);
a77d0377
AHJ
138 if (pfstmp->mountedon)
139 free(pfstmp->mountedon);
140 TAILQ_REMOVE(&fip->list_pseudo, pfstmp, entries);
cb7575e6
AHJ
141 free(pfstmp);
142 }
a77d0377 143 free(fip);
cb7575e6 144}