bsg.c 23 KB

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