Bug 167567

Summary: gpart(8) -a alignment silently overrides -b beginning option
Product: Base System Reporter: Warren Block <wblock>
Component: binAssignee: Andrey V. Elsukov <ae>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
gpart_align_start.diff none

Description Warren Block 2012-05-03 22:50:05 UTC
When both the -a and -b options are given, -a overrides the value given by -b.

gpart add -t freebsd-ufs -a4k -b1m -s2g ada0

adds a partition that starts at the next available even multiple of 4k rather than at 1M.  If the -b value is an even multiple of the -a value, it should be used.  Otherwise, an error should be shown.

Fix: 

If a -b value is given, compare it to the alignment value.  Use if it matches the alignment, otherwise error out.
How-To-Repeat: gpart create -s gpt ada0
gpart add -t freebsd-boot -a4k -s512k ada0
gpart add -t freebsd-ufs -a4k -b1m -s2g ada0
gpart show ada0
=>      34  42680253  ada0  GPT  (20G)
        34         6        - free -  (3.0k)
        40      1024     1  freebsd-boot  (512k)
      1064   4194304     2  freebsd-ufs  (2.0G)
   4195368  38484919        - free -  (18G)

The freebsd-ufs partition should start at 2048, or 1M.
Comment 1 Eitan Adler freebsd_committer freebsd_triage 2012-05-04 01:49:05 UTC
Responsible Changed
From-To: freebsd-bugs->eadler

I'll take it.
Comment 2 Andrey V. Elsukov freebsd_committer freebsd_triage 2012-05-04 05:24:58 UTC
On 04.05.2012 1:47, Warren Block wrote:
>> Description:
> When both the -a and -b options are given, -a overrides the value given by -b.
> 
> gpart add -t freebsd-ufs -a4k -b1m -s2g ada0
> 
> adds a partition that starts at the next available even multiple of 4k rather than at 1M.  If the -b value is an even multiple of the -a value, it should be used.  Otherwise, an error should be shown.
>> How-To-Repeat:
> gpart create -s gpt ada0
> gpart add -t freebsd-boot -a4k -s512k ada0
> gpart add -t freebsd-ufs -a4k -b1m -s2g ada0
> gpart show ada0
> =>      34  42680253  ada0  GPT  (20G)
>         34         6        - free -  (3.0k)
>         40      1024     1  freebsd-boot  (512k)
>       1064   4194304     2  freebsd-ufs  (2.0G)
>    4195368  38484919        - free -  (18G)
> 
> The freebsd-ufs partition should start at 2048, or 1M.

You did request to create 4k aligned partition and gpart did it as you requested.
ada0p2 starts from 1064 sector and it is aligned to 4k. If you want align it to 1m, try this command:
  gpart add -t freebsd-ufs -a1m -b1m -s2g ada0

-- 
WBR, Andrey V. Elsukov
Comment 3 Andrey V. Elsukov freebsd_committer freebsd_triage 2012-05-04 05:27:40 UTC
On 04.05.2012 1:47, Warren Block wrote:
>> Fix:
> If a -b value is given, compare it to the alignment value.  Use if it matches the alignment,
> otherwise error out.

If you don't want align your partition - don't use -a option, just use both -b and -s. :)

-- 
WBR, Andrey V. Elsukov
Comment 4 Warren Block 2012-05-04 14:10:28 UTC
On Fri, 4 May 2012, Andrey V. Elsukov wrote:

> On 04.05.2012 1:47, Warren Block wrote:
>>> Description:
>> When both the -a and -b options are given, -a overrides the value given by -b.
>>
>> gpart add -t freebsd-ufs -a4k -b1m -s2g ada0
>>
>> adds a partition that starts at the next available even multiple of 4k rather than at 1M.  If the -b value is an even multiple of the -a value, it should be used.  Otherwise, an error should be shown.
>>> How-To-Repeat:
>> gpart create -s gpt ada0
>> gpart add -t freebsd-boot -a4k -s512k ada0
>> gpart add -t freebsd-ufs -a4k -b1m -s2g ada0
>> gpart show ada0
>> =>      34  42680253  ada0  GPT  (20G)
>>         34         6        - free -  (3.0k)
>>         40      1024     1  freebsd-boot  (512k)
>>       1064   4194304     2  freebsd-ufs  (2.0G)
>>    4195368  38484919        - free -  (18G)
>>
>> The freebsd-ufs partition should start at 2048, or 1M.
>
> You did request to create 4k aligned partition and gpart did it as you requested.
> ada0p2 starts from 1064 sector and it is aligned to 4k.

The command said "create a 4k aligned partition that begins at 1M", but 
the second part was ignored.  At least, it could say "-a option given, 
-b value ignored".  But more useful to the user, and more consistent 
with the options, would be to use the -b value given if it is an 
even multiple of the alignment value, or error out if it is not.

> If you want align it to 1m, try this command:
>  gpart add -t freebsd-ufs -a1m -b1m -s2g ada0

Thank you, that works.  But that does not give the ability to create an 
aligned partition at an arbitrary (but aligned) starting position.
Comment 5 Andrey V. Elsukov 2012-05-04 14:49:30 UTC
>The command said "create a 4k aligned partition that begins at 1M", but
>the second part was ignored. At least, it could say "-a option given,
>-b value ignored".

Man page says:
-a alignment  If specified, then gpart utility tries to align
              *start offset* and partition *size* to be multiple of
              alignment value.

so i don't think that it does something wrong.

-- 
WBR, Andrey V. Elsukov
Comment 6 Andrey V. Elsukov freebsd_committer freebsd_triage 2012-05-04 15:11:10 UTC
On 04.05.2012 17:49, Andrey V. Elsukov wrote:
>> The command said "create a 4k aligned partition that begins at 1M", but
>> the second part was ignored. At least, it could say "-a option given,
>> -b value ignored".
> 
> Man page says:
> -a alignment  If specified, then gpart utility tries to align
>               *start offset* and partition *size* to be multiple of
>               alignment value.
> 
> so i don't think that it does something wrong.

I understood what do you mean. Can you try this patch?

-- 
WBR, Andrey V. Elsukov
Comment 7 dfilter service freebsd_committer freebsd_triage 2012-05-04 20:49:38 UTC
Author: ae
Date: Fri May  4 19:49:24 2012
New Revision: 235033
URL: http://svn.freebsd.org/changeset/base/235033

Log:
  Don't ignore start offset value when user specifies it together
  with alignment.
  
  PR:		bin/167567
  Tested by:	Warren Block
  MFC after:	1 week

Modified:
  head/sbin/geom/class/part/geom_part.c

Modified: head/sbin/geom/class/part/geom_part.c
==============================================================================
--- head/sbin/geom/class/part/geom_part.c	Fri May  4 19:44:58 2012	(r235032)
+++ head/sbin/geom/class/part/geom_part.c	Fri May  4 19:49:24 2012	(r235033)
@@ -507,6 +507,8 @@ gpart_autofill(struct gctl_req *req)
 	grade = ~0ULL;
 	a_first = ALIGNUP(first + offset, alignment);
 	last = ALIGNDOWN(last + offset, alignment);
+	if (a_first < start)
+		a_first = start;
 	while ((pp = find_provider(gp, first)) != NULL) {
 		s = find_provcfg(pp, "start");
 		lba = (off_t)strtoimax(s, NULL, 0);
@@ -536,7 +538,8 @@ gpart_autofill(struct gctl_req *req)
 
 		s = find_provcfg(pp, "end");
 		first = (off_t)strtoimax(s, NULL, 0) + 1;
-		a_first = ALIGNUP(first + offset, alignment);
+		if (first > a_first)
+			a_first = ALIGNUP(first + offset, alignment);
 	}
 	if (a_first <= last) {
 		/* Free space [first-last] */
_______________________________________________
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"
Comment 8 Eitan Adler freebsd_committer freebsd_triage 2012-05-04 22:47:44 UTC
Responsible Changed
From-To: eadler->ae

over to committer
Comment 9 Andrey V. Elsukov freebsd_committer freebsd_triage 2012-05-05 09:51:18 UTC
State Changed
From-To: open->patched

Patched in head/.
Comment 10 Andrey V. Elsukov freebsd_committer freebsd_triage 2012-05-11 05:05:38 UTC
State Changed
From-To: patched->closed

Merged to stable/8 and stable/9. Thanks!