bsg.c 23 KB

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