bsg.c 23 KB

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