From nobody@FreeBSD.org  Thu Nov  3 02:38:40 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 3F8E11065787
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  3 Nov 2011 02:38:40 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 300D08FC0C
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  3 Nov 2011 02:38:40 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id pA32cdSV093042
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 3 Nov 2011 02:38:39 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id pA32cdLU093040;
	Thu, 3 Nov 2011 02:38:39 GMT
	(envelope-from nobody)
Message-Id: <201111030238.pA32cdLU093040@red.freebsd.org>
Date: Thu, 3 Nov 2011 02:38:39 GMT
From: YIN Xiaofeng <75394094@qq.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: fdisk can't correct/round the slice size after entering invalid number
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         162262
>Category:       bin
>Synopsis:       fdisk can't correct/round the slice size after entering invalid number
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Nov 03 02:40:07 UTC 2011
>Closed-Date:    Wed Nov 16 17:00:15 UTC 2011
>Last-Modified:  Wed Nov 16 17:00:15 UTC 2011
>Originator:     YIN Xiaofeng
>Release:        FreeBSD 9.0 BETA-3
>Organization:
>Environment:
# uname -a
FreeBSD  9.0-BETA3 FreeBSD 9.0-BETA3 #0: Sat Sep 24 20:46:57 UTC 2011     root@o
brian.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
Use FreeBSD-BETA3 iso to boot, select "Live CD" to enter into the live filesystem.
then run: fdisk -i ad0 

first time: input a invalid decimal number when prompt to enter the size of slice.
second time:input a valid decimal number

Two times within one fdisk invocation.

It's erroneous to determine the size of the slice according to your input value when you enter a value like 2G that cause it warning you


----
Sorry for my poor english.
>How-To-Repeat:
Answer the questions as follows:

Do you want to change it? [n] y
..
Supply a decimal value for "size" [16776522] 2G
(fdisk asked for Try again due to invalid decimal number)
Supply a decimal value for "size" [16776522] 4194304 
..
Corrent this automatically? [n] y
fdisk: WARNING: adjusting size of partition to 24193827
(not espected value: 4193847 (4193910-63) )

Don't exit fdisk and repeat steps aforesaid, 
the size value of the slice would be as espected.

fdisk: WARNING: adjusting size of partition to 4193847
>Fix:


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->patched 
State-Changed-By: ae 
State-Changed-When: Sun Nov 6 21:13:20 UTC 2011 
State-Changed-Why:  
Fixed in head/. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162262 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: misc/162262: commit references a PR
Date: Sun,  6 Nov 2011 21:13:06 +0000 (UTC)

 Author: ae
 Date: Sun Nov  6 21:12:52 2011
 New Revision: 227280
 URL: http://svn.freebsd.org/changeset/base/227280
 
 Log:
   Initialize "acc" value inside the loop to reset failed attempts.
   
   PR:		misc/162262
   MFC after:	3 days
 
 Modified:
   head/sbin/fdisk/fdisk.c
 
 Modified: head/sbin/fdisk/fdisk.c
 ==============================================================================
 --- head/sbin/fdisk/fdisk.c	Sun Nov  6 21:11:22 2011	(r227279)
 +++ head/sbin/fdisk/fdisk.c	Sun Nov  6 21:12:52 2011	(r227280)
 @@ -922,11 +922,12 @@ ok(const char *str)
  static int
  decimal(const char *str, int *num, int deflt, uint32_t maxval)
  {
 -	long long acc = 0;
 +	long long acc;
  	int c;
  	char *cp;
  
  	while (1) {
 +		acc = 0;
  		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
  		fflush(stdout);
  		if (fgets(lbuf, LBUF, stdin) == NULL)
 @@ -962,7 +963,6 @@ decimal(const char *str, int *num, int d
  			printf("%s is an invalid decimal number.  Try again.\n",
  				lbuf);
  	}
 -
  }
  
  
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/162262: commit references a PR
Date: Wed, 16 Nov 2011 15:37:02 +0000 (UTC)

 Author: ae
 Date: Wed Nov 16 15:36:53 2011
 New Revision: 227560
 URL: http://svn.freebsd.org/changeset/base/227560
 
 Log:
   MFC r227272:
     Add reference to gpart(8).
   
   MFC r227280:
     Initialize "acc" value inside the loop to reset failed attempts.
   
     PR:           misc/162262
   
   MFC r227292:
     Improve error reporting when MBR can not be written.
     Remove obsolete code which uses DIOCSMBR ioctl.
     When writing MBR first check that GEOM_MBR is available, if it is not
     available, then try write MBR directly to provider. If both are failed,
     then recommend to use gpart(8).
   
   MFC r227295:
     Fix multi-line comment formatting.
   
     Pointed by:   jh
   
   Approved by:	re (kib)
 
 Modified:
   stable/9/sbin/fdisk/fdisk.8
   stable/9/sbin/fdisk/fdisk.c
 Directory Properties:
   stable/9/sbin/fdisk/   (props changed)
 
 Modified: stable/9/sbin/fdisk/fdisk.8
 ==============================================================================
 --- stable/9/sbin/fdisk/fdisk.8	Wed Nov 16 15:35:22 2011	(r227559)
 +++ stable/9/sbin/fdisk/fdisk.8	Wed Nov 16 15:36:53 2011	(r227560)
 @@ -476,6 +476,7 @@ The default boot code.
  .Sh SEE ALSO
  .Xr boot0cfg 8 ,
  .Xr bsdlabel 8 ,
 +.Xr gpart 8 ,
  .Xr newfs 8
  .Sh BUGS
  The default boot code will not necessarily handle all slice types
 
 Modified: stable/9/sbin/fdisk/fdisk.c
 ==============================================================================
 --- stable/9/sbin/fdisk/fdisk.c	Wed Nov 16 15:35:22 2011	(r227559)
 +++ stable/9/sbin/fdisk/fdisk.c	Wed Nov 16 15:36:53 2011	(r227560)
 @@ -232,6 +232,7 @@ get_type(int t)
  }
  
  
 +static int geom_class_available(const char *);
  static void print_s0(void);
  static void print_part(const struct dos_partition *);
  static void init_sector0(unsigned long start);
 @@ -767,49 +768,76 @@ read_disk(off_t sector, void *buf)
  }
  
  static int
 -write_disk(off_t sector, void *buf)
 +geom_class_available(const char *name)
  {
 +	struct gclass *class;
 +	struct gmesh mesh;
  	int error;
 +
 +	error = geom_gettree(&mesh);
 +	if (error != 0)
 +		errc(1, error, "Cannot get GEOM tree");
 +
 +	LIST_FOREACH(class, &mesh.lg_class, lg_class) {
 +		if (strcmp(class->lg_name, name) == 0) {
 +			geom_deletetree(&mesh);
 +			return (1);
 +		}
 +	}
 +
 +	geom_deletetree(&mesh);
 +
 +	return (0);
 +}
 +
 +static int
 +write_disk(off_t sector, void *buf)
 +{
  	struct gctl_req *grq;
  	const char *errmsg;
 -	char fbuf[BUFSIZ], *pname;
 -	int i, fdw;
 +	char *pname;
 +	int error;
  
 -	grq = gctl_get_handle();
 -	gctl_ro_param(grq, "verb", -1, "write MBR");
 -	gctl_ro_param(grq, "class", -1, "MBR");
 -	pname = g_providername(fd);
 -	if (pname == NULL) {
 -		warn("Error getting providername for %s", disk);
 -		return (-1);
 -	}
 -	gctl_ro_param(grq, "geom", -1, pname);
 -	gctl_ro_param(grq, "data", secsize, buf);
 -	errmsg = gctl_issue(grq);
 -	free(pname);
 -	if (errmsg == NULL) {
 +	/* Check that GEOM_MBR is available */
 +	if (geom_class_available("MBR") != 0) {
 +		grq = gctl_get_handle();
 +		gctl_ro_param(grq, "verb", -1, "write MBR");
 +		gctl_ro_param(grq, "class", -1, "MBR");
 +		pname = g_providername(fd);
 +		if (pname == NULL) {
 +			warn("Error getting providername for %s", disk);
 +			return (-1);
 +		}
 +		gctl_ro_param(grq, "geom", -1, pname);
 +		gctl_ro_param(grq, "data", secsize, buf);
 +		errmsg = gctl_issue(grq);
 +		free(pname);
 +		if (errmsg == NULL) {
 +			gctl_free(grq);
 +			return(0);
 +		}
 +		if (!q_flag)
 +			warnx("GEOM_MBR: %s", errmsg);
  		gctl_free(grq);
 -		return(0);
 -	}
 -	if (!q_flag)	/* GEOM errors are benign, not all devices supported */
 -		warnx("%s", errmsg);
 -	gctl_free(grq);
 -
 -	error = pwrite(fd, buf, secsize, (sector * 512));
 -	if (error == secsize)
 -		return (0);
 -
 -	for (i = 1; i < 5; i++) {
 -		sprintf(fbuf, "%ss%d", disk, i);
 -		fdw = open(fbuf, O_RDWR, 0);
 -		if (fdw < 0)
 -			continue;
 -		error = ioctl(fdw, DIOCSMBR, buf);
 -		close(fdw);
 -		if (error == 0)
 +	} else {
 +		/*
 +		 * Try to write MBR directly. This may help when disk
 +		 * is not in use.
 +		 * XXX: hardcoded sectorsize
 +		 */
 +		error = pwrite(fd, buf, secsize, (sector * 512));
 +		if (error == secsize)
  			return (0);
  	}
 -	warnx("Failed to write sector zero");
 +
 +	/*
 +	 * GEOM_MBR is not available or failed to write MBR.
 +	 * Now check that we have GEOM_PART and recommend to use gpart (8).
 +	 */
 +	if (geom_class_available("PART") != 0)
 +		warnx("Failed to write MBR. Try to use gpart(8).");
 +	else
 +		warnx("Failed to write sector zero");
  	return(EINVAL);
  }
  
 @@ -920,11 +948,12 @@ ok(const char *str)
  static int
  decimal(const char *str, int *num, int deflt, uint32_t maxval)
  {
 -	long long acc = 0;
 +	long long acc;
  	int c;
  	char *cp;
  
  	while (1) {
 +		acc = 0;
  		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
  		fflush(stdout);
  		if (fgets(lbuf, LBUF, stdin) == NULL)
 @@ -960,7 +989,6 @@ decimal(const char *str, int *num, int d
  			printf("%s is an invalid decimal number.  Try again.\n",
  				lbuf);
  	}
 -
  }
  
  
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/162262: commit references a PR
Date: Wed, 16 Nov 2011 15:37:22 +0000 (UTC)

 Author: ae
 Date: Wed Nov 16 15:37:13 2011
 New Revision: 227561
 URL: http://svn.freebsd.org/changeset/base/227561
 
 Log:
   MFC r227272:
     Add reference to gpart(8).
   
   MFC r227280:
     Initialize "acc" value inside the loop to reset failed attempts.
   
     PR:           misc/162262
   
   MFC r227292:
     Improve error reporting when MBR can not be written.
     Remove obsolete code which uses DIOCSMBR ioctl.
     When writing MBR first check that GEOM_MBR is available, if it is not
     available, then try write MBR directly to provider. If both are failed,
     then recommend to use gpart(8).
   
   MFC r227295:
     Fix multi-line comment formatting.
   
     Pointed by:   jh
   
   Approved by:    re (kib)
 
 Modified:
   releng/9.0/sbin/fdisk/fdisk.8
   releng/9.0/sbin/fdisk/fdisk.c
 Directory Properties:
   releng/9.0/sbin/fdisk/   (props changed)
 
 Modified: releng/9.0/sbin/fdisk/fdisk.8
 ==============================================================================
 --- releng/9.0/sbin/fdisk/fdisk.8	Wed Nov 16 15:36:53 2011	(r227560)
 +++ releng/9.0/sbin/fdisk/fdisk.8	Wed Nov 16 15:37:13 2011	(r227561)
 @@ -476,6 +476,7 @@ The default boot code.
  .Sh SEE ALSO
  .Xr boot0cfg 8 ,
  .Xr bsdlabel 8 ,
 +.Xr gpart 8 ,
  .Xr newfs 8
  .Sh BUGS
  The default boot code will not necessarily handle all slice types
 
 Modified: releng/9.0/sbin/fdisk/fdisk.c
 ==============================================================================
 --- releng/9.0/sbin/fdisk/fdisk.c	Wed Nov 16 15:36:53 2011	(r227560)
 +++ releng/9.0/sbin/fdisk/fdisk.c	Wed Nov 16 15:37:13 2011	(r227561)
 @@ -232,6 +232,7 @@ get_type(int t)
  }
  
  
 +static int geom_class_available(const char *);
  static void print_s0(void);
  static void print_part(const struct dos_partition *);
  static void init_sector0(unsigned long start);
 @@ -767,49 +768,76 @@ read_disk(off_t sector, void *buf)
  }
  
  static int
 -write_disk(off_t sector, void *buf)
 +geom_class_available(const char *name)
  {
 +	struct gclass *class;
 +	struct gmesh mesh;
  	int error;
 +
 +	error = geom_gettree(&mesh);
 +	if (error != 0)
 +		errc(1, error, "Cannot get GEOM tree");
 +
 +	LIST_FOREACH(class, &mesh.lg_class, lg_class) {
 +		if (strcmp(class->lg_name, name) == 0) {
 +			geom_deletetree(&mesh);
 +			return (1);
 +		}
 +	}
 +
 +	geom_deletetree(&mesh);
 +
 +	return (0);
 +}
 +
 +static int
 +write_disk(off_t sector, void *buf)
 +{
  	struct gctl_req *grq;
  	const char *errmsg;
 -	char fbuf[BUFSIZ], *pname;
 -	int i, fdw;
 +	char *pname;
 +	int error;
  
 -	grq = gctl_get_handle();
 -	gctl_ro_param(grq, "verb", -1, "write MBR");
 -	gctl_ro_param(grq, "class", -1, "MBR");
 -	pname = g_providername(fd);
 -	if (pname == NULL) {
 -		warn("Error getting providername for %s", disk);
 -		return (-1);
 -	}
 -	gctl_ro_param(grq, "geom", -1, pname);
 -	gctl_ro_param(grq, "data", secsize, buf);
 -	errmsg = gctl_issue(grq);
 -	free(pname);
 -	if (errmsg == NULL) {
 +	/* Check that GEOM_MBR is available */
 +	if (geom_class_available("MBR") != 0) {
 +		grq = gctl_get_handle();
 +		gctl_ro_param(grq, "verb", -1, "write MBR");
 +		gctl_ro_param(grq, "class", -1, "MBR");
 +		pname = g_providername(fd);
 +		if (pname == NULL) {
 +			warn("Error getting providername for %s", disk);
 +			return (-1);
 +		}
 +		gctl_ro_param(grq, "geom", -1, pname);
 +		gctl_ro_param(grq, "data", secsize, buf);
 +		errmsg = gctl_issue(grq);
 +		free(pname);
 +		if (errmsg == NULL) {
 +			gctl_free(grq);
 +			return(0);
 +		}
 +		if (!q_flag)
 +			warnx("GEOM_MBR: %s", errmsg);
  		gctl_free(grq);
 -		return(0);
 -	}
 -	if (!q_flag)	/* GEOM errors are benign, not all devices supported */
 -		warnx("%s", errmsg);
 -	gctl_free(grq);
 -
 -	error = pwrite(fd, buf, secsize, (sector * 512));
 -	if (error == secsize)
 -		return (0);
 -
 -	for (i = 1; i < 5; i++) {
 -		sprintf(fbuf, "%ss%d", disk, i);
 -		fdw = open(fbuf, O_RDWR, 0);
 -		if (fdw < 0)
 -			continue;
 -		error = ioctl(fdw, DIOCSMBR, buf);
 -		close(fdw);
 -		if (error == 0)
 +	} else {
 +		/*
 +		 * Try to write MBR directly. This may help when disk
 +		 * is not in use.
 +		 * XXX: hardcoded sectorsize
 +		 */
 +		error = pwrite(fd, buf, secsize, (sector * 512));
 +		if (error == secsize)
  			return (0);
  	}
 -	warnx("Failed to write sector zero");
 +
 +	/*
 +	 * GEOM_MBR is not available or failed to write MBR.
 +	 * Now check that we have GEOM_PART and recommend to use gpart (8).
 +	 */
 +	if (geom_class_available("PART") != 0)
 +		warnx("Failed to write MBR. Try to use gpart(8).");
 +	else
 +		warnx("Failed to write sector zero");
  	return(EINVAL);
  }
  
 @@ -920,11 +948,12 @@ ok(const char *str)
  static int
  decimal(const char *str, int *num, int deflt, uint32_t maxval)
  {
 -	long long acc = 0;
 +	long long acc;
  	int c;
  	char *cp;
  
  	while (1) {
 +		acc = 0;
  		printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
  		fflush(stdout);
  		if (fgets(lbuf, LBUF, stdin) == NULL)
 @@ -960,7 +989,6 @@ decimal(const char *str, int *num, int d
  			printf("%s is an invalid decimal number.  Try again.\n",
  				lbuf);
  	}
 -
  }
  
  
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: ae 
State-Changed-When: Wed Nov 16 16:59:26 UTC 2011 
State-Changed-Why:  
Merged to stable/9 and releng/9.0. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=162262 
>Unformatted:
