bsg.c 23 KB

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