bsg.c 23 KB

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