Changeset View
Changeset View
Standalone View
Standalone View
head/sbin/newfs_msdos/mkfs_msdos.c
Show All 35 Lines | |||||
#include <sys/disk/bsd.h> | #include <sys/disk/bsd.h> | ||||
#else | #else | ||||
#include <sys/fdcio.h> | #include <sys/fdcio.h> | ||||
#include <sys/disk.h> | #include <sys/disk.h> | ||||
#include <sys/disklabel.h> | #include <sys/disklabel.h> | ||||
#include <sys/mount.h> | #include <sys/mount.h> | ||||
#endif | #endif | ||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <sys/sysctl.h> | |||||
#include <sys/time.h> | #include <sys/time.h> | ||||
#include <assert.h> | |||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <inttypes.h> | #include <inttypes.h> | ||||
#include <paths.h> | #include <paths.h> | ||||
#include <signal.h> | #include <signal.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
Show All 19 Lines | |||||
#define RESFTE 2 /* reserved FAT entries */ | #define RESFTE 2 /* reserved FAT entries */ | ||||
#define MINCLS12 1U /* minimum FAT12 clusters */ | #define MINCLS12 1U /* minimum FAT12 clusters */ | ||||
#define MINCLS16 0xff5U /* minimum FAT16 clusters */ | #define MINCLS16 0xff5U /* minimum FAT16 clusters */ | ||||
#define MINCLS32 0xfff5U /* minimum FAT32 clusters */ | #define MINCLS32 0xfff5U /* minimum FAT32 clusters */ | ||||
#define MAXCLS12 0xff4U /* maximum FAT12 clusters */ | #define MAXCLS12 0xff4U /* maximum FAT12 clusters */ | ||||
#define MAXCLS16 0xfff4U /* maximum FAT16 clusters */ | #define MAXCLS16 0xfff4U /* maximum FAT16 clusters */ | ||||
#define MAXCLS32 0xffffff4U /* maximum FAT32 clusters */ | #define MAXCLS32 0xffffff4U /* maximum FAT32 clusters */ | ||||
#ifndef CTASSERT | |||||
#define CTASSERT(x) _CTASSERT(x, __LINE__) | |||||
#define _CTASSERT(x, y) __CTASSERT(x, y) | |||||
#define __CTASSERT(x, y) typedef char __assert_ ## y [(x) ? 1 : -1] | |||||
#endif | |||||
/* | |||||
* For better performance, we want to write larger chunks instead of | |||||
* individual sectors (the size can only be 512, 1024, 2048 or 4096 | |||||
* bytes). Assert that MAXPHYS can always hold an integer number of | |||||
* sectors by asserting that both are power of two numbers and the | |||||
* MAXPHYS is greater than MAXBPS. | |||||
*/ | |||||
CTASSERT(powerof2(MAXPHYS)); | |||||
CTASSERT(powerof2(MAXBPS)); | |||||
CTASSERT(MAXPHYS > MAXBPS); | |||||
const static ssize_t chunksize = MAXPHYS; | |||||
#define mincls(fat) ((fat) == 12 ? MINCLS12 : \ | #define mincls(fat) ((fat) == 12 ? MINCLS12 : \ | ||||
(fat) == 16 ? MINCLS16 : \ | (fat) == 16 ? MINCLS16 : \ | ||||
MINCLS32) | MINCLS32) | ||||
#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \ | #define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \ | ||||
(fat) == 16 ? MAXCLS16 : \ | (fat) == 16 ? MAXCLS16 : \ | ||||
MAXCLS32) | MAXCLS32) | ||||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | static const u_int8_t bootcode[] = { | ||||
0x0d, 0x0a, | 0x0d, 0x0a, | ||||
0 | 0 | ||||
}; | }; | ||||
static volatile sig_atomic_t got_siginfo; | static volatile sig_atomic_t got_siginfo; | ||||
static void infohandler(int); | static void infohandler(int); | ||||
static int check_mounted(const char *, mode_t); | static int check_mounted(const char *, mode_t); | ||||
static ssize_t getchunksize(void); | |||||
static int getstdfmt(const char *, struct bpb *); | static int getstdfmt(const char *, struct bpb *); | ||||
static int getdiskinfo(int, const char *, const char *, int, struct bpb *); | static int getdiskinfo(int, const char *, const char *, int, struct bpb *); | ||||
static void print_bpb(struct bpb *); | static void print_bpb(struct bpb *); | ||||
static int ckgeom(const char *, u_int, const char *); | static int ckgeom(const char *, u_int, const char *); | ||||
static void mklabel(u_int8_t *, const char *); | static void mklabel(u_int8_t *, const char *); | ||||
static int oklabel(const char *); | static int oklabel(const char *); | ||||
static void setstr(u_int8_t *, const char *, size_t); | static void setstr(u_int8_t *, const char *, size_t); | ||||
Show All 16 Lines | mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op) | ||||
const char *bname; | const char *bname; | ||||
ssize_t n; | ssize_t n; | ||||
time_t now; | time_t now; | ||||
u_int fat, bss, rds, cls, dir, lsn, x, x1, x2; | u_int fat, bss, rds, cls, dir, lsn, x, x1, x2; | ||||
u_int extra_res, alignment, saved_x, attempts=0; | u_int extra_res, alignment, saved_x, attempts=0; | ||||
bool set_res, set_spf, set_spc; | bool set_res, set_spf, set_spc; | ||||
int fd, fd1, rv; | int fd, fd1, rv; | ||||
struct msdos_options o = *op; | struct msdos_options o = *op; | ||||
ssize_t chunksize; | |||||
physbuf = NULL; | physbuf = NULL; | ||||
rv = -1; | rv = -1; | ||||
fd = fd1 = -1; | fd = fd1 = -1; | ||||
if (o.block_size && o.sectors_per_cluster) { | if (o.block_size && o.sectors_per_cluster) { | ||||
warnx("Cannot specify both block size and sectors per cluster"); | warnx("Cannot specify both block size and sectors per cluster"); | ||||
goto done; | goto done; | ||||
▲ Show 20 Lines • Show All 352 Lines • ▼ Show 20 Lines | if (o.timestamp_set) { | ||||
tv.tv_usec = 0; | tv.tv_usec = 0; | ||||
tm = gmtime(&now); | tm = gmtime(&now); | ||||
} else { | } else { | ||||
gettimeofday(&tv, NULL); | gettimeofday(&tv, NULL); | ||||
now = tv.tv_sec; | now = tv.tv_sec; | ||||
tm = localtime(&now); | tm = localtime(&now); | ||||
} | } | ||||
chunksize = getchunksize(); | |||||
physbuf = malloc(chunksize); | physbuf = malloc(chunksize); | ||||
if (physbuf == NULL) { | if (physbuf == NULL) { | ||||
warn(NULL); | warn(NULL); | ||||
goto done; | goto done; | ||||
} | } | ||||
physbuf_end = physbuf + chunksize; | physbuf_end = physbuf + chunksize; | ||||
img = physbuf; | img = physbuf; | ||||
▲ Show 20 Lines • Show All 192 Lines • ▼ Show 20 Lines | #if !defined(MAKEFS) | ||||
if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) || | if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) || | ||||
!strcmp(s1, s2)) { | !strcmp(s1, s2)) { | ||||
warnx("%s is mounted on %s", fname, mp->f_mntonname); | warnx("%s is mounted on %s", fname, mp->f_mntonname); | ||||
return -1; | return -1; | ||||
} | } | ||||
} | } | ||||
#endif | #endif | ||||
return 0; | return 0; | ||||
} | |||||
/* | |||||
* Get optimal I/O size | |||||
*/ | |||||
static ssize_t | |||||
getchunksize(void) | |||||
{ | |||||
static int chunksize; | |||||
if (chunksize != 0) | |||||
return ((ssize_t)chunksize); | |||||
#ifdef KERN_MAXPHYS | |||||
int mib[2]; | |||||
size_t len; | |||||
mib[0] = CTL_KERN; | |||||
mib[1] = KERN_MAXPHYS; | |||||
len = sizeof(chunksize); | |||||
if (sysctl(mib, 2, &chunksize, &len, NULL, 0) == -1) { | |||||
warn("sysctl: KERN_MAXPHYS, using %zu", (size_t)MAXPHYS); | |||||
chunksize = 0; | |||||
} | |||||
#endif | |||||
if (chunksize == 0) | |||||
chunksize = MAXPHYS; | |||||
/* | |||||
* For better performance, we want to write larger chunks instead of | |||||
* individual sectors (the size can only be 512, 1024, 2048 or 4096 | |||||
* bytes). Assert that chunksize can always hold an integer number of | |||||
* sectors by asserting that both are power of two numbers and the | |||||
* chunksize is greater than MAXBPS. | |||||
*/ | |||||
static_assert(powerof2(MAXBPS), "MAXBPS is not power of 2"); | |||||
assert(powerof2(chunksize)); | |||||
assert(chunksize > MAXBPS); | |||||
return ((ssize_t)chunksize); | |||||
} | } | ||||
/* | /* | ||||
* Get a standard format. | * Get a standard format. | ||||
*/ | */ | ||||
static int | static int | ||||
getstdfmt(const char *fmt, struct bpb *bpb) | getstdfmt(const char *fmt, struct bpb *bpb) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 215 Lines • Show Last 20 Lines |