bsg.c 24 KB

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