bsg.c 23 KB

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