bsg.c 23 KB

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