From owner-svn-src-all@FreeBSD.ORG Thu Jul 22 10:24:28 2010 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D0E481065676; Thu, 22 Jul 2010 10:24:28 +0000 (UTC) (envelope-from jh@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id A67648FC17; Thu, 22 Jul 2010 10:24:28 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o6MAOSub016474; Thu, 22 Jul 2010 10:24:28 GMT (envelope-from jh@svn.freebsd.org) Received: (from jh@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o6MAOSsr016472; Thu, 22 Jul 2010 10:24:28 GMT (envelope-from jh@svn.freebsd.org) Message-Id: <201007221024.o6MAOSsr016472@svn.freebsd.org> From: Jaakko Heinonen Date: Thu, 22 Jul 2010 10:24:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r210371 - head/sys/dev/md X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Jul 2010 10:24:28 -0000 Author: jh Date: Thu Jul 22 10:24:28 2010 New Revision: 210371 URL: http://svn.freebsd.org/changeset/base/210371 Log: Convert md(4) to use alloc_unr(9) and alloc_unr_specific(9) for unit number allocation. The old approach had some problems such as it allowed an overflow to occur in the unit number calculation. PR: kern/122288 Modified: head/sys/dev/md/md.c Modified: head/sys/dev/md/md.c ============================================================================== --- head/sys/dev/md/md.c Thu Jul 22 09:14:18 2010 (r210370) +++ head/sys/dev/md/md.c Thu Jul 22 10:24:28 2010 (r210371) @@ -130,6 +130,7 @@ static void g_md_dumpconf(struct sbuf *s static int mdunits; static struct cdev *status_dev = 0; static struct sx md_sx; +static struct unrhdr *md_uh; static d_ioctl_t mdctlioctl; @@ -748,20 +749,20 @@ mdfind(int unit) static struct md_s * mdnew(int unit, int *errp, enum md_types type) { - struct md_s *sc, *sc2; - int error, max = -1; + struct md_s *sc; + int error; *errp = 0; - LIST_FOREACH(sc2, &md_softc_list, list) { - if (unit == sc2->unit) { - *errp = EBUSY; - return (NULL); - } - if (unit == -1 && sc2->unit > max) - max = sc2->unit; - } if (unit == -1) - unit = max + 1; + unit = alloc_unr(md_uh); + else + unit = alloc_unr_specific(md_uh, unit); + + if (unit == -1) { + *errp = EBUSY; + return (NULL); + } + sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO); sc->type = type; bioq_init(&sc->bio_queue); @@ -774,6 +775,7 @@ mdnew(int unit, int *errp, enum md_types return (sc); LIST_REMOVE(sc, list); mtx_destroy(&sc->queue_mtx); + free_unr(md_uh, sc->unit); free(sc, M_MD); *errp = error; return (NULL); @@ -1012,6 +1014,7 @@ mddestroy(struct md_s *sc, struct thread uma_zdestroy(sc->uma); LIST_REMOVE(sc, list); + free_unr(md_uh, sc->unit); free(sc, M_MD); return (0); } @@ -1097,8 +1100,11 @@ xmdctlioctl(struct cdev *dev, u_long cmd } if (mdio->md_options & MD_AUTOUNIT) sc = mdnew(-1, &error, mdio->md_type); - else + else { + if (mdio->md_unit > INT_MAX) + return (EINVAL); sc = mdnew(mdio->md_unit, &error, mdio->md_type); + } if (sc == NULL) return (error); if (mdio->md_options & MD_AUTOUNIT) @@ -1226,6 +1232,7 @@ g_md_init(struct g_class *mp __unused) mod = NULL; sx_init(&md_sx, "MD config lock"); g_topology_unlock(); + md_uh = new_unrhdr(0, INT_MAX, NULL); #ifdef MD_ROOT_SIZE sx_xlock(&md_sx); md_preloaded(mfs_root.start, sizeof(mfs_root.start)); @@ -1322,4 +1329,5 @@ g_md_fini(struct g_class *mp __unused) sx_destroy(&md_sx); if (status_dev != NULL) destroy_dev(status_dev); + delete_unrhdr(md_uh); }