scsi-spin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. File: scsi-spin.c
  3. A simple program to manually spin up and down a scsi device.
  4. Copyright 1998 Rob Browning <rlb@cs.utexas.edu>
  5. Copyright 2001 Eric Delaunay <delaunay@debian.org>
  6. This source is covered by the terms the GNU Public License.
  7. Some of the original code came from
  8. The Linux SCSI programming HOWTO
  9. Heiko Ei<DF>feldt heiko@colossus.escape.de
  10. v1.5, 7 May 1996
  11. */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <getopt.h>
  16. #include <string.h>
  17. #include <fcntl.h>
  18. #include <errno.h>
  19. #include <mntent.h>
  20. #include <sys/ioctl.h>
  21. #include <scsi/sg.h>
  22. #include <scsi/scsi.h>
  23. #include <scsi/scsi_ioctl.h>
  24. #include <linux/major.h>
  25. #include <sys/sysmacros.h>
  26. #include <sys/stat.h>
  27. #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
  28. ((M) >= SCSI_DISK1_MAJOR && \
  29. (M) <= SCSI_DISK7_MAJOR) || \
  30. ((M) >= SCSI_DISK8_MAJOR && \
  31. (M) <= SCSI_DISK15_MAJOR))
  32. #define SCSI_BLK_MAJOR(M) \
  33. (SCSI_DISK_MAJOR(M) || \
  34. (M) == SCSI_CDROM_MAJOR)
  35. /* define USE_SG_IO to send commands using scsi generic interface
  36. */
  37. #define USE_SG_IO
  38. #ifdef USE_SG_IO
  39. int opt_oldioctl = 0;
  40. int opt_verbose = 0;
  41. const char* SENSE_KEY_STR[16] = {
  42. "NO SENSE",
  43. "RECOVERED ERROR",
  44. "NOT READY",
  45. "MEDIUM ERROR",
  46. "HARDWARE ERROR",
  47. "ILLEGAL REQUEST",
  48. "UNIT ATTENTION",
  49. "DATA PROJECT",
  50. "BLANK CHECK",
  51. "VENDOR-SPECIFIC",
  52. "COPY ARBORTED",
  53. "ABORTED COMMAND",
  54. "EQUAL",
  55. "VOLUME OVERFLOW",
  56. "MISCOMPARED",
  57. "RESERVED"
  58. };
  59. /* process a complete SCSI cmd. Use the generic SCSI interface. */
  60. static int handle_SCSI_cmd(const int fd,
  61. const unsigned cmd_len, /* command length */
  62. unsigned char *cmd, /* command buffer */
  63. const unsigned in_size, /* input data size */
  64. const unsigned out_size, /* output data size */
  65. unsigned char *io_buff, /* i/o buffer */
  66. unsigned sense_size, /* sense buf length */
  67. unsigned char* sense_buff, /* sense buffer */
  68. const unsigned timeout /* timeout in s */
  69. ) {
  70. ssize_t status = 0;
  71. int k, err;
  72. sg_io_hdr_t sg_hdr;
  73. unsigned char sense[16];
  74. /* safety checks */
  75. if (!cmd_len) return -1; /* need a cmd_len != 0 */
  76. if (in_size > 0 && io_buff == NULL) return -1; /* need an input buffer != NULL */
  77. /* generic SCSI device header construction */
  78. memset(&sg_hdr, 0, sizeof(sg_hdr));
  79. sg_hdr.interface_id = 'S';
  80. sg_hdr.dxfer_direction = SG_DXFER_NONE;
  81. sg_hdr.cmd_len = cmd_len;
  82. sg_hdr.cmdp = cmd;
  83. sg_hdr.dxfer_len = in_size;
  84. sg_hdr.dxferp = io_buff;
  85. sg_hdr.timeout = (timeout ? timeout : 2)*1000; /* timeout in ms */
  86. if (sense_buff == NULL) {
  87. sense_buff = sense;
  88. sense_size = sizeof(sense);
  89. }
  90. sg_hdr.mx_sb_len = sense_size;
  91. sg_hdr.sbp = sense_buff;
  92. if (opt_verbose > 1) {
  93. fprintf( stderr, " cmd = " );
  94. for( k = 0 ; k < cmd_len ; k++ )
  95. fprintf( stderr, " %02x", cmd[k] );
  96. fputc( '\n', stderr );
  97. }
  98. /* send command */
  99. status = ioctl( fd, SG_IO, &sg_hdr );
  100. if (status < 0 || sg_hdr.masked_status == CHECK_CONDITION) {
  101. /* some error happened */
  102. fprintf( stderr, "SG_IO: status = 0x%x cmd = 0x%x\n",
  103. sg_hdr.status, cmd[0] );
  104. if (opt_verbose > 0) {
  105. fprintf( stderr, " sense = " );
  106. for( k = 0 ; k < sg_hdr.sb_len_wr ; k++ )
  107. fprintf( stderr, " %02x", sense_buff[k] );
  108. fputc( '\n', stderr );
  109. err = sense_buff[0] & 0x7f;
  110. if (err == 0x70 || err == 0x71) {
  111. fprintf( stderr, " (%s)\n", SENSE_KEY_STR[sense_buff[2] & 0xf] );
  112. }
  113. }
  114. perror("");
  115. }
  116. return status; /* 0 means no error */
  117. }
  118. #endif
  119. static void
  120. scsi_spin(const int fd, const int desired_state, const int load_eject, const int wait) {
  121. #ifdef USE_SG_IO
  122. if (! opt_oldioctl) {
  123. unsigned char cmdblk [6] =
  124. { START_STOP, /* command */
  125. (wait ? 0 : 1), /* lun(3 bits)/reserved(4 bits)/immed(1 bit) */
  126. 0, /* reserved */
  127. 0, /* reserved */
  128. (load_eject ? 2 : 0)
  129. | (desired_state ? 1 : 0), /* reserved(6)/LoEj(1)/Start(1)*/
  130. 0 };/* reserved/flag/link */
  131. if (handle_SCSI_cmd(fd, sizeof(cmdblk), cmdblk, 0, 0, NULL, 0, NULL, wait)) {
  132. fprintf( stderr, "start/stop failed\n" );
  133. exit(2);
  134. }
  135. return;
  136. }
  137. #endif
  138. int ret;
  139. if (desired_state != 0)
  140. ret = ioctl( fd, SCSI_IOCTL_START_UNIT );
  141. else
  142. ret = ioctl( fd, SCSI_IOCTL_STOP_UNIT );
  143. if (ret < 0)
  144. perror( "scsi_spin: ioctl" );
  145. }
  146. static void
  147. scsi_lock(const int fd, const int door_lock) {
  148. #ifdef USE_SG_IO
  149. if (! opt_oldioctl) {
  150. unsigned char cmdblk [6] =
  151. { ALLOW_MEDIUM_REMOVAL, /* command */
  152. 0, /* lun(3 bits)/reserved(5 bits) */
  153. 0, /* reserved */
  154. 0, /* reserved */
  155. (door_lock ? 1 : 0), /* reserved(7)/Prevent(1)*/
  156. 0 };/* control */
  157. if (handle_SCSI_cmd(fd, sizeof(cmdblk), cmdblk, 0, 0, NULL, 0, NULL, 2)) {
  158. fprintf( stderr, "lock/unlock failed\n" );
  159. exit(2);
  160. }
  161. return;
  162. }
  163. #endif
  164. int ret;
  165. if (door_lock != 0)
  166. ret = ioctl( fd, SCSI_IOCTL_DOORLOCK );
  167. else
  168. ret = ioctl( fd, SCSI_IOCTL_DOORUNLOCK );
  169. if (ret < 0)
  170. perror( "scsi_lock: ioctl" );
  171. }
  172. /* -- [ED] --
  173. * Check if the device has some of its partitions mounted.
  174. * The check is done by comparison between device major and minor numbers so it
  175. * even works when the device name of the mount point is not the same of the
  176. * one passed to scsi-spin (for example, scsidev creates device aliases under
  177. * /dev/scsi).
  178. */
  179. static int
  180. is_mounted( const char* device, int use_proc, int devmaj, int devmin )
  181. {
  182. struct mntent *mnt;
  183. struct stat devstat;
  184. int mounted = 0;
  185. struct {
  186. __uint32_t dev_id;
  187. __uint32_t host_unique_id;
  188. } scsi_dev_id, scsi_id;
  189. FILE *mtab;
  190. char *mtabfile = use_proc ? "/proc/mounts" : "/etc/mtab";
  191. if (devmaj == SCSI_GENERIC_MAJOR) {
  192. /* scsi-spin device arg is /dev/sgN */
  193. int fd = open( device, O_RDONLY );
  194. if (fd >= 0) {
  195. int ret = ioctl( fd, SCSI_IOCTL_GET_IDLUN, &scsi_dev_id );
  196. close( fd );
  197. if (ret < 0)
  198. return -1;
  199. }
  200. }
  201. /*printf("devid=%x\n",scsi_dev_id.dev_id);*/
  202. mtab = setmntent( mtabfile, "r" );
  203. if (mtab == NULL)
  204. return -1;
  205. while ((mnt = getmntent( mtab )) != 0) {
  206. char * mdev = mnt->mnt_fsname;
  207. if (stat( mdev, &devstat ) == 0) {
  208. int maj = major(devstat.st_rdev);
  209. int min = minor(devstat.st_rdev);
  210. if (SCSI_DISK_MAJOR(maj) && SCSI_DISK_MAJOR(devmaj)) {
  211. if (maj == devmaj && (min & ~15) == (devmin & ~15)) {
  212. mounted = 1;
  213. break;
  214. }
  215. }
  216. else if (devmaj == SCSI_GENERIC_MAJOR && SCSI_BLK_MAJOR(maj)) {
  217. /* scsi-spin device arg is /dev/sgN */
  218. int fd = open( mdev, O_RDONLY );
  219. if (fd >= 0) {
  220. int ret = ioctl( fd, SCSI_IOCTL_GET_IDLUN, &scsi_id );
  221. close( fd );
  222. /*printf("id=%x\n",scsi_id.dev_id);*/
  223. if (ret == 0 && scsi_id.dev_id == scsi_dev_id.dev_id) {
  224. /* same SCSI ID => same device */
  225. mounted = 1;
  226. break;
  227. }
  228. }
  229. }
  230. else if (maj == SCSI_CDROM_MAJOR && maj == devmaj && min == devmin) {
  231. mounted = 1;
  232. break;
  233. }
  234. }
  235. }
  236. endmntent( mtab );
  237. return mounted;
  238. }
  239. static void
  240. usage()
  241. {
  242. static char usage_string[] =
  243. "usage: scsi-spin {-u,-d} [-nfpe] device\n"
  244. " -u, --up spin up device.\n"
  245. " -d, --down spin down device.\n"
  246. " -v, --verbose[=n] verbose mode (1: normal, 2: debug).\n"
  247. #ifdef SG_IO
  248. " -e, --loej load (-u) or eject (-d) removable medium.\n"
  249. " -w, --wait=[n] wait the spin up/down operation to be completed\n"
  250. " (n is the number of seconds to timeout).\n"
  251. " -I, --oldioctl use legacy ioctl instead of SG I/O (-e,-w ignored).\n"
  252. #endif
  253. " -l, --lock prevent medium removal.\n"
  254. " -L, --unlock allow medium removal.\n"
  255. " -n, --noact do nothing but check if the device is in use.\n"
  256. " -f, --force force spinning up/down even if the device is in use.\n"
  257. " -p, --proc use /proc/mounts instead of /etc/mtab to do the check.\n"
  258. " device is one of /dev/sd[a-z], /dev/scd[0-9]* or /dev/sg[0-9]*.\n";
  259. fputs(usage_string, stderr);
  260. }
  261. int
  262. main(int argc, char *argv[])
  263. {
  264. int result = 0;
  265. int fd;
  266. int opt_up = 0;
  267. int opt_down = 0;
  268. int opt_loej = 0;
  269. int opt_wait = 0;
  270. int opt_force = 0;
  271. int opt_noact = 0;
  272. int opt_proc = 0;
  273. int opt_lock = 0;
  274. int opt_unlock = 0;
  275. struct option cmd_line_opts[] = {
  276. {"verbose", 2, NULL, 'v'},
  277. {"up", 0, NULL, 'u'},
  278. {"down", 0, NULL, 'd'},
  279. #ifdef SG_IO
  280. {"loej", 0, NULL, 'e'},
  281. {"wait", 2, NULL, 'w'},
  282. {"oldioctl", 0, NULL, 'I'},
  283. #endif
  284. {"lock", 0, NULL, 'l'},
  285. {"unlock", 0, NULL, 'L'},
  286. {"force", 0, NULL, 'f'},
  287. {"noact", 0, NULL, 'n'},
  288. {"proc", 0, NULL, 'p'},
  289. {0, 0, 0, 0},
  290. };
  291. char* endptr = "";
  292. char* device;
  293. struct stat devstat;
  294. char c;
  295. while((c = getopt_long(argc, argv, "vudewlLfnp", cmd_line_opts, NULL)) != EOF) {
  296. switch (c) {
  297. case 'v': opt_verbose = optarg ? strtol(optarg, &endptr, 10) : opt_verbose+1;
  298. if (*endptr) goto error;
  299. break;
  300. case 'u': opt_up = 1; break;
  301. case 'd': opt_down = 1; break;
  302. #ifdef SG_IO
  303. case 'e': opt_loej = 1; break;
  304. case 'w': opt_wait = optarg ? strtol(optarg, &endptr, 10) : opt_wait+1;
  305. if (*endptr) goto error;
  306. break;
  307. case 'I': opt_oldioctl = 1; break;
  308. #endif
  309. case 'f': opt_force = 1; break;
  310. case 'l': opt_lock = 1; break;
  311. case 'L': opt_unlock = 1; break;
  312. case 'n': opt_noact = 1; break;
  313. case 'p': opt_proc = 1; break;
  314. default:
  315. error:
  316. usage();
  317. exit(1);
  318. }
  319. }
  320. if(opt_up && opt_down) {
  321. fputs("scsi-spin: specified both --up and --down. "
  322. "Is this some kind of test?\n", stderr);
  323. exit(1);
  324. }
  325. if(opt_lock && opt_unlock) {
  326. fputs("scsi-spin: specified both --lock and --unlock. "
  327. "Is this some kind of test?\n", stderr);
  328. exit(1);
  329. }
  330. if (opt_oldioctl && (opt_wait || opt_loej)) {
  331. fputs("scsi-spin: -e or -w not working in old ioctl mode.\n", stderr);
  332. exit(1);
  333. }
  334. if(!(opt_up || opt_down || opt_lock || opt_unlock)) {
  335. fputs("scsi-spin: must specify --up, --down, --lock or --unlock at least.\n", stderr);
  336. exit(1);
  337. }
  338. if(optind != (argc - 1)) {
  339. usage();
  340. exit(1);
  341. }
  342. device = argv[optind];
  343. if(stat(device, &devstat) == -1) {
  344. fprintf(stderr, "scsi-spin [stat]: %s: %s\n", device, strerror(errno));
  345. result = 1;
  346. }
  347. if (is_mounted( device, opt_proc, major(devstat.st_rdev), minor(devstat.st_rdev) )) {
  348. if (! opt_force) {
  349. fprintf( stderr, "scsi-spin: device already in use (mounted partition)\n" );
  350. exit(1);
  351. }
  352. else {
  353. fprintf( stderr, "scsi-spin [warning]: device is mounted but --force is passed\n" );
  354. }
  355. }
  356. /* first try to open the device r/w */
  357. fd = open(device, O_RDWR);
  358. if (fd < 0) {
  359. /* if it's fail, then try ro */
  360. fd = open(device, O_RDONLY);
  361. if (fd < 0) {
  362. fprintf(stderr, "scsi-spin [open]: %s: %s\n", device, strerror(errno));
  363. exit(1);
  364. }
  365. }
  366. if ((S_ISBLK(devstat.st_mode) &&
  367. SCSI_BLK_MAJOR(major(devstat.st_rdev))) ||
  368. (S_ISCHR(devstat.st_mode) &&
  369. major(devstat.st_rdev) == SCSI_GENERIC_MAJOR))
  370. {
  371. if (! opt_noact) {
  372. if (opt_lock || opt_unlock)
  373. scsi_lock(fd, opt_lock);
  374. if (opt_up || opt_down)
  375. scsi_spin(fd, opt_up, opt_loej, opt_wait);
  376. }
  377. }
  378. else {
  379. fprintf(stderr, "scsi-spin: %s is not a disk or generic SCSI device.\n", device);
  380. result = 1;
  381. }
  382. close(fd);
  383. return result;
  384. }