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