bsg.c 23 KB

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