bsg.c 23 KB

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