bsg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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 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(struct request_queue *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(struct request_queue *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. struct request_queue *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. next_rq->cmd_type = rq->cmd_type;
  237. dxferp = (void*)(unsigned long)hdr->din_xferp;
  238. ret = blk_rq_map_user(q, next_rq, dxferp, hdr->din_xfer_len);
  239. if (ret)
  240. goto out;
  241. }
  242. if (hdr->dout_xfer_len) {
  243. dxfer_len = hdr->dout_xfer_len;
  244. dxferp = (void*)(unsigned long)hdr->dout_xferp;
  245. } else if (hdr->din_xfer_len) {
  246. dxfer_len = hdr->din_xfer_len;
  247. dxferp = (void*)(unsigned long)hdr->din_xferp;
  248. } else
  249. dxfer_len = 0;
  250. if (dxfer_len) {
  251. ret = blk_rq_map_user(q, rq, dxferp, dxfer_len);
  252. if (ret)
  253. goto out;
  254. }
  255. return rq;
  256. out:
  257. blk_put_request(rq);
  258. if (next_rq) {
  259. blk_rq_unmap_user(next_rq->bio);
  260. blk_put_request(next_rq);
  261. }
  262. return ERR_PTR(ret);
  263. }
  264. /*
  265. * async completion call-back from the block layer, when scsi/ide/whatever
  266. * calls end_that_request_last() on a request
  267. */
  268. static void bsg_rq_end_io(struct request *rq, int uptodate)
  269. {
  270. struct bsg_command *bc = rq->end_io_data;
  271. struct bsg_device *bd = bc->bd;
  272. unsigned long flags;
  273. dprintk("%s: finished rq %p bc %p, bio %p stat %d\n",
  274. bd->name, rq, bc, bc->bio, uptodate);
  275. bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
  276. spin_lock_irqsave(&bd->lock, flags);
  277. list_move_tail(&bc->list, &bd->done_list);
  278. bd->done_cmds++;
  279. spin_unlock_irqrestore(&bd->lock, flags);
  280. wake_up(&bd->wq_done);
  281. }
  282. /*
  283. * do final setup of a 'bc' and submit the matching 'rq' to the block
  284. * layer for io
  285. */
  286. static void bsg_add_command(struct bsg_device *bd, struct request_queue *q,
  287. struct bsg_command *bc, struct request *rq)
  288. {
  289. rq->sense = bc->sense;
  290. rq->sense_len = 0;
  291. /*
  292. * add bc command to busy queue and submit rq for io
  293. */
  294. bc->rq = rq;
  295. bc->bio = rq->bio;
  296. if (rq->next_rq)
  297. bc->bidi_bio = rq->next_rq->bio;
  298. bc->hdr.duration = jiffies;
  299. spin_lock_irq(&bd->lock);
  300. list_add_tail(&bc->list, &bd->busy_list);
  301. spin_unlock_irq(&bd->lock);
  302. dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
  303. rq->end_io_data = bc;
  304. blk_execute_rq_nowait(q, NULL, rq, 1, bsg_rq_end_io);
  305. }
  306. static struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
  307. {
  308. struct bsg_command *bc = NULL;
  309. spin_lock_irq(&bd->lock);
  310. if (bd->done_cmds) {
  311. bc = list_entry(bd->done_list.next, struct bsg_command, list);
  312. list_del(&bc->list);
  313. bd->done_cmds--;
  314. }
  315. spin_unlock_irq(&bd->lock);
  316. return bc;
  317. }
  318. /*
  319. * Get a finished command from the done list
  320. */
  321. static struct bsg_command *bsg_get_done_cmd(struct bsg_device *bd)
  322. {
  323. struct bsg_command *bc;
  324. int ret;
  325. do {
  326. bc = bsg_next_done_cmd(bd);
  327. if (bc)
  328. break;
  329. if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
  330. bc = ERR_PTR(-EAGAIN);
  331. break;
  332. }
  333. ret = wait_event_interruptible(bd->wq_done, bd->done_cmds);
  334. if (ret) {
  335. bc = ERR_PTR(-ERESTARTSYS);
  336. break;
  337. }
  338. } while (1);
  339. dprintk("%s: returning done %p\n", bd->name, bc);
  340. return bc;
  341. }
  342. static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
  343. struct bio *bio, struct bio *bidi_bio)
  344. {
  345. int ret = 0;
  346. dprintk("rq %p bio %p %u\n", rq, bio, rq->errors);
  347. /*
  348. * fill in all the output members
  349. */
  350. hdr->device_status = status_byte(rq->errors);
  351. hdr->transport_status = host_byte(rq->errors);
  352. hdr->driver_status = driver_byte(rq->errors);
  353. hdr->info = 0;
  354. if (hdr->device_status || hdr->transport_status || hdr->driver_status)
  355. hdr->info |= SG_INFO_CHECK;
  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. hdr->dout_resid = rq->data_len;
  369. hdr->din_resid = rq->next_rq->data_len;
  370. blk_rq_unmap_user(bidi_bio);
  371. blk_put_request(rq->next_rq);
  372. } else if (rq_data_dir(rq) == READ)
  373. hdr->din_resid = rq->data_len;
  374. else
  375. hdr->dout_resid = rq->data_len;
  376. /*
  377. * If the request generated a negative error number, return it
  378. * (providing we aren't already returning an error); if it's
  379. * just a protocol response (i.e. non negative), that gets
  380. * processed above.
  381. */
  382. if (!ret && rq->errors < 0)
  383. ret = rq->errors;
  384. blk_rq_unmap_user(bio);
  385. blk_put_request(rq);
  386. return ret;
  387. }
  388. static int bsg_complete_all_commands(struct bsg_device *bd)
  389. {
  390. struct bsg_command *bc;
  391. int ret, tret;
  392. dprintk("%s: entered\n", bd->name);
  393. set_bit(BSG_F_BLOCK, &bd->flags);
  394. /*
  395. * wait for all commands to complete
  396. */
  397. ret = 0;
  398. do {
  399. ret = bsg_io_schedule(bd);
  400. /*
  401. * look for -ENODATA specifically -- we'll sometimes get
  402. * -ERESTARTSYS when we've taken a signal, but we can't
  403. * return until we're done freeing the queue, so ignore
  404. * it. The signal will get handled when we're done freeing
  405. * the bsg_device.
  406. */
  407. } while (ret != -ENODATA);
  408. /*
  409. * discard done commands
  410. */
  411. ret = 0;
  412. do {
  413. spin_lock_irq(&bd->lock);
  414. if (!bd->queued_cmds) {
  415. spin_unlock_irq(&bd->lock);
  416. break;
  417. }
  418. spin_unlock_irq(&bd->lock);
  419. bc = bsg_get_done_cmd(bd);
  420. if (IS_ERR(bc))
  421. break;
  422. tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio,
  423. bc->bidi_bio);
  424. if (!ret)
  425. ret = tret;
  426. bsg_free_command(bc);
  427. } while (1);
  428. return ret;
  429. }
  430. static int
  431. __bsg_read(char __user *buf, size_t count, struct bsg_device *bd,
  432. const struct iovec *iov, ssize_t *bytes_read)
  433. {
  434. struct bsg_command *bc;
  435. int nr_commands, ret;
  436. if (count % sizeof(struct sg_io_v4))
  437. return -EINVAL;
  438. ret = 0;
  439. nr_commands = count / sizeof(struct sg_io_v4);
  440. while (nr_commands) {
  441. bc = bsg_get_done_cmd(bd);
  442. if (IS_ERR(bc)) {
  443. ret = PTR_ERR(bc);
  444. break;
  445. }
  446. /*
  447. * this is the only case where we need to copy data back
  448. * after completing the request. so do that here,
  449. * bsg_complete_work() cannot do that for us
  450. */
  451. ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio,
  452. bc->bidi_bio);
  453. if (copy_to_user(buf, &bc->hdr, sizeof(bc->hdr)))
  454. ret = -EFAULT;
  455. bsg_free_command(bc);
  456. if (ret)
  457. break;
  458. buf += sizeof(struct sg_io_v4);
  459. *bytes_read += sizeof(struct sg_io_v4);
  460. nr_commands--;
  461. }
  462. return ret;
  463. }
  464. static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
  465. {
  466. if (file->f_flags & O_NONBLOCK)
  467. clear_bit(BSG_F_BLOCK, &bd->flags);
  468. else
  469. set_bit(BSG_F_BLOCK, &bd->flags);
  470. }
  471. static inline void bsg_set_write_perm(struct bsg_device *bd, struct file *file)
  472. {
  473. if (file->f_mode & FMODE_WRITE)
  474. set_bit(BSG_F_WRITE_PERM, &bd->flags);
  475. else
  476. clear_bit(BSG_F_WRITE_PERM, &bd->flags);
  477. }
  478. /*
  479. * Check if the error is a "real" error that we should return.
  480. */
  481. static inline int err_block_err(int ret)
  482. {
  483. if (ret && ret != -ENOSPC && ret != -ENODATA && ret != -EAGAIN)
  484. return 1;
  485. return 0;
  486. }
  487. static ssize_t
  488. bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  489. {
  490. struct bsg_device *bd = file->private_data;
  491. int ret;
  492. ssize_t bytes_read;
  493. dprintk("%s: read %Zd bytes\n", bd->name, count);
  494. bsg_set_block(bd, file);
  495. bytes_read = 0;
  496. ret = __bsg_read(buf, count, bd, NULL, &bytes_read);
  497. *ppos = bytes_read;
  498. if (!bytes_read || (bytes_read && err_block_err(ret)))
  499. bytes_read = ret;
  500. return bytes_read;
  501. }
  502. static int __bsg_write(struct bsg_device *bd, const char __user *buf,
  503. size_t count, ssize_t *bytes_written)
  504. {
  505. struct bsg_command *bc;
  506. struct request *rq;
  507. int ret, nr_commands;
  508. if (count % sizeof(struct sg_io_v4))
  509. return -EINVAL;
  510. nr_commands = count / sizeof(struct sg_io_v4);
  511. rq = NULL;
  512. bc = NULL;
  513. ret = 0;
  514. while (nr_commands) {
  515. struct request_queue *q = bd->queue;
  516. bc = bsg_alloc_command(bd);
  517. if (IS_ERR(bc)) {
  518. ret = PTR_ERR(bc);
  519. bc = NULL;
  520. break;
  521. }
  522. if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
  523. ret = -EFAULT;
  524. break;
  525. }
  526. /*
  527. * get a request, fill in the blanks, and add to request queue
  528. */
  529. rq = bsg_map_hdr(bd, &bc->hdr);
  530. if (IS_ERR(rq)) {
  531. ret = PTR_ERR(rq);
  532. rq = NULL;
  533. break;
  534. }
  535. bsg_add_command(bd, q, bc, rq);
  536. bc = NULL;
  537. rq = NULL;
  538. nr_commands--;
  539. buf += sizeof(struct sg_io_v4);
  540. *bytes_written += sizeof(struct sg_io_v4);
  541. }
  542. if (bc)
  543. bsg_free_command(bc);
  544. return ret;
  545. }
  546. static ssize_t
  547. bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  548. {
  549. struct bsg_device *bd = file->private_data;
  550. ssize_t bytes_written;
  551. int ret;
  552. dprintk("%s: write %Zd bytes\n", bd->name, count);
  553. bsg_set_block(bd, file);
  554. bsg_set_write_perm(bd, file);
  555. bytes_written = 0;
  556. ret = __bsg_write(bd, buf, count, &bytes_written);
  557. *ppos = bytes_written;
  558. /*
  559. * return bytes written on non-fatal errors
  560. */
  561. if (!bytes_written || (bytes_written && err_block_err(ret)))
  562. bytes_written = ret;
  563. dprintk("%s: returning %Zd\n", bd->name, bytes_written);
  564. return bytes_written;
  565. }
  566. static struct bsg_device *bsg_alloc_device(void)
  567. {
  568. struct bsg_device *bd;
  569. bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
  570. if (unlikely(!bd))
  571. return NULL;
  572. spin_lock_init(&bd->lock);
  573. bd->max_queue = BSG_DEFAULT_CMDS;
  574. INIT_LIST_HEAD(&bd->busy_list);
  575. INIT_LIST_HEAD(&bd->done_list);
  576. INIT_HLIST_NODE(&bd->dev_list);
  577. init_waitqueue_head(&bd->wq_free);
  578. init_waitqueue_head(&bd->wq_done);
  579. return bd;
  580. }
  581. static int bsg_put_device(struct bsg_device *bd)
  582. {
  583. int ret = 0;
  584. struct device *dev = bd->queue->bsg_dev.dev;
  585. mutex_lock(&bsg_mutex);
  586. if (!atomic_dec_and_test(&bd->ref_count))
  587. goto out;
  588. dprintk("%s: tearing down\n", bd->name);
  589. /*
  590. * close can always block
  591. */
  592. set_bit(BSG_F_BLOCK, &bd->flags);
  593. /*
  594. * correct error detection baddies here again. it's the responsibility
  595. * of the app to properly reap commands before close() if it wants
  596. * fool-proof error detection
  597. */
  598. ret = bsg_complete_all_commands(bd);
  599. blk_put_queue(bd->queue);
  600. hlist_del(&bd->dev_list);
  601. kfree(bd);
  602. out:
  603. mutex_unlock(&bsg_mutex);
  604. put_device(dev);
  605. return ret;
  606. }
  607. static struct bsg_device *bsg_add_device(struct inode *inode,
  608. struct request_queue *rq,
  609. struct file *file)
  610. {
  611. struct bsg_device *bd;
  612. int ret;
  613. #ifdef BSG_DEBUG
  614. unsigned char buf[32];
  615. #endif
  616. ret = blk_get_queue(rq);
  617. if (ret)
  618. return ERR_PTR(-ENXIO);
  619. bd = bsg_alloc_device();
  620. if (!bd) {
  621. blk_put_queue(rq);
  622. return ERR_PTR(-ENOMEM);
  623. }
  624. bd->queue = rq;
  625. bsg_set_block(bd, file);
  626. atomic_set(&bd->ref_count, 1);
  627. bd->minor = iminor(inode);
  628. mutex_lock(&bsg_mutex);
  629. hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(bd->minor));
  630. strncpy(bd->name, rq->bsg_dev.class_dev->class_id, sizeof(bd->name) - 1);
  631. dprintk("bound to <%s>, max queue %d\n",
  632. format_dev_t(buf, inode->i_rdev), bd->max_queue);
  633. mutex_unlock(&bsg_mutex);
  634. return bd;
  635. }
  636. static struct bsg_device *__bsg_get_device(int minor)
  637. {
  638. struct bsg_device *bd = NULL;
  639. struct hlist_node *entry;
  640. mutex_lock(&bsg_mutex);
  641. hlist_for_each(entry, bsg_dev_idx_hash(minor)) {
  642. bd = hlist_entry(entry, struct bsg_device, dev_list);
  643. if (bd->minor == minor) {
  644. atomic_inc(&bd->ref_count);
  645. break;
  646. }
  647. bd = NULL;
  648. }
  649. mutex_unlock(&bsg_mutex);
  650. return bd;
  651. }
  652. static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
  653. {
  654. struct bsg_device *bd;
  655. struct bsg_class_device *bcd;
  656. /*
  657. * find the class device
  658. */
  659. mutex_lock(&bsg_mutex);
  660. bcd = idr_find(&bsg_minor_idr, iminor(inode));
  661. if (bcd)
  662. get_device(bcd->dev);
  663. mutex_unlock(&bsg_mutex);
  664. if (!bcd)
  665. return ERR_PTR(-ENODEV);
  666. bd = __bsg_get_device(iminor(inode));
  667. if (bd)
  668. return bd;
  669. bd = bsg_add_device(inode, bcd->queue, file);
  670. if (IS_ERR(bd))
  671. put_device(bcd->dev);
  672. return bd;
  673. }
  674. static int bsg_open(struct inode *inode, struct file *file)
  675. {
  676. struct bsg_device *bd = bsg_get_device(inode, file);
  677. if (IS_ERR(bd))
  678. return PTR_ERR(bd);
  679. file->private_data = bd;
  680. return 0;
  681. }
  682. static int bsg_release(struct inode *inode, struct file *file)
  683. {
  684. struct bsg_device *bd = file->private_data;
  685. file->private_data = NULL;
  686. return bsg_put_device(bd);
  687. }
  688. static unsigned int bsg_poll(struct file *file, poll_table *wait)
  689. {
  690. struct bsg_device *bd = file->private_data;
  691. unsigned int mask = 0;
  692. poll_wait(file, &bd->wq_done, wait);
  693. poll_wait(file, &bd->wq_free, wait);
  694. spin_lock_irq(&bd->lock);
  695. if (!list_empty(&bd->done_list))
  696. mask |= POLLIN | POLLRDNORM;
  697. if (bd->queued_cmds >= bd->max_queue)
  698. mask |= POLLOUT;
  699. spin_unlock_irq(&bd->lock);
  700. return mask;
  701. }
  702. static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  703. {
  704. struct bsg_device *bd = file->private_data;
  705. int __user *uarg = (int __user *) arg;
  706. int ret;
  707. switch (cmd) {
  708. /*
  709. * our own ioctls
  710. */
  711. case SG_GET_COMMAND_Q:
  712. return put_user(bd->max_queue, uarg);
  713. case SG_SET_COMMAND_Q: {
  714. int queue;
  715. if (get_user(queue, uarg))
  716. return -EFAULT;
  717. if (queue < 1)
  718. return -EINVAL;
  719. spin_lock_irq(&bd->lock);
  720. bd->max_queue = queue;
  721. spin_unlock_irq(&bd->lock);
  722. return 0;
  723. }
  724. /*
  725. * SCSI/sg ioctls
  726. */
  727. case SG_GET_VERSION_NUM:
  728. case SCSI_IOCTL_GET_IDLUN:
  729. case SCSI_IOCTL_GET_BUS_NUMBER:
  730. case SG_SET_TIMEOUT:
  731. case SG_GET_TIMEOUT:
  732. case SG_GET_RESERVED_SIZE:
  733. case SG_SET_RESERVED_SIZE:
  734. case SG_EMULATED_HOST:
  735. case SCSI_IOCTL_SEND_COMMAND: {
  736. void __user *uarg = (void __user *) arg;
  737. return scsi_cmd_ioctl(file, bd->queue, NULL, cmd, uarg);
  738. }
  739. case SG_IO: {
  740. struct request *rq;
  741. struct bio *bio, *bidi_bio = NULL;
  742. struct sg_io_v4 hdr;
  743. if (copy_from_user(&hdr, uarg, sizeof(hdr)))
  744. return -EFAULT;
  745. rq = bsg_map_hdr(bd, &hdr);
  746. if (IS_ERR(rq))
  747. return PTR_ERR(rq);
  748. bio = rq->bio;
  749. if (rq->next_rq)
  750. bidi_bio = rq->next_rq->bio;
  751. blk_execute_rq(bd->queue, NULL, rq, 0);
  752. ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio);
  753. if (copy_to_user(uarg, &hdr, sizeof(hdr)))
  754. return -EFAULT;
  755. return ret;
  756. }
  757. /*
  758. * block device ioctls
  759. */
  760. default:
  761. #if 0
  762. return ioctl_by_bdev(bd->bdev, cmd, arg);
  763. #else
  764. return -ENOTTY;
  765. #endif
  766. }
  767. }
  768. static const struct file_operations bsg_fops = {
  769. .read = bsg_read,
  770. .write = bsg_write,
  771. .poll = bsg_poll,
  772. .open = bsg_open,
  773. .release = bsg_release,
  774. .unlocked_ioctl = bsg_ioctl,
  775. .owner = THIS_MODULE,
  776. };
  777. void bsg_unregister_queue(struct request_queue *q)
  778. {
  779. struct bsg_class_device *bcd = &q->bsg_dev;
  780. if (!bcd->class_dev)
  781. return;
  782. mutex_lock(&bsg_mutex);
  783. idr_remove(&bsg_minor_idr, bcd->minor);
  784. sysfs_remove_link(&q->kobj, "bsg");
  785. class_device_unregister(bcd->class_dev);
  786. put_device(bcd->dev);
  787. bcd->class_dev = NULL;
  788. mutex_unlock(&bsg_mutex);
  789. }
  790. EXPORT_SYMBOL_GPL(bsg_unregister_queue);
  791. int bsg_register_queue(struct request_queue *q, struct device *gdev,
  792. const char *name)
  793. {
  794. struct bsg_class_device *bcd;
  795. dev_t dev;
  796. int ret, minor;
  797. struct class_device *class_dev = NULL;
  798. const char *devname;
  799. if (name)
  800. devname = name;
  801. else
  802. devname = gdev->bus_id;
  803. /*
  804. * we need a proper transport to send commands, not a stacked device
  805. */
  806. if (!q->request_fn)
  807. return 0;
  808. bcd = &q->bsg_dev;
  809. memset(bcd, 0, sizeof(*bcd));
  810. mutex_lock(&bsg_mutex);
  811. ret = idr_pre_get(&bsg_minor_idr, GFP_KERNEL);
  812. if (!ret) {
  813. ret = -ENOMEM;
  814. goto unlock;
  815. }
  816. ret = idr_get_new(&bsg_minor_idr, bcd, &minor);
  817. if (ret < 0)
  818. goto unlock;
  819. if (minor >= BSG_MAX_DEVS) {
  820. printk(KERN_ERR "bsg: too many bsg devices\n");
  821. ret = -EINVAL;
  822. goto remove_idr;
  823. }
  824. bcd->minor = minor;
  825. bcd->queue = q;
  826. bcd->dev = get_device(gdev);
  827. dev = MKDEV(bsg_major, bcd->minor);
  828. class_dev = class_device_create(bsg_class, NULL, dev, gdev, "%s",
  829. devname);
  830. if (IS_ERR(class_dev)) {
  831. ret = PTR_ERR(class_dev);
  832. goto put_dev;
  833. }
  834. bcd->class_dev = class_dev;
  835. if (q->kobj.sd) {
  836. ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
  837. if (ret)
  838. goto unregister_class_dev;
  839. }
  840. mutex_unlock(&bsg_mutex);
  841. return 0;
  842. unregister_class_dev:
  843. class_device_unregister(class_dev);
  844. put_dev:
  845. put_device(gdev);
  846. remove_idr:
  847. idr_remove(&bsg_minor_idr, minor);
  848. unlock:
  849. mutex_unlock(&bsg_mutex);
  850. return ret;
  851. }
  852. EXPORT_SYMBOL_GPL(bsg_register_queue);
  853. static struct cdev bsg_cdev;
  854. static int __init bsg_init(void)
  855. {
  856. int ret, i;
  857. dev_t devid;
  858. bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
  859. sizeof(struct bsg_command), 0, 0, NULL);
  860. if (!bsg_cmd_cachep) {
  861. printk(KERN_ERR "bsg: failed creating slab cache\n");
  862. return -ENOMEM;
  863. }
  864. for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
  865. INIT_HLIST_HEAD(&bsg_device_list[i]);
  866. bsg_class = class_create(THIS_MODULE, "bsg");
  867. if (IS_ERR(bsg_class)) {
  868. ret = PTR_ERR(bsg_class);
  869. goto destroy_kmemcache;
  870. }
  871. ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
  872. if (ret)
  873. goto destroy_bsg_class;
  874. bsg_major = MAJOR(devid);
  875. cdev_init(&bsg_cdev, &bsg_fops);
  876. ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
  877. if (ret)
  878. goto unregister_chrdev;
  879. printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
  880. " loaded (major %d)\n", bsg_major);
  881. return 0;
  882. unregister_chrdev:
  883. unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
  884. destroy_bsg_class:
  885. class_destroy(bsg_class);
  886. destroy_kmemcache:
  887. kmem_cache_destroy(bsg_cmd_cachep);
  888. return ret;
  889. }
  890. MODULE_AUTHOR("Jens Axboe");
  891. MODULE_DESCRIPTION(BSG_DESCRIPTION);
  892. MODULE_LICENSE("GPL");
  893. device_initcall(bsg_init);