bsg.c 22 KB

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