bsg.c 24 KB

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