bsg.c 23 KB

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