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