Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * osd_uld.c - OSD Upper Layer Driver |
| 3 | * |
| 4 | * A Linux driver module that registers as a SCSI ULD and probes |
| 5 | * for OSD type SCSI devices. |
| 6 | * It's main function is to export osd devices to in-kernel users like |
| 7 | * osdfs and pNFS-objects-LD. It also provides one ioctl for running |
| 8 | * in Kernel tests. |
| 9 | * |
| 10 | * Copyright (C) 2008 Panasas Inc. All rights reserved. |
| 11 | * |
| 12 | * Authors: |
Boaz Harrosh | aa281ac | 2014-10-19 19:38:58 +0300 | [diff] [blame] | 13 | * Boaz Harrosh <ooo@electrozaur.com> |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 14 | * Benny Halevy <bhalevy@panasas.com> |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License version 2 |
| 18 | * |
| 19 | * Redistribution and use in source and binary forms, with or without |
| 20 | * modification, are permitted provided that the following conditions |
| 21 | * are met: |
| 22 | * |
| 23 | * 1. Redistributions of source code must retain the above copyright |
| 24 | * notice, this list of conditions and the following disclaimer. |
| 25 | * 2. Redistributions in binary form must reproduce the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer in the |
| 27 | * documentation and/or other materials provided with the distribution. |
| 28 | * 3. Neither the name of the Panasas company nor the names of its |
| 29 | * contributors may be used to endorse or promote products derived |
| 30 | * from this software without specific prior written permission. |
| 31 | * |
| 32 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 33 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 34 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 35 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 36 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 37 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 38 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 39 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 40 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 41 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 42 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 | */ |
| 44 | |
| 45 | #include <linux/namei.h> |
| 46 | #include <linux/cdev.h> |
| 47 | #include <linux/fs.h> |
| 48 | #include <linux/module.h> |
| 49 | #include <linux/device.h> |
| 50 | #include <linux/idr.h> |
| 51 | #include <linux/major.h> |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 52 | #include <linux/file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 54 | |
| 55 | #include <scsi/scsi.h> |
| 56 | #include <scsi/scsi_driver.h> |
| 57 | #include <scsi/scsi_device.h> |
| 58 | #include <scsi/scsi_ioctl.h> |
| 59 | |
| 60 | #include <scsi/osd_initiator.h> |
| 61 | #include <scsi/osd_sec.h> |
| 62 | |
| 63 | #include "osd_debug.h" |
| 64 | |
| 65 | #ifndef TYPE_OSD |
| 66 | # define TYPE_OSD 0x11 |
| 67 | #endif |
| 68 | |
| 69 | #ifndef SCSI_OSD_MAJOR |
| 70 | # define SCSI_OSD_MAJOR 260 |
| 71 | #endif |
Boaz Harrosh | 41f8ad7 | 2012-01-25 21:42:58 +0200 | [diff] [blame] | 72 | #define SCSI_OSD_MAX_MINOR MINORMASK |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 73 | |
| 74 | static const char osd_name[] = "osd"; |
Boaz Harrosh | 41f8ad7 | 2012-01-25 21:42:58 +0200 | [diff] [blame] | 75 | static const char *osd_version_string = "open-osd 0.2.1"; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 76 | |
Boaz Harrosh | aa281ac | 2014-10-19 19:38:58 +0300 | [diff] [blame] | 77 | MODULE_AUTHOR("Boaz Harrosh <ooo@electrozaur.com>"); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 78 | MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko"); |
| 79 | MODULE_LICENSE("GPL"); |
| 80 | MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR); |
| 81 | MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD); |
| 82 | |
| 83 | struct osd_uld_device { |
| 84 | int minor; |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 85 | struct device class_dev; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 86 | struct cdev cdev; |
| 87 | struct osd_dev od; |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 88 | struct osd_dev_info odi; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 89 | struct gendisk *disk; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 92 | struct osd_dev_handle { |
| 93 | struct osd_dev od; |
| 94 | struct file *file; |
| 95 | struct osd_uld_device *oud; |
| 96 | } ; |
| 97 | |
| 98 | static DEFINE_IDA(osd_minor_ida); |
| 99 | |
Boaz Harrosh | 51976a8 | 2012-10-24 14:51:41 -0700 | [diff] [blame] | 100 | /* |
| 101 | * scsi sysfs attribute operations |
| 102 | */ |
| 103 | static ssize_t osdname_show(struct device *dev, struct device_attribute *attr, |
| 104 | char *buf) |
| 105 | { |
| 106 | struct osd_uld_device *ould = container_of(dev, struct osd_uld_device, |
| 107 | class_dev); |
| 108 | return sprintf(buf, "%s\n", ould->odi.osdname); |
| 109 | } |
Greg Kroah-Hartman | 92a3e66 | 2013-07-24 15:05:27 -0700 | [diff] [blame] | 110 | static DEVICE_ATTR_RO(osdname); |
Boaz Harrosh | 51976a8 | 2012-10-24 14:51:41 -0700 | [diff] [blame] | 111 | |
| 112 | static ssize_t systemid_show(struct device *dev, struct device_attribute *attr, |
| 113 | char *buf) |
| 114 | { |
| 115 | struct osd_uld_device *ould = container_of(dev, struct osd_uld_device, |
| 116 | class_dev); |
| 117 | |
| 118 | memcpy(buf, ould->odi.systemid, ould->odi.systemid_len); |
| 119 | return ould->odi.systemid_len; |
| 120 | } |
Greg Kroah-Hartman | 92a3e66 | 2013-07-24 15:05:27 -0700 | [diff] [blame] | 121 | static DEVICE_ATTR_RO(systemid); |
Boaz Harrosh | 51976a8 | 2012-10-24 14:51:41 -0700 | [diff] [blame] | 122 | |
Greg Kroah-Hartman | 92a3e66 | 2013-07-24 15:05:27 -0700 | [diff] [blame] | 123 | static struct attribute *osd_uld_attrs[] = { |
| 124 | &dev_attr_osdname.attr, |
| 125 | &dev_attr_systemid.attr, |
| 126 | NULL, |
Boaz Harrosh | 51976a8 | 2012-10-24 14:51:41 -0700 | [diff] [blame] | 127 | }; |
Greg Kroah-Hartman | 92a3e66 | 2013-07-24 15:05:27 -0700 | [diff] [blame] | 128 | ATTRIBUTE_GROUPS(osd_uld); |
Boaz Harrosh | 51976a8 | 2012-10-24 14:51:41 -0700 | [diff] [blame] | 129 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 130 | static struct class osd_uld_class = { |
| 131 | .owner = THIS_MODULE, |
| 132 | .name = "scsi_osd", |
Greg Kroah-Hartman | 92a3e66 | 2013-07-24 15:05:27 -0700 | [diff] [blame] | 133 | .dev_groups = osd_uld_groups, |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 134 | }; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Char Device operations |
| 138 | */ |
| 139 | |
| 140 | static int osd_uld_open(struct inode *inode, struct file *file) |
| 141 | { |
| 142 | struct osd_uld_device *oud = container_of(inode->i_cdev, |
| 143 | struct osd_uld_device, cdev); |
| 144 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 145 | get_device(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 146 | /* cache osd_uld_device on file handle */ |
| 147 | file->private_data = oud; |
| 148 | OSD_DEBUG("osd_uld_open %p\n", oud); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int osd_uld_release(struct inode *inode, struct file *file) |
| 153 | { |
| 154 | struct osd_uld_device *oud = file->private_data; |
| 155 | |
| 156 | OSD_DEBUG("osd_uld_release %p\n", file->private_data); |
| 157 | file->private_data = NULL; |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 158 | put_device(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* FIXME: Only one vector for now */ |
| 163 | unsigned g_test_ioctl; |
| 164 | do_test_fn *g_do_test; |
| 165 | |
| 166 | int osduld_register_test(unsigned ioctl, do_test_fn *do_test) |
| 167 | { |
| 168 | if (g_test_ioctl) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | g_test_ioctl = ioctl; |
| 172 | g_do_test = do_test; |
| 173 | return 0; |
| 174 | } |
| 175 | EXPORT_SYMBOL(osduld_register_test); |
| 176 | |
| 177 | void osduld_unregister_test(unsigned ioctl) |
| 178 | { |
| 179 | if (ioctl == g_test_ioctl) { |
| 180 | g_test_ioctl = 0; |
| 181 | g_do_test = NULL; |
| 182 | } |
| 183 | } |
| 184 | EXPORT_SYMBOL(osduld_unregister_test); |
| 185 | |
| 186 | static do_test_fn *_find_ioctl(unsigned cmd) |
| 187 | { |
| 188 | if (g_test_ioctl == cmd) |
| 189 | return g_do_test; |
| 190 | else |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | static long osd_uld_ioctl(struct file *file, unsigned int cmd, |
| 195 | unsigned long arg) |
| 196 | { |
| 197 | struct osd_uld_device *oud = file->private_data; |
| 198 | int ret; |
| 199 | do_test_fn *do_test; |
| 200 | |
| 201 | do_test = _find_ioctl(cmd); |
| 202 | if (do_test) |
| 203 | ret = do_test(&oud->od, cmd, arg); |
| 204 | else { |
| 205 | OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud); |
| 206 | ret = -ENOIOCTLCMD; |
| 207 | } |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static const struct file_operations osd_fops = { |
| 212 | .owner = THIS_MODULE, |
| 213 | .open = osd_uld_open, |
| 214 | .release = osd_uld_release, |
| 215 | .unlocked_ioctl = osd_uld_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 216 | .llseek = noop_llseek, |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 219 | struct osd_dev *osduld_path_lookup(const char *name) |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 220 | { |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 221 | struct osd_uld_device *oud; |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 222 | struct osd_dev_handle *odh; |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 223 | struct file *file; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 224 | int error; |
| 225 | |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 226 | if (!name || !*name) { |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 227 | OSD_ERR("Mount with !path || !*path\n"); |
| 228 | return ERR_PTR(-EINVAL); |
| 229 | } |
| 230 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 231 | odh = kzalloc(sizeof(*odh), GFP_KERNEL); |
| 232 | if (unlikely(!odh)) |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 233 | return ERR_PTR(-ENOMEM); |
| 234 | |
| 235 | file = filp_open(name, O_RDWR, 0); |
| 236 | if (IS_ERR(file)) { |
| 237 | error = PTR_ERR(file); |
| 238 | goto free_od; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 241 | if (file->f_op != &osd_fops){ |
| 242 | error = -EINVAL; |
| 243 | goto close_file; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 246 | oud = file->private_data; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 247 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 248 | odh->od = oud->od; |
| 249 | odh->file = file; |
| 250 | odh->oud = oud; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 251 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 252 | return &odh->od; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 253 | |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 254 | close_file: |
| 255 | fput(file); |
| 256 | free_od: |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 257 | kfree(odh); |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 258 | return ERR_PTR(error); |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 259 | } |
| 260 | EXPORT_SYMBOL(osduld_path_lookup); |
| 261 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 262 | static inline bool _the_same_or_null(const u8 *a1, unsigned a1_len, |
| 263 | const u8 *a2, unsigned a2_len) |
| 264 | { |
| 265 | if (!a2_len) /* User string is Empty means don't care */ |
| 266 | return true; |
| 267 | |
| 268 | if (a1_len != a2_len) |
| 269 | return false; |
| 270 | |
| 271 | return 0 == memcmp(a1, a2, a1_len); |
| 272 | } |
| 273 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 274 | static int _match_odi(struct device *dev, const void *find_data) |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 275 | { |
| 276 | struct osd_uld_device *oud = container_of(dev, struct osd_uld_device, |
| 277 | class_dev); |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 278 | const struct osd_dev_info *odi = find_data; |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 279 | |
| 280 | if (_the_same_or_null(oud->odi.systemid, oud->odi.systemid_len, |
| 281 | odi->systemid, odi->systemid_len) && |
| 282 | _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len, |
| 283 | odi->osdname, odi->osdname_len)) { |
| 284 | OSD_DEBUG("found device sysid_len=%d osdname=%d\n", |
| 285 | odi->systemid_len, odi->osdname_len); |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 286 | return 1; |
| 287 | } else { |
| 288 | return 0; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* osduld_info_lookup - Loop through all devices, return the requested osd_dev. |
| 293 | * |
| 294 | * if @odi->systemid_len and/or @odi->osdname_len are zero, they act as a don't |
| 295 | * care. .e.g if they're both zero /dev/osd0 is returned. |
| 296 | */ |
| 297 | struct osd_dev *osduld_info_lookup(const struct osd_dev_info *odi) |
| 298 | { |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 299 | struct device *dev = class_find_device(&osd_uld_class, NULL, odi, _match_odi); |
| 300 | if (likely(dev)) { |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 301 | struct osd_dev_handle *odh = kzalloc(sizeof(*odh), GFP_KERNEL); |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 302 | struct osd_uld_device *oud = container_of(dev, |
| 303 | struct osd_uld_device, class_dev); |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 304 | |
| 305 | if (unlikely(!odh)) { |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 306 | put_device(dev); |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 307 | return ERR_PTR(-ENOMEM); |
| 308 | } |
| 309 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 310 | odh->od = oud->od; |
| 311 | odh->oud = oud; |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 312 | |
| 313 | return &odh->od; |
| 314 | } |
| 315 | |
| 316 | return ERR_PTR(-ENODEV); |
| 317 | } |
| 318 | EXPORT_SYMBOL(osduld_info_lookup); |
| 319 | |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 320 | void osduld_put_device(struct osd_dev *od) |
| 321 | { |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 322 | if (od && !IS_ERR(od)) { |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 323 | struct osd_dev_handle *odh = |
| 324 | container_of(od, struct osd_dev_handle, od); |
| 325 | struct osd_uld_device *oud = odh->oud; |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 326 | |
| 327 | BUG_ON(od->scsi_device != oud->od.scsi_device); |
| 328 | |
Boaz Harrosh | 89f5e1f | 2009-11-16 20:44:02 +0200 | [diff] [blame] | 329 | /* If scsi has released the device (logout), and exofs has last |
| 330 | * reference on oud it will be freed by above osd_uld_release |
| 331 | * within fput below. But this will oops in cdev_release which |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 332 | * is called after the fops->release. A get_/put_ pair makes |
Boaz Harrosh | 89f5e1f | 2009-11-16 20:44:02 +0200 | [diff] [blame] | 333 | * sure we have a cdev for the duration of fput |
| 334 | */ |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 335 | if (odh->file) { |
| 336 | get_device(&oud->class_dev); |
| 337 | fput(odh->file); |
| 338 | } |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 339 | put_device(&oud->class_dev); |
| 340 | kfree(odh); |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | EXPORT_SYMBOL(osduld_put_device); |
| 344 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 345 | const struct osd_dev_info *osduld_device_info(struct osd_dev *od) |
| 346 | { |
| 347 | struct osd_dev_handle *odh = |
| 348 | container_of(od, struct osd_dev_handle, od); |
| 349 | return &odh->oud->odi; |
| 350 | } |
| 351 | EXPORT_SYMBOL(osduld_device_info); |
| 352 | |
| 353 | bool osduld_device_same(struct osd_dev *od, const struct osd_dev_info *odi) |
| 354 | { |
| 355 | struct osd_dev_handle *odh = |
| 356 | container_of(od, struct osd_dev_handle, od); |
| 357 | struct osd_uld_device *oud = odh->oud; |
| 358 | |
| 359 | return (oud->odi.systemid_len == odi->systemid_len) && |
| 360 | _the_same_or_null(oud->odi.systemid, oud->odi.systemid_len, |
| 361 | odi->systemid, odi->systemid_len) && |
| 362 | (oud->odi.osdname_len == odi->osdname_len) && |
| 363 | _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len, |
| 364 | odi->osdname, odi->osdname_len); |
| 365 | } |
| 366 | EXPORT_SYMBOL(osduld_device_same); |
| 367 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 368 | /* |
| 369 | * Scsi Device operations |
| 370 | */ |
| 371 | |
| 372 | static int __detect_osd(struct osd_uld_device *oud) |
| 373 | { |
| 374 | struct scsi_device *scsi_device = oud->od.scsi_device; |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 375 | char caps[OSD_CAP_LEN]; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 376 | int error; |
| 377 | |
| 378 | /* sending a test_unit_ready as first command seems to be needed |
| 379 | * by some targets |
| 380 | */ |
| 381 | OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n", |
| 382 | oud, scsi_device, scsi_device->request_queue); |
| 383 | error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL); |
| 384 | if (error) |
| 385 | OSD_ERR("warning: scsi_test_unit_ready failed\n"); |
| 386 | |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 387 | osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true); |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 388 | if (osd_auto_detect_ver(&oud->od, caps, &oud->odi)) |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 389 | return -ENODEV; |
| 390 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 394 | static void __remove(struct device *dev) |
| 395 | { |
| 396 | struct osd_uld_device *oud = container_of(dev, struct osd_uld_device, |
| 397 | class_dev); |
| 398 | struct scsi_device *scsi_device = oud->od.scsi_device; |
| 399 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 400 | kfree(oud->odi.osdname); |
| 401 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 402 | if (oud->cdev.owner) |
| 403 | cdev_del(&oud->cdev); |
| 404 | |
| 405 | osd_dev_fini(&oud->od); |
| 406 | scsi_device_put(scsi_device); |
| 407 | |
| 408 | OSD_INFO("osd_remove %s\n", |
| 409 | oud->disk ? oud->disk->disk_name : NULL); |
| 410 | |
| 411 | if (oud->disk) |
| 412 | put_disk(oud->disk); |
| 413 | ida_remove(&osd_minor_ida, oud->minor); |
| 414 | |
| 415 | kfree(oud); |
| 416 | } |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 417 | |
| 418 | static int osd_probe(struct device *dev) |
| 419 | { |
| 420 | struct scsi_device *scsi_device = to_scsi_device(dev); |
| 421 | struct gendisk *disk; |
| 422 | struct osd_uld_device *oud; |
| 423 | int minor; |
| 424 | int error; |
| 425 | |
| 426 | if (scsi_device->type != TYPE_OSD) |
| 427 | return -ENODEV; |
| 428 | |
| 429 | do { |
| 430 | if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL)) |
| 431 | return -ENODEV; |
| 432 | |
| 433 | error = ida_get_new(&osd_minor_ida, &minor); |
| 434 | } while (error == -EAGAIN); |
| 435 | |
| 436 | if (error) |
| 437 | return error; |
| 438 | if (minor >= SCSI_OSD_MAX_MINOR) { |
| 439 | error = -EBUSY; |
| 440 | goto err_retract_minor; |
| 441 | } |
| 442 | |
| 443 | error = -ENOMEM; |
| 444 | oud = kzalloc(sizeof(*oud), GFP_KERNEL); |
| 445 | if (NULL == oud) |
| 446 | goto err_retract_minor; |
| 447 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 448 | dev_set_drvdata(dev, oud); |
| 449 | oud->minor = minor; |
| 450 | |
| 451 | /* allocate a disk and set it up */ |
| 452 | /* FIXME: do we need this since sg has already done that */ |
| 453 | disk = alloc_disk(1); |
| 454 | if (!disk) { |
| 455 | OSD_ERR("alloc_disk failed\n"); |
| 456 | goto err_free_osd; |
| 457 | } |
| 458 | disk->major = SCSI_OSD_MAJOR; |
| 459 | disk->first_minor = oud->minor; |
| 460 | sprintf(disk->disk_name, "osd%d", oud->minor); |
| 461 | oud->disk = disk; |
| 462 | |
| 463 | /* hold one more reference to the scsi_device that will get released |
| 464 | * in __release, in case a logout is happening while fs is mounted |
| 465 | */ |
| 466 | scsi_device_get(scsi_device); |
| 467 | osd_dev_init(&oud->od, scsi_device); |
| 468 | |
| 469 | /* Detect the OSD Version */ |
| 470 | error = __detect_osd(oud); |
| 471 | if (error) { |
| 472 | OSD_ERR("osd detection failed, non-compatible OSD device\n"); |
| 473 | goto err_put_disk; |
| 474 | } |
| 475 | |
| 476 | /* init the char-device for communication with user-mode */ |
| 477 | cdev_init(&oud->cdev, &osd_fops); |
| 478 | oud->cdev.owner = THIS_MODULE; |
| 479 | error = cdev_add(&oud->cdev, |
| 480 | MKDEV(SCSI_OSD_MAJOR, oud->minor), 1); |
| 481 | if (error) { |
| 482 | OSD_ERR("cdev_add failed\n"); |
| 483 | goto err_put_disk; |
| 484 | } |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 485 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 486 | /* class device member */ |
| 487 | oud->class_dev.devt = oud->cdev.dev; |
| 488 | oud->class_dev.class = &osd_uld_class; |
| 489 | oud->class_dev.parent = dev; |
| 490 | oud->class_dev.release = __remove; |
Kees Cook | ffc8b30 | 2013-07-03 15:01:14 -0700 | [diff] [blame] | 491 | error = dev_set_name(&oud->class_dev, "%s", disk->disk_name); |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 492 | if (error) { |
| 493 | OSD_ERR("dev_set_name failed => %d\n", error); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 494 | goto err_put_cdev; |
| 495 | } |
| 496 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 497 | error = device_register(&oud->class_dev); |
| 498 | if (error) { |
| 499 | OSD_ERR("device_register failed => %d\n", error); |
| 500 | goto err_put_cdev; |
| 501 | } |
| 502 | |
| 503 | get_device(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 504 | |
| 505 | OSD_INFO("osd_probe %s\n", disk->disk_name); |
| 506 | return 0; |
| 507 | |
| 508 | err_put_cdev: |
| 509 | cdev_del(&oud->cdev); |
| 510 | err_put_disk: |
| 511 | scsi_device_put(scsi_device); |
| 512 | put_disk(disk); |
| 513 | err_free_osd: |
| 514 | dev_set_drvdata(dev, NULL); |
| 515 | kfree(oud); |
| 516 | err_retract_minor: |
| 517 | ida_remove(&osd_minor_ida, minor); |
| 518 | return error; |
| 519 | } |
| 520 | |
| 521 | static int osd_remove(struct device *dev) |
| 522 | { |
| 523 | struct scsi_device *scsi_device = to_scsi_device(dev); |
| 524 | struct osd_uld_device *oud = dev_get_drvdata(dev); |
| 525 | |
| 526 | if (!oud || (oud->od.scsi_device != scsi_device)) { |
| 527 | OSD_ERR("Half cooked osd-device %p,%p || %p!=%p", |
| 528 | dev, oud, oud ? oud->od.scsi_device : NULL, |
| 529 | scsi_device); |
| 530 | } |
| 531 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 532 | device_unregister(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 533 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 534 | put_device(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 538 | /* |
| 539 | * Global driver and scsi registration |
| 540 | */ |
| 541 | |
| 542 | static struct scsi_driver osd_driver = { |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 543 | .gendrv = { |
| 544 | .name = osd_name, |
Christoph Hellwig | 3af6b35 | 2014-11-12 18:34:51 +0100 | [diff] [blame] | 545 | .owner = THIS_MODULE, |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 546 | .probe = osd_probe, |
| 547 | .remove = osd_remove, |
| 548 | } |
| 549 | }; |
| 550 | |
| 551 | static int __init osd_uld_init(void) |
| 552 | { |
| 553 | int err; |
| 554 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 555 | err = class_register(&osd_uld_class); |
| 556 | if (err) { |
| 557 | OSD_ERR("Unable to register sysfs class => %d\n", err); |
| 558 | return err; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), |
| 562 | SCSI_OSD_MAX_MINOR, osd_name); |
| 563 | if (err) { |
| 564 | OSD_ERR("Unable to register major %d for osd ULD => %d\n", |
| 565 | SCSI_OSD_MAJOR, err); |
| 566 | goto err_out; |
| 567 | } |
| 568 | |
| 569 | err = scsi_register_driver(&osd_driver.gendrv); |
| 570 | if (err) { |
| 571 | OSD_ERR("scsi_register_driver failed => %d\n", err); |
| 572 | goto err_out_chrdev; |
| 573 | } |
| 574 | |
| 575 | OSD_INFO("LOADED %s\n", osd_version_string); |
| 576 | return 0; |
| 577 | |
| 578 | err_out_chrdev: |
| 579 | unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR); |
| 580 | err_out: |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 581 | class_unregister(&osd_uld_class); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 582 | return err; |
| 583 | } |
| 584 | |
| 585 | static void __exit osd_uld_exit(void) |
| 586 | { |
| 587 | scsi_unregister_driver(&osd_driver.gendrv); |
| 588 | unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR); |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 589 | class_unregister(&osd_uld_class); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 590 | OSD_INFO("UNLOADED %s\n", osd_version_string); |
| 591 | } |
| 592 | |
| 593 | module_init(osd_uld_init); |
| 594 | module_exit(osd_uld_exit); |