Changeset View
Standalone View
sys/geom/label/g_label_ufs.c
Show All 39 Lines | |||||
#include <geom/label/g_label.h> | #include <geom/label/g_label.h> | ||||
#define G_LABEL_UFS_VOLUME_DIR "ufs" | #define G_LABEL_UFS_VOLUME_DIR "ufs" | ||||
#define G_LABEL_UFS_ID_DIR "ufsid" | #define G_LABEL_UFS_ID_DIR "ufsid" | ||||
#define G_LABEL_UFS_VOLUME 0 | #define G_LABEL_UFS_VOLUME 0 | ||||
#define G_LABEL_UFS_ID 1 | #define G_LABEL_UFS_ID 1 | ||||
/* | |||||
sobomax: My first impression was that is somewhat over-engineered. It's like adding "#define sum(a, b)… | |||||
* G_LABEL_UFS_CMP returns true if difference between provider mediasize | |||||
* and filesystem size is less than G_LABEL_UFS_MAXDIFF sectors | |||||
*/ | |||||
#define G_LABEL_UFS_CMP(prov, fsys, size) \ | |||||
Done Inline ActionsNot strictly required, but it's good practice to put names of "variables" in the cpp macro into (), otherwise say you have a macro like this: #define mul(a, b) (a * b) Then you can use it in your code in a way as you would any function, i.e. mul(2, 10 + 1) and expect it to evaluate to "22" (i.e. 2 * 11). However it would expand into 2 * 10 + 1 and hence evaluate to "21". Would work properly if you do it: #define mul(a, b) ((a) * (b)) This is especially easy to screw if you accept pointers as "arguments", where you may need to do pointer arithmetic when calling macro, i.e. MACRO(p + i, x, y), would evaluate into "p + i->foobar" unless you have () around (p). sobomax: Not strictly required, but it's good practice to put names of "variables" in the cpp macro into… | |||||
Not Done Inline ActionsThanks! I've removed brackets due to compiler errors. Issue might be in one bracket, but I removed all. I'll check tomorrow mizhka: Thanks! I've removed brackets due to compiler errors. Issue might be in one bracket, but I… | |||||
( abs( ((fsys)->size) - ( (prov)->mediasize / (fsys)->fs_fsize )) \ | |||||
< G_LABEL_UFS_MAXDIFF ) | |||||
#define G_LABEL_UFS_MAXDIFF 0x100 | |||||
static const int superblocks[] = SBLOCKSEARCH; | static const int superblocks[] = SBLOCKSEARCH; | ||||
static void | static void | ||||
g_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int what) | g_label_ufs_taste_common(struct g_consumer *cp, char *label, size_t size, int what) | ||||
{ | { | ||||
struct g_provider *pp; | struct g_provider *pp; | ||||
int sb, superblock; | int sb, superblock; | ||||
struct fs *fs; | struct fs *fs; | ||||
Show All 21 Lines | for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) { | ||||
*/ | */ | ||||
if (superblock % cp->provider->sectorsize != 0) | if (superblock % cp->provider->sectorsize != 0) | ||||
continue; | continue; | ||||
fs = (struct fs *)g_read_data(cp, superblock, SBLOCKSIZE, NULL); | fs = (struct fs *)g_read_data(cp, superblock, SBLOCKSIZE, NULL); | ||||
if (fs == NULL) | if (fs == NULL) | ||||
continue; | continue; | ||||
/* | /* | ||||
* Check for magic. We also need to check if file system size is equal | * Check for magic. We also need to check if file system size | ||||
* to providers size, because sysinstall(8) used to bogusly put first | * is almost equal to providers size, because sysinstall(8) | ||||
* partition at offset 0 instead of 16, and glabel/ufs would find file | * used to bogusly put first partition at offset 0 | ||||
* system on slice instead of partition. | * instead of 16, and glabel/ufs would find file system on slice | ||||
* instead of partition. | |||||
* | |||||
* In addition, media size can be a bit bigger than file system | |||||
* size. For instance, mkuzip can append bytes to align data | |||||
* to large sector size (it improves compression rates). | |||||
*/ | */ | ||||
switch (fs->fs_magic){ | |||||
case FS_UFS1_MAGIC: | |||||
case FS_UFS2_MAGIC: | |||||
G_LABEL_DEBUG(1, "%s %s params: %jd, %d, %d, %jd\n", | |||||
fs->fs_magic == FS_UFS1_MAGIC ? "UFS1" : "UFS2", | |||||
pp->name, pp->mediasize, fs->fs_fsize, | |||||
fs->fs_old_size, fs->fs_providersize); | |||||
break; | |||||
default: | |||||
break; | |||||
} | |||||
if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 && | if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 && | ||||
((pp->mediasize / fs->fs_fsize == fs->fs_old_size) || | ( G_LABEL_UFS_CMP(pp, fs, fs_old_size) | ||||
(pp->mediasize / fs->fs_fsize == fs->fs_providersize))) { | || G_LABEL_UFS_CMP(pp, fs, fs_providersize))) { | ||||
/* Valid UFS1. */ | /* Valid UFS1. */ | ||||
} else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 && | } else if (fs->fs_magic == FS_UFS2_MAGIC && fs->fs_fsize > 0 && | ||||
((pp->mediasize / fs->fs_fsize == fs->fs_size) || | ( G_LABEL_UFS_CMP(pp, fs, fs_size) | ||||
(pp->mediasize / fs->fs_fsize == fs->fs_providersize))) { | || G_LABEL_UFS_CMP(pp, fs, fs_providersize))) { | ||||
/* Valid UFS2. */ | /* Valid UFS2. */ | ||||
} else { | } else { | ||||
g_free(fs); | g_free(fs); | ||||
continue; | continue; | ||||
} | } | ||||
if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 || | if (fs->fs_sblockloc != superblock || fs->fs_ncg < 1 || | ||||
fs->fs_bsize < MINBSIZE || | fs->fs_bsize < MINBSIZE || | ||||
fs->fs_bsize < sizeof(struct fs)) { | fs->fs_bsize < sizeof(struct fs)) { | ||||
▲ Show 20 Lines • Show All 56 Lines • Show Last 20 Lines |
My first impression was that is somewhat over-engineered. It's like adding "#define sum(a, b) (a + b)" to do 1 + 2.
But I can see some merit of it, however I'd extend it to take pp and fs:
G_LABEL_DIFF(pp, fs, sm) (((pp)->(mediasize) / (fs)->(fs_fsize)) - (fs)->(sm))
Then your code would reduce from:
G_LABEL_DIFF(pp->mediasize / fs->fs_fsize, fs->fs_old_size) -> G_LABEL_DIFF(pp, fs, fs_old_size)
You can also further reduce it by combining it with the comparison, i.e.:
G_LABEL_CMP(pp, fs, sm) ((((pp)->(mediasize) / (fs)->(fs_fsize)) - (fs)->(sm)) < G_LABEL_MAX_DIFF), then the whole comparison becomes:
if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_fsize > 0 && (G_LABEL_CMP(pp, fs, fs_old_size) || (G_LABEL_CMP(pp, fs, fs_providersize))