bsg.c 23 KB

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