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