Example #1
0
/// get_used_block - get FAT used blocks
static unsigned long long get_used_block()
{
    unsigned long long     block, bused = 0, bfree = 0;
    int                    i = 0;
    unsigned char	   *p;


    /// read group
    while ((i = cgread(&disk)) != 0) {
        log_mesg(2, 0, 0, fs_opt.debug, "%s: \ncg = %d\n", __FILE__, disk.d_lcg);
        log_mesg(2, 0, 0, fs_opt.debug, "%s: blocks = %i\n", __FILE__, acg.cg_ndblk);
        p = cg_blksfree(&acg);

        for (block = 0; block < acg.cg_ndblk; block++){
            if (isset(p, block)) {
                bfree++;
            } else {
                bused++;
            }
        }

    }
    log_mesg(1, 0, 0, fs_opt.debug, "%s: total used = %lli, total free = %lli\n", __FILE__, bused, bfree);
    return bused;
}
Example #2
0
static ufs2_daddr_t journal_balloc( void ) {
	ufs2_daddr_t blk;
	struct cg *cgp;
	int valid;
	static int contig = 1;
	cgp = &disk.d_cg;
	for ( ;; ) {
		blk = cgballoc( &disk );
		if ( blk > 0 ) break;
		if ( cgwrite( &disk ) < 0 ) {
			warn( "Failed to write updated cg" );
			return ( -1 );
		}
		while ( ( valid = cgread( &disk ) ) == 1 ) {
			if ( cgp->cg_cs.cs_nbfree > 256 * 1024 ) break;
			if ( contig == 0 && cgp->cg_cs.cs_nbfree ) break;
		}
		if ( valid ) continue;
		if ( contig ) {
			contig = 0;
			disk.d_ccg = 0;
			warnx( "Journal file fragmented." );
			continue;
		}
		warnx( "Failed to find sufficient free blocks for the journal" );
		return -1;
	}
	if ( bwrite( &disk, fsbtodb( &sblock, blk ), clrbuf,
	sblock.fs_bsize ) <= 0 ) {
		warn( "Failed to initialize new block" );
		return -1;
	}
	return ( blk );
}
Example #3
0
static ufs2_daddr_t
journal_balloc(void)
{
	ufs2_daddr_t blk;
	struct cg *cgp;
	int valid;
	static int contig = 1;

	cgp = &disk.d_cg;
	for (;;) {
		blk = cgballoc(&disk);
		if (blk > 0)
			break;
		/*
		 * If we failed to allocate a block from this cg, move to
		 * the next.
		 */
		if (cgwrite(&disk) < 0) {
			warn("Failed to write updated cg");
			return (-1);
		}
		while ((valid = cgread(&disk)) == 1) {
			/*
			 * Try to minimize fragmentation by requiring a minimum
			 * number of blocks present.
			 */
			if (cgp->cg_cs.cs_nbfree > 256 * 1024)
				break;
			if (contig == 0 && cgp->cg_cs.cs_nbfree)
				break;
		}
		if (valid)
			continue;
		/*
		 * Try once through looking only for large contiguous regions
		 * and again taking any space we can find.
		 */
		if (contig) {
			contig = 0;
			disk.d_ccg = 0;
			warnx("Journal file fragmented.");
			continue;
		}
		warnx("Failed to find sufficient free blocks for the journal");
		return -1;
	}
	if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
	    sblock.fs_bsize) <= 0) {
		warn("Failed to initialize new block");
		return -1;
	}
	return (blk);
}
Example #4
0
int
dumpfreespace(const char *name, int fflag)
{
	int i;

	while ((i = cgread(&disk)) != 0) {
		if (i == -1)
			goto err;
		dumpfreespacecg(fflag);
	}
	return (0);
err:
	ufserr(name);
	return (1);
}
/// readbitmap - read bitmap
extern void readbitmap(char* device, image_head image_hdr, char* bitmap, int pui)
{
    unsigned long long     total_block, block, bused = 0, bfree = 0;
    int                    done = 0, i = 0, start = 0, bit_size = 1;
    char* p;


    fs_open(device);

    /// init progress
    progress_bar   bprog;	/// progress_bar structure defined in progress.h
    progress_init(&bprog, start, image_hdr.totalblock, bit_size);

    total_block = 0;
    /// read group
    while ((i = cgread(&disk)) != 0) {
        log_mesg(2, 0, 0, fs_opt.debug, "%s: \ncg = %d\n", __FILE__, disk.d_lcg);
        log_mesg(2, 0, 0, fs_opt.debug, "%s: blocks = %i\n", __FILE__, acg.cg_ndblk);
        p = cg_blksfree(&acg);

        for (block = 0; block < acg.cg_ndblk; block++) {
            if (isset(p, block)) {
                bitmap[total_block] = 0;
                bfree++;
                log_mesg(3, 0, 0, fs_opt.debug, "%s: bitmap is free %lli\n", __FILE__, block);
            } else {
                bitmap[total_block] = 1;
                bused++;
                log_mesg(3, 0, 0, fs_opt.debug, "%s: bitmap is used %lli\n", __FILE__, block);
            }
            total_block++;
            update_pui(&bprog, total_block ,done);
        }
        log_mesg(1, 0, 0, fs_opt.debug, "%s: read bitmap done\n", __FILE__);

    }

    fs_close();

    log_mesg(1, 0, 0, fs_opt.debug, "%s: total used = %lli, total free = %lli\n", __FILE__, bused, bfree);
    done = 1;
    update_pui(&bprog, 1, done);

}
Example #6
0
static int
journal_alloc(int64_t size)
{
	struct ufs1_dinode *dp1;
	struct ufs2_dinode *dp2;
	ufs2_daddr_t blk;
	void *ip;
	struct cg *cgp;
	int resid;
	ino_t ino;
	int blks;
	int mode;
	time_t utime;
	int i;

	cgp = &disk.d_cg;
	ino = 0;

	/*
	 * If the journal file exists we can't allocate it.
	 */
	ino = journal_findfile();
	if (ino == (ino_t)-1)
		return (-1);
	if (ino > 0) {
		warnx("Journal file %s already exists, please remove.",
		    SUJ_FILE);
		return (-1);
	}
	/*
	 * If the user didn't supply a size pick one based on the filesystem
	 * size constrained with hardcoded MIN and MAX values.  We opt for
	 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
	 * not less than the MIN.
	 */
	if (size == 0) {
		size = (sblock.fs_size * sblock.fs_bsize) / 1024;
		size = MIN(SUJ_MAX, size);
		if (size / sblock.fs_fsize > sblock.fs_fpg)
			size = sblock.fs_fpg * sblock.fs_fsize;
		size = MAX(SUJ_MIN, size);
		/* fsck does not support fragments in journal files. */
		size = roundup(size, sblock.fs_bsize);
	}
	resid = blocks = size / sblock.fs_bsize;
	if (sblock.fs_cstotal.cs_nbfree < blocks) {
		warn("Insufficient free space for %jd byte journal", size);
		return (-1);
	}
	/*
	 * Find a cg with enough blocks to satisfy the journal
	 * size.  Presently the journal does not span cgs.
	 */
	while (cgread(&disk) == 1) {
		if (cgp->cg_cs.cs_nifree == 0)
			continue;
		ino = cgialloc(&disk);
		if (ino <= 0)
			break;
		printf("Using inode %ju in cg %d for %jd byte journal\n",
		    (uintmax_t)ino, cgp->cg_cgx, size);
		if (getino(&disk, &ip, ino, &mode) != 0) {
			warn("Failed to get allocated inode");
			sbdirty();
			goto out;
		}
		/*
		 * We leave fields unrelated to the number of allocated
		 * blocks and size uninitialized.  This causes legacy
		 * fsck implementations to clear the inode.
		 */
		dp2 = ip;
		dp1 = ip;
		time(&utime);
		if (sblock.fs_magic == FS_UFS1_MAGIC) {
			bzero(dp1, sizeof(*dp1));
			dp1->di_size = size;
			dp1->di_mode = IFREG | IREAD;
			dp1->di_nlink = 1;
			dp1->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
			dp1->di_atime = utime;
			dp1->di_mtime = utime;
			dp1->di_ctime = utime;
		} else {
			bzero(dp2, sizeof(*dp2));
			dp2->di_size = size;
			dp2->di_mode = IFREG | IREAD;
			dp2->di_nlink = 1;
			dp2->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
			dp2->di_atime = utime;
			dp2->di_mtime = utime;
			dp2->di_ctime = utime;
			dp2->di_birthtime = utime;
		}
		for (i = 0; i < NDADDR && resid; i++, resid--) {
			blk = journal_balloc();
			if (blk <= 0)
				goto out;
			if (sblock.fs_magic == FS_UFS1_MAGIC) {
				dp1->di_db[i] = blk;
				dp1->di_blocks++;
			} else {
				dp2->di_db[i] = blk;
				dp2->di_blocks++;
			}
		}
		for (i = 0; i < NIADDR && resid; i++) {
			blk = journal_balloc();
			if (blk <= 0)
				goto out;
			blks = indir_fill(blk, i, &resid) + 1;
			if (blks <= 0) {
				sbdirty();
				goto out;
			}
			if (sblock.fs_magic == FS_UFS1_MAGIC) {
				dp1->di_ib[i] = blk;
				dp1->di_blocks += blks;
			} else {
				dp2->di_ib[i] = blk;
				dp2->di_blocks += blks;
			}
		}
		if (sblock.fs_magic == FS_UFS1_MAGIC)
			dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
		else
			dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
		if (putino(&disk) < 0) {
			warn("Failed to write inode");
			sbdirty();
			return (-1);
		}
		if (cgwrite(&disk) < 0) {
			warn("Failed to write updated cg");
			sbdirty();
			return (-1);
		}
		if (journal_insertfile(ino) < 0) {
			sbdirty();
			return (-1);
		}
		sblock.fs_sujfree = 0;
		return (0);
	}
	warnx("Insufficient free space for the journal.");
out:
	return (-1);
}
Example #7
0
int
dumpfs(const char *name)
{
	time_t fstime;
	int64_t fssize;
	int32_t fsflags;
	int i;

	switch (disk.d_ufs) {
	case 2:
		fssize = afs.fs_size;
		fstime = afs.fs_time;
		printf("magic\t%x (UFS2)\ttime\t%s",
		    afs.fs_magic, ctime(&fstime));
		printf("superblock location\t%jd\tid\t[ %x %x ]\n",
		    (intmax_t)afs.fs_sblockloc, afs.fs_id[0], afs.fs_id[1]);
		printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
		    afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
		break;
	case 1:
		fssize = afs.fs_old_size;
		fstime = afs.fs_old_time;
		printf("magic\t%x (UFS1)\ttime\t%s",
		    afs.fs_magic, ctime(&fstime));
		printf("id\t[ %08x %08x ]\n", afs.fs_id[0], afs.fs_id[1]);
		printf("ncg\t%d\tsize\t%jd\tblocks\t%jd\n",
		    afs.fs_ncg, (intmax_t)fssize, (intmax_t)afs.fs_dsize);
		break;
	default:
		goto err;
	}
	printf("bsize\t%d\tshift\t%d\tmask\t0x%08x\n",
	    afs.fs_bsize, afs.fs_bshift, afs.fs_bmask);
	printf("fsize\t%d\tshift\t%d\tmask\t0x%08x\n",
	    afs.fs_fsize, afs.fs_fshift, afs.fs_fmask);
	printf("frag\t%d\tshift\t%d\tfsbtodb\t%d\n",
	    afs.fs_frag, afs.fs_fragshift, afs.fs_fsbtodb);
	printf("minfree\t%d%%\toptim\t%s\tsymlinklen %d\n",
	    afs.fs_minfree, afs.fs_optim == FS_OPTSPACE ? "space" : "time",
	    afs.fs_maxsymlinklen);
	switch (disk.d_ufs) {
	case 2:
		printf("%s %d\tmaxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
		    "maxbsize", afs.fs_maxbsize, afs.fs_maxbpg,
		    afs.fs_maxcontig, afs.fs_contigsumsize);
		printf("nbfree\t%jd\tndir\t%jd\tnifree\t%jd\tnffree\t%jd\n",
		    (intmax_t)afs.fs_cstotal.cs_nbfree, 
		    (intmax_t)afs.fs_cstotal.cs_ndir,
		    (intmax_t)afs.fs_cstotal.cs_nifree, 
		    (intmax_t)afs.fs_cstotal.cs_nffree);
		printf("bpg\t%d\tfpg\t%d\tipg\t%d\tunrefs\t%jd\n",
		    afs.fs_fpg / afs.fs_frag, afs.fs_fpg, afs.fs_ipg,
		    (intmax_t)afs.fs_unrefs);
		printf("nindir\t%d\tinopb\t%d\tmaxfilesize\t%ju\n",
		    afs.fs_nindir, afs.fs_inopb, 
		    (uintmax_t)afs.fs_maxfilesize);
		printf("sbsize\t%d\tcgsize\t%d\tcsaddr\t%jd\tcssize\t%d\n",
		    afs.fs_sbsize, afs.fs_cgsize, (intmax_t)afs.fs_csaddr,
		    afs.fs_cssize);
		break;
	case 1:
		printf("maxbpg\t%d\tmaxcontig %d\tcontigsumsize %d\n",
		    afs.fs_maxbpg, afs.fs_maxcontig, afs.fs_contigsumsize);
		printf("nbfree\t%d\tndir\t%d\tnifree\t%d\tnffree\t%d\n",
		    afs.fs_old_cstotal.cs_nbfree, afs.fs_old_cstotal.cs_ndir,
		    afs.fs_old_cstotal.cs_nifree, afs.fs_old_cstotal.cs_nffree);
		printf("cpg\t%d\tbpg\t%d\tfpg\t%d\tipg\t%d\n",
		    afs.fs_old_cpg, afs.fs_fpg / afs.fs_frag, afs.fs_fpg,
		    afs.fs_ipg);
		printf("nindir\t%d\tinopb\t%d\tnspf\t%d\tmaxfilesize\t%ju\n",
		    afs.fs_nindir, afs.fs_inopb, afs.fs_old_nspf,
		    (uintmax_t)afs.fs_maxfilesize);
		printf("sbsize\t%d\tcgsize\t%d\tcgoffset %d\tcgmask\t0x%08x\n",
		    afs.fs_sbsize, afs.fs_cgsize, afs.fs_old_cgoffset,
		    afs.fs_old_cgmask);
		printf("csaddr\t%d\tcssize\t%d\n",
		    afs.fs_old_csaddr, afs.fs_cssize);
		printf("rotdelay %dms\trps\t%d\ttrackskew %d\tinterleave %d\n",
		    afs.fs_old_rotdelay, afs.fs_old_rps, afs.fs_old_trackskew,
		    afs.fs_old_interleave);
		printf("nsect\t%d\tnpsect\t%d\tspc\t%d\n",
		    afs.fs_old_nsect, afs.fs_old_npsect, afs.fs_old_spc);
		break;
	default:
		goto err;
	}
	printf("sblkno\t%d\tcblkno\t%d\tiblkno\t%d\tdblkno\t%d\n",
	    afs.fs_sblkno, afs.fs_cblkno, afs.fs_iblkno, afs.fs_dblkno);
	printf("cgrotor\t%d\tfmod\t%d\tronly\t%d\tclean\t%d\n",
	    afs.fs_cgrotor, afs.fs_fmod, afs.fs_ronly, afs.fs_clean);
	printf("avgfpdir %d\tavgfilesize %d\n",
	    afs.fs_avgfpdir, afs.fs_avgfilesize);
	printf("flags\t");
	if (afs.fs_old_flags & FS_FLAGS_UPDATED)
		fsflags = afs.fs_flags;
	else
		fsflags = afs.fs_old_flags;
	if (fsflags == 0)
		printf("none");
	if (fsflags & FS_UNCLEAN)
		printf("unclean ");
	if (fsflags & FS_DOSOFTDEP)
		printf("soft-updates%s ", (fsflags & FS_SUJ) ? "+journal" : "");
	if (fsflags & FS_NEEDSFSCK)
		printf("needs fsck run ");
	if (fsflags & FS_INDEXDIRS)
		printf("indexed directories ");
	if (fsflags & FS_ACLS)
		printf("acls ");
	if (fsflags & FS_MULTILABEL)
		printf("multilabel ");
	if (fsflags & FS_GJOURNAL)
		printf("gjournal ");
	if (fsflags & FS_FLAGS_UPDATED)
		printf("fs_flags expanded ");
	if (fsflags & FS_NFS4ACLS)
		printf("nfsv4acls ");
	if (fsflags & FS_TRIM)
		printf("trim ");
	fsflags &= ~(FS_UNCLEAN | FS_DOSOFTDEP | FS_NEEDSFSCK | FS_INDEXDIRS |
		     FS_ACLS | FS_MULTILABEL | FS_GJOURNAL | FS_FLAGS_UPDATED |
		     FS_NFS4ACLS | FS_SUJ | FS_TRIM);
	if (fsflags != 0)
		printf("unknown flags (%#x)", fsflags);
	putchar('\n');
	printf("fsmnt\t%s\n", afs.fs_fsmnt);
	printf("volname\t%s\tswuid\t%ju\n",
		afs.fs_volname, (uintmax_t)afs.fs_swuid);
	printf("\ncs[].cs_(nbfree,ndir,nifree,nffree):\n\t");
	afs.fs_csp = calloc(1, afs.fs_cssize);
	if (bread(&disk, fsbtodb(&afs, afs.fs_csaddr), afs.fs_csp, afs.fs_cssize) == -1)
		goto err;
	for (i = 0; i < afs.fs_ncg; i++) {
		struct csum *cs = &afs.fs_cs(&afs, i);
		if (i && i % 4 == 0)
			printf("\n\t");
		printf("(%d,%d,%d,%d) ",
		    cs->cs_nbfree, cs->cs_ndir, cs->cs_nifree, cs->cs_nffree);
	}
	printf("\n");
	if (fssize % afs.fs_fpg) {
		if (disk.d_ufs == 1)
			printf("cylinders in last group %d\n",
			    howmany(afs.fs_old_size % afs.fs_fpg,
			    afs.fs_old_spc / afs.fs_old_nspf));
		printf("blocks in last group %ld\n\n",
		    (long)((fssize % afs.fs_fpg) / afs.fs_frag));
	}
	while ((i = cgread(&disk)) != 0) {
		if (i == -1 || dumpcg())
			goto err;
	}
	return (0);

err:	ufserr(name);
	return (1);
}
Example #8
0
static int journal_alloc( int64_t size ) {
	struct ufs1_dinode *dp1;
	struct ufs2_dinode *dp2;
	ufs2_daddr_t blk;
	void *ip;
	struct cg *cgp;
	int resid;
	ino_t ino;
	int blks;
	int mode;
	time_t utime;
	int i;
	cgp = &disk.d_cg;
	ino = 0;
	ino = journal_findfile();
	if ( ino == ( ino_t ) - 1 ) return ( -1 );
	if ( ino > 0 ) {
		warnx( "Journal file %s already exists, please remove.", SUJ_FILE );
		return ( -1 );
	}
	if ( size == 0 ) {
		size = ( sblock.fs_size * sblock.fs_bsize ) / 1024;
		size = MIN( SUJ_MAX, size );
		if ( size / sblock.fs_fsize > sblock.fs_fpg ) size = sblock.fs_fpg * sblock.fs_fsize;
		size = MAX( SUJ_MIN, size );
		size = roundup( size, sblock.fs_bsize );
	}
	resid = blocks = size / sblock.fs_bsize;
	if ( sblock.fs_cstotal.cs_nbfree < blocks ) {
		warn( "Insufficient free space for %jd byte journal", size );
		return ( -1 );
	}
	while ( cgread( &disk ) == 1 ) {
		if ( cgp->cg_cs.cs_nifree == 0 ) continue;
		ino = cgialloc( &disk );
		if ( ino <= 0 ) break;
		printf( "Using inode %ju in cg %d for %jd byte journal\n", (uintmax_t) ino, cgp->cg_cgx, size );
		if ( getino( &disk, &ip, ino, &mode ) != 0 ) {
			warn( "Failed to get allocated inode" );
			sbdirty();
			goto out;
		}
		dp2 = ip;
		dp1 = ip;
		time( &utime );
		if ( sblock.fs_magic == FS_UFS1_MAGIC ) {
			bzero( dp1, sizeof( *dp1 ) );
			dp1->di_size = size;
			dp1->di_mode = IFREG | IREAD;
			dp1->di_nlink = 1;
			dp1->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
			dp1->di_atime = utime;
			dp1->di_mtime = utime;
			dp1->di_ctime = utime;
		} else {
			bzero( dp2, sizeof( *dp2 ) );
			dp2->di_size = size;
			dp2->di_mode = IFREG | IREAD;
			dp2->di_nlink = 1;
			dp2->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
			dp2->di_atime = utime;
			dp2->di_mtime = utime;
			dp2->di_ctime = utime;
			dp2->di_birthtime = utime;
		}
		for ( i = 0; i < NDADDR && resid; i++, resid-- ) {
			blk = journal_balloc();
			if ( blk <= 0 ) goto out;
			if ( sblock.fs_magic == FS_UFS1_MAGIC ) {
				dp1->di_db[i] = blk;
				dp1->di_blocks++;
			} else {
				dp2->di_db[i] = blk;
				dp2->di_blocks++;
			}
		}
		for ( i = 0; i < NIADDR && resid; i++ ) {
			blk = journal_balloc();
			if ( blk <= 0 ) goto out;
			blks = indir_fill( blk, i, &resid ) + 1;
			if ( blks <= 0 ) {
				sbdirty();
				goto out;
			}
			if ( sblock.fs_magic == FS_UFS1_MAGIC ) {
				dp1->di_ib[i] = blk;
				dp1->di_blocks += blks;
			} else {
				dp2->di_ib[i] = blk;
				dp2->di_blocks += blks;
			}
		}
		if ( sblock.fs_magic == FS_UFS1_MAGIC ) dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
		else dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
		if ( putino( &disk ) < 0 ) {
			warn( "Failed to write inode" );
			sbdirty();
			return ( -1 );
		}
		if ( cgwrite( &disk ) < 0 ) {
			warn( "Failed to write updated cg" );
			sbdirty();
			return ( -1 );
		}
		if ( journal_insertfile( ino ) < 0 ) {
			sbdirty();
			return ( -1 );
		}
		sblock.fs_sujfree = 0;
		return ( 0 );
	}
	warnx( "Insufficient free space for the journal." );
	out: return ( -1 );
}