bsg.c 23 KB

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