st.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. This file contains brief information about the SCSI tape driver.
  2. The driver is currently maintained by Kai Mäkisara (email
  3. Kai.Makisara@kolumbus.fi)
  4. Last modified: Mon Mar 7 21:14:44 2005 by kai.makisara
  5. BASICS
  6. The driver is generic, i.e., it does not contain any code tailored
  7. to any specific tape drive. The tape parameters can be specified with
  8. one of the following three methods:
  9. 1. Each user can specify the tape parameters he/she wants to use
  10. directly with ioctls. This is administratively a very simple and
  11. flexible method and applicable to single-user workstations. However,
  12. in a multiuser environment the next user finds the tape parameters in
  13. state the previous user left them.
  14. 2. The system manager (root) can define default values for some tape
  15. parameters, like block size and density using the MTSETDRVBUFFER ioctl.
  16. These parameters can be programmed to come into effect either when a
  17. new tape is loaded into the drive or if writing begins at the
  18. beginning of the tape. The second method is applicable if the tape
  19. drive performs auto-detection of the tape format well (like some
  20. QIC-drives). The result is that any tape can be read, writing can be
  21. continued using existing format, and the default format is used if
  22. the tape is rewritten from the beginning (or a new tape is written
  23. for the first time). The first method is applicable if the drive
  24. does not perform auto-detection well enough and there is a single
  25. "sensible" mode for the device. An example is a DAT drive that is
  26. used only in variable block mode (I don't know if this is sensible
  27. or not :-).
  28. The user can override the parameters defined by the system
  29. manager. The changes persist until the defaults again come into
  30. effect.
  31. 3. By default, up to four modes can be defined and selected using the minor
  32. number (bits 5 and 6). The number of modes can be changed by changing
  33. ST_NBR_MODE_BITS in st.h. Mode 0 corresponds to the defaults discussed
  34. above. Additional modes are dormant until they are defined by the
  35. system manager (root). When specification of a new mode is started,
  36. the configuration of mode 0 is used to provide a starting point for
  37. definition of the new mode.
  38. Using the modes allows the system manager to give the users choices
  39. over some of the buffering parameters not directly accessible to the
  40. users (buffered and asynchronous writes). The modes also allow choices
  41. between formats in multi-tape operations (the explicitly overridden
  42. parameters are reset when a new tape is loaded).
  43. If more than one mode is used, all modes should contain definitions
  44. for the same set of parameters.
  45. Many Unices contain internal tables that associate different modes to
  46. supported devices. The Linux SCSI tape driver does not contain such
  47. tables (and will not do that in future). Instead of that, a utility
  48. program can be made that fetches the inquiry data sent by the device,
  49. scans its database, and sets up the modes using the ioctls. Another
  50. alternative is to make a small script that uses mt to set the defaults
  51. tailored to the system.
  52. The driver supports fixed and variable block size (within buffer
  53. limits). Both the auto-rewind (minor equals device number) and
  54. non-rewind devices (minor is 128 + device number) are implemented.
  55. In variable block mode, the byte count in write() determines the size
  56. of the physical block on tape. When reading, the drive reads the next
  57. tape block and returns to the user the data if the read() byte count
  58. is at least the block size. Otherwise, error ENOMEM is returned.
  59. In fixed block mode, the data transfer between the drive and the
  60. driver is in multiples of the block size. The write() byte count must
  61. be a multiple of the block size. This is not required when reading but
  62. may be advisable for portability.
  63. Support is provided for changing the tape partition and partitioning
  64. of the tape with one or two partitions. By default support for
  65. partitioned tape is disabled for each driver and it can be enabled
  66. with the ioctl MTSETDRVBUFFER.
  67. By default the driver writes one filemark when the device is closed after
  68. writing and the last operation has been a write. Two filemarks can be
  69. optionally written. In both cases end of data is signified by
  70. returning zero bytes for two consecutive reads.
  71. If rewind, offline, bsf, or seek is done and previous tape operation was
  72. write, a filemark is written before moving tape.
  73. The compile options are defined in the file linux/drivers/scsi/st_options.h.
  74. 4. If the open option O_NONBLOCK is used, open succeeds even if the
  75. drive is not ready. If O_NONBLOCK is not used, the driver waits for
  76. the drive to become ready. If this does not happen in ST_BLOCK_SECONDS
  77. seconds, open fails with the errno value EIO. With O_NONBLOCK the
  78. device can be opened for writing even if there is a write protected
  79. tape in the drive (commands trying to write something return error if
  80. attempted).
  81. MINOR NUMBERS
  82. The tape driver currently supports 128 drives by default. This number
  83. can be increased by editing st.h and recompiling the driver if
  84. necessary. The upper limit is 2^17 drives if 4 modes for each drive
  85. are used.
  86. The minor numbers consist of the following bit fields:
  87. dev_upper non-rew mode dev-lower
  88. 20 - 8 7 6 5 4 0
  89. The non-rewind bit is always bit 7 (the uppermost bit in the lowermost
  90. byte). The bits defining the mode are below the non-rewind bit. The
  91. remaining bits define the tape device number. This numbering is
  92. backward compatible with the numbering used when the minor number was
  93. only 8 bits wide.
  94. SYSFS SUPPORT
  95. The driver creates the directory /sys/class/scsi_tape and populates it with
  96. directories corresponding to the existing tape devices. There are autorewind
  97. and non-rewind entries for each mode. The names are stxy and nstxy, where x
  98. is the tape number and y a character corresponding to the mode (none, l, m,
  99. a). For example, the directories for the first tape device are (assuming four
  100. modes): st0 nst0 st0l nst0l st0m nst0m st0a nst0a.
  101. Each directory contains the entries: default_blksize default_compression
  102. default_density defined dev device driver. The file 'defined' contains 1
  103. if the mode is defined and zero if not defined. The files 'default_*' contain
  104. the defaults set by the user. The value -1 means the default is not set. The
  105. file 'dev' contains the device numbers corresponding to this device. The links
  106. 'device' and 'driver' point to the SCSI device and driver entries.
  107. A link named 'tape' is made from the SCSI device directory to the class
  108. directory corresponding to the mode 0 auto-rewind device (e.g., st0).
  109. BSD AND SYS V SEMANTICS
  110. The user can choose between these two behaviours of the tape driver by
  111. defining the value of the symbol ST_SYSV. The semantics differ when a
  112. file being read is closed. The BSD semantics leaves the tape where it
  113. currently is whereas the SYS V semantics moves the tape past the next
  114. filemark unless the filemark has just been crossed.
  115. The default is BSD semantics.
  116. BUFFERING
  117. The driver tries to do transfers directly to/from user space. If this
  118. is not possible, a driver buffer allocated at run-time is used. If
  119. direct i/o is not possible for the whole transfer, the driver buffer
  120. is used (i.e., bounce buffers for individual pages are not
  121. used). Direct i/o can be impossible because of several reasons, e.g.:
  122. - one or more pages are at addresses not reachable by the HBA
  123. - the number of pages in the transfer exceeds the number of
  124. scatter/gather segments permitted by the HBA
  125. - one or more pages can't be locked into memory (should not happen in
  126. any reasonable situation)
  127. The size of the driver buffers is always at least one tape block. In fixed
  128. block mode, the minimum buffer size is defined (in 1024 byte units) by
  129. ST_FIXED_BUFFER_BLOCKS. With small block size this allows buffering of
  130. several blocks and using one SCSI read or write to transfer all of the
  131. blocks. Buffering of data across write calls in fixed block mode is
  132. allowed if ST_BUFFER_WRITES is non-zero and direct i/o is not used.
  133. Buffer allocation uses chunks of memory having sizes 2^n * (page
  134. size). Because of this the actual buffer size may be larger than the
  135. minimum allowable buffer size.
  136. NOTE that if direct i/o is used, the small writes are not buffered. This may
  137. cause a surprise when moving from 2.4. There small writes (e.g., tar without
  138. -b option) may have had good throughput but this is not true any more with
  139. 2.6. Direct i/o can be turned off to solve this problem but a better solution
  140. is to use bigger write() byte counts (e.g., tar -b 64).
  141. Asynchronous writing. Writing the buffer contents to the tape is
  142. started and the write call returns immediately. The status is checked
  143. at the next tape operation. Asynchronous writes are not done with
  144. direct i/o and not in fixed block mode.
  145. Buffered writes and asynchronous writes may in some rare cases cause
  146. problems in multivolume operations if there is not enough space on the
  147. tape after the early-warning mark to flush the driver buffer.
  148. Read ahead for fixed block mode (ST_READ_AHEAD). Filling the buffer is
  149. attempted even if the user does not want to get all of the data at
  150. this read command. Should be disabled for those drives that don't like
  151. a filemark to truncate a read request or that don't like backspacing.
  152. Scatter/gather buffers (buffers that consist of chunks non-contiguous
  153. in the physical memory) are used if contiguous buffers can't be
  154. allocated. To support all SCSI adapters (including those not
  155. supporting scatter/gather), buffer allocation is using the following
  156. three kinds of chunks:
  157. 1. The initial segment that is used for all SCSI adapters including
  158. those not supporting scatter/gather. The size of this buffer will be
  159. (PAGE_SIZE << ST_FIRST_ORDER) bytes if the system can give a chunk of
  160. this size (and it is not larger than the buffer size specified by
  161. ST_BUFFER_BLOCKS). If this size is not available, the driver halves
  162. the size and tries again until the size of one page. The default
  163. settings in st_options.h make the driver to try to allocate all of the
  164. buffer as one chunk.
  165. 2. The scatter/gather segments to fill the specified buffer size are
  166. allocated so that as many segments as possible are used but the number
  167. of segments does not exceed ST_FIRST_SG.
  168. 3. The remaining segments between ST_MAX_SG (or the module parameter
  169. max_sg_segs) and the number of segments used in phases 1 and 2
  170. are used to extend the buffer at run-time if this is necessary. The
  171. number of scatter/gather segments allowed for the SCSI adapter is not
  172. exceeded if it is smaller than the maximum number of scatter/gather
  173. segments specified. If the maximum number allowed for the SCSI adapter
  174. is smaller than the number of segments used in phases 1 and 2,
  175. extending the buffer will always fail.
  176. EOM BEHAVIOUR WHEN WRITING
  177. When the end of medium early warning is encountered, the current write
  178. is finished and the number of bytes is returned. The next write
  179. returns -1 and errno is set to ENOSPC. To enable writing a trailer,
  180. the next write is allowed to proceed and, if successful, the number of
  181. bytes is returned. After this, -1 and the number of bytes are
  182. alternately returned until the physical end of medium (or some other
  183. error) is encountered.
  184. MODULE PARAMETERS
  185. The buffer size, write threshold, and the maximum number of allocated buffers
  186. are configurable when the driver is loaded as a module. The keywords are:
  187. buffer_kbs=xxx the buffer size for fixed block mode is set
  188. to xxx kilobytes
  189. write_threshold_kbs=xxx the write threshold in kilobytes set to xxx
  190. max_sg_segs=xxx the maximum number of scatter/gather
  191. segments
  192. try_direct_io=x try direct transfer between user buffer and
  193. tape drive if this is non-zero
  194. Note that if the buffer size is changed but the write threshold is not
  195. set, the write threshold is set to the new buffer size - 2 kB.
  196. BOOT TIME CONFIGURATION
  197. If the driver is compiled into the kernel, the same parameters can be
  198. also set using, e.g., the LILO command line. The preferred syntax is
  199. to use the same keyword used when loading as module but prepended
  200. with 'st.'. For instance, to set the maximum number of scatter/gather
  201. segments, the parameter 'st.max_sg_segs=xx' should be used (xx is the
  202. number of scatter/gather segments).
  203. For compatibility, the old syntax from early 2.5 and 2.4 kernel
  204. versions is supported. The same keywords can be used as when loading
  205. the driver as module. If several parameters are set, the keyword-value
  206. pairs are separated with a comma (no spaces allowed). A colon can be
  207. used instead of the equal mark. The definition is prepended by the
  208. string st=. Here is an example:
  209. st=buffer_kbs:64,write_threshold_kbs:60
  210. The following syntax used by the old kernel versions is also supported:
  211. st=aa[,bb[,dd]]
  212. where
  213. aa is the buffer size for fixed block mode in 1024 byte units
  214. bb is the write threshold in 1024 byte units
  215. dd is the maximum number of scatter/gather segments
  216. IOCTLS
  217. The tape is positioned and the drive parameters are set with ioctls
  218. defined in mtio.h The tape control program 'mt' uses these ioctls. Try
  219. to find an mt that supports all of the Linux SCSI tape ioctls and
  220. opens the device for writing if the tape contents will be modified
  221. (look for a package mt-st* from the Linux ftp sites; the GNU mt does
  222. not open for writing for, e.g., erase).
  223. The supported ioctls are:
  224. The following use the structure mtop:
  225. MTFSF Space forward over count filemarks. Tape positioned after filemark.
  226. MTFSFM As above but tape positioned before filemark.
  227. MTBSF Space backward over count filemarks. Tape positioned before
  228. filemark.
  229. MTBSFM As above but ape positioned after filemark.
  230. MTFSR Space forward over count records.
  231. MTBSR Space backward over count records.
  232. MTFSS Space forward over count setmarks.
  233. MTBSS Space backward over count setmarks.
  234. MTWEOF Write count filemarks.
  235. MTWSM Write count setmarks.
  236. MTREW Rewind tape.
  237. MTOFFL Set device off line (often rewind plus eject).
  238. MTNOP Do nothing except flush the buffers.
  239. MTRETEN Re-tension tape.
  240. MTEOM Space to end of recorded data.
  241. MTERASE Erase tape. If the argument is zero, the short erase command
  242. is used. The long erase command is used with all other values
  243. of the argument.
  244. MTSEEK Seek to tape block count. Uses Tandberg-compatible seek (QFA)
  245. for SCSI-1 drives and SCSI-2 seek for SCSI-2 drives. The file and
  246. block numbers in the status are not valid after a seek.
  247. MTSETBLK Set the drive block size. Setting to zero sets the drive into
  248. variable block mode (if applicable).
  249. MTSETDENSITY Sets the drive density code to arg. See drive
  250. documentation for available codes.
  251. MTLOCK and MTUNLOCK Explicitly lock/unlock the tape drive door.
  252. MTLOAD and MTUNLOAD Explicitly load and unload the tape. If the
  253. command argument x is between MT_ST_HPLOADER_OFFSET + 1 and
  254. MT_ST_HPLOADER_OFFSET + 6, the number x is used sent to the
  255. drive with the command and it selects the tape slot to use of
  256. HP C1553A changer.
  257. MTCOMPRESSION Sets compressing or uncompressing drive mode using the
  258. SCSI mode page 15. Note that some drives other methods for
  259. control of compression. Some drives (like the Exabytes) use
  260. density codes for compression control. Some drives use another
  261. mode page but this page has not been implemented in the
  262. driver. Some drives without compression capability will accept
  263. any compression mode without error.
  264. MTSETPART Moves the tape to the partition given by the argument at the
  265. next tape operation. The block at which the tape is positioned
  266. is the block where the tape was previously positioned in the
  267. new active partition unless the next tape operation is
  268. MTSEEK. In this case the tape is moved directly to the block
  269. specified by MTSEEK. MTSETPART is inactive unless
  270. MT_ST_CAN_PARTITIONS set.
  271. MTMKPART Formats the tape with one partition (argument zero) or two
  272. partitions (the argument gives in megabytes the size of
  273. partition 1 that is physically the first partition of the
  274. tape). The drive has to support partitions with size specified
  275. by the initiator. Inactive unless MT_ST_CAN_PARTITIONS set.
  276. MTSETDRVBUFFER
  277. Is used for several purposes. The command is obtained from count
  278. with mask MT_SET_OPTIONS, the low order bits are used as argument.
  279. This command is only allowed for the superuser (root). The
  280. subcommands are:
  281. 0
  282. The drive buffer option is set to the argument. Zero means
  283. no buffering.
  284. MT_ST_BOOLEANS
  285. Sets the buffering options. The bits are the new states
  286. (enabled/disabled) the following options (in the
  287. parenthesis is specified whether the option is global or
  288. can be specified differently for each mode):
  289. MT_ST_BUFFER_WRITES write buffering (mode)
  290. MT_ST_ASYNC_WRITES asynchronous writes (mode)
  291. MT_ST_READ_AHEAD read ahead (mode)
  292. MT_ST_TWO_FM writing of two filemarks (global)
  293. MT_ST_FAST_EOM using the SCSI spacing to EOD (global)
  294. MT_ST_AUTO_LOCK automatic locking of the drive door (global)
  295. MT_ST_DEF_WRITES the defaults are meant only for writes (mode)
  296. MT_ST_CAN_BSR backspacing over more than one records can
  297. be used for repositioning the tape (global)
  298. MT_ST_NO_BLKLIMS the driver does not ask the block limits
  299. from the drive (block size can be changed only to
  300. variable) (global)
  301. MT_ST_CAN_PARTITIONS enables support for partitioned
  302. tapes (global)
  303. MT_ST_SCSI2LOGICAL the logical block number is used in
  304. the MTSEEK and MTIOCPOS for SCSI-2 drives instead of
  305. the device dependent address. It is recommended to set
  306. this flag unless there are tapes using the device
  307. dependent (from the old times) (global)
  308. MT_ST_SYSV sets the SYSV semantics (mode)
  309. MT_ST_NOWAIT enables immediate mode (i.e., don't wait for
  310. the command to finish) for some commands (e.g., rewind)
  311. MT_ST_DEBUGGING debugging (global; debugging must be
  312. compiled into the driver)
  313. MT_ST_SETBOOLEANS
  314. MT_ST_CLEARBOOLEANS
  315. Sets or clears the option bits.
  316. MT_ST_WRITE_THRESHOLD
  317. Sets the write threshold for this device to kilobytes
  318. specified by the lowest bits.
  319. MT_ST_DEF_BLKSIZE
  320. Defines the default block size set automatically. Value
  321. 0xffffff means that the default is not used any more.
  322. MT_ST_DEF_DENSITY
  323. MT_ST_DEF_DRVBUFFER
  324. Used to set or clear the density (8 bits), and drive buffer
  325. state (3 bits). If the value is MT_ST_CLEAR_DEFAULT
  326. (0xfffff) the default will not be used any more. Otherwise
  327. the lowermost bits of the value contain the new value of
  328. the parameter.
  329. MT_ST_DEF_COMPRESSION
  330. The compression default will not be used if the value of
  331. the lowermost byte is 0xff. Otherwise the lowermost bit
  332. contains the new default. If the bits 8-15 are set to a
  333. non-zero number, and this number is not 0xff, the number is
  334. used as the compression algorithm. The value
  335. MT_ST_CLEAR_DEFAULT can be used to clear the compression
  336. default.
  337. MT_ST_SET_TIMEOUT
  338. Set the normal timeout in seconds for this device. The
  339. default is 900 seconds (15 minutes). The timeout should be
  340. long enough for the retries done by the device while
  341. reading/writing.
  342. MT_ST_SET_LONG_TIMEOUT
  343. Set the long timeout that is used for operations that are
  344. known to take a long time. The default is 14000 seconds
  345. (3.9 hours). For erase this value is further multiplied by
  346. eight.
  347. MT_ST_SET_CLN
  348. Set the cleaning request interpretation parameters using
  349. the lowest 24 bits of the argument. The driver can set the
  350. generic status bit GMT_CLN if a cleaning request bit pattern
  351. is found from the extended sense data. Many drives set one or
  352. more bits in the extended sense data when the drive needs
  353. cleaning. The bits are device-dependent. The driver is
  354. given the number of the sense data byte (the lowest eight
  355. bits of the argument; must be >= 18 (values 1 - 17
  356. reserved) and <= the maximum requested sense data sixe),
  357. a mask to select the relevant bits (the bits 9-16), and the
  358. bit pattern (bits 17-23). If the bit pattern is zero, one
  359. or more bits under the mask indicate cleaning request. If
  360. the pattern is non-zero, the pattern must match the masked
  361. sense data byte.
  362. (The cleaning bit is set if the additional sense code and
  363. qualifier 00h 17h are seen regardless of the setting of
  364. MT_ST_SET_CLN.)
  365. The following ioctl uses the structure mtpos:
  366. MTIOCPOS Reads the current position from the drive. Uses
  367. Tandberg-compatible QFA for SCSI-1 drives and the SCSI-2
  368. command for the SCSI-2 drives.
  369. The following ioctl uses the structure mtget to return the status:
  370. MTIOCGET Returns some status information.
  371. The file number and block number within file are returned. The
  372. block is -1 when it can't be determined (e.g., after MTBSF).
  373. The drive type is either MTISSCSI1 or MTISSCSI2.
  374. The number of recovered errors since the previous status call
  375. is stored in the lower word of the field mt_erreg.
  376. The current block size and the density code are stored in the field
  377. mt_dsreg (shifts for the subfields are MT_ST_BLKSIZE_SHIFT and
  378. MT_ST_DENSITY_SHIFT).
  379. The GMT_xxx status bits reflect the drive status. GMT_DR_OPEN
  380. is set if there is no tape in the drive. GMT_EOD means either
  381. end of recorded data or end of tape. GMT_EOT means end of tape.
  382. MISCELLANEOUS COMPILE OPTIONS
  383. The recovered write errors are considered fatal if ST_RECOVERED_WRITE_FATAL
  384. is defined.
  385. The maximum number of tape devices is determined by the define
  386. ST_MAX_TAPES. If more tapes are detected at driver initialization, the
  387. maximum is adjusted accordingly.
  388. Immediate return from tape positioning SCSI commands can be enabled by
  389. defining ST_NOWAIT. If this is defined, the user should take care that
  390. the next tape operation is not started before the previous one has
  391. finished. The drives and SCSI adapters should handle this condition
  392. gracefully, but some drive/adapter combinations are known to hang the
  393. SCSI bus in this case.
  394. The MTEOM command is by default implemented as spacing over 32767
  395. filemarks. With this method the file number in the status is
  396. correct. The user can request using direct spacing to EOD by setting
  397. ST_FAST_EOM 1 (or using the MT_ST_OPTIONS ioctl). In this case the file
  398. number will be invalid.
  399. When using read ahead or buffered writes the position within the file
  400. may not be correct after the file is closed (correct position may
  401. require backspacing over more than one record). The correct position
  402. within file can be obtained if ST_IN_FILE_POS is defined at compile
  403. time or the MT_ST_CAN_BSR bit is set for the drive with an ioctl.
  404. (The driver always backs over a filemark crossed by read ahead if the
  405. user does not request data that far.)
  406. DEBUGGING HINTS
  407. To enable debugging messages, edit st.c and #define DEBUG 1. As seen
  408. above, debugging can be switched off with an ioctl if debugging is
  409. compiled into the driver. The debugging output is not voluminous.
  410. If the tape seems to hang, I would be very interested to hear where
  411. the driver is waiting. With the command 'ps -l' you can see the state
  412. of the process using the tape. If the state is D, the process is
  413. waiting for something. The field WCHAN tells where the driver is
  414. waiting. If you have the current System.map in the correct place (in
  415. /boot for the procps I use) or have updated /etc/psdatabase (for kmem
  416. ps), ps writes the function name in the WCHAN field. If not, you have
  417. to look up the function from System.map.
  418. Note also that the timeouts are very long compared to most other
  419. drivers. This means that the Linux driver may appear hung although the
  420. real reason is that the tape firmware has got confused.