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: |
| 13 | * Boaz Harrosh <bharrosh@panasas.com> |
| 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> |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 53 | |
| 54 | #include <scsi/scsi.h> |
| 55 | #include <scsi/scsi_driver.h> |
| 56 | #include <scsi/scsi_device.h> |
| 57 | #include <scsi/scsi_ioctl.h> |
| 58 | |
| 59 | #include <scsi/osd_initiator.h> |
| 60 | #include <scsi/osd_sec.h> |
| 61 | |
| 62 | #include "osd_debug.h" |
| 63 | |
| 64 | #ifndef TYPE_OSD |
| 65 | # define TYPE_OSD 0x11 |
| 66 | #endif |
| 67 | |
| 68 | #ifndef SCSI_OSD_MAJOR |
| 69 | # define SCSI_OSD_MAJOR 260 |
| 70 | #endif |
| 71 | #define SCSI_OSD_MAX_MINOR 64 |
| 72 | |
| 73 | static const char osd_name[] = "osd"; |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 74 | static const char *osd_version_string = "open-osd 0.2.0"; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 75 | |
| 76 | MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>"); |
| 77 | MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko"); |
| 78 | MODULE_LICENSE("GPL"); |
| 79 | MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR); |
| 80 | MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD); |
| 81 | |
| 82 | struct osd_uld_device { |
| 83 | int minor; |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 84 | struct device class_dev; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 85 | struct cdev cdev; |
| 86 | struct osd_dev od; |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 87 | struct osd_dev_info odi; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 88 | struct gendisk *disk; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 91 | struct osd_dev_handle { |
| 92 | struct osd_dev od; |
| 93 | struct file *file; |
| 94 | struct osd_uld_device *oud; |
| 95 | } ; |
| 96 | |
| 97 | static DEFINE_IDA(osd_minor_ida); |
| 98 | |
| 99 | static struct class osd_uld_class = { |
| 100 | .owner = THIS_MODULE, |
| 101 | .name = "scsi_osd", |
| 102 | }; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Char Device operations |
| 106 | */ |
| 107 | |
| 108 | static int osd_uld_open(struct inode *inode, struct file *file) |
| 109 | { |
| 110 | struct osd_uld_device *oud = container_of(inode->i_cdev, |
| 111 | struct osd_uld_device, cdev); |
| 112 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 113 | get_device(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 114 | /* cache osd_uld_device on file handle */ |
| 115 | file->private_data = oud; |
| 116 | OSD_DEBUG("osd_uld_open %p\n", oud); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int osd_uld_release(struct inode *inode, struct file *file) |
| 121 | { |
| 122 | struct osd_uld_device *oud = file->private_data; |
| 123 | |
| 124 | OSD_DEBUG("osd_uld_release %p\n", file->private_data); |
| 125 | file->private_data = NULL; |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 126 | put_device(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* FIXME: Only one vector for now */ |
| 131 | unsigned g_test_ioctl; |
| 132 | do_test_fn *g_do_test; |
| 133 | |
| 134 | int osduld_register_test(unsigned ioctl, do_test_fn *do_test) |
| 135 | { |
| 136 | if (g_test_ioctl) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | g_test_ioctl = ioctl; |
| 140 | g_do_test = do_test; |
| 141 | return 0; |
| 142 | } |
| 143 | EXPORT_SYMBOL(osduld_register_test); |
| 144 | |
| 145 | void osduld_unregister_test(unsigned ioctl) |
| 146 | { |
| 147 | if (ioctl == g_test_ioctl) { |
| 148 | g_test_ioctl = 0; |
| 149 | g_do_test = NULL; |
| 150 | } |
| 151 | } |
| 152 | EXPORT_SYMBOL(osduld_unregister_test); |
| 153 | |
| 154 | static do_test_fn *_find_ioctl(unsigned cmd) |
| 155 | { |
| 156 | if (g_test_ioctl == cmd) |
| 157 | return g_do_test; |
| 158 | else |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | static long osd_uld_ioctl(struct file *file, unsigned int cmd, |
| 163 | unsigned long arg) |
| 164 | { |
| 165 | struct osd_uld_device *oud = file->private_data; |
| 166 | int ret; |
| 167 | do_test_fn *do_test; |
| 168 | |
| 169 | do_test = _find_ioctl(cmd); |
| 170 | if (do_test) |
| 171 | ret = do_test(&oud->od, cmd, arg); |
| 172 | else { |
| 173 | OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud); |
| 174 | ret = -ENOIOCTLCMD; |
| 175 | } |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | static const struct file_operations osd_fops = { |
| 180 | .owner = THIS_MODULE, |
| 181 | .open = osd_uld_open, |
| 182 | .release = osd_uld_release, |
| 183 | .unlocked_ioctl = osd_uld_ioctl, |
| 184 | }; |
| 185 | |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 186 | struct osd_dev *osduld_path_lookup(const char *name) |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 187 | { |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 188 | struct osd_uld_device *oud; |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 189 | struct osd_dev_handle *odh; |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 190 | struct file *file; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 191 | int error; |
| 192 | |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 193 | if (!name || !*name) { |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 194 | OSD_ERR("Mount with !path || !*path\n"); |
| 195 | return ERR_PTR(-EINVAL); |
| 196 | } |
| 197 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 198 | odh = kzalloc(sizeof(*odh), GFP_KERNEL); |
| 199 | if (unlikely(!odh)) |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 200 | return ERR_PTR(-ENOMEM); |
| 201 | |
| 202 | file = filp_open(name, O_RDWR, 0); |
| 203 | if (IS_ERR(file)) { |
| 204 | error = PTR_ERR(file); |
| 205 | goto free_od; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 208 | if (file->f_op != &osd_fops){ |
| 209 | error = -EINVAL; |
| 210 | goto close_file; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 211 | } |
| 212 | |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 213 | oud = file->private_data; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 214 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 215 | odh->od = oud->od; |
| 216 | odh->file = file; |
| 217 | odh->oud = oud; |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 218 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 219 | return &odh->od; |
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 | close_file: |
| 222 | fput(file); |
| 223 | free_od: |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 224 | kfree(odh); |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 225 | return ERR_PTR(error); |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 226 | } |
| 227 | EXPORT_SYMBOL(osduld_path_lookup); |
| 228 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 229 | static inline bool _the_same_or_null(const u8 *a1, unsigned a1_len, |
| 230 | const u8 *a2, unsigned a2_len) |
| 231 | { |
| 232 | if (!a2_len) /* User string is Empty means don't care */ |
| 233 | return true; |
| 234 | |
| 235 | if (a1_len != a2_len) |
| 236 | return false; |
| 237 | |
| 238 | return 0 == memcmp(a1, a2, a1_len); |
| 239 | } |
| 240 | |
| 241 | struct find_oud_t { |
| 242 | const struct osd_dev_info *odi; |
| 243 | struct device *dev; |
| 244 | struct osd_uld_device *oud; |
| 245 | } ; |
| 246 | |
| 247 | int _mach_odi(struct device *dev, void *find_data) |
| 248 | { |
| 249 | struct osd_uld_device *oud = container_of(dev, struct osd_uld_device, |
| 250 | class_dev); |
| 251 | struct find_oud_t *fot = find_data; |
| 252 | const struct osd_dev_info *odi = fot->odi; |
| 253 | |
| 254 | if (_the_same_or_null(oud->odi.systemid, oud->odi.systemid_len, |
| 255 | odi->systemid, odi->systemid_len) && |
| 256 | _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len, |
| 257 | odi->osdname, odi->osdname_len)) { |
| 258 | OSD_DEBUG("found device sysid_len=%d osdname=%d\n", |
| 259 | odi->systemid_len, odi->osdname_len); |
| 260 | fot->oud = oud; |
| 261 | return 1; |
| 262 | } else { |
| 263 | return 0; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* osduld_info_lookup - Loop through all devices, return the requested osd_dev. |
| 268 | * |
| 269 | * if @odi->systemid_len and/or @odi->osdname_len are zero, they act as a don't |
| 270 | * care. .e.g if they're both zero /dev/osd0 is returned. |
| 271 | */ |
| 272 | struct osd_dev *osduld_info_lookup(const struct osd_dev_info *odi) |
| 273 | { |
| 274 | struct find_oud_t find = {.odi = odi}; |
| 275 | |
| 276 | find.dev = class_find_device(&osd_uld_class, NULL, &find, _mach_odi); |
| 277 | if (likely(find.dev)) { |
| 278 | struct osd_dev_handle *odh = kzalloc(sizeof(*odh), GFP_KERNEL); |
| 279 | |
| 280 | if (unlikely(!odh)) { |
| 281 | put_device(find.dev); |
| 282 | return ERR_PTR(-ENOMEM); |
| 283 | } |
| 284 | |
| 285 | odh->od = find.oud->od; |
| 286 | odh->oud = find.oud; |
| 287 | |
| 288 | return &odh->od; |
| 289 | } |
| 290 | |
| 291 | return ERR_PTR(-ENODEV); |
| 292 | } |
| 293 | EXPORT_SYMBOL(osduld_info_lookup); |
| 294 | |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 295 | void osduld_put_device(struct osd_dev *od) |
| 296 | { |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 297 | if (od && !IS_ERR(od)) { |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 298 | struct osd_dev_handle *odh = |
| 299 | container_of(od, struct osd_dev_handle, od); |
| 300 | struct osd_uld_device *oud = odh->oud; |
Boaz Harrosh | 021e223 | 2009-05-24 20:05:05 +0300 | [diff] [blame] | 301 | |
| 302 | BUG_ON(od->scsi_device != oud->od.scsi_device); |
| 303 | |
Boaz Harrosh | 89f5e1f | 2009-11-16 20:44:02 +0200 | [diff] [blame] | 304 | /* If scsi has released the device (logout), and exofs has last |
| 305 | * reference on oud it will be freed by above osd_uld_release |
| 306 | * within fput below. But this will oops in cdev_release which |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 307 | * is called after the fops->release. A get_/put_ pair makes |
Boaz Harrosh | 89f5e1f | 2009-11-16 20:44:02 +0200 | [diff] [blame] | 308 | * sure we have a cdev for the duration of fput |
| 309 | */ |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 310 | if (odh->file) { |
| 311 | get_device(&oud->class_dev); |
| 312 | fput(odh->file); |
| 313 | } |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 314 | put_device(&oud->class_dev); |
| 315 | kfree(odh); |
Boaz Harrosh | b799bc7 | 2009-01-25 16:58:03 +0200 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | EXPORT_SYMBOL(osduld_put_device); |
| 319 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 320 | const struct osd_dev_info *osduld_device_info(struct osd_dev *od) |
| 321 | { |
| 322 | struct osd_dev_handle *odh = |
| 323 | container_of(od, struct osd_dev_handle, od); |
| 324 | return &odh->oud->odi; |
| 325 | } |
| 326 | EXPORT_SYMBOL(osduld_device_info); |
| 327 | |
| 328 | bool osduld_device_same(struct osd_dev *od, const struct osd_dev_info *odi) |
| 329 | { |
| 330 | struct osd_dev_handle *odh = |
| 331 | container_of(od, struct osd_dev_handle, od); |
| 332 | struct osd_uld_device *oud = odh->oud; |
| 333 | |
| 334 | return (oud->odi.systemid_len == odi->systemid_len) && |
| 335 | _the_same_or_null(oud->odi.systemid, oud->odi.systemid_len, |
| 336 | odi->systemid, odi->systemid_len) && |
| 337 | (oud->odi.osdname_len == odi->osdname_len) && |
| 338 | _the_same_or_null(oud->odi.osdname, oud->odi.osdname_len, |
| 339 | odi->osdname, odi->osdname_len); |
| 340 | } |
| 341 | EXPORT_SYMBOL(osduld_device_same); |
| 342 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 343 | /* |
| 344 | * Scsi Device operations |
| 345 | */ |
| 346 | |
| 347 | static int __detect_osd(struct osd_uld_device *oud) |
| 348 | { |
| 349 | struct scsi_device *scsi_device = oud->od.scsi_device; |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 350 | char caps[OSD_CAP_LEN]; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 351 | int error; |
| 352 | |
| 353 | /* sending a test_unit_ready as first command seems to be needed |
| 354 | * by some targets |
| 355 | */ |
| 356 | OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n", |
| 357 | oud, scsi_device, scsi_device->request_queue); |
| 358 | error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL); |
| 359 | if (error) |
| 360 | OSD_ERR("warning: scsi_test_unit_ready failed\n"); |
| 361 | |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 362 | osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true); |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 363 | if (osd_auto_detect_ver(&oud->od, caps, &oud->odi)) |
Boaz Harrosh | 1b9dce9 | 2009-01-25 17:13:38 +0200 | [diff] [blame] | 364 | return -ENODEV; |
| 365 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 369 | static void __remove(struct device *dev) |
| 370 | { |
| 371 | struct osd_uld_device *oud = container_of(dev, struct osd_uld_device, |
| 372 | class_dev); |
| 373 | struct scsi_device *scsi_device = oud->od.scsi_device; |
| 374 | |
Boaz Harrosh | 2cdd641 | 2009-11-29 16:26:45 +0200 | [diff] [blame] | 375 | kfree(oud->odi.osdname); |
| 376 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 377 | if (oud->cdev.owner) |
| 378 | cdev_del(&oud->cdev); |
| 379 | |
| 380 | osd_dev_fini(&oud->od); |
| 381 | scsi_device_put(scsi_device); |
| 382 | |
| 383 | OSD_INFO("osd_remove %s\n", |
| 384 | oud->disk ? oud->disk->disk_name : NULL); |
| 385 | |
| 386 | if (oud->disk) |
| 387 | put_disk(oud->disk); |
| 388 | ida_remove(&osd_minor_ida, oud->minor); |
| 389 | |
| 390 | kfree(oud); |
| 391 | } |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 392 | |
| 393 | static int osd_probe(struct device *dev) |
| 394 | { |
| 395 | struct scsi_device *scsi_device = to_scsi_device(dev); |
| 396 | struct gendisk *disk; |
| 397 | struct osd_uld_device *oud; |
| 398 | int minor; |
| 399 | int error; |
| 400 | |
| 401 | if (scsi_device->type != TYPE_OSD) |
| 402 | return -ENODEV; |
| 403 | |
| 404 | do { |
| 405 | if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL)) |
| 406 | return -ENODEV; |
| 407 | |
| 408 | error = ida_get_new(&osd_minor_ida, &minor); |
| 409 | } while (error == -EAGAIN); |
| 410 | |
| 411 | if (error) |
| 412 | return error; |
| 413 | if (minor >= SCSI_OSD_MAX_MINOR) { |
| 414 | error = -EBUSY; |
| 415 | goto err_retract_minor; |
| 416 | } |
| 417 | |
| 418 | error = -ENOMEM; |
| 419 | oud = kzalloc(sizeof(*oud), GFP_KERNEL); |
| 420 | if (NULL == oud) |
| 421 | goto err_retract_minor; |
| 422 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 423 | dev_set_drvdata(dev, oud); |
| 424 | oud->minor = minor; |
| 425 | |
| 426 | /* allocate a disk and set it up */ |
| 427 | /* FIXME: do we need this since sg has already done that */ |
| 428 | disk = alloc_disk(1); |
| 429 | if (!disk) { |
| 430 | OSD_ERR("alloc_disk failed\n"); |
| 431 | goto err_free_osd; |
| 432 | } |
| 433 | disk->major = SCSI_OSD_MAJOR; |
| 434 | disk->first_minor = oud->minor; |
| 435 | sprintf(disk->disk_name, "osd%d", oud->minor); |
| 436 | oud->disk = disk; |
| 437 | |
| 438 | /* hold one more reference to the scsi_device that will get released |
| 439 | * in __release, in case a logout is happening while fs is mounted |
| 440 | */ |
| 441 | scsi_device_get(scsi_device); |
| 442 | osd_dev_init(&oud->od, scsi_device); |
| 443 | |
| 444 | /* Detect the OSD Version */ |
| 445 | error = __detect_osd(oud); |
| 446 | if (error) { |
| 447 | OSD_ERR("osd detection failed, non-compatible OSD device\n"); |
| 448 | goto err_put_disk; |
| 449 | } |
| 450 | |
| 451 | /* init the char-device for communication with user-mode */ |
| 452 | cdev_init(&oud->cdev, &osd_fops); |
| 453 | oud->cdev.owner = THIS_MODULE; |
| 454 | error = cdev_add(&oud->cdev, |
| 455 | MKDEV(SCSI_OSD_MAJOR, oud->minor), 1); |
| 456 | if (error) { |
| 457 | OSD_ERR("cdev_add failed\n"); |
| 458 | goto err_put_disk; |
| 459 | } |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 460 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 461 | /* class device member */ |
| 462 | oud->class_dev.devt = oud->cdev.dev; |
| 463 | oud->class_dev.class = &osd_uld_class; |
| 464 | oud->class_dev.parent = dev; |
| 465 | oud->class_dev.release = __remove; |
| 466 | error = dev_set_name(&oud->class_dev, disk->disk_name); |
| 467 | if (error) { |
| 468 | OSD_ERR("dev_set_name failed => %d\n", error); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 469 | goto err_put_cdev; |
| 470 | } |
| 471 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 472 | error = device_register(&oud->class_dev); |
| 473 | if (error) { |
| 474 | OSD_ERR("device_register failed => %d\n", error); |
| 475 | goto err_put_cdev; |
| 476 | } |
| 477 | |
| 478 | get_device(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 479 | |
| 480 | OSD_INFO("osd_probe %s\n", disk->disk_name); |
| 481 | return 0; |
| 482 | |
| 483 | err_put_cdev: |
| 484 | cdev_del(&oud->cdev); |
| 485 | err_put_disk: |
| 486 | scsi_device_put(scsi_device); |
| 487 | put_disk(disk); |
| 488 | err_free_osd: |
| 489 | dev_set_drvdata(dev, NULL); |
| 490 | kfree(oud); |
| 491 | err_retract_minor: |
| 492 | ida_remove(&osd_minor_ida, minor); |
| 493 | return error; |
| 494 | } |
| 495 | |
| 496 | static int osd_remove(struct device *dev) |
| 497 | { |
| 498 | struct scsi_device *scsi_device = to_scsi_device(dev); |
| 499 | struct osd_uld_device *oud = dev_get_drvdata(dev); |
| 500 | |
| 501 | if (!oud || (oud->od.scsi_device != scsi_device)) { |
| 502 | OSD_ERR("Half cooked osd-device %p,%p || %p!=%p", |
| 503 | dev, oud, oud ? oud->od.scsi_device : NULL, |
| 504 | scsi_device); |
| 505 | } |
| 506 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 507 | device_unregister(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 508 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 509 | put_device(&oud->class_dev); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 513 | /* |
| 514 | * Global driver and scsi registration |
| 515 | */ |
| 516 | |
| 517 | static struct scsi_driver osd_driver = { |
| 518 | .owner = THIS_MODULE, |
| 519 | .gendrv = { |
| 520 | .name = osd_name, |
| 521 | .probe = osd_probe, |
| 522 | .remove = osd_remove, |
| 523 | } |
| 524 | }; |
| 525 | |
| 526 | static int __init osd_uld_init(void) |
| 527 | { |
| 528 | int err; |
| 529 | |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 530 | err = class_register(&osd_uld_class); |
| 531 | if (err) { |
| 532 | OSD_ERR("Unable to register sysfs class => %d\n", err); |
| 533 | return err; |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), |
| 537 | SCSI_OSD_MAX_MINOR, osd_name); |
| 538 | if (err) { |
| 539 | OSD_ERR("Unable to register major %d for osd ULD => %d\n", |
| 540 | SCSI_OSD_MAJOR, err); |
| 541 | goto err_out; |
| 542 | } |
| 543 | |
| 544 | err = scsi_register_driver(&osd_driver.gendrv); |
| 545 | if (err) { |
| 546 | OSD_ERR("scsi_register_driver failed => %d\n", err); |
| 547 | goto err_out_chrdev; |
| 548 | } |
| 549 | |
| 550 | OSD_INFO("LOADED %s\n", osd_version_string); |
| 551 | return 0; |
| 552 | |
| 553 | err_out_chrdev: |
| 554 | unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR); |
| 555 | err_out: |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 556 | class_unregister(&osd_uld_class); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 557 | return err; |
| 558 | } |
| 559 | |
| 560 | static void __exit osd_uld_exit(void) |
| 561 | { |
| 562 | scsi_unregister_driver(&osd_driver.gendrv); |
| 563 | unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR); |
Boaz Harrosh | d6ae433 | 2009-11-29 16:25:26 +0200 | [diff] [blame] | 564 | class_unregister(&osd_uld_class); |
Boaz Harrosh | 95b05a7 | 2009-01-25 16:56:47 +0200 | [diff] [blame] | 565 | OSD_INFO("UNLOADED %s\n", osd_version_string); |
| 566 | } |
| 567 | |
| 568 | module_init(osd_uld_init); |
| 569 | module_exit(osd_uld_exit); |