bsg.c 23 KB

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