Files
scst/scst_local
Vladislav Bolkhovitin fabf9fceda Avoid that removing the scst_local kernel module triggers a deadlock
when one of its LUNs is a CD-ROM device. sr_block_release() invokes
scst_local_release_adapter() indirectly with sr_mutex held. So if 
scst_local_release_adapter() blocks until the scst_local session has
been freed a deadlock may be triggered if another CD-ROM device has
to be closed in order to free that session. Fix this by changing
scst_local_release_adapter() such that it does not wait until the
session unregistration has finished.

Call stacks obtained with echo w > /proc/sysrq-trigger:

SysRq : Show Blocked State
  task                        PC stack   pid father
scst_mgmtd      D ffffffff812f979d     0  5552      2 0x00000000
 ffff88003c335b30 0000000000000046 ffffffff812f979d 0000000000000002
 ffff8800171d47a0 ffff88003c335fd8 ffff88003c335fd8 ffff88003c335fd8
 ffff88003d1b0000 ffff8800171d47a0 ffff88003c335b60 ffff88003c334000
Call Trace:
 [<ffffffff812f979d>] ? sr_block_release+0x2d/0x60
 [<ffffffff812f979d>] ? sr_block_release+0x2d/0x60
 [<ffffffff814a9b19>] schedule+0x29/0x70
 [<ffffffff814a9e57>] schedule_preempt_disabled+0x27/0x40
 [<ffffffff814a79b7>] mutex_lock_nested+0x187/0x390
 [<ffffffff812f979d>] ? sr_block_release+0x2d/0x60
 [<ffffffff812f979d>] sr_block_release+0x2d/0x60
 [<ffffffff8118a1ec>] __blkdev_put+0x19c/0x1d0
 [<ffffffff8118a27d>] blkdev_put+0x5d/0x180
 [<ffffffff8118a3c8>] blkdev_close+0x28/0x30
 [<ffffffff81153633>] fput+0x103/0x280
 [<ffffffff8114fad9>] filp_close+0x69/0x90
 [<ffffffffa022ec24>] vdisk_detach_tgt+0x134/0x150 [scst_vdisk]
 [<ffffffffa02d8f2f>] scst_free_tgt_dev+0xdf/0x220 [scst]
 [<ffffffffa02d949f>] ? scst_free_session_callback+0x7f/0x170 [scst]
 [<ffffffffa02d90eb>] scst_sess_free_tgt_devs+0x7b/0x110 [scst]
 [<ffffffffa02d91de>] scst_free_session+0x5e/0x2a0 [scst]
 [<ffffffffa02d94be>] scst_free_session_callback+0x9e/0x170 [scst]
 [<ffffffffa02c6a05>] scst_global_mgmt_thread+0x2e5/0x580 [scst]
 [<ffffffff8105e850>] ? __init_waitqueue_head+0x60/0x60
 [<ffffffffa02c6720>] ? scst_register_session_non_gpl+0x20/0x20 [scst]
 [<ffffffff8105dd77>] kthread+0xb7/0xc0
 [<ffffffff814b50b4>] kernel_thread_helper+0x4/0x10
 [<ffffffff814abb30>] ? retint_restore_args+0x13/0x13
 [<ffffffff8105dcc0>] ? __init_kthread_worker+0x70/0x70
 [<ffffffff814b50b0>] ? gs_change+0x13/0x13
iscsi-scstd     D ffffffffa02b1b1f     0  5567      1 0x00000004
 ffff88003c2f59a8 0000000000000046 ffffffffa02b1b1f 0000000000000002
 ffff8800173b23d0 ffff88003c2f5fd8 ffff88003c2f5fd8 ffff88003c2f5fd8
 ffff88003d198000 ffff8800173b23d0 ffff88003c2f59d8 ffff88003c2f4000
Call Trace:
 [<ffffffffa02b1b1f>] ? scst_unregister_target+0x8f/0x450 [scst]
 [<ffffffffa02b1b1f>] ? scst_unregister_target+0x8f/0x450 [scst]
 [<ffffffff814a9b19>] schedule+0x29/0x70
 [<ffffffff814a9e57>] schedule_preempt_disabled+0x27/0x40
 [<ffffffff814a79b7>] mutex_lock_nested+0x187/0x390
 [<ffffffffa02b1b1f>] ? scst_unregister_target+0x8f/0x450 [scst]
 [<ffffffffa025c7ba>] ? __iscsi_del_attr+0xea/0x190 [iscsi_scst]
 [<ffffffffa02b1b1f>] scst_unregister_target+0x8f/0x450 [scst]
 [<ffffffffa025c7ba>] ? __iscsi_del_attr+0xea/0x190 [iscsi_scst]
 [<ffffffffa026249f>] target_destroy+0x5f/0xc0 [iscsi_scst]
 [<ffffffffa02631f2>] target_del_all+0xf2/0x2d0 [iscsi_scst]
 [<ffffffffa025c8aa>] release+0x4a/0xe0 [iscsi_scst]
 [<ffffffff81153633>] fput+0x103/0x280
 [<ffffffff8114fad9>] filp_close+0x69/0x90
 [<ffffffff8103e533>] put_files_struct+0x103/0x1c0
 [<ffffffff8103e478>] ? put_files_struct+0x48/0x1c0
 [<ffffffff8103e6b2>] exit_files+0x52/0x60
 [<ffffffff8103eb80>] do_exit+0x180/0x8e0
 [<ffffffff8104ef87>] ? get_signal_to_deliver+0xe7/0x6e0
 [<ffffffff8103f631>] do_group_exit+0x51/0xc0
 [<ffffffff8104f0bf>] get_signal_to_deliver+0x21f/0x6e0
 [<ffffffff8100223c>] do_signal+0x3c/0x580
 [<ffffffff81002805>] do_notify_resume+0x65/0xa0
 [<ffffffff812149de>] ? trace_hardirqs_on_thunk+0x3a/0x3f
 [<ffffffff814b4122>] int_signal+0x12/0x17
rmmod           D ffffffffa02b1b70     0  5714   5681 0x00000000
 ffff88000b497d48 0000000000000046 ffffffffa02b1b70 0000000000000002
 ffff88003920a3d0 ffff88000b497fd8 ffff88000b497fd8 ffff88000b497fd8
 ffff88003d17a3d0 ffff88003920a3d0 ffff88000b497d78 ffff88000b496000
Call Trace:
 [<ffffffffa02b1b70>] ? scst_unregister_target+0xe0/0x450 [scst]
 [<ffffffffa02b1b70>] ? scst_unregister_target+0xe0/0x450 [scst]
 [<ffffffff814a9b19>] schedule+0x29/0x70
 [<ffffffff814a9e57>] schedule_preempt_disabled+0x27/0x40
 [<ffffffff814a79b7>] mutex_lock_nested+0x187/0x390
 [<ffffffffa02b1b70>] ? scst_unregister_target+0xe0/0x450 [scst]
 [<ffffffffa02b1b70>] scst_unregister_target+0xe0/0x450 [scst]
 [<ffffffffa0287e51>] ? scst_local_exit+0x6b/0x21a [scst_local]
 [<ffffffff812c2d37>] ? put_device+0x17/0x20
 [<ffffffff812c3efa>] ? device_unregister+0x2a/0x60
 [<ffffffffa0285108>] __scst_local_remove_target+0xa8/0x130 [scst_local]
 [<ffffffffa0287e6a>] scst_local_exit+0x84/0x21a [scst_local]
 [<ffffffff810a40c6>] sys_delete_module+0x1a6/0x2b0
 [<ffffffff812149de>] ? trace_hardirqs_on_thunk+0x3a/0x3f
 [<ffffffff814b3de9>] system_call_fastpath+0x16/0x1b
ata_id          D ffff88003920c7a0     0  5715   5195 0x00000000
 ffff880015e97818 0000000000000046 0000000000000000 0000000000000046
 ffff88003920c7a0 ffff880015e97fd8 ffff880015e97fd8 ffff880015e97fd8
 ffff880021788000 ffff88003920c7a0 ffff880015e977f8 7fffffffffffffff
Call Trace:
 [<ffffffff814a9b19>] schedule+0x29/0x70
 [<ffffffff814a7355>] schedule_timeout+0x315/0x390
 [<ffffffff814a98e3>] ? wait_for_common+0x43/0x170
 [<ffffffff814a98e3>] ? wait_for_common+0x43/0x170
 [<ffffffff814a99c2>] wait_for_common+0x122/0x170
 [<ffffffff810705b0>] ? try_to_wake_up+0x2f0/0x2f0
 [<ffffffff814a9aed>] wait_for_completion+0x1d/0x20
 [<ffffffffa02bfad3>] scst_unregister_session+0x1a3/0x320 [scst]
 [<ffffffffa0284990>] scst_local_release_adapter+0xa0/0x140 [scst_local]
 [<ffffffff812c2f87>] device_release+0x27/0xa0
 [<ffffffff81209abb>] kobject_release+0x8b/0x1d0
 [<ffffffff8120993c>] kobject_put+0x2c/0x60
 [<ffffffff812c2d37>] put_device+0x17/0x20
 [<ffffffff812e1850>] scsi_host_dev_release+0xc0/0xe0
 [<ffffffff812c2f87>] device_release+0x27/0xa0
 [<ffffffff81209abb>] kobject_release+0x8b/0x1d0
 [<ffffffff8120993c>] kobject_put+0x2c/0x60
 [<ffffffff812c2d37>] put_device+0x17/0x20
 [<ffffffff812ea0c2>] scsi_target_dev_release+0x22/0x30
 [<ffffffff812c2f87>] device_release+0x27/0xa0
 [<ffffffff81209abb>] kobject_release+0x8b/0x1d0
 [<ffffffff8120993c>] kobject_put+0x2c/0x60
 [<ffffffff812c2d37>] put_device+0x17/0x20
 [<ffffffff812ed818>] scsi_device_dev_release_usercontext+0x118/0x130
 [<ffffffff812ed700>] ? scsi_device_cls_release+0x20/0x20
 [<ffffffff81056576>] execute_in_process_context+0x86/0xa0
 [<ffffffff812ed6dc>] scsi_device_dev_release+0x1c/0x20
 [<ffffffff812c2f87>] device_release+0x27/0xa0
 [<ffffffff81209abb>] kobject_release+0x8b/0x1d0
 [<ffffffff8120993c>] kobject_put+0x2c/0x60
 [<ffffffff812c2d37>] put_device+0x17/0x20
 [<ffffffff812df594>] scsi_device_put+0x44/0x60
 [<ffffffff812f974d>] scsi_cd_put+0x4d/0x70
 [<ffffffff812f97b1>] sr_block_release+0x41/0x60
 [<ffffffff8118a1ec>] __blkdev_put+0x19c/0x1d0
 [<ffffffff8118a27d>] blkdev_put+0x5d/0x180
 [<ffffffff8118a3c8>] blkdev_close+0x28/0x30
 [<ffffffff81153633>] fput+0x103/0x280
 [<ffffffff814a93b3>] ? __schedule+0x3c3/0x820
 [<ffffffff8114fad9>] filp_close+0x69/0x90
 [<ffffffff8114fba0>] sys_close+0xa0/0x100
 [<ffffffff814b3de9>] system_call_fastpath+0x16/0x1b

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Cc: Richard Sharpe <realrichardsharpe@gmail.com>




git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@4425 d57e44dd-8a1f-0410-8b47-8ef2f437770f
2012-07-27 01:14:55 +00:00
..
2011-11-04 02:24:35 +00:00
2011-09-01 20:15:42 +00:00

SCST Local ...
Richard Sharpe, 30-Nov-2008

This is the SCST Local driver. Its function is to allow you to access devices
that are exported via SCST directly on the same Linux system that they are
exported from.

No assumptions are made in the code about the device types on the target, so
any device handlers that you load in SCST should be visible, including tapes
and so forth.

You can freely use any sg, sd, st, etc. devices imported from target,
except the following: you can't mount file systems or put swap on them
for all dev handlers, except BLOCKIO and pass-through, because it can
lead to recursive memory allocation deadlock. This is a limitation of
Linux memory/cache manager. See SCST README file for details. For
BLOCKIO and pass-through dev handlers there's no such limitation, so you
can freely mount file systems over them.

To build, simply issue 'make' in the scst_local directory.

Try 'modinfo scst_local' for a listing of module parameters so far.

Here is how I have used it so far:

1. Load up scst:

  modprobe scst
  modprobe scst_vdisk

2. Create a virtual disk (or your own device handler):

  dd if=/dev/zero of=/some/path/vdisk1.img bs=16384 count=1000000
  echo "add_device vm_disk1 filename=/some/path/vdisk1.img" >/sys/kernel/scst_tgt/handlers/vdisk_fileio/mgmt

3. Load the scst_local driver:

  insmod scst_local
  echo "add vm_disk1 0" >/sys/kernel/scst_tgt/targets/scst_local/scst_local_tgt/luns/mgmt

4. Check what you have

   cat /proc/scsi/scsi
  Attached devices:
  Host: scsi0 Channel: 00 Id: 00 Lun: 00
    Vendor: ATA      Model: ST9320320AS      Rev: 0303
    Type:   Direct-Access                    ANSI  SCSI revision: 05
  Host: scsi4 Channel: 00 Id: 00 Lun: 00
    Vendor: TSSTcorp Model: CD/DVDW TS-L632D Rev: TO04
    Type:   CD-ROM                           ANSI  SCSI revision: 05
  Host: scsi7 Channel: 00 Id: 00 Lun: 00
    Vendor: SCST_FIO Model: vm_disk1         Rev:  200
    Type:   Direct-Access                    ANSI  SCSI revision: 04

Or instead of manually "add_device" in (2) and step (3) write a
scstadmin config:

HANDLER vdisk_fileio {
        DEVICE vm_disk1 {
        	filename /some/path/vdisk1.img
        }
}

TARGET_DRIVER scst_local {
	TARGET scst_local_tgt {
		LUN 0 vm_disk1
	}
}

then:

  insmod scst_local
  scstadmin -config conf_file.cfg

More advanced examples:

For (3) you can:

  insmod scst_local add_default_tgt=0
  echo "add_target scst_local_tgt session_name=scst_local_host" >/sys/kernel/scst_tgt/targets/scst_local//mgmt
  echo "add vm_disk1 0" >/sys/kernel/scst_tgt/targets/scst_local/scst_local_tgt/luns/mgmt

Scst_local module's parameter add_default_tgt disables creation of
default target "scst_local_tgt" and session "scst_local_host", so you
needed to create it manually.

There can be any number of targets and sessions created. Each SCST
session corresponds to SCSI host. You can change which LUNs assigned to
each session by using SCST access control. This mode is intended for
user space target drivers (see below).

Alternatively, you can write an scstadmin's config file conf_file.cfg:

HANDLER vdisk_fileio {
        DEVICE vm_disk1 {
        	filename /some/path/vdisk1.img
        }
}

TARGET_DRIVER scst_local {
	TARGET scst_local_tgt {
		session_name scst_local_host

		LUN 0 vm_disk1
	}
}

then:

  insmod scst_local add_default_tgt=0
  scstadmin -config conf_file.cfg

NOTE! Although scstadmin allows to create scst_local's sessions using
"session_name" expression, it doesn't save existing sessions during
writing config file by "write_config" command. If you need this
functionality, feel free to send a request for it in SCST development
mailing list.

5. Have fun.

Some of this was coded while in Santa Clara, some in Bangalore, and some in
Hyderabad. Noe doubt some will be coded on the way back to Santa Clara.

The code still has bugs, so if you encounter any, email me the fixes at:

   realrichardsharpe@gmail.com

I am thinking of renaming this to something more interesting.


Sysfs interface
===============

See SCST's README for a common SCST sysfs description.

Root of this driver is /sys/kernel/scst_tgt/targets/scst_local. It has
the following additional entry:

 - stats - read-only attribute with some statistical information.

Each target subdirectory contains the following additional entries:

 - phys_transport_version - contains and allows to change physical
   transport version descriptor. It determines by which physical
   interface this target will look like. See SPC for more details. By
   default, it is not defined (0).

 - scsi_transport_version - contains and allows to change SCSI
   transport version descriptor. It determines by which SCSI
   transport this target will look like. See SPC for more details. By
   default, it is SAS.

Each session subdirectory contains the following additional entries:

 - transport_id - contains this host's TransportID. This TransportID
   used to identify initiator in Persisten Reservation commands. If you
   change scsi_transport_version for a target, make sure you set for all
   its sessions correct TransportID. See SPC for more details.

 - host - links to the corresponding SCSI host. Using it you can find
   local sg/bsg/sd/etc. devices of this session. For instance, this
   links points out to host12, so you can find your sg devices by:

$ lsscsi -g|grep "\[12:"
[12:0:0:0]   disk    SCST_FIO rd1               200  /dev/sdc  /dev/sg2
[12:0:0:1]   disk    SCST_FIO nullio            200  /dev/sdd  /dev/sg3

They are /dev/sg2 and /dev/sg3.

The following management commands available via /sys/kernel/scst_tgt/targets/scst_local/mgmt:

 - add_target target_name [session_name=sess_name; [session_name=sess_name1;] [...]] -
   creates a target with optionally one or more sessions.

 - del_target target_name - deletes a target.

 - add_session target_name session_name - adds to target target_name
   session (SCSI host) with name session_name.

 - del_session target_name session_name - deletes session session_name
    from target target_name.


Note on performance
===================

Although this driver implemented in the most performance effective way,
including zero-copy passing data between SCSI/block subsystems and SCST,
in many cases it is NOT suited to measure performance as a NULL link.
For example, it is not suited for max IOPS measurements. This is because
for such cases not performance of the link between the target and
initiator is the bottleneck, but CPU or memory speed on the target or
initiator. For scst_local you have both initiator and target on the same
system, which means each your initiator and target are much less
CPU/memory powerful.


User space target drivers
=========================

Scst_local can be used to write full featured SCST target drivers in
user space:

1. For each SCSI target a user space target driver should create an
   scst_local's target using "add_target" command.

2. Then the user space target driver should, if needed, set its SCSI and
   physical transport version descriptors using attributes
   scsi_transport_version and phys_transport_version correspondingly in
   /sys/kernel/scst_tgt/targets/scst_local/target_name directory.

3. For incoming session (I_T nexus) from an initiator the user space
   target driver should create scst_local's session using "add_session"
   command.

4. Then, if needed, the user space target driver should set TransportID
   for this session (I_T nexus) using attribute
   /sys/kernel/scst_tgt/targets/scst_local/target_name/sessions/session_name/transport_id

5. Then the user space target driver should find out sg/bsg devices for
   the LUNs the created session has using link
   /sys/kernel/scst_tgt/targets/scst_local/target_name/sessions/session_name/host
   as described above.

6. Then the user space target driver can start serving the initiator using
   found sg/bsg devices.

For other connected initiators steps 3-6 should be repeated.


Compilation options
===================

There are the following compilation options, that could be commented
in/out in Makefile:

 - CONFIG_SCST_LOCAL_FORCE_DIRECT_PROCESSING - by default, when this option
   is not defined, scst_local reschedules all commands for processing in
   one of the SCST threads. If this option is defined, scst_local tries
   to not do it, if possible (sometimes queuecommand() called under
   various locks held), but instead process them in the submitter's
   context. This is to increase performance, but as on 2.6.37 and below
   Linux block layer doesn't work with such kind of reentrance, hence
   this option disabled by default. Note! At the moment in
   scst_estimate_context*() returning DIRECT contexts disabled, so this
   option doesn't have any real effect.


Change log
==========

V0.1 24-Sep-2008 (Hyderabad) Initial coding, pretty chatty and messy,
                             but worked.

V0.2 25-Sep-2008 (Hong Kong) Cleaned up the code a lot, reduced the log
			     chatter, fixed a bug where multiple LUNs did not
			     work. Also, added logging control. Tested with
			     five virtual disks. They all came up as /dev/sdb
			     through /dev/sdf and I could dd to them. Also
			     fixed a bug preventing multiple adapters.

V0.3 26-Sep-2008 (Santa Clara) Added back a copyright plus cleaned up some
			       unused functions and structures.

V0.4 5-Oct-2008 (Santa Clara) Changed name to scst_local as suggested, cleaned
			      up some unused variables (made them used) and
			      change allocation to a kmem_cache pool.

V0.5 5-Oct-2008 (Santa Clara) Added mgmt commands to handle dev reset and
			      aborts. Not sure if aborts works. Also corrected
			      the version info and renamed readme to README.

V0.6 7-Oct-2008 (Santa Clara) Removed some redundant code and made some
			      changes suggested by Vladislav.

V0.7 11-Oct-2008 (Santa Clara) Moved into the scst tree. Cleaned up some
			       unused functions, used TRACE macros etc.

V0.9 30-Nov-2008 (Mtn View) Cleaned up an additional problem with symbols not
			    being defined in older version of the kernel. Also
			    fixed some English and cleaned up this doc.

V1.0 10-Sep-2010 (Moscow)   Sysfs management added. Reviewed and cleaned up.

V2.1                        Update for kernels up to 3.0. Cleanups.