bsg.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * bsg.c - block layer implementation of the sg v3 interface
  3. *
  4. * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs
  5. * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License version 2. See the file "COPYING" in the main directory of this
  9. * archive for more details.
  10. *
  11. */
  12. /*
  13. * TODO
  14. * - Should this get merged, block/scsi_ioctl.c will be migrated into
  15. * this file. To keep maintenance down, it's easier to have them
  16. * seperated right now.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/file.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/poll.h>
  24. #include <linux/cdev.h>
  25. #include <linux/percpu.h>
  26. #include <linux/uio.h>
  27. #include <linux/bsg.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_ioctl.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_driver.h>
  33. #include <scsi/sg.h>
  34. static char bsg_version[] = "block layer sg (bsg) 0.4";
  35. struct bsg_device {
  36. request_queue_t *queue;
  37. spinlock_t lock;
  38. struct list_head busy_list;
  39. struct list_head done_list;
  40. struct hlist_node dev_list;
  41. atomic_t ref_count;
  42. int minor;
  43. int queued_cmds;
  44. int done_cmds;
  45. wait_queue_head_t wq_done;
  46. wait_queue_head_t wq_free;
  47. char name[BUS_ID_SIZE];
  48. int max_queue;
  49. unsigned long flags;
  50. };
  51. enum {
  52. BSG_F_BLOCK = 1,
  53. BSG_F_WRITE_PERM = 2,
  54. };
  55. #define BSG_DEFAULT_CMDS 64
  56. #define BSG_MAX_DEVS 32768
  57. #undef BSG_DEBUG
  58. #ifdef BSG_DEBUG
  59. #define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ##args)
  60. #else
  61. #define dprintk(fmt, args...)
  62. #endif
  63. #define list_entry_bc(entry) list_entry((entry), struct bsg_command, list)
  64. /*
  65. * just for testing
  66. */
  67. #define BSG_MAJOR (240)
  68. static DEFINE_MUTEX(bsg_mutex);
  69. static int bsg_device_nr, bsg_minor_idx;
  70. #define BSG_LIST_SIZE (8)
  71. #define bsg_list_idx(minor) ((minor) & (BSG_LIST_SIZE - 1))
  72. static struct hlist_head bsg_device_list[BSG_LIST_SIZE];
  73. static struct class *bsg_class;
  74. static LIST_HEAD(bsg_class_list);
  75. static struct kmem_cache *bsg_cmd_cachep;
  76. /*
  77. * our internal command type
  78. */
  79. struct bsg_command {
  80. struct bsg_device *bd;
  81. struct list_head list;
  82. struct request *rq;
  83. struct bio *bio;
  84. int err;
  85. struct sg_io_v4 hdr;
  86. struct sg_io_v4 __user *uhdr;
  87. char sense[SCSI_SENSE_BUFFERSIZE];
  88. };
  89. static void bsg_free_command(struct bsg_command *bc)
  90. {
  91. struct bsg_device *bd = bc->bd;
  92. unsigned long flags;
  93. kmem_cache_free(bsg_cmd_cachep, bc);
  94. spin_lock_irqsave(&bd->lock, flags);
  95. bd->queued_cmds--;
  96. spin_unlock_irqrestore(&bd->lock, flags);
  97. wake_up(&bd->wq_free);
  98. }
  99. static struct bsg_command *bsg_alloc_command(struct bsg_device *bd)
  100. {
  101. struct bsg_command *bc = ERR_PTR(-EINVAL);
  102. spin_lock_irq(&bd->lock);
  103. if (bd->queued_cmds >= bd->max_queue)
  104. goto out;
  105. bd->queued_cmds++;
  106. spin_unlock_irq(&bd->lock);
  107. bc = kmem_cache_alloc(bsg_cmd_cachep, GFP_USER);
  108. if (unlikely(!bc)) {
  109. spin_lock_irq(&bd->lock);
  110. bd->queued_cmds--;
  111. bc = ERR_PTR(-ENOMEM);
  112. goto out;
  113. }
  114. memset(bc, 0, sizeof(*bc));
  115. bc->bd = bd;
  116. INIT_LIST_HEAD(&bc->list);
  117. dprintk("%s: returning free cmd %p\n", bd->name, bc);
  118. return bc;
  119. out:
  120. spin_unlock_irq(&bd->lock);
  121. return bc;
  122. }
  123. static inline void
  124. bsg_del_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
  125. {
  126. bd->done_cmds--;
  127. list_del(&bc->list);
  128. }
  129. static inline void
  130. bsg_add_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
  131. {
  132. bd->done_cmds++;
  133. list_add_tail(&bc->list, &bd->done_list);
  134. wake_up(&bd->wq_done);
  135. }
  136. static inline int bsg_io_schedule(struct bsg_device *bd, int state)
  137. {
  138. DEFINE_WAIT(wait);
  139. int ret = 0;
  140. spin_lock_irq(&bd->lock);
  141. BUG_ON(bd->done_cmds > bd->queued_cmds);
  142. /*
  143. * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
  144. * work to do", even though we return -ENOSPC after this same test
  145. * during bsg_write() -- there, it means our buffer can't have more
  146. * bsg_commands added to it, thus has no space left.
  147. */
  148. if (bd->done_cmds == bd->queued_cmds) {
  149. ret = -ENODATA;
  150. goto unlock;
  151. }
  152. if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
  153. ret = -EAGAIN;
  154. goto unlock;
  155. }
  156. prepare_to_wait(&bd->wq_done, &wait, state);
  157. spin_unlock_irq(&bd->lock);
  158. io_schedule();
  159. finish_wait(&bd->wq_done, &wait);
  160. if ((state == TASK_INTERRUPTIBLE) && signal_pending(current))
  161. ret = -ERESTARTSYS;
  162. return ret;
  163. unlock:
  164. spin_unlock_irq(&bd->lock);
  165. return ret;
  166. }
  167. static int blk_fill_sgv4_hdr_rq(request_queue_t *q, struct request *rq,
  168. struct sg_io_v4 *hdr, int has_write_perm)
  169. {
  170. memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  171. if (copy_from_user(rq->cmd, (void *)(unsigned long)hdr->request,
  172. hdr->request_len))
  173. return -EFAULT;
  174. if (blk_verify_command(rq->cmd, has_write_perm))
  175. return -EPERM;
  176. /*
  177. * fill in request structure
  178. */
  179. rq->cmd_len = hdr->request_len;
  180. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  181. rq->timeout = (hdr->timeout * HZ) / 1000;
  182. if (!rq->timeout)
  183. rq->timeout = q->sg_timeout;
  184. if (!rq->timeout)
  185. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  186. return 0;
  187. }
  188. /*
  189. * Check if sg_io_v4 from user is allowed and valid
  190. */
  191. static int
  192. bsg_validate_sgv4_hdr(request_queue_t *q, struct sg_io_v4 *hdr, int *rw)
  193. {
  194. if (hdr->guard != 'Q')
  195. return -EINVAL;
  196. if (hdr->request_len > BLK_MAX_CDB)
  197. return -EINVAL;
  198. if (hdr->dout_xfer_len > (q->max_sectors << 9) ||
  199. hdr->din_xfer_len > (q->max_sectors << 9))
  200. return -EIO;
  201. /* not supported currently */
  202. if (hdr->protocol || hdr->subprotocol)
  203. return -EINVAL;
  204. /*
  205. * looks sane, if no data then it should be fine from our POV
  206. */
  207. if (!hdr->dout_xfer_len && !hdr->din_xfer_len)
  208. return 0;
  209. /* not supported currently */
  210. if (hdr->dout_xfer_len && hdr->din_xfer_len)
  211. return -EINVAL;
  212. *rw = hdr->dout_xfer_len ? WRITE : READ;
  213. return 0;
  214. }
  215. /*
  216. * map sg_io_v4 to a request.
  217. */
  218. static struct request *
  219. bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
  220. {
  221. request_queue_t *q = bd->queue;
  222. struct request *rq;
  223. int ret, rw = 0; /* shut up gcc */
  224. unsigned int dxfer_len;
  225. void *dxferp = NULL;
  226. dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr->dout_xferp,
  227. hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp,
  228. hdr->din_xfer_len);
  229. ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
  230. if (ret)
  231. return ERR_PTR(ret);
  232. /*
  233. * map scatter-gather elements seperately and string them to request
  234. */
  235. rq = blk_get_request(q, rw, GFP_KERNEL);
  236. ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, test_bit(BSG_F_WRITE_PERM,
  237. &bd->flags));
  238. if (ret) {
  239. blk_put_request(rq);
  240. return ERR_PTR(ret);
  241. }
  242. if (hdr->dout_xfer_len) {
  243. dxfer_len = hdr->dout_xfer_len;
  244. dxferp = (void*)(unsigned long)hdr->dout_xferp;
  245. } else if (hdr->din_xfer_len) {
  246. dxfer_len = hdr->din_xfer_len;
  247. dxferp = (void*)(unsigned long)hdr->din_xferp;
  248. } else
  249. dxfer_len = 0;
  250. if (dxfer_len) {
  251. ret = blk_rq_map_user(q, rq, dxferp, dxfer_len);
  252. if (ret) {
  253. dprintk("failed map at %d\n", ret);
  254. blk_put_request(rq);
  255. rq = ERR_PTR(ret);
  256. }
  257. }
  258. return rq;
  259. }
  260. /*
  261. * async completion call-back from the block layer, when scsi/ide/whatever
  262. * calls end_that_request_last() on a request
  263. */
  264. static void bsg_rq_end_io(struct request *rq, int uptodate)
  265. {
  266. struct bsg_command *bc = rq->end_io_data;
  267. struct bsg_device *bd = bc->bd;
  268. unsigned long flags;
  269. dprintk("%s: finished rq %p bc %p, bio %p stat %d\n",
  270. bd->name, rq, bc, bc->bio, uptodate);
  271. bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
  272. spin_lock_irqsave(&bd->lock, flags);
  273. list_del(&bc->list);
  274. bsg_add_done_cmd(bd, bc);
  275. spin_unlock_irqrestore(&bd->lock, flags);
  276. }
  277. /*
  278. * do final setup of a 'bc' and submit the matching 'rq' to the block
  279. * layer for io
  280. */
  281. static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
  282. struct bsg_command *bc, struct request *rq)
  283. {
  284. rq->sense = bc->sense;
  285. rq->sense_len = 0;
  286. /*
  287. * add bc command to busy queue and submit rq for io
  288. */
  289. bc->rq = rq;
  290. bc->bio = rq->bio;
  291. bc->hdr.duration = jiffies;
  292. spin_lock_irq(&bd->lock);
  293. list_add_tail(&bc->list, &bd->busy_list);
  294. spin_unlock_irq(&bd->lock);
  295. dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
  296. rq->end_io_data = bc;
  297. blk_execute_rq_nowait(q, NULL, rq, 1, bsg_rq_end_io);
  298. }
  299. static inline struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
  300. {
  301. struct bsg_command *bc = NULL;
  302. spin_lock_irq(&bd->lock);
  303. if (bd->done_cmds) {
  304. bc = list_entry_bc(bd->done_list.next);
  305. bsg_del_done_cmd(bd, bc);
  306. }
  307. spin_unlock_irq(&bd->lock);
  308. return bc;
  309. }
  310. /*
  311. * Get a finished command from the done list
  312. */
  313. static struct bsg_command *bsg_get_done_cmd(struct bsg_device *bd)
  314. {
  315. struct bsg_command *bc;
  316. int ret;
  317. do {
  318. bc = bsg_next_done_cmd(bd);
  319. if (bc)
  320. break;
  321. if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
  322. bc = ERR_PTR(-EAGAIN);
  323. break;
  324. }
  325. ret = wait_event_interruptible(bd->wq_done, bd->done_cmds);
  326. if (ret) {
  327. bc = ERR_PTR(-ERESTARTSYS);
  328. break;
  329. }
  330. } while (1);
  331. dprintk("%s: returning done %p\n", bd->name, bc);
  332. return bc;
  333. }
  334. static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
  335. struct bio *bio)
  336. {
  337. int ret = 0;
  338. dprintk("rq %p bio %p %u\n", rq, bio, rq->errors);
  339. /*
  340. * fill in all the output members
  341. */
  342. hdr->device_status = status_byte(rq->errors);
  343. hdr->transport_status = host_byte(rq->errors);
  344. hdr->driver_status = driver_byte(rq->errors);
  345. hdr->info = 0;
  346. if (hdr->device_status || hdr->transport_status || hdr->driver_status)
  347. hdr->info |= SG_INFO_CHECK;
  348. hdr->din_resid = rq->data_len;
  349. hdr->response_len = 0;
  350. if (rq->sense_len && hdr->response) {
  351. int len = min((unsigned int) hdr->max_response_len,
  352. rq->sense_len);
  353. ret = copy_to_user((void*)(unsigned long)hdr->response,
  354. rq->sense, len);
  355. if (!ret)
  356. hdr->response_len = len;
  357. else
  358. ret = -EFAULT;
  359. }
  360. blk_rq_unmap_user(bio);
  361. blk_put_request(rq);
  362. return ret;
  363. }
  364. static int bsg_complete_all_commands(struct bsg_device *bd)
  365. {
  366. struct bsg_command *bc;
  367. int ret, tret;
  368. dprintk("%s: entered\n", bd->name);
  369. set_bit(BSG_F_BLOCK, &bd->flags);
  370. /*
  371. * wait for all commands to complete
  372. */
  373. ret = 0;
  374. do {
  375. ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
  376. /*
  377. * look for -ENODATA specifically -- we'll sometimes get
  378. * -ERESTARTSYS when we've taken a signal, but we can't
  379. * return until we're done freeing the queue, so ignore
  380. * it. The signal will get handled when we're done freeing
  381. * the bsg_device.
  382. */
  383. } while (ret != -ENODATA);
  384. /*
  385. * discard done commands
  386. */
  387. ret = 0;
  388. do {
  389. spin_lock_irq(&bd->lock);
  390. if (!bd->queued_cmds) {
  391. spin_unlock_irq(&bd->lock);
  392. break;
  393. }
  394. spin_unlock_irq(&bd->lock);
  395. bc = bsg_get_done_cmd(bd);
  396. if (IS_ERR(bc))
  397. break;
  398. tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
  399. if (!ret)
  400. ret = tret;
  401. bsg_free_command(bc);
  402. } while (1);
  403. return ret;
  404. }
  405. static ssize_t
  406. __bsg_read(char __user *buf, size_t count, struct bsg_device *bd,
  407. const struct iovec *iov, ssize_t *bytes_read)
  408. {
  409. struct bsg_command *bc;
  410. int nr_commands, ret;
  411. if (count % sizeof(struct sg_io_v4))
  412. return -EINVAL;
  413. ret = 0;
  414. nr_commands = count / sizeof(struct sg_io_v4);
  415. while (nr_commands) {
  416. bc = bsg_get_done_cmd(bd);
  417. if (IS_ERR(bc)) {
  418. ret = PTR_ERR(bc);
  419. break;
  420. }
  421. /*
  422. * this is the only case where we need to copy data back
  423. * after completing the request. so do that here,
  424. * bsg_complete_work() cannot do that for us
  425. */
  426. ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
  427. if (copy_to_user(buf, (char *) &bc->hdr, sizeof(bc->hdr)))
  428. ret = -EFAULT;
  429. bsg_free_command(bc);
  430. if (ret)
  431. break;
  432. buf += sizeof(struct sg_io_v4);
  433. *bytes_read += sizeof(struct sg_io_v4);
  434. nr_commands--;
  435. }
  436. return ret;
  437. }
  438. static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
  439. {
  440. if (file->f_flags & O_NONBLOCK)
  441. clear_bit(BSG_F_BLOCK, &bd->flags);
  442. else
  443. set_bit(BSG_F_BLOCK, &bd->flags);
  444. }
  445. static inline void bsg_set_write_perm(struct bsg_device *bd, struct file *file)
  446. {
  447. if (file->f_mode & FMODE_WRITE)
  448. set_bit(BSG_F_WRITE_PERM, &bd->flags);
  449. else
  450. clear_bit(BSG_F_WRITE_PERM, &bd->flags);
  451. }
  452. static inline int err_block_err(int ret)
  453. {
  454. if (ret && ret != -ENOSPC && ret != -ENODATA && ret != -EAGAIN)
  455. return 1;
  456. return 0;
  457. }
  458. static ssize_t
  459. bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  460. {
  461. struct bsg_device *bd = file->private_data;
  462. int ret;
  463. ssize_t bytes_read;
  464. dprintk("%s: read %Zd bytes\n", bd->name, count);
  465. bsg_set_block(bd, file);
  466. bytes_read = 0;
  467. ret = __bsg_read(buf, count, bd, NULL, &bytes_read);
  468. *ppos = bytes_read;
  469. if (!bytes_read || (bytes_read && err_block_err(ret)))
  470. bytes_read = ret;
  471. return bytes_read;
  472. }
  473. static ssize_t __bsg_write(struct bsg_device *bd, const char __user *buf,
  474. size_t count, ssize_t *bytes_read)
  475. {
  476. struct bsg_command *bc;
  477. struct request *rq;
  478. int ret, nr_commands;
  479. if (count % sizeof(struct sg_io_v4))
  480. return -EINVAL;
  481. nr_commands = count / sizeof(struct sg_io_v4);
  482. rq = NULL;
  483. bc = NULL;
  484. ret = 0;
  485. while (nr_commands) {
  486. request_queue_t *q = bd->queue;
  487. bc = bsg_alloc_command(bd);
  488. if (IS_ERR(bc)) {
  489. ret = PTR_ERR(bc);
  490. bc = NULL;
  491. break;
  492. }
  493. bc->uhdr = (struct sg_io_v4 __user *) buf;
  494. if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
  495. ret = -EFAULT;
  496. break;
  497. }
  498. /*
  499. * get a request, fill in the blanks, and add to request queue
  500. */
  501. rq = bsg_map_hdr(bd, &bc->hdr);
  502. if (IS_ERR(rq)) {
  503. ret = PTR_ERR(rq);
  504. rq = NULL;
  505. break;
  506. }
  507. bsg_add_command(bd, q, bc, rq);
  508. bc = NULL;
  509. rq = NULL;
  510. nr_commands--;
  511. buf += sizeof(struct sg_io_v4);
  512. *bytes_read += sizeof(struct sg_io_v4);
  513. }
  514. if (bc)
  515. bsg_free_command(bc);
  516. return ret;
  517. }
  518. static ssize_t
  519. bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  520. {
  521. struct bsg_device *bd = file->private_data;
  522. ssize_t bytes_read;
  523. int ret;
  524. dprintk("%s: write %Zd bytes\n", bd->name, count);
  525. bsg_set_block(bd, file);
  526. bsg_set_write_perm(bd, file);
  527. bytes_read = 0;
  528. ret = __bsg_write(bd, buf, count, &bytes_read);
  529. *ppos = bytes_read;
  530. /*
  531. * return bytes written on non-fatal errors
  532. */
  533. if (!bytes_read || (bytes_read && err_block_err(ret)))
  534. bytes_read = ret;
  535. dprintk("%s: returning %Zd\n", bd->name, bytes_read);
  536. return bytes_read;
  537. }
  538. static struct bsg_device *bsg_alloc_device(void)
  539. {
  540. struct bsg_device *bd;
  541. bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
  542. if (unlikely(!bd))
  543. return NULL;
  544. spin_lock_init(&bd->lock);
  545. bd->max_queue = BSG_DEFAULT_CMDS;
  546. INIT_LIST_HEAD(&bd->busy_list);
  547. INIT_LIST_HEAD(&bd->done_list);
  548. INIT_HLIST_NODE(&bd->dev_list);
  549. init_waitqueue_head(&bd->wq_free);
  550. init_waitqueue_head(&bd->wq_done);
  551. return bd;
  552. }
  553. static int bsg_put_device(struct bsg_device *bd)
  554. {
  555. int ret = 0;
  556. mutex_lock(&bsg_mutex);
  557. if (!atomic_dec_and_test(&bd->ref_count))
  558. goto out;
  559. dprintk("%s: tearing down\n", bd->name);
  560. /*
  561. * close can always block
  562. */
  563. set_bit(BSG_F_BLOCK, &bd->flags);
  564. /*
  565. * correct error detection baddies here again. it's the responsibility
  566. * of the app to properly reap commands before close() if it wants
  567. * fool-proof error detection
  568. */
  569. ret = bsg_complete_all_commands(bd);
  570. blk_put_queue(bd->queue);
  571. hlist_del(&bd->dev_list);
  572. kfree(bd);
  573. out:
  574. mutex_unlock(&bsg_mutex);
  575. return ret;
  576. }
  577. static struct bsg_device *bsg_add_device(struct inode *inode,
  578. struct request_queue *rq,
  579. struct file *file)
  580. {
  581. struct bsg_device *bd = NULL;
  582. #ifdef BSG_DEBUG
  583. unsigned char buf[32];
  584. #endif
  585. bd = bsg_alloc_device();
  586. if (!bd)
  587. return ERR_PTR(-ENOMEM);
  588. bd->queue = rq;
  589. kobject_get(&rq->kobj);
  590. bsg_set_block(bd, file);
  591. atomic_set(&bd->ref_count, 1);
  592. bd->minor = iminor(inode);
  593. mutex_lock(&bsg_mutex);
  594. hlist_add_head(&bd->dev_list, &bsg_device_list[bsg_list_idx(bd->minor)]);
  595. strncpy(bd->name, rq->bsg_dev.class_dev->class_id, sizeof(bd->name) - 1);
  596. dprintk("bound to <%s>, max queue %d\n",
  597. format_dev_t(buf, inode->i_rdev), bd->max_queue);
  598. mutex_unlock(&bsg_mutex);
  599. return bd;
  600. }
  601. static struct bsg_device *__bsg_get_device(int minor)
  602. {
  603. struct hlist_head *list = &bsg_device_list[bsg_list_idx(minor)];
  604. struct bsg_device *bd = NULL;
  605. struct hlist_node *entry;
  606. mutex_lock(&bsg_mutex);
  607. hlist_for_each(entry, list) {
  608. bd = hlist_entry(entry, struct bsg_device, dev_list);
  609. if (bd->minor == minor) {
  610. atomic_inc(&bd->ref_count);
  611. break;
  612. }
  613. bd = NULL;
  614. }
  615. mutex_unlock(&bsg_mutex);
  616. return bd;
  617. }
  618. static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
  619. {
  620. struct bsg_device *bd = __bsg_get_device(iminor(inode));
  621. struct bsg_class_device *bcd, *__bcd;
  622. if (bd)
  623. return bd;
  624. /*
  625. * find the class device
  626. */
  627. bcd = NULL;
  628. mutex_lock(&bsg_mutex);
  629. list_for_each_entry(__bcd, &bsg_class_list, list) {
  630. if (__bcd->minor == iminor(inode)) {
  631. bcd = __bcd;
  632. break;
  633. }
  634. }
  635. mutex_unlock(&bsg_mutex);
  636. if (!bcd)
  637. return ERR_PTR(-ENODEV);
  638. return bsg_add_device(inode, bcd->queue, file);
  639. }
  640. static int bsg_open(struct inode *inode, struct file *file)
  641. {
  642. struct bsg_device *bd = bsg_get_device(inode, file);
  643. if (IS_ERR(bd))
  644. return PTR_ERR(bd);
  645. file->private_data = bd;
  646. return 0;
  647. }
  648. static int bsg_release(struct inode *inode, struct file *file)
  649. {
  650. struct bsg_device *bd = file->private_data;
  651. file->private_data = NULL;
  652. return bsg_put_device(bd);
  653. }
  654. static unsigned int bsg_poll(struct file *file, poll_table *wait)
  655. {
  656. struct bsg_device *bd = file->private_data;
  657. unsigned int mask = 0;
  658. poll_wait(file, &bd->wq_done, wait);
  659. poll_wait(file, &bd->wq_free, wait);
  660. spin_lock_irq(&bd->lock);
  661. if (!list_empty(&bd->done_list))
  662. mask |= POLLIN | POLLRDNORM;
  663. if (bd->queued_cmds >= bd->max_queue)
  664. mask |= POLLOUT;
  665. spin_unlock_irq(&bd->lock);
  666. return mask;
  667. }
  668. static int
  669. bsg_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  670. unsigned long arg)
  671. {
  672. struct bsg_device *bd = file->private_data;
  673. int __user *uarg = (int __user *) arg;
  674. if (!bd)
  675. return -ENXIO;
  676. switch (cmd) {
  677. /*
  678. * our own ioctls
  679. */
  680. case SG_GET_COMMAND_Q:
  681. return put_user(bd->max_queue, uarg);
  682. case SG_SET_COMMAND_Q: {
  683. int queue;
  684. if (get_user(queue, uarg))
  685. return -EFAULT;
  686. if (queue < 1)
  687. return -EINVAL;
  688. spin_lock_irq(&bd->lock);
  689. bd->max_queue = queue;
  690. spin_unlock_irq(&bd->lock);
  691. return 0;
  692. }
  693. /*
  694. * SCSI/sg ioctls
  695. */
  696. case SG_GET_VERSION_NUM:
  697. case SCSI_IOCTL_GET_IDLUN:
  698. case SCSI_IOCTL_GET_BUS_NUMBER:
  699. case SG_SET_TIMEOUT:
  700. case SG_GET_TIMEOUT:
  701. case SG_GET_RESERVED_SIZE:
  702. case SG_SET_RESERVED_SIZE:
  703. case SG_EMULATED_HOST:
  704. case SCSI_IOCTL_SEND_COMMAND: {
  705. void __user *uarg = (void __user *) arg;
  706. return scsi_cmd_ioctl(file, bd->queue, NULL, cmd, uarg);
  707. }
  708. case SG_IO: {
  709. struct request *rq;
  710. struct bio *bio;
  711. struct sg_io_v4 hdr;
  712. if (copy_from_user(&hdr, uarg, sizeof(hdr)))
  713. return -EFAULT;
  714. rq = bsg_map_hdr(bd, &hdr);
  715. if (IS_ERR(rq))
  716. return PTR_ERR(rq);
  717. bio = rq->bio;
  718. blk_execute_rq(bd->queue, NULL, rq, 0);
  719. blk_complete_sgv4_hdr_rq(rq, &hdr, bio);
  720. if (copy_to_user(uarg, &hdr, sizeof(hdr)))
  721. return -EFAULT;
  722. return 0;
  723. }
  724. /*
  725. * block device ioctls
  726. */
  727. default:
  728. #if 0
  729. return ioctl_by_bdev(bd->bdev, cmd, arg);
  730. #else
  731. return -ENOTTY;
  732. #endif
  733. }
  734. }
  735. static struct file_operations bsg_fops = {
  736. .read = bsg_read,
  737. .write = bsg_write,
  738. .poll = bsg_poll,
  739. .open = bsg_open,
  740. .release = bsg_release,
  741. .ioctl = bsg_ioctl,
  742. .owner = THIS_MODULE,
  743. };
  744. void bsg_unregister_queue(struct request_queue *q)
  745. {
  746. struct bsg_class_device *bcd = &q->bsg_dev;
  747. if (!bcd->class_dev)
  748. return;
  749. mutex_lock(&bsg_mutex);
  750. sysfs_remove_link(&q->kobj, "bsg");
  751. class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
  752. bcd->class_dev = NULL;
  753. list_del_init(&bcd->list);
  754. bsg_device_nr--;
  755. mutex_unlock(&bsg_mutex);
  756. }
  757. EXPORT_SYMBOL_GPL(bsg_unregister_queue);
  758. int bsg_register_queue(struct request_queue *q, const char *name)
  759. {
  760. struct bsg_class_device *bcd, *__bcd;
  761. dev_t dev;
  762. int ret = -EMFILE;
  763. struct class_device *class_dev = NULL;
  764. /*
  765. * we need a proper transport to send commands, not a stacked device
  766. */
  767. if (!q->request_fn)
  768. return 0;
  769. bcd = &q->bsg_dev;
  770. memset(bcd, 0, sizeof(*bcd));
  771. INIT_LIST_HEAD(&bcd->list);
  772. mutex_lock(&bsg_mutex);
  773. if (bsg_device_nr == BSG_MAX_DEVS) {
  774. printk(KERN_ERR "bsg: too many bsg devices\n");
  775. goto err;
  776. }
  777. retry:
  778. list_for_each_entry(__bcd, &bsg_class_list, list) {
  779. if (__bcd->minor == bsg_minor_idx) {
  780. bsg_minor_idx++;
  781. if (bsg_minor_idx == BSG_MAX_DEVS)
  782. bsg_minor_idx = 0;
  783. goto retry;
  784. }
  785. }
  786. bcd->minor = bsg_minor_idx++;
  787. if (bsg_minor_idx == BSG_MAX_DEVS)
  788. bsg_minor_idx = 0;
  789. bcd->queue = q;
  790. dev = MKDEV(BSG_MAJOR, bcd->minor);
  791. class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", name);
  792. if (IS_ERR(class_dev)) {
  793. ret = PTR_ERR(class_dev);
  794. goto err;
  795. }
  796. bcd->class_dev = class_dev;
  797. if (q->kobj.dentry) {
  798. ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
  799. if (ret)
  800. goto err;
  801. }
  802. list_add_tail(&bcd->list, &bsg_class_list);
  803. bsg_device_nr++;
  804. mutex_unlock(&bsg_mutex);
  805. return 0;
  806. err:
  807. if (class_dev)
  808. class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
  809. mutex_unlock(&bsg_mutex);
  810. return ret;
  811. }
  812. EXPORT_SYMBOL_GPL(bsg_register_queue);
  813. static int bsg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
  814. {
  815. int ret;
  816. struct scsi_device *sdp = to_scsi_device(cl_dev->dev);
  817. struct request_queue *rq = sdp->request_queue;
  818. if (rq->kobj.parent)
  819. ret = bsg_register_queue(rq, kobject_name(rq->kobj.parent));
  820. else
  821. ret = bsg_register_queue(rq, kobject_name(&sdp->sdev_gendev.kobj));
  822. return ret;
  823. }
  824. static void bsg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
  825. {
  826. bsg_unregister_queue(to_scsi_device(cl_dev->dev)->request_queue);
  827. }
  828. static struct class_interface bsg_intf = {
  829. .add = bsg_add,
  830. .remove = bsg_remove,
  831. };
  832. static struct cdev bsg_cdev = {
  833. .kobj = {.name = "bsg", },
  834. .owner = THIS_MODULE,
  835. };
  836. static int __init bsg_init(void)
  837. {
  838. int ret, i;
  839. bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
  840. sizeof(struct bsg_command), 0, 0, NULL, NULL);
  841. if (!bsg_cmd_cachep) {
  842. printk(KERN_ERR "bsg: failed creating slab cache\n");
  843. return -ENOMEM;
  844. }
  845. for (i = 0; i < BSG_LIST_SIZE; i++)
  846. INIT_HLIST_HEAD(&bsg_device_list[i]);
  847. bsg_class = class_create(THIS_MODULE, "bsg");
  848. if (IS_ERR(bsg_class)) {
  849. kmem_cache_destroy(bsg_cmd_cachep);
  850. return PTR_ERR(bsg_class);
  851. }
  852. ret = register_chrdev_region(MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS, "bsg");
  853. if (ret) {
  854. kmem_cache_destroy(bsg_cmd_cachep);
  855. class_destroy(bsg_class);
  856. return ret;
  857. }
  858. cdev_init(&bsg_cdev, &bsg_fops);
  859. ret = cdev_add(&bsg_cdev, MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS);
  860. if (ret) {
  861. kmem_cache_destroy(bsg_cmd_cachep);
  862. class_destroy(bsg_class);
  863. unregister_chrdev_region(MKDEV(BSG_MAJOR, 0), BSG_MAX_DEVS);
  864. return ret;
  865. }
  866. ret = scsi_register_interface(&bsg_intf);
  867. if (ret) {
  868. printk(KERN_ERR "bsg: failed register scsi interface %d\n", ret);
  869. kmem_cache_destroy(bsg_cmd_cachep);
  870. class_destroy(bsg_class);
  871. unregister_chrdev(BSG_MAJOR, "bsg");
  872. return ret;
  873. }
  874. printk(KERN_INFO "%s loaded\n", bsg_version);
  875. return 0;
  876. }
  877. MODULE_AUTHOR("Jens Axboe");
  878. MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
  879. MODULE_LICENSE("GPL");
  880. device_initcall(bsg_init);