blob: 1ea6447f9418faec3a9a4294f05c8a7acf3c2803 [file] [log] [blame]
Boaz Harrosh95b05a72009-01-25 16:56:47 +02001/*
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 Harrosh021e2232009-05-24 20:05:05 +030052#include <linux/file.h>
Boaz Harrosh95b05a72009-01-25 16:56:47 +020053
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
73static const char osd_name[] = "osd";
74static const char *osd_version_string = "open-osd 0.1.0";
75const char osd_symlink[] = "scsi_osd";
76
77MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
78MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko");
79MODULE_LICENSE("GPL");
80MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR);
81MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD);
82
83struct osd_uld_device {
84 int minor;
85 struct kref kref;
86 struct cdev cdev;
87 struct osd_dev od;
88 struct gendisk *disk;
89 struct device *class_member;
90};
91
92static void __uld_get(struct osd_uld_device *oud);
93static void __uld_put(struct osd_uld_device *oud);
94
95/*
96 * Char Device operations
97 */
98
99static int osd_uld_open(struct inode *inode, struct file *file)
100{
101 struct osd_uld_device *oud = container_of(inode->i_cdev,
102 struct osd_uld_device, cdev);
103
104 __uld_get(oud);
105 /* cache osd_uld_device on file handle */
106 file->private_data = oud;
107 OSD_DEBUG("osd_uld_open %p\n", oud);
108 return 0;
109}
110
111static int osd_uld_release(struct inode *inode, struct file *file)
112{
113 struct osd_uld_device *oud = file->private_data;
114
115 OSD_DEBUG("osd_uld_release %p\n", file->private_data);
116 file->private_data = NULL;
117 __uld_put(oud);
118 return 0;
119}
120
121/* FIXME: Only one vector for now */
122unsigned g_test_ioctl;
123do_test_fn *g_do_test;
124
125int osduld_register_test(unsigned ioctl, do_test_fn *do_test)
126{
127 if (g_test_ioctl)
128 return -EINVAL;
129
130 g_test_ioctl = ioctl;
131 g_do_test = do_test;
132 return 0;
133}
134EXPORT_SYMBOL(osduld_register_test);
135
136void osduld_unregister_test(unsigned ioctl)
137{
138 if (ioctl == g_test_ioctl) {
139 g_test_ioctl = 0;
140 g_do_test = NULL;
141 }
142}
143EXPORT_SYMBOL(osduld_unregister_test);
144
145static do_test_fn *_find_ioctl(unsigned cmd)
146{
147 if (g_test_ioctl == cmd)
148 return g_do_test;
149 else
150 return NULL;
151}
152
153static long osd_uld_ioctl(struct file *file, unsigned int cmd,
154 unsigned long arg)
155{
156 struct osd_uld_device *oud = file->private_data;
157 int ret;
158 do_test_fn *do_test;
159
160 do_test = _find_ioctl(cmd);
161 if (do_test)
162 ret = do_test(&oud->od, cmd, arg);
163 else {
164 OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud);
165 ret = -ENOIOCTLCMD;
166 }
167 return ret;
168}
169
170static const struct file_operations osd_fops = {
171 .owner = THIS_MODULE,
172 .open = osd_uld_open,
173 .release = osd_uld_release,
174 .unlocked_ioctl = osd_uld_ioctl,
175};
176
Al Viroe24977d2009-04-02 21:17:03 -0400177struct osd_dev *osduld_path_lookup(const char *name)
Boaz Harroshb799bc72009-01-25 16:58:03 +0200178{
Boaz Harrosh021e2232009-05-24 20:05:05 +0300179 struct osd_uld_device *oud;
180 struct osd_dev *od;
181 struct file *file;
Boaz Harroshb799bc72009-01-25 16:58:03 +0200182 int error;
183
Al Viroe24977d2009-04-02 21:17:03 -0400184 if (!name || !*name) {
Boaz Harroshb799bc72009-01-25 16:58:03 +0200185 OSD_ERR("Mount with !path || !*path\n");
186 return ERR_PTR(-EINVAL);
187 }
188
Boaz Harrosh021e2232009-05-24 20:05:05 +0300189 od = kzalloc(sizeof(*od), GFP_KERNEL);
190 if (!od)
191 return ERR_PTR(-ENOMEM);
192
193 file = filp_open(name, O_RDWR, 0);
194 if (IS_ERR(file)) {
195 error = PTR_ERR(file);
196 goto free_od;
Boaz Harroshb799bc72009-01-25 16:58:03 +0200197 }
198
Boaz Harrosh021e2232009-05-24 20:05:05 +0300199 if (file->f_op != &osd_fops){
200 error = -EINVAL;
201 goto close_file;
Boaz Harroshb799bc72009-01-25 16:58:03 +0200202 }
203
Boaz Harrosh021e2232009-05-24 20:05:05 +0300204 oud = file->private_data;
Boaz Harroshb799bc72009-01-25 16:58:03 +0200205
Boaz Harrosh021e2232009-05-24 20:05:05 +0300206 *od = oud->od;
207 od->file = file;
Boaz Harroshb799bc72009-01-25 16:58:03 +0200208
Boaz Harrosh021e2232009-05-24 20:05:05 +0300209 return od;
Boaz Harroshb799bc72009-01-25 16:58:03 +0200210
Boaz Harrosh021e2232009-05-24 20:05:05 +0300211close_file:
212 fput(file);
213free_od:
214 kfree(od);
215 return ERR_PTR(error);
Boaz Harroshb799bc72009-01-25 16:58:03 +0200216}
217EXPORT_SYMBOL(osduld_path_lookup);
218
219void osduld_put_device(struct osd_dev *od)
220{
Boaz Harroshb799bc72009-01-25 16:58:03 +0200221
Boaz Harrosh021e2232009-05-24 20:05:05 +0300222 if (od && !IS_ERR(od)) {
223 struct osd_uld_device *oud = od->file->private_data;
224
225 BUG_ON(od->scsi_device != oud->od.scsi_device);
226
Boaz Harrosh89f5e1f2009-11-16 20:44:02 +0200227 /* If scsi has released the device (logout), and exofs has last
228 * reference on oud it will be freed by above osd_uld_release
229 * within fput below. But this will oops in cdev_release which
230 * is called after the fops->release. __uld_get/put pair makes
231 * sure we have a cdev for the duration of fput
232 */
233 __uld_get(oud);
Boaz Harrosh021e2232009-05-24 20:05:05 +0300234 fput(od->file);
Boaz Harrosh89f5e1f2009-11-16 20:44:02 +0200235 __uld_put(oud);
Boaz Harrosh021e2232009-05-24 20:05:05 +0300236 kfree(od);
Boaz Harroshb799bc72009-01-25 16:58:03 +0200237 }
238}
239EXPORT_SYMBOL(osduld_put_device);
240
Boaz Harrosh95b05a72009-01-25 16:56:47 +0200241/*
242 * Scsi Device operations
243 */
244
245static int __detect_osd(struct osd_uld_device *oud)
246{
247 struct scsi_device *scsi_device = oud->od.scsi_device;
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200248 char caps[OSD_CAP_LEN];
Boaz Harrosh95b05a72009-01-25 16:56:47 +0200249 int error;
250
251 /* sending a test_unit_ready as first command seems to be needed
252 * by some targets
253 */
254 OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n",
255 oud, scsi_device, scsi_device->request_queue);
256 error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL);
257 if (error)
258 OSD_ERR("warning: scsi_test_unit_ready failed\n");
259
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200260 osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true);
261 if (osd_auto_detect_ver(&oud->od, caps))
262 return -ENODEV;
263
Boaz Harrosh95b05a72009-01-25 16:56:47 +0200264 return 0;
265}
266
267static struct class *osd_sysfs_class;
268static DEFINE_IDA(osd_minor_ida);
269
270static int osd_probe(struct device *dev)
271{
272 struct scsi_device *scsi_device = to_scsi_device(dev);
273 struct gendisk *disk;
274 struct osd_uld_device *oud;
275 int minor;
276 int error;
277
278 if (scsi_device->type != TYPE_OSD)
279 return -ENODEV;
280
281 do {
282 if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL))
283 return -ENODEV;
284
285 error = ida_get_new(&osd_minor_ida, &minor);
286 } while (error == -EAGAIN);
287
288 if (error)
289 return error;
290 if (minor >= SCSI_OSD_MAX_MINOR) {
291 error = -EBUSY;
292 goto err_retract_minor;
293 }
294
295 error = -ENOMEM;
296 oud = kzalloc(sizeof(*oud), GFP_KERNEL);
297 if (NULL == oud)
298 goto err_retract_minor;
299
300 kref_init(&oud->kref);
301 dev_set_drvdata(dev, oud);
302 oud->minor = minor;
303
304 /* allocate a disk and set it up */
305 /* FIXME: do we need this since sg has already done that */
306 disk = alloc_disk(1);
307 if (!disk) {
308 OSD_ERR("alloc_disk failed\n");
309 goto err_free_osd;
310 }
311 disk->major = SCSI_OSD_MAJOR;
312 disk->first_minor = oud->minor;
313 sprintf(disk->disk_name, "osd%d", oud->minor);
314 oud->disk = disk;
315
316 /* hold one more reference to the scsi_device that will get released
317 * in __release, in case a logout is happening while fs is mounted
318 */
319 scsi_device_get(scsi_device);
320 osd_dev_init(&oud->od, scsi_device);
321
322 /* Detect the OSD Version */
323 error = __detect_osd(oud);
324 if (error) {
325 OSD_ERR("osd detection failed, non-compatible OSD device\n");
326 goto err_put_disk;
327 }
328
329 /* init the char-device for communication with user-mode */
330 cdev_init(&oud->cdev, &osd_fops);
331 oud->cdev.owner = THIS_MODULE;
332 error = cdev_add(&oud->cdev,
333 MKDEV(SCSI_OSD_MAJOR, oud->minor), 1);
334 if (error) {
335 OSD_ERR("cdev_add failed\n");
336 goto err_put_disk;
337 }
338 kobject_get(&oud->cdev.kobj); /* 2nd ref see osd_remove() */
339
340 /* class_member */
341 oud->class_member = device_create(osd_sysfs_class, dev,
342 MKDEV(SCSI_OSD_MAJOR, oud->minor), "%s", disk->disk_name);
343 if (IS_ERR(oud->class_member)) {
344 OSD_ERR("class_device_create failed\n");
345 error = PTR_ERR(oud->class_member);
346 goto err_put_cdev;
347 }
348
349 dev_set_drvdata(oud->class_member, oud);
Boaz Harrosh95b05a72009-01-25 16:56:47 +0200350
351 OSD_INFO("osd_probe %s\n", disk->disk_name);
352 return 0;
353
354err_put_cdev:
355 cdev_del(&oud->cdev);
356err_put_disk:
357 scsi_device_put(scsi_device);
358 put_disk(disk);
359err_free_osd:
360 dev_set_drvdata(dev, NULL);
361 kfree(oud);
362err_retract_minor:
363 ida_remove(&osd_minor_ida, minor);
364 return error;
365}
366
367static int osd_remove(struct device *dev)
368{
369 struct scsi_device *scsi_device = to_scsi_device(dev);
370 struct osd_uld_device *oud = dev_get_drvdata(dev);
371
372 if (!oud || (oud->od.scsi_device != scsi_device)) {
373 OSD_ERR("Half cooked osd-device %p,%p || %p!=%p",
374 dev, oud, oud ? oud->od.scsi_device : NULL,
375 scsi_device);
376 }
377
Boaz Harrosh95b05a72009-01-25 16:56:47 +0200378 if (oud->class_member)
379 device_destroy(osd_sysfs_class,
380 MKDEV(SCSI_OSD_MAJOR, oud->minor));
381
382 /* We have 2 references to the cdev. One is released here
383 * and also takes down the /dev/osdX mapping. The second
384 * Will be released in __remove() after all users have released
385 * the osd_uld_device.
386 */
387 if (oud->cdev.owner)
388 cdev_del(&oud->cdev);
389
390 __uld_put(oud);
391 return 0;
392}
393
394static void __remove(struct kref *kref)
395{
396 struct osd_uld_device *oud = container_of(kref,
397 struct osd_uld_device, kref);
398 struct scsi_device *scsi_device = oud->od.scsi_device;
399
400 /* now let delete the char_dev */
401 kobject_put(&oud->cdev.kobj);
402
403 osd_dev_fini(&oud->od);
404 scsi_device_put(scsi_device);
405
406 OSD_INFO("osd_remove %s\n",
407 oud->disk ? oud->disk->disk_name : NULL);
408
409 if (oud->disk)
410 put_disk(oud->disk);
411
412 ida_remove(&osd_minor_ida, oud->minor);
413 kfree(oud);
414}
415
416static void __uld_get(struct osd_uld_device *oud)
417{
418 kref_get(&oud->kref);
419}
420
421static void __uld_put(struct osd_uld_device *oud)
422{
423 kref_put(&oud->kref, __remove);
424}
425
426/*
427 * Global driver and scsi registration
428 */
429
430static struct scsi_driver osd_driver = {
431 .owner = THIS_MODULE,
432 .gendrv = {
433 .name = osd_name,
434 .probe = osd_probe,
435 .remove = osd_remove,
436 }
437};
438
439static int __init osd_uld_init(void)
440{
441 int err;
442
443 osd_sysfs_class = class_create(THIS_MODULE, osd_symlink);
444 if (IS_ERR(osd_sysfs_class)) {
445 OSD_ERR("Unable to register sysfs class => %ld\n",
446 PTR_ERR(osd_sysfs_class));
447 return PTR_ERR(osd_sysfs_class);
448 }
449
450 err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0),
451 SCSI_OSD_MAX_MINOR, osd_name);
452 if (err) {
453 OSD_ERR("Unable to register major %d for osd ULD => %d\n",
454 SCSI_OSD_MAJOR, err);
455 goto err_out;
456 }
457
458 err = scsi_register_driver(&osd_driver.gendrv);
459 if (err) {
460 OSD_ERR("scsi_register_driver failed => %d\n", err);
461 goto err_out_chrdev;
462 }
463
464 OSD_INFO("LOADED %s\n", osd_version_string);
465 return 0;
466
467err_out_chrdev:
468 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
469err_out:
470 class_destroy(osd_sysfs_class);
471 return err;
472}
473
474static void __exit osd_uld_exit(void)
475{
476 scsi_unregister_driver(&osd_driver.gendrv);
477 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
478 class_destroy(osd_sysfs_class);
479 OSD_INFO("UNLOADED %s\n", osd_version_string);
480}
481
482module_init(osd_uld_init);
483module_exit(osd_uld_exit);