bsg.c 21 KB

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