bsg.c 22 KB

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