1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2002, 2003 Gordon Tetlow 5 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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 the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/vnode.h> 37 38 #include <ufs/ufs/dinode.h> 39 #include <ufs/ffs/fs.h> 40 #include <ufs/ufs/quota.h> 41 #include <ufs/ufs/extattr.h> 42 #include <ufs/ffs/ffs_extern.h> 43 44 #include <geom/geom.h> 45 #include <geom/geom_dbg.h> 46 #include <geom/label/g_label.h> 47 48 #define G_LABEL_UFS_VOLUME_DIR "ufs" 49 #define G_LABEL_UFS_ID_DIR "ufsid" 50 51 #define G_LABEL_UFS_VOLUME 0 52 #define G_LABEL_UFS_ID 1 53 54 /* 55 * G_LABEL_UFS_CMP returns true if difference between provider mediasize 56 * and filesystem size is less than G_LABEL_UFS_MAXDIFF sectors 57 */ 58 #define G_LABEL_UFS_CMP(prov, fsys, size) \ 59 ( abs( ((fsys)->size) - ( (prov)->mediasize / (fsys)->fs_fsize )) \ 60 < G_LABEL_UFS_MAXDIFF ) 61 #define G_LABEL_UFS_MAXDIFF 0x100 62 63 /* 64 * Try to find a superblock on the provider. If successful, then 65 * check that the size in the superblock corresponds to the size 66 * of the underlying provider. Finally, look for a volume label 67 * and create an appropriate provider based on that. 68 */ 69 static void 70 g_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int what) 71 { 72 struct g_provider *pp; 73 struct fs *fs; 74 75 g_topology_assert_not(); 76 pp = cp->provider; 77 label[0] = '\0'; 78 79 fs = NULL; 80 if (SBLOCKSIZE % pp->sectorsize != 0 || ffs_sbget(cp, &fs, 81 STDSB_NOHASHFAIL, M_GEOM, g_use_g_read_data) != 0) { 82 KASSERT(fs == NULL, 83 ("g_label_ufs_taste_common: non-NULL fs %p\n", fs)); 84 return; 85 } 86 87 /* 88 * Check for magic. We also need to check if file system size 89 * is almost equal to providers size, because sysinstall(8) 90 * used to bogusly put first partition at offset 0 91 * instead of 16, and glabel/ufs would find file system on slice 92 * instead of partition. 93 * 94 * In addition, media size can be a bit bigger than file system 95 * size. For instance, mkuzip can append bytes to align data 96 * to large sector size (it improves compression rates). 97 */ 98 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 && 99 ( G_LABEL_UFS_CMP(pp, fs, fs_old_size) 100 || G_LABEL_UFS_CMP(pp, fs, fs_providersize))) { 101 /* Valid UFS1. */ 102 } else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 && 103 ( G_LABEL_UFS_CMP(pp, fs, fs_size) 104 || G_LABEL_UFS_CMP(pp, fs, fs_providersize))) { 105 /* Valid UFS2. */ 106 } else { 107 goto out; 108 } 109 G_LABEL_DEBUG(1, "%s file system detected on %s.", 110 fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2", pp->name); 111 switch (what) { 112 case G_LABEL_UFS_VOLUME: 113 /* Check for volume label */ 114 if (fs->fs_volname[0] != '\0') 115 strlcpy(label, fs->fs_volname, size); 116 break; 117 case G_LABEL_UFS_ID: 118 if (fs->fs_id[0] != 0 || fs->fs_id[1] != 0) 119 snprintf(label, size, "%08x%08x", fs->fs_id[0], 120 fs->fs_id[1]); 121 break; 122 } 123 out: 124 g_free(fs->fs_csp); 125 g_free(fs); 126 } 127 128 static void 129 g_label_ufs_volume_taste(struct g_consumer *cp, char *label, size_t size) 130 { 131 132 g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_VOLUME); 133 } 134 135 static void 136 g_label_ufs_id_taste(struct g_consumer *cp, char *label, size_t size) 137 { 138 139 g_label_ufs_taste_common(cp, label, size, G_LABEL_UFS_ID); 140 } 141 142 struct g_label_desc g_label_ufs_volume = { 143 .ld_taste = g_label_ufs_volume_taste, 144 .ld_dir = G_LABEL_UFS_VOLUME_DIR, 145 .ld_enabled = 1 146 }; 147 148 struct g_label_desc g_label_ufs_id = { 149 .ld_taste = g_label_ufs_id_taste, 150 .ld_dir = G_LABEL_UFS_ID_DIR, 151 .ld_enabled = 1 152 }; 153 154 G_LABEL_INIT(ufsid, g_label_ufs_id, "Create device nodes for UFS file system IDs"); 155 G_LABEL_INIT(ufs, g_label_ufs_volume, "Create device nodes for UFS volume names"); 156 157 MODULE_DEPEND(g_label, ufs, 1, 1, 1); 158