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