From nobody@FreeBSD.org  Wed May 16 19:59:44 2012
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id B5ED1106566B
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 16 May 2012 19:59:44 +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 A131D8FC0A
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 16 May 2012 19:59:44 +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 q4GJxidb053894
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 16 May 2012 19:59:44 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id q4GJximB053893;
	Wed, 16 May 2012 19:59:44 GMT
	(envelope-from nobody)
Message-Id: <201205161959.q4GJximB053893@red.freebsd.org>
Date: Wed, 16 May 2012 19:59:44 GMT
From: Spencer Minear <spencer_minear@mcafee.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: DIOCGDINFO ioctl does not work on 8.2 file systems
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         167979
>Category:       kern
>Synopsis:       [ufs] DIOCGDINFO ioctl does not work on 8.2 file systems
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-fs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed May 16 20:00:12 UTC 2012
>Closed-Date:    
>Last-Modified:  Mon Jan 13 15:00:00 UTC 2014
>Originator:     Spencer Minear
>Release:        FreeBSD 8.2
>Organization:
McAfee
>Environment:
FreeBSD freebe1.scur.com 8.2-RELEASE FreeBSD 8.2-RELEASE #0: Fri Feb 18 02:24:46 UTC 2011     root@almeida.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
Use of ioctl(fd, DIOCGDINFO, &dl) fails with a 'Inappropriate ioctl for device' message when run on a file system like ad0s1a.  On a similar 7.2 system one receives the expected "Filesystem type 4.2BSD" type of information.

My analysis suggests that the problem is related to changes in the GEOM layer, specifically the introduction of the PART class.  In 7.2 the ioctl ends up getting to the underlying BSD geom layer and obtains the required information.  In 8.3 the ioctl appears to get to a PART entry that has the name of the device, but does not know how to get to the underlying BSD layer to find out the file type.

In addition I've noted that if you have a slice that contains a number of BSD file systems, that are not being used, the device information shows many devices with completely bogus names like /dev/ad0s3aa, /dev/ad0s3ac, etc.  In most cases these names get repeated.  If the file system ad0s3a is mounted then all off the bogus names disappear.  It appears that the mount action will initiate geom spoil processing that cleans up these entries.

The following simple test program was used to show that the ioctl behavior changes between FreeBSD 7.2 and 8.2.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#define FSTYPENAMES
#include <sys/disklabel.h>

int
main(int argc, char** argv)
{

    struct disklabel dl;
    int fd;
    char *dev;
    char *dev_path;
    char p;
    struct stat sb;
    u_char t;
    const char *vfstype;

    if (argc != 2) {
	printf("Usage: tstdioginfo device\n");
	exit(1);
    }

    dev = argv[1];

    asprintf(&dev_path, "/dev/%s", dev);

    if (stat(dev_path, &sb) != 0) {
	err(1, "Failed to stat dev %s\n", dev);
	exit(1);
    }
	
    p = dev[strlen(dev) - 1];

    /* deduce the file system type from the disk label */
    if ((fd = open(dev_path, O_RDONLY)) == -1)
	err(1, "cannot open '%s'", dev_path);

    if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
	(void) close(fd);
	err(1, "DIOCGDIFNO failed on '%s'", dev);
    }
    
    
    if ((p - 'a') >= dl.d_npartitions)
	errx(1, "partition `%s' is not defined on disk", dev);
    
    if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES) 
	errx(1, "partition `%s' is not of a legal vfstype",
	     dev);

    if ((vfstype = fstypenames[t]) == NULL)
	errx(1, "vfstype `%s' on partition `%s' is not supported",
	     fstypenames[t], dev);

    printf("Filesystem type %s\n", vfstype);
    return(0);
}




>How-To-Repeat:
Run the test program 
>Fix:
None Known

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-fs 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Thu May 17 07:12:38 UTC 2012 
Responsible-Changed-Why:  
Over to maintainer(s). 

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

From: Fahad Ahmed <ahmedatnet@gmail.com>
To: bug-followup@FreeBSD.org,
 spencer_minear@mcafee.com
Cc:  
Subject: Re: kern/167979: [ufs] DIOCGDINFO ioctl does not work on 8.2 file systems
Date: Mon, 13 Jan 2014 06:55:37 -0800

 Bug 167979 states that ioctl(fd, DIOCGDINFO, &dl) fails with error: =
 'Inappropriate ioctl for device'. The reason for failure of ioctl call =
 with DIOCGDINFO option is that Makefile used to build kernel image does =
 not have /usr/src/sys/geom/geom_bsd.c and =
 /usr/src/sys/geom/geom_bsd_enc.c files included in it. When these two =
 files are included in the Makefile and information is added for how to =
 make object files of these files and how to link them with kernel image, =
 DIOCGDINFO option works fine when used with ioctl.
 
 
 
 To add these files in Makefile automatically, "/usr/src/sys/conf/files" =
 has to be changed as follows:
 
 
 # diff -u /usr/src/sys/conf/files_org /usr/src/sys/conf/files
 --- /usr/src/sys/conf/files_org 2012-09-20 23:47:07.000000000 -0700
 +++ /usr/src/sys/conf/files     2012-09-20 23:48:04.000000000 -0700
 @@ -2011,8 +2011,8 @@
  geom/eli/pkcs5v2.c             optional geom_eli
  geom/gate/g_gate.c             optional geom_gate
  geom/geom_aes.c                optional geom_aes
 -geom/geom_bsd.c                optional geom_bsd
 -geom/geom_bsd_enc.c            optional geom_bsd
 +geom/geom_bsd.c                standard
 +geom/geom_bsd_enc.c            standard
  geom/geom_ccd.c                optional ccd | geom_ccd
  geom/geom_ctl.c                standard
  geom/geom_dev.c                standard
 
 
 
 After these changes, automatically generated Makefile will have =
 inclusion of  "/usr/src/sys/geom/geom_bsd.c" and =
 "/usr/src/sys/geom/geom_bsd_enc.c" files.
 =20
 
 
 Difference in Makefile generated by making above changes in =
 "/usr/src/sys/conf/files":
 
 # diff -u Makefile ../BUG_FIX/Makefile | grep geom
 -       pseudofs_fileno.o pseudofs_vncache.o pseudofs_vnops.o geom_ctl.o =
 \
 -       geom_dev.o geom_disk.o geom_dump.o geom_event.o geom_io.o \
 -       geom_kern.o geom_slice.o geom_subr.o geom_vfs.o g_label.o \
 +       pseudofs_fileno.o pseudofs_vncache.o pseudofs_vnops.o geom_bsd.o =
 \
 +       geom_bsd_enc.o geom_ctl.o geom_dev.o geom_disk.o geom_dump.o \
 +       geom_event.o geom_io.o geom_kern.o geom_slice.o geom_subr.o \
 +       geom_vfs.o g_label.o g_label_ext2fs.o g_label_iso9660.o \
 -       $S/fs/pseudofs/pseudofs_vnops.c $S/geom/geom_ctl.c \
 -       $S/geom/geom_dev.c $S/geom/geom_disk.c $S/geom/geom_dump.c \
 -       $S/geom/geom_event.c $S/geom/geom_io.c $S/geom/geom_kern.c \
 -       $S/geom/geom_slice.c $S/geom/geom_subr.c $S/geom/geom_vfs.c \
 -       $S/geom/label/g_label.c $S/geom/label/g_label_ext2fs.c \
 -       $S/geom/label/g_label_iso9660.c $S/geom/label/g_label_msdosfs.c =
 \
 -       $S/geom/label/g_label_ntfs.c $S/geom/label/g_label_reiserfs.c \
 -       $S/geom/label/g_label_ufs.c $S/geom/label/g_label_gpt.c \
 -       $S/geom/part/g_part.c $S/geom/part/g_part_bsd.c \
 -       $S/geom/part/g_part_ebr.c $S/geom/part/g_part_gpt.c \
 -       $S/geom/part/g_part_mbr.c $S/isa/isa_common.c $S/isa/isahint.c \
 +       $S/fs/pseudofs/pseudofs_vnops.c $S/geom/geom_bsd.c \
 +       $S/geom/geom_bsd_enc.c $S/geom/geom_ctl.c $S/geom/geom_dev.c \
 +       $S/geom/geom_disk.c $S/geom/geom_dump.c $S/geom/geom_event.c \
 +       $S/geom/geom_io.c $S/geom/geom_kern.c $S/geom/geom_slice.c \
 +       $S/geom/geom_subr.c $S/geom/geom_vfs.c $S/geom/label/g_label.c \
 +       $S/geom/label/g_label_ext2fs.c $S/geom/label/g_label_iso9660.c \
 +       $S/geom/label/g_label_msdosfs.c $S/geom/label/g_label_ntfs.c \
 +       $S/geom/label/g_label_reiserfs.c $S/geom/label/g_label_ufs.c \
 +       $S/geom/label/g_label_gpt.c $S/geom/part/g_part.c \
 +       $S/geom/part/g_part_bsd.c $S/geom/part/g_part_ebr.c \
 +       $S/geom/part/g_part_gpt.c $S/geom/part/g_part_mbr.c \
 +geom_bsd.ln: $S/geom/geom_bsd.c
 +geom_bsd.o: $S/geom/geom_bsd.c
 +geom_bsd_enc.ln: $S/geom/geom_bsd_enc.c
 +geom_bsd_enc.o: $S/geom/geom_bsd_enc.c
  geom_ctl.ln: $S/geom/geom_ctl.c
 
 
 =20
 Fix was tested on FreeBSD 8.3, with the test program provided with the =
 bug. Following are the results:
 
 Before Fix:
 
 # ./bug ad0s1a
 bug: DIOCGDIFNO failed on 'ad0s1a': Inappropriate ioctl for device
 
 
 # ./bug ad0s1b
 bug: DIOCGDIFNO failed on 'ad0s1b': Inappropriate ioctl for device
 
 
 After Fix:
 
 # ./bug ad0s1a
 Filesystem type 4.2BSD
 
 # ./bug ad0s1b
 Filesystem type swap
 
 
 Fahad Ahmed, Engineer
 Dorr H. Clark, advisor
 COEN 284, Operating Systems Case Study
 Santa Clara University
 
>Unformatted:
