bsg.c 23 KB

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