blob: f6f9a99adaa6bbe9d38baf3290ab7f1939e1c98d [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>
52
53#include <scsi/scsi.h>
54#include <scsi/scsi_driver.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_ioctl.h>
57
58#include <scsi/osd_initiator.h>
59#include <scsi/osd_sec.h>
60
61#include "osd_debug.h"
62
63#ifndef TYPE_OSD
64# define TYPE_OSD 0x11
65#endif
66
67#ifndef SCSI_OSD_MAJOR
68# define SCSI_OSD_MAJOR 260
69#endif
70#define SCSI_OSD_MAX_MINOR 64
71
72static const char osd_name[] = "osd";
73static const char *osd_version_string = "open-osd 0.1.0";
74const char osd_symlink[] = "scsi_osd";
75
76MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
77MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko");
78MODULE_LICENSE("GPL");
79MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR);
80MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD);
81
82struct osd_uld_device {
83 int minor;
84 struct kref kref;
85 struct cdev cdev;
86 struct osd_dev od;
87 struct gendisk *disk;
88 struct device *class_member;
89};
90
91static void __uld_get(struct osd_uld_device *oud);
92static void __uld_put(struct osd_uld_device *oud);
93
94/*
95 * Char Device operations
96 */
97
98static int osd_uld_open(struct inode *inode, struct file *file)
99{
100 struct osd_uld_device *oud = container_of(inode->i_cdev,
101 struct osd_uld_device, cdev);
102
103 __uld_get(oud);
104 /* cache osd_uld_device on file handle */
105 file->private_data = oud;
106 OSD_DEBUG("osd_uld_open %p\n", oud);
107 return 0;
108}
109
110static int osd_uld_release(struct inode *inode, struct file *file)
111{
112 struct osd_uld_device *oud = file->private_data;
113
114 OSD_DEBUG("osd_uld_release %p\n", file->private_data);
115 file->private_data = NULL;
116 __uld_put(oud);
117 return 0;
118}
119
120/* FIXME: Only one vector for now */
121unsigned g_test_ioctl;
122do_test_fn *g_do_test;
123
124int osduld_register_test(unsigned ioctl, do_test_fn *do_test)
125{
126 if (g_test_ioctl)
127 return -EINVAL;
128
129 g_test_ioctl = ioctl;
130 g_do_test = do_test;
131 return 0;
132}
133EXPORT_SYMBOL(osduld_register_test);
134
135void osduld_unregister_test(unsigned ioctl)
136{
137 if (ioctl == g_test_ioctl) {
138 g_test_ioctl = 0;
139 g_do_test = NULL;
140 }
141}
142EXPORT_SYMBOL(osduld_unregister_test);
143
144static do_test_fn *_find_ioctl(unsigned cmd)
145{
146 if (g_test_ioctl == cmd)
147 return g_do_test;
148 else
149 return NULL;
150}
151
152static long osd_uld_ioctl(struct file *file, unsigned int cmd,
153 unsigned long arg)
154{
155 struct osd_uld_device *oud = file->private_data;
156 int ret;
157 do_test_fn *do_test;
158
159 do_test = _find_ioctl(cmd);
160 if (do_test)
161 ret = do_test(&oud->od, cmd, arg);
162 else {
163 OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud);
164 ret = -ENOIOCTLCMD;
165 }
166 return ret;
167}
168
169static const struct file_operations osd_fops = {
170 .owner = THIS_MODULE,
171 .open = osd_uld_open,
172 .release = osd_uld_release,
173 .unlocked_ioctl = osd_uld_ioctl,
174};
175
176/*
177 * Scsi Device operations
178 */
179
180static int __detect_osd(struct osd_uld_device *oud)
181{
182 struct scsi_device *scsi_device = oud->od.scsi_device;
183 int error;
184
185 /* sending a test_unit_ready as first command seems to be needed
186 * by some targets
187 */
188 OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n",
189 oud, scsi_device, scsi_device->request_queue);
190 error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL);
191 if (error)
192 OSD_ERR("warning: scsi_test_unit_ready failed\n");
193
194 return 0;
195}
196
197static struct class *osd_sysfs_class;
198static DEFINE_IDA(osd_minor_ida);
199
200static int osd_probe(struct device *dev)
201{
202 struct scsi_device *scsi_device = to_scsi_device(dev);
203 struct gendisk *disk;
204 struct osd_uld_device *oud;
205 int minor;
206 int error;
207
208 if (scsi_device->type != TYPE_OSD)
209 return -ENODEV;
210
211 do {
212 if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL))
213 return -ENODEV;
214
215 error = ida_get_new(&osd_minor_ida, &minor);
216 } while (error == -EAGAIN);
217
218 if (error)
219 return error;
220 if (minor >= SCSI_OSD_MAX_MINOR) {
221 error = -EBUSY;
222 goto err_retract_minor;
223 }
224
225 error = -ENOMEM;
226 oud = kzalloc(sizeof(*oud), GFP_KERNEL);
227 if (NULL == oud)
228 goto err_retract_minor;
229
230 kref_init(&oud->kref);
231 dev_set_drvdata(dev, oud);
232 oud->minor = minor;
233
234 /* allocate a disk and set it up */
235 /* FIXME: do we need this since sg has already done that */
236 disk = alloc_disk(1);
237 if (!disk) {
238 OSD_ERR("alloc_disk failed\n");
239 goto err_free_osd;
240 }
241 disk->major = SCSI_OSD_MAJOR;
242 disk->first_minor = oud->minor;
243 sprintf(disk->disk_name, "osd%d", oud->minor);
244 oud->disk = disk;
245
246 /* hold one more reference to the scsi_device that will get released
247 * in __release, in case a logout is happening while fs is mounted
248 */
249 scsi_device_get(scsi_device);
250 osd_dev_init(&oud->od, scsi_device);
251
252 /* Detect the OSD Version */
253 error = __detect_osd(oud);
254 if (error) {
255 OSD_ERR("osd detection failed, non-compatible OSD device\n");
256 goto err_put_disk;
257 }
258
259 /* init the char-device for communication with user-mode */
260 cdev_init(&oud->cdev, &osd_fops);
261 oud->cdev.owner = THIS_MODULE;
262 error = cdev_add(&oud->cdev,
263 MKDEV(SCSI_OSD_MAJOR, oud->minor), 1);
264 if (error) {
265 OSD_ERR("cdev_add failed\n");
266 goto err_put_disk;
267 }
268 kobject_get(&oud->cdev.kobj); /* 2nd ref see osd_remove() */
269
270 /* class_member */
271 oud->class_member = device_create(osd_sysfs_class, dev,
272 MKDEV(SCSI_OSD_MAJOR, oud->minor), "%s", disk->disk_name);
273 if (IS_ERR(oud->class_member)) {
274 OSD_ERR("class_device_create failed\n");
275 error = PTR_ERR(oud->class_member);
276 goto err_put_cdev;
277 }
278
279 dev_set_drvdata(oud->class_member, oud);
280 error = sysfs_create_link(&scsi_device->sdev_gendev.kobj,
281 &oud->class_member->kobj, osd_symlink);
282 if (error)
283 OSD_ERR("warning: unable to make symlink\n");
284
285 OSD_INFO("osd_probe %s\n", disk->disk_name);
286 return 0;
287
288err_put_cdev:
289 cdev_del(&oud->cdev);
290err_put_disk:
291 scsi_device_put(scsi_device);
292 put_disk(disk);
293err_free_osd:
294 dev_set_drvdata(dev, NULL);
295 kfree(oud);
296err_retract_minor:
297 ida_remove(&osd_minor_ida, minor);
298 return error;
299}
300
301static int osd_remove(struct device *dev)
302{
303 struct scsi_device *scsi_device = to_scsi_device(dev);
304 struct osd_uld_device *oud = dev_get_drvdata(dev);
305
306 if (!oud || (oud->od.scsi_device != scsi_device)) {
307 OSD_ERR("Half cooked osd-device %p,%p || %p!=%p",
308 dev, oud, oud ? oud->od.scsi_device : NULL,
309 scsi_device);
310 }
311
312 sysfs_remove_link(&oud->od.scsi_device->sdev_gendev.kobj, osd_symlink);
313
314 if (oud->class_member)
315 device_destroy(osd_sysfs_class,
316 MKDEV(SCSI_OSD_MAJOR, oud->minor));
317
318 /* We have 2 references to the cdev. One is released here
319 * and also takes down the /dev/osdX mapping. The second
320 * Will be released in __remove() after all users have released
321 * the osd_uld_device.
322 */
323 if (oud->cdev.owner)
324 cdev_del(&oud->cdev);
325
326 __uld_put(oud);
327 return 0;
328}
329
330static void __remove(struct kref *kref)
331{
332 struct osd_uld_device *oud = container_of(kref,
333 struct osd_uld_device, kref);
334 struct scsi_device *scsi_device = oud->od.scsi_device;
335
336 /* now let delete the char_dev */
337 kobject_put(&oud->cdev.kobj);
338
339 osd_dev_fini(&oud->od);
340 scsi_device_put(scsi_device);
341
342 OSD_INFO("osd_remove %s\n",
343 oud->disk ? oud->disk->disk_name : NULL);
344
345 if (oud->disk)
346 put_disk(oud->disk);
347
348 ida_remove(&osd_minor_ida, oud->minor);
349 kfree(oud);
350}
351
352static void __uld_get(struct osd_uld_device *oud)
353{
354 kref_get(&oud->kref);
355}
356
357static void __uld_put(struct osd_uld_device *oud)
358{
359 kref_put(&oud->kref, __remove);
360}
361
362/*
363 * Global driver and scsi registration
364 */
365
366static struct scsi_driver osd_driver = {
367 .owner = THIS_MODULE,
368 .gendrv = {
369 .name = osd_name,
370 .probe = osd_probe,
371 .remove = osd_remove,
372 }
373};
374
375static int __init osd_uld_init(void)
376{
377 int err;
378
379 osd_sysfs_class = class_create(THIS_MODULE, osd_symlink);
380 if (IS_ERR(osd_sysfs_class)) {
381 OSD_ERR("Unable to register sysfs class => %ld\n",
382 PTR_ERR(osd_sysfs_class));
383 return PTR_ERR(osd_sysfs_class);
384 }
385
386 err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0),
387 SCSI_OSD_MAX_MINOR, osd_name);
388 if (err) {
389 OSD_ERR("Unable to register major %d for osd ULD => %d\n",
390 SCSI_OSD_MAJOR, err);
391 goto err_out;
392 }
393
394 err = scsi_register_driver(&osd_driver.gendrv);
395 if (err) {
396 OSD_ERR("scsi_register_driver failed => %d\n", err);
397 goto err_out_chrdev;
398 }
399
400 OSD_INFO("LOADED %s\n", osd_version_string);
401 return 0;
402
403err_out_chrdev:
404 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
405err_out:
406 class_destroy(osd_sysfs_class);
407 return err;
408}
409
410static void __exit osd_uld_exit(void)
411{
412 scsi_unregister_driver(&osd_driver.gendrv);
413 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
414 class_destroy(osd_sysfs_class);
415 OSD_INFO("UNLOADED %s\n", osd_version_string);
416}
417
418module_init(osd_uld_init);
419module_exit(osd_uld_exit);