1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/disklabel.h>
31 #include <sys/mount.h>
32
33 #include <ufs/ufs/dinode.h>
34 #include <ufs/ffs/fs.h>
35
36 #include <libufs.h>
37 #include <libgeom.h>
38 #include <core/geom.h>
39 #include <misc/subr.h>
40
41 #include "geom_journal.h"
42
43 static struct fs *
read_superblock(const char * prov)44 read_superblock(const char *prov)
45 {
46 static struct uufsd disk;
47 struct fs *fs;
48
49 if (ufs_disk_fillout(&disk, prov) == -1)
50 return (NULL);
51 fs = &disk.d_fs;
52 ufs_disk_close(&disk);
53 return (fs);
54 }
55
56 int
g_journal_ufs_exists(const char * prov)57 g_journal_ufs_exists(const char *prov)
58 {
59
60 return (read_superblock(prov) != NULL);
61 }
62
63 int
g_journal_ufs_using_last_sector(const char * prov)64 g_journal_ufs_using_last_sector(const char *prov)
65 {
66 struct fs *fs;
67 off_t psize, fssize;
68
69 fs = read_superblock(prov);
70 if (fs == NULL)
71 return (0);
72 /* Provider size in 512 bytes blocks. */
73 psize = g_get_mediasize(prov) / DEV_BSIZE;
74 /* File system size in 512 bytes blocks. */
75 fssize = fsbtodb(fs, fs->fs_size);
76 return (psize <= fssize);
77 }
78