bsg.c 23 KB

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