- 论坛徽章:
- 0
|
/*
* make file system for cylinder-group style file systems
*
* usage:
*
* mkfs [-F FSType] [-V] [-G [-P]] [-M dirname] [-m] [options]
* [-o specific_options] special size
* [nsect ntrack bsize fsize cpg minfree rps nbpi opt apc rotdelay
* 2 3 4 5 6 7 8 9 10 11 12
* nrpos maxcontig mtb]
* 13 14 15
*
* where specific_options are:
* N - no create
* nsect - The number of sectors per track
* ntrack - The number of tracks per cylinder
* bsize - block size
* fragsize - fragment size
* cgsize - The number of disk cylinders per cylinder group.
* free - minimum free space
* rps - rotational speed (rev/sec).
* nbpi - number of data bytes per allocated inode
* opt - optimization (space, time)
* apc - number of alternates
* gap - gap size
* nrpos - number of rotational positions
* maxcontig - maximum number of logical blocks that will be
* allocated contiguously before inserting rotational delay
* mtb - if "y", set up file system for eventual growth to over a
* a terabyte
* -P Do not grow the file system, but print on stdout the maximal
* size in sectors to which the file system can be increased. The calculated
* size is limited by the value provided by the operand size.
*/
mkfs options (manual):
- nsect=n the number of sectors per track on the disk. default is 32.
- ntrack=n the number of tracks per cylinder. default is 16.
- bsize=n the logical block size of the file system in bytes. default is 8192.
- cgsize=n the number of cylinders per cylinder group, ranging from 16 to 256. calculated by dividing the number of sectors in the file system by the number of sectors in a gigabyte.
- fragsize=n the smallest amount of disk space in bytes. default is 1024.
-bash-3.00# newfs -v -s 8192 /dev/rdsk/ramd0
newfs: construct a new file system /dev/rdsk/ramd0: (y/n)? y
mkfs -F ufs /dev/rdsk/ramd0 8192 8 1 8192 1024 16 10 60 2048 t 0 -1 8 0 n
/dev/rdsk/ramd0: 8192 sectors in 1024 cylinders of 1 tracks, 8 sectors
4.0MB in 64 cyl groups (16 c/g, 0.06MB/g, 64 i/g)
super-block backups (for fsck -F ufs -o b=#) at:
32, 160, 288, 416, 544, 672, 800, 928, 1056, 1184,
6944, 7072, 7200, 7328, 7456, 7584, 7712, 7840, 7968, 8096
======================================
code reference:
1. Super block construction.(not write to the disk )
(void) fprintf(stderr, gettext(
"%s:\t%lld sectors in %d cylinders of %d tracks, %d sectors\n"),
fsys, (uint64_t)sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
sblock.fs_ntrak, sblock.fs_nsect);
(void) fprintf(stderr, gettext(
"\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n"),
(float)sblock.fs_size * sblock.fs_fsize / MB, sblock.fs_ncg,
sblock.fs_cpg, (float)sblock.fs_fpg * sblock.fs_fsize / MB,
sblock.fs_ipg);
// read from ioctl.
nsect = geom_nsect;
ntrack = geom_ntrack;
cpg = geom_cpg;
long bbsize = BBSIZE; /* boot block size, 8192 */
long sbsize = SBSIZE; /* superblock size, 8192 */
sectorsize = DEV_SIZE; //(512)
long fragsize = DESFRAGSIZE; /* 1024*/
sblock.fs_bsize = bsize; //8192
sblock.fs_fsize = fragsize; //1024
sblock.fs_nspf = sblock.fs_fsize / sectorsize; //fs_fsize=1024, fs_nspf=2
fssize_db = number(max_fssize, "size", 0); //8192
fssize_frag = (int64_t)dbtofsb(&sblock, fssize_db); //move 1 to left, 4096
#define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
#define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
fssize_frag = (int64_t)dbtofsb(&sblock, fssize_db);
sblock.fs_size = (int32_t)fssize_frag; //1024
sblock.fs_ncyl = (int32_t)(fssize_frag * NSPF(&sblock) / sblock.fs_spc);
/* NSPF(*sblock)=fs_nspf=2 , 4096*2/8=1024 */
sblock.fs_size = (int32_t)fssize_frag; /* number of blocks in fs(in frag block size.), 4096*/
sblock.fs_spc = sblock.fs_ntrak * sblock.fs_nsect; //8
/* this comes from the disk driver partitioning */
int32_t fs_ncyl; /* cylinders in file system */
int32_t fs_spc; /* sectors per cylinder */
fs_ncyl is from ioctl of the driver:
size = ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"disksize", -1);//disksize is in config file,8192
size *= 1024;
dkg.dkg_nsect = 8;
dkg.dkg_ncyl = (rs->size / DEV_BSIZE) / (int)dkg.dkg_nsect;//1024
2. cg
tprintf(gettext(
"super-block backups (for fsck -F ufs -o b=#) at:\n"));
for (width = cylno = 0; cylno = grow_fs_ncg))
initcg(cylno); //Initialize a cylinder group,including cg,inode and blocks.
num = fsbtodb(&sblock, (uint64_t)cgsblock(&sblock, cylno));
......
/* Don't print ',' for the last superblock */
if (cylno == sblock.fs_ncg-1)
(void) sprintf(pbuf, " %llu", num);
else
(void) sprintf(pbuf, " %llu,", num);
initcg(int cylno)
{
......
icg.cg_time = mkfstime;
icg.cg_magic = CG_MAGIC;
icg.cg_cgx = cylno;
/* last one gets whatever's left */
if (cylno == sblock.fs_ncg - 1)
icg.cg_ncyl = sblock.fs_ncyl - (sblock.fs_cpg * cylno);
else
icg.cg_ncyl = sblock.fs_cpg;
icg.cg_niblk = sblock.fs_ipg;
icg.cg_ndblk = dmax - cbase;
icg.cg_cs.cs_ndir = 0;
icg.cg_cs.cs_nffree = 0;
icg.cg_cs.cs_nbfree = 0;
icg.cg_cs.cs_nifree = 0;
......
/*
* Write all inodes in a single write for performance.
*/
awtfs(fsbtodb(&sblock, (uint64_t)cgimin(&sblock, cylno)), (int)size,
(char *)inode_buffer, RELEASE);
......
awtfs(fsbtodb(&sblock, (uint64_t)cgtod(&sblock, cylno)),
sblock.fs_bsize, (char *)&icg, RELEASE);
}
3. fs
/*
* Now construct the initial file system,
* then write out the super-block.
*/
fsinit();
fsinit()
{
int i;
/*
* initialize the node
*/
node.i_atime = mkfstime;
node.i_mtime = mkfstime;
node.i_ctime = mkfstime;
#ifdef LOSTDIR
/*
* create the lost+found directory
*/
(void) makedir(lost_found_dir, 2);
for (i = DIRBLKSIZ; i
wtfs(diskaddr_t bno, int size, char *bf), use bno*SECTOR_SIZE to seek for the disk address, so fsbtodb(&sblock,node.i_db[0]) is used to do the transmition.
4. write super block
/*
* write the superblock and csum information
*/
wtsb();
/*
* Write out the duplicate super blocks to the first 10
* cylinder groups (or fewer, if there are fewer than 10
* cylinder groups).
*/
for (cylno = 0; cylno
flush_writes();
======================================
fsdb
-bash-3.00# fsdb -F ufs -o w /dev/rdsk/ramd0
"/"operations:
/dev/rdsk/ramd0 > 2:inode ?i
i#: 2 md: d---rwxr-xr-x uid: 0 gid: 0
ln: 3 bs: 2 sz : c_flags : 0 200
db#0: 38
accessed: Mon May 21 11:42:53 2007
modified: Mon May 21 11:42:53 2007
created : Mon May 21 11:42:53 2007
/dev/rdsk/ramd0 > :ls
/c:
./ a/ d/ nam/
../ c/ lost+found/ test
/dev/rdsk/ramd0 > 5:dir:nm="z"
i#: 3 z
/dev/rdsk/ramd0 > :ls
/c:
./ a/ d/ test
../ c/ lost+found/ z/
/dev/rdsk/ramd0 > 0:file,*/c
end of file
?
/* failed, don't know why. */
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/38597/showart_305715.html |
|