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