bsg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. #undef BSG_DEBUG
  57. #ifdef BSG_DEBUG
  58. #define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ##args)
  59. #else
  60. #define dprintk(fmt, args...)
  61. #endif
  62. #define list_entry_bc(entry) list_entry((entry), struct bsg_command, list)
  63. /*
  64. * just for testing
  65. */
  66. #define BSG_MAJOR (240)
  67. static DEFINE_MUTEX(bsg_mutex);
  68. static int bsg_device_nr;
  69. #define BSG_LIST_SIZE (8)
  70. #define bsg_list_idx(minor) ((minor) & (BSG_LIST_SIZE - 1))
  71. static struct hlist_head bsg_device_list[BSG_LIST_SIZE];
  72. static struct class *bsg_class;
  73. static LIST_HEAD(bsg_class_list);
  74. static struct kmem_cache *bsg_cmd_cachep;
  75. /*
  76. * our internal command type
  77. */
  78. struct bsg_command {
  79. struct bsg_device *bd;
  80. struct list_head list;
  81. struct request *rq;
  82. struct bio *bio;
  83. int err;
  84. struct sg_io_v4 hdr;
  85. struct sg_io_v4 __user *uhdr;
  86. char sense[SCSI_SENSE_BUFFERSIZE];
  87. };
  88. static void bsg_free_command(struct bsg_command *bc)
  89. {
  90. struct bsg_device *bd = bc->bd;
  91. unsigned long flags;
  92. kmem_cache_free(bsg_cmd_cachep, bc);
  93. spin_lock_irqsave(&bd->lock, flags);
  94. bd->queued_cmds--;
  95. spin_unlock_irqrestore(&bd->lock, flags);
  96. wake_up(&bd->wq_free);
  97. }
  98. static struct bsg_command *__bsg_alloc_command(struct bsg_device *bd)
  99. {
  100. struct bsg_command *bc = NULL;
  101. spin_lock_irq(&bd->lock);
  102. if (bd->queued_cmds >= bd->max_queue)
  103. goto out;
  104. bd->queued_cmds++;
  105. spin_unlock_irq(&bd->lock);
  106. bc = kmem_cache_alloc(bsg_cmd_cachep, GFP_USER);
  107. if (unlikely(!bc)) {
  108. spin_lock_irq(&bd->lock);
  109. bd->queued_cmds--;
  110. goto out;
  111. }
  112. memset(bc, 0, sizeof(*bc));
  113. bc->bd = bd;
  114. INIT_LIST_HEAD(&bc->list);
  115. dprintk("%s: returning free cmd %p\n", bd->name, bc);
  116. return bc;
  117. out:
  118. spin_unlock_irq(&bd->lock);
  119. return bc;
  120. }
  121. static inline void
  122. bsg_del_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
  123. {
  124. bd->done_cmds--;
  125. list_del(&bc->list);
  126. }
  127. static inline void
  128. bsg_add_done_cmd(struct bsg_device *bd, struct bsg_command *bc)
  129. {
  130. bd->done_cmds++;
  131. list_add_tail(&bc->list, &bd->done_list);
  132. wake_up(&bd->wq_done);
  133. }
  134. static inline int bsg_io_schedule(struct bsg_device *bd, int state)
  135. {
  136. DEFINE_WAIT(wait);
  137. int ret = 0;
  138. spin_lock_irq(&bd->lock);
  139. BUG_ON(bd->done_cmds > bd->queued_cmds);
  140. /*
  141. * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
  142. * work to do", even though we return -ENOSPC after this same test
  143. * during bsg_write() -- there, it means our buffer can't have more
  144. * bsg_commands added to it, thus has no space left.
  145. */
  146. if (bd->done_cmds == bd->queued_cmds) {
  147. ret = -ENODATA;
  148. goto unlock;
  149. }
  150. if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
  151. ret = -EAGAIN;
  152. goto unlock;
  153. }
  154. prepare_to_wait(&bd->wq_done, &wait, state);
  155. spin_unlock_irq(&bd->lock);
  156. io_schedule();
  157. finish_wait(&bd->wq_done, &wait);
  158. if ((state == TASK_INTERRUPTIBLE) && signal_pending(current))
  159. ret = -ERESTARTSYS;
  160. return ret;
  161. unlock:
  162. spin_unlock_irq(&bd->lock);
  163. return ret;
  164. }
  165. /*
  166. * get a new free command, blocking if needed and specified
  167. */
  168. static struct bsg_command *bsg_get_command(struct bsg_device *bd)
  169. {
  170. struct bsg_command *bc;
  171. int ret;
  172. do {
  173. bc = __bsg_alloc_command(bd);
  174. if (bc)
  175. break;
  176. ret = bsg_io_schedule(bd, TASK_INTERRUPTIBLE);
  177. if (ret) {
  178. bc = ERR_PTR(ret);
  179. break;
  180. }
  181. } while (1);
  182. return bc;
  183. }
  184. static int blk_fill_sgv4_hdr_rq(request_queue_t *q, struct request *rq,
  185. struct sg_io_v4 *hdr, int has_write_perm)
  186. {
  187. memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  188. if (copy_from_user(rq->cmd, (void *)(unsigned long)hdr->request,
  189. hdr->request_len))
  190. return -EFAULT;
  191. if (blk_verify_command(rq->cmd, has_write_perm))
  192. return -EPERM;
  193. /*
  194. * fill in request structure
  195. */
  196. rq->cmd_len = hdr->request_len;
  197. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  198. rq->timeout = (hdr->timeout * HZ) / 1000;
  199. if (!rq->timeout)
  200. rq->timeout = q->sg_timeout;
  201. if (!rq->timeout)
  202. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  203. return 0;
  204. }
  205. /*
  206. * Check if sg_io_v4 from user is allowed and valid
  207. */
  208. static int
  209. bsg_validate_sgv4_hdr(request_queue_t *q, struct sg_io_v4 *hdr, int *rw)
  210. {
  211. if (hdr->guard != 'Q')
  212. return -EINVAL;
  213. if (hdr->request_len > BLK_MAX_CDB)
  214. return -EINVAL;
  215. if (hdr->dout_xfer_len > (q->max_sectors << 9) ||
  216. hdr->din_xfer_len > (q->max_sectors << 9))
  217. return -EIO;
  218. /* not supported currently */
  219. if (hdr->protocol || hdr->subprotocol)
  220. return -EINVAL;
  221. /*
  222. * looks sane, if no data then it should be fine from our POV
  223. */
  224. if (!hdr->dout_xfer_len && !hdr->din_xfer_len)
  225. return 0;
  226. /* not supported currently */
  227. if (hdr->dout_xfer_len && hdr->din_xfer_len)
  228. return -EINVAL;
  229. *rw = hdr->dout_xfer_len ? WRITE : READ;
  230. return 0;
  231. }
  232. /*
  233. * map sg_io_v4 to a request.
  234. */
  235. static struct request *
  236. bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
  237. {
  238. request_queue_t *q = bd->queue;
  239. struct request *rq;
  240. int ret, rw = 0; /* shut up gcc */
  241. unsigned int dxfer_len;
  242. void *dxferp = NULL;
  243. dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr->dout_xferp,
  244. hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp,
  245. hdr->din_xfer_len);
  246. ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
  247. if (ret)
  248. return ERR_PTR(ret);
  249. /*
  250. * map scatter-gather elements seperately and string them to request
  251. */
  252. rq = blk_get_request(q, rw, GFP_KERNEL);
  253. ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, test_bit(BSG_F_WRITE_PERM,
  254. &bd->flags));
  255. if (ret) {
  256. blk_put_request(rq);
  257. return ERR_PTR(ret);
  258. }
  259. if (hdr->dout_xfer_len) {
  260. dxfer_len = hdr->dout_xfer_len;
  261. dxferp = (void*)(unsigned long)hdr->dout_xferp;
  262. } else if (hdr->din_xfer_len) {
  263. dxfer_len = hdr->din_xfer_len;
  264. dxferp = (void*)(unsigned long)hdr->din_xferp;
  265. } else
  266. dxfer_len = 0;
  267. if (dxfer_len) {
  268. ret = blk_rq_map_user(q, rq, dxferp, dxfer_len);
  269. if (ret) {
  270. dprintk("failed map at %d\n", ret);
  271. blk_put_request(rq);
  272. rq = ERR_PTR(ret);
  273. }
  274. }
  275. return rq;
  276. }
  277. /*
  278. * async completion call-back from the block layer, when scsi/ide/whatever
  279. * calls end_that_request_last() on a request
  280. */
  281. static void bsg_rq_end_io(struct request *rq, int uptodate)
  282. {
  283. struct bsg_command *bc = rq->end_io_data;
  284. struct bsg_device *bd = bc->bd;
  285. unsigned long flags;
  286. dprintk("%s: finished rq %p bc %p, bio %p stat %d\n",
  287. bd->name, rq, bc, bc->bio, uptodate);
  288. bc->hdr.duration = jiffies_to_msecs(jiffies - bc->hdr.duration);
  289. spin_lock_irqsave(&bd->lock, flags);
  290. list_del(&bc->list);
  291. bsg_add_done_cmd(bd, bc);
  292. spin_unlock_irqrestore(&bd->lock, flags);
  293. }
  294. /*
  295. * do final setup of a 'bc' and submit the matching 'rq' to the block
  296. * layer for io
  297. */
  298. static void bsg_add_command(struct bsg_device *bd, request_queue_t *q,
  299. struct bsg_command *bc, struct request *rq)
  300. {
  301. rq->sense = bc->sense;
  302. rq->sense_len = 0;
  303. /*
  304. * add bc command to busy queue and submit rq for io
  305. */
  306. bc->rq = rq;
  307. bc->bio = rq->bio;
  308. bc->hdr.duration = jiffies;
  309. spin_lock_irq(&bd->lock);
  310. list_add_tail(&bc->list, &bd->busy_list);
  311. spin_unlock_irq(&bd->lock);
  312. dprintk("%s: queueing rq %p, bc %p\n", bd->name, rq, bc);
  313. rq->end_io_data = bc;
  314. blk_execute_rq_nowait(q, NULL, rq, 1, bsg_rq_end_io);
  315. }
  316. static inline struct bsg_command *bsg_next_done_cmd(struct bsg_device *bd)
  317. {
  318. struct bsg_command *bc = NULL;
  319. spin_lock_irq(&bd->lock);
  320. if (bd->done_cmds) {
  321. bc = list_entry_bc(bd->done_list.next);
  322. bsg_del_done_cmd(bd, bc);
  323. }
  324. spin_unlock_irq(&bd->lock);
  325. return bc;
  326. }
  327. /*
  328. * Get a finished command from the done list
  329. */
  330. static struct bsg_command *__bsg_get_done_cmd(struct bsg_device *bd, int state)
  331. {
  332. struct bsg_command *bc;
  333. int ret;
  334. do {
  335. bc = bsg_next_done_cmd(bd);
  336. if (bc)
  337. break;
  338. ret = bsg_io_schedule(bd, state);
  339. if (ret) {
  340. bc = ERR_PTR(ret);
  341. break;
  342. }
  343. } while (1);
  344. dprintk("%s: returning done %p\n", bd->name, bc);
  345. return bc;
  346. }
  347. static struct bsg_command *
  348. bsg_get_done_cmd(struct bsg_device *bd, const struct iovec *iov)
  349. {
  350. return __bsg_get_done_cmd(bd, TASK_INTERRUPTIBLE);
  351. }
  352. static struct bsg_command *
  353. bsg_get_done_cmd_nosignals(struct bsg_device *bd)
  354. {
  355. return __bsg_get_done_cmd(bd, TASK_UNINTERRUPTIBLE);
  356. }
  357. static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
  358. struct bio *bio)
  359. {
  360. int ret = 0;
  361. dprintk("rq %p bio %p %u\n", rq, bio, rq->errors);
  362. /*
  363. * fill in all the output members
  364. */
  365. hdr->device_status = status_byte(rq->errors);
  366. hdr->transport_status = host_byte(rq->errors);
  367. hdr->driver_status = driver_byte(rq->errors);
  368. hdr->info = 0;
  369. if (hdr->device_status || hdr->transport_status || hdr->driver_status)
  370. hdr->info |= SG_INFO_CHECK;
  371. hdr->din_resid = rq->data_len;
  372. hdr->response_len = 0;
  373. if (rq->sense_len && hdr->response) {
  374. int len = min((unsigned int) hdr->max_response_len,
  375. rq->sense_len);
  376. ret = copy_to_user((void*)(unsigned long)hdr->response,
  377. rq->sense, len);
  378. if (!ret)
  379. hdr->response_len = len;
  380. else
  381. ret = -EFAULT;
  382. }
  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. set_bit(BSG_F_BLOCK, &bd->flags);
  393. /*
  394. * wait for all commands to complete
  395. */
  396. ret = 0;
  397. do {
  398. ret = bsg_io_schedule(bd, TASK_UNINTERRUPTIBLE);
  399. /*
  400. * look for -ENODATA specifically -- we'll sometimes get
  401. * -ERESTARTSYS when we've taken a signal, but we can't
  402. * return until we're done freeing the queue, so ignore
  403. * it. The signal will get handled when we're done freeing
  404. * the bsg_device.
  405. */
  406. } while (ret != -ENODATA);
  407. /*
  408. * discard done commands
  409. */
  410. ret = 0;
  411. do {
  412. bc = bsg_get_done_cmd_nosignals(bd);
  413. /*
  414. * we _must_ complete before restarting, because
  415. * bsg_release can't handle this failing.
  416. */
  417. if (PTR_ERR(bc) == -ERESTARTSYS)
  418. continue;
  419. if (IS_ERR(bc)) {
  420. ret = PTR_ERR(bc);
  421. break;
  422. }
  423. tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
  424. if (!ret)
  425. ret = tret;
  426. bsg_free_command(bc);
  427. } while (1);
  428. return ret;
  429. }
  430. typedef struct bsg_command *(*bsg_command_callback)(struct bsg_device *bd, const struct iovec *iov);
  431. static ssize_t
  432. __bsg_read(char __user *buf, size_t count, bsg_command_callback get_bc,
  433. struct bsg_device *bd, const struct iovec *iov, ssize_t *bytes_read)
  434. {
  435. struct bsg_command *bc;
  436. int nr_commands, ret;
  437. if (count % sizeof(struct sg_io_v4))
  438. return -EINVAL;
  439. ret = 0;
  440. nr_commands = count / sizeof(struct sg_io_v4);
  441. while (nr_commands) {
  442. bc = get_bc(bd, iov);
  443. if (IS_ERR(bc)) {
  444. ret = PTR_ERR(bc);
  445. break;
  446. }
  447. /*
  448. * this is the only case where we need to copy data back
  449. * after completing the request. so do that here,
  450. * bsg_complete_work() cannot do that for us
  451. */
  452. ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio);
  453. if (copy_to_user(buf, (char *) &bc->hdr, sizeof(bc->hdr)))
  454. ret = -EFAULT;
  455. bsg_free_command(bc);
  456. if (ret)
  457. break;
  458. buf += sizeof(struct sg_io_v4);
  459. *bytes_read += sizeof(struct sg_io_v4);
  460. nr_commands--;
  461. }
  462. return ret;
  463. }
  464. static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
  465. {
  466. if (file->f_flags & O_NONBLOCK)
  467. clear_bit(BSG_F_BLOCK, &bd->flags);
  468. else
  469. set_bit(BSG_F_BLOCK, &bd->flags);
  470. }
  471. static inline void bsg_set_write_perm(struct bsg_device *bd, struct file *file)
  472. {
  473. if (file->f_mode & FMODE_WRITE)
  474. set_bit(BSG_F_WRITE_PERM, &bd->flags);
  475. else
  476. clear_bit(BSG_F_WRITE_PERM, &bd->flags);
  477. }
  478. static inline int err_block_err(int ret)
  479. {
  480. if (ret && ret != -ENOSPC && ret != -ENODATA && ret != -EAGAIN)
  481. return 1;
  482. return 0;
  483. }
  484. static ssize_t
  485. bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  486. {
  487. struct bsg_device *bd = file->private_data;
  488. int ret;
  489. ssize_t bytes_read;
  490. dprintk("%s: read %Zd bytes\n", bd->name, count);
  491. bsg_set_block(bd, file);
  492. bytes_read = 0;
  493. ret = __bsg_read(buf, count, bsg_get_done_cmd,
  494. 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 ssize_t __bsg_write(struct bsg_device *bd, const char __user *buf,
  501. size_t count, ssize_t *bytes_read)
  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. request_queue_t *q = bd->queue;
  514. bc = bsg_get_command(bd);
  515. if (!bc)
  516. break;
  517. if (IS_ERR(bc)) {
  518. ret = PTR_ERR(bc);
  519. bc = NULL;
  520. break;
  521. }
  522. bc->uhdr = (struct sg_io_v4 __user *) buf;
  523. if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
  524. ret = -EFAULT;
  525. break;
  526. }
  527. /*
  528. * get a request, fill in the blanks, and add to request queue
  529. */
  530. rq = bsg_map_hdr(bd, &bc->hdr);
  531. if (IS_ERR(rq)) {
  532. ret = PTR_ERR(rq);
  533. rq = NULL;
  534. break;
  535. }
  536. bsg_add_command(bd, q, bc, rq);
  537. bc = NULL;
  538. rq = NULL;
  539. nr_commands--;
  540. buf += sizeof(struct sg_io_v4);
  541. *bytes_read += sizeof(struct sg_io_v4);
  542. }
  543. if (bc)
  544. bsg_free_command(bc);
  545. return ret;
  546. }
  547. static ssize_t
  548. bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  549. {
  550. struct bsg_device *bd = file->private_data;
  551. ssize_t bytes_read;
  552. int ret;
  553. dprintk("%s: write %Zd bytes\n", bd->name, count);
  554. bsg_set_block(bd, file);
  555. bsg_set_write_perm(bd, file);
  556. bytes_read = 0;
  557. ret = __bsg_write(bd, buf, count, &bytes_read);
  558. *ppos = bytes_read;
  559. /*
  560. * return bytes written on non-fatal errors
  561. */
  562. if (!bytes_read || (bytes_read && err_block_err(ret)))
  563. bytes_read = ret;
  564. dprintk("%s: returning %Zd\n", bd->name, bytes_read);
  565. return bytes_read;
  566. }
  567. static struct bsg_device *bsg_alloc_device(void)
  568. {
  569. struct bsg_device *bd;
  570. bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
  571. if (unlikely(!bd))
  572. return NULL;
  573. spin_lock_init(&bd->lock);
  574. bd->max_queue = BSG_DEFAULT_CMDS;
  575. INIT_LIST_HEAD(&bd->busy_list);
  576. INIT_LIST_HEAD(&bd->done_list);
  577. INIT_HLIST_NODE(&bd->dev_list);
  578. init_waitqueue_head(&bd->wq_free);
  579. init_waitqueue_head(&bd->wq_done);
  580. return bd;
  581. }
  582. static int bsg_put_device(struct bsg_device *bd)
  583. {
  584. int ret = 0;
  585. mutex_lock(&bsg_mutex);
  586. if (!atomic_dec_and_test(&bd->ref_count))
  587. goto out;
  588. dprintk("%s: tearing down\n", bd->name);
  589. /*
  590. * close can always block
  591. */
  592. set_bit(BSG_F_BLOCK, &bd->flags);
  593. /*
  594. * correct error detection baddies here again. it's the responsibility
  595. * of the app to properly reap commands before close() if it wants
  596. * fool-proof error detection
  597. */
  598. ret = bsg_complete_all_commands(bd);
  599. blk_put_queue(bd->queue);
  600. hlist_del(&bd->dev_list);
  601. kfree(bd);
  602. out:
  603. mutex_unlock(&bsg_mutex);
  604. return ret;
  605. }
  606. static struct bsg_device *bsg_add_device(struct inode *inode,
  607. struct request_queue *rq,
  608. struct file *file)
  609. {
  610. struct bsg_device *bd = NULL;
  611. #ifdef BSG_DEBUG
  612. unsigned char buf[32];
  613. #endif
  614. bd = bsg_alloc_device();
  615. if (!bd)
  616. return ERR_PTR(-ENOMEM);
  617. bd->queue = rq;
  618. kobject_get(&rq->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, rq->bsg_dev.class_dev->class_id, 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->queue, 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, NULL, 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, NULL, 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_queue(struct request_queue *q)
  774. {
  775. struct bsg_class_device *bcd = &q->bsg_dev;
  776. if (!bcd->class_dev)
  777. return;
  778. mutex_lock(&bsg_mutex);
  779. sysfs_remove_link(&q->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_queue(struct request_queue *q, char *name)
  786. {
  787. struct bsg_class_device *bcd;
  788. dev_t dev;
  789. int ret;
  790. struct class_device *class_dev = NULL;
  791. /*
  792. * we need a proper transport to send commands, not a stacked device
  793. */
  794. if (!q->request_fn)
  795. return 0;
  796. bcd = &q->bsg_dev;
  797. memset(bcd, 0, sizeof(*bcd));
  798. INIT_LIST_HEAD(&bcd->list);
  799. mutex_lock(&bsg_mutex);
  800. dev = MKDEV(BSG_MAJOR, bsg_device_nr);
  801. bcd->minor = bsg_device_nr;
  802. bsg_device_nr++;
  803. bcd->queue = q;
  804. class_dev = class_device_create(bsg_class, NULL, dev, bcd->dev, "%s", name);
  805. if (IS_ERR(class_dev)) {
  806. ret = PTR_ERR(class_dev);
  807. goto err;
  808. }
  809. bcd->class_dev = class_dev;
  810. if (q->kobj.dentry) {
  811. ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
  812. if (ret)
  813. goto err;
  814. }
  815. list_add_tail(&bcd->list, &bsg_class_list);
  816. mutex_unlock(&bsg_mutex);
  817. return 0;
  818. err:
  819. bsg_device_nr--;
  820. if (class_dev)
  821. class_device_destroy(bsg_class, MKDEV(BSG_MAJOR, bcd->minor));
  822. mutex_unlock(&bsg_mutex);
  823. return ret;
  824. }
  825. static int bsg_add(struct class_device *cl_dev, struct class_interface *cl_intf)
  826. {
  827. int ret;
  828. struct scsi_device *sdp = to_scsi_device(cl_dev->dev);
  829. struct request_queue *rq = sdp->request_queue;
  830. if (rq->kobj.parent)
  831. ret = bsg_register_queue(rq, kobject_name(rq->kobj.parent));
  832. else
  833. ret = bsg_register_queue(rq, kobject_name(&sdp->sdev_gendev.kobj));
  834. return ret;
  835. }
  836. static void bsg_remove(struct class_device *cl_dev, struct class_interface *cl_intf)
  837. {
  838. bsg_unregister_queue(to_scsi_device(cl_dev->dev)->request_queue);
  839. }
  840. static struct class_interface bsg_intf = {
  841. .add = bsg_add,
  842. .remove = bsg_remove,
  843. };
  844. static int __init bsg_init(void)
  845. {
  846. int ret, i;
  847. bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
  848. sizeof(struct bsg_command), 0, 0, NULL, NULL);
  849. if (!bsg_cmd_cachep) {
  850. printk(KERN_ERR "bsg: failed creating slab cache\n");
  851. return -ENOMEM;
  852. }
  853. for (i = 0; i < BSG_LIST_SIZE; i++)
  854. INIT_HLIST_HEAD(&bsg_device_list[i]);
  855. bsg_class = class_create(THIS_MODULE, "bsg");
  856. if (IS_ERR(bsg_class)) {
  857. kmem_cache_destroy(bsg_cmd_cachep);
  858. return PTR_ERR(bsg_class);
  859. }
  860. ret = register_chrdev(BSG_MAJOR, "bsg", &bsg_fops);
  861. if (ret) {
  862. kmem_cache_destroy(bsg_cmd_cachep);
  863. class_destroy(bsg_class);
  864. return ret;
  865. }
  866. ret = scsi_register_interface(&bsg_intf);
  867. if (ret) {
  868. printk(KERN_ERR "bsg: failed register scsi interface %d\n", ret);
  869. kmem_cache_destroy(bsg_cmd_cachep);
  870. class_destroy(bsg_class);
  871. unregister_chrdev(BSG_MAJOR, "bsg");
  872. return ret;
  873. }
  874. printk(KERN_INFO "%s loaded\n", bsg_version);
  875. return 0;
  876. }
  877. MODULE_AUTHOR("Jens Axboe");
  878. MODULE_DESCRIPTION("Block layer SGSI generic (sg) driver");
  879. MODULE_LICENSE("GPL");
  880. device_initcall(bsg_init);