From owner-freebsd-hackers Mon Mar 12 23:17:51 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from bazooka.unixfreak.org (bazooka.unixfreak.org [63.198.170.138]) by hub.freebsd.org (Postfix) with ESMTP id 296E437B71A for ; Mon, 12 Mar 2001 23:17:45 -0800 (PST) (envelope-from dima@unixfreak.org) Received: from hornet.unixfreak.org (hornet [63.198.170.140]) by bazooka.unixfreak.org (Postfix) with ESMTP id C988C3E09 for ; Mon, 12 Mar 2001 23:17:44 -0800 (PST) To: hackers@freebsd.org Subject: Patch to fix panic when detaching a mounted md device Date: Mon, 12 Mar 2001 23:17:44 -0800 From: Dima Dorfman Message-Id: <20010313071744.C988C3E09@bazooka.unixfreak.org> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Hello -hackers Right now, if you try to detach an md device that's currently mounted, you will get a panic (maybe not immedietely, but it will come, esp. if you ever try to use that mountpoint again). I'm pretty sure this is a known problem, with the solution--or workaround--being, "then don't do that." The problem is, someone will invariably do it. The fix is to make md complain if you try to detach a device that's currently opened. Below is a patch that implements this by keeping track of the number of times a device has been opened. Comments? Thanks Dima Dorfman dima@unixfreak.org Index: md.c =================================================================== RCS file: /st/src/FreeBSD/src/sys/dev/md/md.c,v retrieving revision 1.26 diff -u -r1.26 md.c --- md.c 2001/03/09 20:06:30 1.26 +++ md.c 2001/03/13 07:04:43 @@ -122,11 +122,12 @@ static d_strategy_t mdstrategy; static d_open_t mdopen; +static d_close_t mdclose; static d_ioctl_t mdioctl, mdctlioctl; static struct cdevsw md_cdevsw = { /* open */ mdopen, - /* close */ nullclose, + /* close */ mdclose, /* read */ physread, /* write */ physwrite, /* ioctl */ mdioctl, @@ -169,6 +170,7 @@ unsigned nsect; unsigned secsize; unsigned flags; + unsigned opencnt; /* MD_MALLOC related fields */ u_char **secp; @@ -205,6 +207,18 @@ dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks; dl->d_secperunit = sc->nsect; dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl; + sc->opencnt++; + return (0); +} + +static int +mdclose(dev_t dev, int flag, int fmt, struct proc *p) +{ + struct md_s *sc; + + sc = dev->si_drv1; + KASSERT(sc->opencnt > 0, ("md: closing an unopened device")); + sc->opencnt--; return (0); } @@ -824,6 +838,8 @@ sc = mdfind(mdio->md_unit); if (sc == NULL) return (ENOENT); + if (sc->opencnt != 0) + return (EBUSY); switch(sc->type) { case MD_VNODE: case MD_SWAP: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message