bsg.c 22 KB

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