bsg.c 23 KB

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