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