bsg.c 22 KB

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