From nobody@FreeBSD.org  Sat Mar 20 13:59:48 2010
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 EB322106566C
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 20 Mar 2010 13:59:48 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id BEC498FC0A
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 20 Mar 2010 13:59:48 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o2KDxm7V076603
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 20 Mar 2010 13:59:48 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o2KDxmu1076602;
	Sat, 20 Mar 2010 13:59:48 GMT
	(envelope-from nobody)
Message-Id: <201003201359.o2KDxmu1076602@www.freebsd.org>
Date: Sat, 20 Mar 2010 13:59:48 GMT
From: Bruce Cran <bruce@cran.org.uk>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [geom][gpart] panic in gpart_ctlreq when unplugging card reader
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         144905
>Category:       kern
>Synopsis:       [geom][geom_part] panic in gpart_ctlreq when unplugging card reader
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-geom
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Mar 20 14:00:10 UTC 2010
>Closed-Date:    Fri Nov 25 04:32:00 UTC 2011
>Last-Modified:  Fri Nov 25 04:32:00 UTC 2011
>Originator:     Bruce Cran
>Release:        9.0-CURRENT
>Organization:
>Environment:
FreeBSD core.draftnet 9.0-CURRENT FreeBSD 9.0-CURRENT #0 r204965M: Wed Mar 10 19:02:41 GMT 2010     brucec@core.draftnet:/usr/obj/usr/src/head/sys/CORE  amd64
>Description:
A crash can occur if a gpart query is being run when a card reader is removed from the computer.

Using an 8GB uSD card partitioned using GPT attached to a card reader, I plugged the card reader into the computer, then the card into the reader. When I unplugged the reader, a panic with the following stack trace occurred:

g_part_ctlreq
g_run_events
g_event_procbody
fork_exit
fork_trampoline


>How-To-Repeat:
It can be reliably triggered by running the attached program in a script with 1 second between each run.
>Fix:


Patch attached with submission follows:

#include <libgeom.h>
#include <stdio.h>
#include <string.h>

int 
main(void)
{
	int		nEntries = 4;
	struct gctl_req *g = gctl_get_handle();
	gctl_ro_param(g, "class", -1, "PART");
	gctl_ro_param(g, "verb", -1, "create");
	gctl_ro_param(g, "provider", -1, "da0");
	gctl_ro_param(g, "scheme", -1, "mbr");
	const char     *err = gctl_issue(g);

	struct gmesh	mesh;
	int		i = geom_gettree(&mesh);
	if (i >= 0) {
		struct gclass  *classp;
		LIST_FOREACH(classp, &(&mesh)->lg_class, lg_class) {
			if (strcmp(classp->lg_name, "DISK") == 0 || strcmp(classp->lg_name, "MD") == 0) {
				struct ggeom   *geomp;
				LIST_FOREACH(geomp, &classp->lg_geom, lg_geom) {
					struct gprovider *providerp;
					if (strncmp(geomp->lg_name, "cd", 2) == 0)
						continue;

					LIST_FOREACH(providerp, &geomp->lg_provider, lg_provider) {
						if (providerp->lg_mediasize > 0)
							printf("GEOM provider %s"
							       " has media size %lluMB\n", providerp->lg_name, providerp->lg_mediasize / 1024 / 1024);
					}
				}
			}
		}
		geom_deletetree(&mesh);
	}
	gctl_free(g);

	return 0;
}


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-geom 
Responsible-Changed-By: brucec 
Responsible-Changed-When: Sat Mar 20 14:03:35 UTC 2010 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=144905 
State-Changed-From-To: open->feedback 
State-Changed-By: ae 
State-Changed-When: Fri Oct 28 20:18:29 UTC 2011 
State-Changed-Why:  
Feedback requested. 

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

From: "Andrey V. Elsukov" <ae@FreeBSD.org>
To: bug-followup@FreeBSD.org, bruce@cran.org.uk
Cc:  
Subject: Re: kern/144905: [geom][geom_part] panic in gpart_ctlreq when unplugging
 card reader
Date: Sat, 29 Oct 2011 00:11:53 +0400

 Hi, Bruce,
 
 Can you reproduce this problem after r226880?
 
 -- 
 WBR, Andrey V. Elsukov
State-Changed-From-To: feedback->closed 
State-Changed-By: ae 
State-Changed-When: Fri Nov 25 04:31:18 UTC 2011 
State-Changed-Why:  
Feedback timeout. I hope the problem is fixed now. 

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