1 /* $NetBSD: ata_raid_subr.c,v 1.4 2022/03/19 13:51:01 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.4 2022/03/19 13:51:01 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 *
ata_raid_disk_vnode_find(struct ataraid_disk_info * adi)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 = kmem_zalloc(sizeof(struct ataraid_disk_vnode), KM_SLEEP);
76
77 bmajor = devsw_name2blk(device_xname(adi->adi_dev), NULL, 0);
78 dev = MAKEDISKDEV(bmajor, device_unit(adi->adi_dev), RAW_PART);
79
80 error = bdevvp(dev, &vp);
81 if (error) {
82 kmem_free(adv, sizeof(struct ataraid_disk_vnode));
83 return NULL;
84 }
85 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
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 VOP_UNLOCK(vp);
93
94 adv->adv_adi = adi;
95 adv->adv_vnode = vp;
96
97 SLIST_INSERT_HEAD(&ataraid_disk_vnode_list, adv, adv_next);
98
99 return adv->adv_vnode;
100 }
101