From owner-freebsd-current Thu Jan 23 13:44:59 2003 Delivered-To: freebsd-current@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 931) id 8C8C937B401; Thu, 23 Jan 2003 13:44:48 -0800 (PST) Date: Thu, 23 Jan 2003 13:44:48 -0800 From: Juli Mallett To: current@FreeBSD.org Subject: Test this! Patch to make newfs(8) use libufs. Message-ID: <20030123134448.A57729@FreeBSD.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i Organisation: The FreeBSD Project X-Alternate-Addresses: , , , , X-Towel: Yes X-LiveJournal: flata, jmallett X-Negacore: Yes Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Thanks to Ruslan's reminder that tunefs now uses libufs and tunefs is one of the crunched programs, I realised that I really needed to make newfs(8) use libufs. To show off that it *can* help us reduce space, a good bit in some cases. Well, after an afternoon of work, here's the diff. Some of it is just direct conversions from wtfs to bwrite, etc. Some of it can make use of libufs API. Hopefully libufs will *grow* some API to help out newfs. One of those will be cgwrite/cgwrite1, but I'm holding off as I am not sure whether to make cgwrite operate on the cg we last read in, or the next cg (it will probably be the last cg). Another will probably be something like putino. Not sure. Anyway, it passes the tests (up to what my machine's memory can do, I should say) and it shouldn't be prone to any mistakes (I _am_ perfect after all *cough*), but here it is. It isn't pretty, but it's a start. %%% newfs-libufs.diff Index: Makefile =================================================================== RCS file: /home/ncvs/src/sbin/newfs/Makefile,v retrieving revision 1.19 diff -d -u -r1.19 Makefile --- Makefile 19 Mar 2002 21:05:29 -0000 1.19 +++ Makefile 23 Jan 2003 21:35:28 -0000 @@ -2,6 +2,8 @@ # $FreeBSD: src/sbin/newfs/Makefile,v 1.19 2002/03/19 21:05:29 phk Exp $ PROG= newfs +DPADD= -lufs +LDADD= -lufs SRCS= newfs.c mkfs.c WARNS?= 2 MAN= newfs.8 Index: mkfs.c =================================================================== RCS file: /home/ncvs/src/sbin/newfs/mkfs.c,v retrieving revision 1.67 diff -d -u -r1.67 mkfs.c --- mkfs.c 2 Dec 2002 19:31:53 -0000 1.67 +++ mkfs.c 23 Jan 2003 21:35:28 -0000 @@ -80,18 +80,10 @@ #define UMASK 0755 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) -static union { - struct fs fs; - char pad[SBLOCKSIZE]; -} fsun; -#define sblock fsun.fs +struct uufsd disk; static struct csum *fscs; - -static union { - struct cg cg; - char pad[MAXBSIZE]; -} cgun; -#define acg cgun.cg +#define sblock disk.d_fs +#define acg disk.d_cg union dinode { struct ufs1_dinode dp1; @@ -113,10 +105,7 @@ static int isblock(struct fs *, unsigned char *, int); static void iput(union dinode *, ino_t); static int makedir(struct direct *, int); -static void rdfs(ufs2_daddr_t, int, char *); static void setblock(struct fs *, unsigned char *, int); -static void wtfs(ufs2_daddr_t, int, char *); -static void wtfsflush(void); void mkfs(struct partition *pp, char *fsys) @@ -149,8 +138,8 @@ printf("preposterous size %jd\n", (intmax_t)fssize); exit(13); } - wtfs(fssize - (realsectorsize / DEV_BSIZE), realsectorsize, - (char *)&sblock); + bwrite(&disk, fssize - (realsectorsize / DEV_BSIZE), + (char *)&sblock, realsectorsize); /* * collect and verify the file system density info */ @@ -487,13 +476,12 @@ sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; } - wtfs(sblock.fs_sblockloc / sectorsize, SBLOCKSIZE, (char *)&sblock); + sbwrite(&disk, 1); for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) - wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)), + bwrite(&disk, fsbtodb(&sblock, sblock.fs_csaddr + + numfrags(&sblock, i)), ((char *)fscs) + i, sblock.fs_cssize - i < sblock.fs_bsize ? - sblock.fs_cssize - i : sblock.fs_bsize, - ((char *)fscs) + i); - wtfsflush(); + sblock.fs_cssize - i : sblock.fs_bsize); /* * Update information about this partion in pack * label, to that it may be updated on disk. @@ -662,7 +650,8 @@ dp2++; } } - wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf); + bwrite(&disk, fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobuf, + iobufsize); /* * For the old file system, we have to initialize all the inodes. */ @@ -675,8 +664,9 @@ dp1->di_gen = random(); dp1++; } - wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i), - sblock.fs_bsize, &iobuf[start]); + bwrite(&disk, + fsbtodb(&sblock, cgimin(&sblock, cylno) + i), + &iobuf[start], sblock.fs_bsize); } } } @@ -713,8 +703,8 @@ node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode); node.dp1.di_blocks = btodb(fragroundup(&sblock, node.dp1.di_size)); - wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, - iobuf); + bwrite(&disk, fsbtodb(&sblock, node.dp1.di_db[0]), iobuf, + sblock.fs_fsize); } else { /* * initialize the node @@ -732,8 +722,8 @@ node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode); node.dp2.di_blocks = btodb(fragroundup(&sblock, node.dp2.di_size)); - wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, - iobuf); + bwrite(&disk, fsbtodb(&sblock, node.dp2.di_db[0]), iobuf, + sblock.fs_fsize); } iput(&node, ROOTINO); } @@ -769,8 +759,7 @@ { int i, d, blkno, frag; - rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, - (char *)&acg); + cgread1(&disk, 0); if (acg.cg_magic != CG_MAGIC) { printf("cg 0: bad magic number\n"); return (0); @@ -806,8 +795,9 @@ for (i = frag; i < sblock.fs_frag; i++) setbit(cg_blksfree(&acg), d + i); } - wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, - (char *)&acg); + /* XXX cgwrite(&disk, 0)??? */ + bwrite(&disk, fsbtodb(&sblock, cgtod(&sblock, 0)), (char *)&acg, + sblock.fs_cgsize); return ((ufs2_daddr_t)d); } @@ -821,16 +811,14 @@ int c; c = ino_to_cg(&sblock, ino); - rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, - (char *)&acg); - if (acg.cg_magic != CG_MAGIC) { - printf("cg 0: bad magic number\n"); + if (cgread1(&disk, 0) == -1) { + printf("cg 0: bad cylinder group\n"); exit(31); } acg.cg_cs.cs_nifree--; setbit(cg_inosused(&acg), ino); - wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, - (char *)&acg); + bwrite(&disk, fsbtodb(&sblock, cgtod(&sblock, 0)), (char *)&acg, + sblock.fs_cgsize); sblock.fs_cstotal.cs_nifree--; fscs[0].cs_nifree--; if (ino >= (unsigned long)sblock.fs_ipg * sblock.fs_ncg) { @@ -838,101 +826,14 @@ exit(32); } d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino)); - rdfs(d, sblock.fs_bsize, (char *)iobuf); + bread(&disk, d, (char *)iobuf, sblock.fs_bsize); if (sblock.fs_magic == FS_UFS1_MAGIC) ((struct ufs1_dinode *)iobuf)[ino_to_fsbo(&sblock, ino)] = ip->dp1; else ((struct ufs2_dinode *)iobuf)[ino_to_fsbo(&sblock, ino)] = ip->dp2; - wtfs(d, sblock.fs_bsize, (char *)iobuf); -} - -/* - * read a block from the file system - */ -void -rdfs(ufs2_daddr_t bno, int size, char *bf) -{ - int n; - - wtfsflush(); - if (lseek(fso, (off_t)bno * sectorsize, 0) < 0) { - printf("seek error: %ld\n", (long)bno); - err(33, "rdfs"); - } - n = read(fso, bf, size); - if (n != size) { - printf("read error: %ld\n", (long)bno); - err(34, "rdfs"); - } -} - -#define WCSIZE (128 * 1024) -ufs2_daddr_t wc_sect; /* units of sectorsize */ -int wc_end; /* bytes */ -static char wc[WCSIZE]; /* bytes */ - -/* - * Flush dirty write behind buffer. - */ -static void -wtfsflush() -{ - int n; - if (wc_end) { - if (lseek(fso, (off_t)wc_sect * sectorsize, SEEK_SET) < 0) { - printf("seek error: %ld\n", (long)wc_sect); - err(35, "wtfs - writecombine"); - } - n = write(fso, wc, wc_end); - if (n != wc_end) { - printf("write error: %ld\n", (long)wc_sect); - err(36, "wtfs - writecombine"); - } - wc_end = 0; - } -} - -/* - * write a block to the file system - */ -static void -wtfs(ufs2_daddr_t bno, int size, char *bf) -{ - int done, n; - - if (Nflag) - return; - done = 0; - if (wc_end == 0 && size <= WCSIZE) { - wc_sect = bno; - bcopy(bf, wc, size); - wc_end = size; - if (wc_end < WCSIZE) - return; - done = 1; - } - if ((off_t)wc_sect * sectorsize + wc_end == (off_t)bno * sectorsize && - wc_end + size <= WCSIZE) { - bcopy(bf, wc + wc_end, size); - wc_end += size; - if (wc_end < WCSIZE) - return; - done = 1; - } - wtfsflush(); - if (done) - return; - if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) { - printf("seek error: %ld\n", (long)bno); - err(35, "wtfs"); - } - n = write(fso, bf, size); - if (n != size) { - printf("write error: %ld\n", (long)bno); - err(36, "wtfs"); - } + bwrite(&disk, d, (char *)iobuf, sblock.fs_bsize); } /* Index: newfs.c =================================================================== RCS file: /home/ncvs/src/sbin/newfs/newfs.c,v retrieving revision 1.66 diff -d -u -r1.66 newfs.c --- newfs.c 30 Nov 2002 18:28:26 -0000 1.66 +++ newfs.c 23 Jan 2003 21:35:28 -0000 @@ -135,7 +135,6 @@ int maxbpg; /* maximum blocks per file in a cyl group */ int avgfilesize = AVFILESIZ;/* expected average file size */ int avgfilesperdir = AFPDIR;/* expected number of files per directory */ -int fso; /* filedescriptor to device */ static char device[MAXPATHLEN]; static char *disktype; @@ -261,17 +260,20 @@ special = device; } - fso = open(special, Nflag ? O_RDONLY : O_RDWR); - if (fso < 0) - err(1, "%s", special); - if (fstat(fso, &st) < 0) + if (ufs_disk_fillout_blank(&disk, special) == -1) { + if (disk.d_error != NULL) + errx(1, "%s: %s", special, disk.d_error); + else + err(1, "%s", special); + } + if (fstat(disk.d_fd, &st) < 0) err(1, "%s", special); if ((st.st_mode & S_IFMT) != S_IFCHR) errx(1, "%s: not a character-special device", special); if (sectorsize == 0) - ioctl(fso, DIOCGSECTORSIZE, §orsize); - if (sectorsize && !ioctl(fso, DIOCGMEDIASIZE, &mediasize)) { + ioctl(disk.d_fd, DIOCGSECTORSIZE, §orsize); + if (sectorsize && !ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize)) { if (fssize == 0) fssize = mediasize / sectorsize; else if (fssize > mediasize / sectorsize) @@ -350,7 +352,7 @@ if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) rewritelabel(special, lp); } - close(fso); + ufs_disk_close(&disk); exit(0); } @@ -360,7 +362,7 @@ static struct disklabel lab; struct disklabel *lp; - if (!ioctl(fso, DIOCGDINFO, (char *)&lab)) + if (!ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab)) return (&lab); unlabeled++; if (disktype) { @@ -378,12 +380,12 @@ return; lp->d_checksum = 0; lp->d_checksum = dkcksum(lp); - if (ioctl(fso, DIOCWDINFO, (char *)lp) < 0) + if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) < 0) warn("ioctl (WDINFO): %s: can't rewrite disk label", s); } static void -usage() +usage(void) { fprintf(stderr, "usage: %s [ -fsoptions ] special-device%s\n", Index: newfs.h =================================================================== RCS file: /home/ncvs/src/sbin/newfs/newfs.h,v retrieving revision 1.8 diff -d -u -r1.8 newfs.h --- newfs.h 21 Aug 2002 18:11:21 -0000 1.8 +++ newfs.h 23 Jan 2003 21:35:28 -0000 @@ -45,6 +45,13 @@ * $FreeBSD: src/sbin/newfs/newfs.h,v 1.8 2002/08/21 18:11:21 trhodes Exp $ */ +/* + * libufs interfaces. + */ +#include + +extern struct uufsd disk; /* the disk where we will operate */ + /* * variables set up by front end. @@ -67,6 +74,5 @@ extern int maxbpg; /* maximum blocks per file in a cyl group */ extern int avgfilesize; /* expected average file size */ extern int avgfilesperdir; /* expected number of files per directory */ -extern int fso; /* filedescriptor to device */ void mkfs (struct partition *, char *); %%% Thanx, juli. -- Juli Mallett AIM: BSDFlata -- IRC: juli on EFnet. OpenDarwin, Mono, FreeBSD Developer. ircd-hybrid Developer, EFnet addict. FreeBSD on MIPS-Anything on FreeBSD. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message