bsg.c 23 KB

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