1 /* $NetBSD: ata_raid_subr.c,v 1.2 2010/06/24 13:03:08 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 Juan Romero Pardines. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: ata_raid_subr.c,v 1.2 2010/06/24 13:03:08 hannken Exp $"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 #include <sys/dkio.h> 36 #include <sys/disk.h> 37 #include <sys/disklabel.h> 38 #include <sys/fcntl.h> 39 #include <sys/vnode.h> 40 #include <sys/kauth.h> 41 #include <sys/kmem.h> 42 43 #include <dev/ata/ata_raidvar.h> 44 45 struct ataraid_disk_vnode { 46 struct ataraid_disk_info *adv_adi; 47 struct vnode *adv_vnode; 48 SLIST_ENTRY(ataraid_disk_vnode) adv_next; 49 }; 50 51 static SLIST_HEAD(, ataraid_disk_vnode) ataraid_disk_vnode_list = 52 SLIST_HEAD_INITIALIZER(ataraid_disk_vnode_list); 53 54 /* 55 * Finds the RAW_PART vnode of the block device associated with a component 56 * by looking at the singly linked list; otherwise creates, opens and 57 * returns the vnode to the caller. 58 */ 59 struct vnode * 60 ata_raid_disk_vnode_find(struct ataraid_disk_info *adi) 61 { 62 struct ataraid_disk_vnode *adv = NULL; 63 struct vnode *vp = NULL; 64 device_t devlist; 65 int bmajor, error = 0; 66 dev_t dev; 67 68 SLIST_FOREACH(adv, &ataraid_disk_vnode_list, adv_next) { 69 devlist = adv->adv_adi->adi_dev; 70 if (strcmp(device_xname(devlist), 71 device_xname(adi->adi_dev)) == 0) 72 return adv->adv_vnode; 73 } 74 75 adv = NULL; 76 adv = kmem_zalloc(sizeof(struct ataraid_disk_vnode), KM_SLEEP); 77 78 bmajor = devsw_name2blk(device_xname(adi->adi_dev), NULL, 0); 79 dev = MAKEDISKDEV(bmajor, device_unit(adi->adi_dev), RAW_PART); 80 81 error = bdevvp(dev, &vp); 82 if (error) { 83 kmem_free(adv, sizeof(struct ataraid_disk_vnode)); 84 return NULL; 85 } 86 error = VOP_OPEN(vp, FREAD|FWRITE, NOCRED); 87 if (error) { 88 vput(vp); 89 kmem_free(adv, sizeof(struct ataraid_disk_vnode)); 90 return NULL; 91 } 92 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 93 VOP_UNLOCK(vp); 94 95 adv->adv_adi = adi; 96 adv->adv_vnode = vp; 97 98 SLIST_INSERT_HEAD(&ataraid_disk_vnode_list, adv, adv_next); 99 100 return adv->adv_vnode; 101 } 102