bsg.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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 minor;
  38. int queued_cmds;
  39. int done_cmds;
  40. wait_queue_head_t wq_done;
  41. wait_queue_head_t wq_free;
  42. char name[BUS_ID_SIZE];
  43. int max_queue;
  44. unsigned long flags;
  45. };
  46. enum {
  47. BSG_F_BLOCK = 1,
  48. BSG_F_WRITE_PERM = 2,
  49. };
  50. #define BSG_DEFAULT_CMDS 64
  51. #define BSG_MAX_DEVS 32768
  52. #undef BSG_DEBUG
  53. #ifdef BSG_DEBUG
  54. #define dprintk(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ##args)
  55. #else
  56. #define dprintk(fmt, args...)
  57. #endif
  58. static DEFINE_MUTEX(bsg_mutex);
  59. static DEFINE_IDR(bsg_minor_idr);
  60. #define BSG_LIST_ARRAY_SIZE 8
  61. static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
  62. static struct class *bsg_class;
  63. static int bsg_major;
  64. static struct kmem_cache *bsg_cmd_cachep;
  65. /*
  66. * our internal command type
  67. */
  68. struct bsg_command {
  69. struct bsg_device *bd;
  70. struct list_head list;
  71. struct request *rq;
  72. struct bio *bio;
  73. struct bio *bidi_bio;
  74. int err;
  75. struct sg_io_v4 hdr;
  76. char sense[SCSI_SENSE_BUFFERSIZE];
  77. };
  78. static void bsg_free_command(struct bsg_command *bc)
  79. {
  80. struct bsg_device *bd = bc->bd;
  81. unsigned long flags;
  82. kmem_cache_free(bsg_cmd_cachep, bc);
  83. spin_lock_irqsave(&bd->lock, flags);
  84. bd->queued_cmds--;
  85. spin_unlock_irqrestore(&bd->lock, flags);
  86. wake_up(&bd->wq_free);
  87. }
  88. static struct bsg_command *bsg_alloc_command(struct bsg_device *bd)
  89. {
  90. struct bsg_command *bc = ERR_PTR(-EINVAL);
  91. spin_lock_irq(&bd->lock);
  92. if (bd->queued_cmds >= bd->max_queue)
  93. goto out;
  94. bd->queued_cmds++;
  95. spin_unlock_irq(&bd->lock);
  96. bc = kmem_cache_zalloc(bsg_cmd_cachep, GFP_KERNEL);
  97. if (unlikely(!bc)) {
  98. spin_lock_irq(&bd->lock);
  99. bd->queued_cmds--;
  100. bc = ERR_PTR(-ENOMEM);
  101. goto out;
  102. }
  103. bc->bd = bd;
  104. INIT_LIST_HEAD(&bc->list);
  105. dprintk("%s: returning free cmd %p\n", bd->name, bc);
  106. return bc;
  107. out:
  108. spin_unlock_irq(&bd->lock);
  109. return bc;
  110. }
  111. static inline struct hlist_head *bsg_dev_idx_hash(int index)
  112. {
  113. return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
  114. }
  115. static int bsg_io_schedule(struct bsg_device *bd)
  116. {
  117. DEFINE_WAIT(wait);
  118. int ret = 0;
  119. spin_lock_irq(&bd->lock);
  120. BUG_ON(bd->done_cmds > bd->queued_cmds);
  121. /*
  122. * -ENOSPC or -ENODATA? I'm going for -ENODATA, meaning "I have no
  123. * work to do", even though we return -ENOSPC after this same test
  124. * during bsg_write() -- there, it means our buffer can't have more
  125. * bsg_commands added to it, thus has no space left.
  126. */
  127. if (bd->done_cmds == bd->queued_cmds) {
  128. ret = -ENODATA;
  129. goto unlock;
  130. }
  131. if (!test_bit(BSG_F_BLOCK, &bd->flags)) {
  132. ret = -EAGAIN;
  133. goto unlock;
  134. }
  135. prepare_to_wait(&bd->wq_done, &wait, TASK_UNINTERRUPTIBLE);
  136. spin_unlock_irq(&bd->lock);
  137. io_schedule();
  138. finish_wait(&bd->wq_done, &wait);
  139. return ret;
  140. unlock:
  141. spin_unlock_irq(&bd->lock);
  142. return ret;
  143. }
  144. static int blk_fill_sgv4_hdr_rq(struct request_queue *q, struct request *rq,
  145. struct sg_io_v4 *hdr, int has_write_perm)
  146. {
  147. memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  148. if (copy_from_user(rq->cmd, (void *)(unsigned long)hdr->request,
  149. hdr->request_len))
  150. return -EFAULT;
  151. if (hdr->subprotocol == BSG_SUB_PROTOCOL_SCSI_CMD) {
  152. if (blk_verify_command(rq->cmd, has_write_perm))
  153. return -EPERM;
  154. } else if (!capable(CAP_SYS_RAWIO))
  155. return -EPERM;
  156. /*
  157. * fill in request structure
  158. */
  159. rq->cmd_len = hdr->request_len;
  160. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  161. rq->timeout = (hdr->timeout * HZ) / 1000;
  162. if (!rq->timeout)
  163. rq->timeout = q->sg_timeout;
  164. if (!rq->timeout)
  165. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  166. return 0;
  167. }
  168. /*
  169. * Check if sg_io_v4 from user is allowed and valid
  170. */
  171. static int
  172. bsg_validate_sgv4_hdr(struct request_queue *q, struct sg_io_v4 *hdr, int *rw)
  173. {
  174. int ret = 0;
  175. if (hdr->guard != 'Q')
  176. return -EINVAL;
  177. if (hdr->request_len > BLK_MAX_CDB)
  178. return -EINVAL;
  179. if (hdr->dout_xfer_len > (q->max_sectors << 9) ||
  180. hdr->din_xfer_len > (q->max_sectors << 9))
  181. return -EIO;
  182. switch (hdr->protocol) {
  183. case BSG_PROTOCOL_SCSI:
  184. switch (hdr->subprotocol) {
  185. case BSG_SUB_PROTOCOL_SCSI_CMD:
  186. case BSG_SUB_PROTOCOL_SCSI_TRANSPORT:
  187. break;
  188. default:
  189. ret = -EINVAL;
  190. }
  191. break;
  192. default:
  193. ret = -EINVAL;
  194. }
  195. *rw = hdr->dout_xfer_len ? WRITE : READ;
  196. return ret;
  197. }
  198. /*
  199. * map sg_io_v4 to a request.
  200. */
  201. static struct request *
  202. bsg_map_hdr(struct bsg_device *bd, struct sg_io_v4 *hdr)
  203. {
  204. struct request_queue *q = bd->queue;
  205. struct request *rq, *next_rq = NULL;
  206. int ret, rw;
  207. unsigned int dxfer_len;
  208. void *dxferp = NULL;
  209. dprintk("map hdr %llx/%u %llx/%u\n", (unsigned long long) hdr->dout_xferp,
  210. hdr->dout_xfer_len, (unsigned long long) hdr->din_xferp,
  211. hdr->din_xfer_len);
  212. ret = bsg_validate_sgv4_hdr(q, hdr, &rw);
  213. if (ret)
  214. return ERR_PTR(ret);
  215. /*
  216. * map scatter-gather elements seperately and string them to request
  217. */
  218. rq = blk_get_request(q, rw, GFP_KERNEL);
  219. if (!rq)
  220. return ERR_PTR(-ENOMEM);
  221. ret = blk_fill_sgv4_hdr_rq(q, rq, hdr, test_bit(BSG_F_WRITE_PERM,
  222. &bd->flags));
  223. if (ret)
  224. goto out;
  225. if (rw == WRITE && hdr->din_xfer_len) {
  226. if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
  227. ret = -EOPNOTSUPP;
  228. goto out;
  229. }
  230. next_rq = blk_get_request(q, READ, GFP_KERNEL);
  231. if (!next_rq) {
  232. ret = -ENOMEM;
  233. goto out;
  234. }
  235. rq->next_rq = next_rq;
  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_entry(bd->done_list.next, 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. set_bit(BSG_F_BLOCK, &bd->flags);
  393. /*
  394. * wait for all commands to complete
  395. */
  396. ret = 0;
  397. do {
  398. ret = bsg_io_schedule(bd);
  399. /*
  400. * look for -ENODATA specifically -- we'll sometimes get
  401. * -ERESTARTSYS when we've taken a signal, but we can't
  402. * return until we're done freeing the queue, so ignore
  403. * it. The signal will get handled when we're done freeing
  404. * the bsg_device.
  405. */
  406. } while (ret != -ENODATA);
  407. /*
  408. * discard done commands
  409. */
  410. ret = 0;
  411. do {
  412. spin_lock_irq(&bd->lock);
  413. if (!bd->queued_cmds) {
  414. spin_unlock_irq(&bd->lock);
  415. break;
  416. }
  417. spin_unlock_irq(&bd->lock);
  418. bc = bsg_get_done_cmd(bd);
  419. if (IS_ERR(bc))
  420. break;
  421. tret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio,
  422. bc->bidi_bio);
  423. if (!ret)
  424. ret = tret;
  425. bsg_free_command(bc);
  426. } while (1);
  427. return ret;
  428. }
  429. static int
  430. __bsg_read(char __user *buf, size_t count, struct bsg_device *bd,
  431. const struct iovec *iov, ssize_t *bytes_read)
  432. {
  433. struct bsg_command *bc;
  434. int nr_commands, ret;
  435. if (count % sizeof(struct sg_io_v4))
  436. return -EINVAL;
  437. ret = 0;
  438. nr_commands = count / sizeof(struct sg_io_v4);
  439. while (nr_commands) {
  440. bc = bsg_get_done_cmd(bd);
  441. if (IS_ERR(bc)) {
  442. ret = PTR_ERR(bc);
  443. break;
  444. }
  445. /*
  446. * this is the only case where we need to copy data back
  447. * after completing the request. so do that here,
  448. * bsg_complete_work() cannot do that for us
  449. */
  450. ret = blk_complete_sgv4_hdr_rq(bc->rq, &bc->hdr, bc->bio,
  451. bc->bidi_bio);
  452. if (copy_to_user(buf, &bc->hdr, sizeof(bc->hdr)))
  453. ret = -EFAULT;
  454. bsg_free_command(bc);
  455. if (ret)
  456. break;
  457. buf += sizeof(struct sg_io_v4);
  458. *bytes_read += sizeof(struct sg_io_v4);
  459. nr_commands--;
  460. }
  461. return ret;
  462. }
  463. static inline void bsg_set_block(struct bsg_device *bd, struct file *file)
  464. {
  465. if (file->f_flags & O_NONBLOCK)
  466. clear_bit(BSG_F_BLOCK, &bd->flags);
  467. else
  468. set_bit(BSG_F_BLOCK, &bd->flags);
  469. }
  470. static inline void bsg_set_write_perm(struct bsg_device *bd, struct file *file)
  471. {
  472. if (file->f_mode & FMODE_WRITE)
  473. set_bit(BSG_F_WRITE_PERM, &bd->flags);
  474. else
  475. clear_bit(BSG_F_WRITE_PERM, &bd->flags);
  476. }
  477. /*
  478. * Check if the error is a "real" error that we should return.
  479. */
  480. static inline int err_block_err(int ret)
  481. {
  482. if (ret && ret != -ENOSPC && ret != -ENODATA && ret != -EAGAIN)
  483. return 1;
  484. return 0;
  485. }
  486. static ssize_t
  487. bsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  488. {
  489. struct bsg_device *bd = file->private_data;
  490. int ret;
  491. ssize_t bytes_read;
  492. dprintk("%s: read %Zd bytes\n", bd->name, count);
  493. bsg_set_block(bd, file);
  494. bytes_read = 0;
  495. ret = __bsg_read(buf, count, bd, NULL, &bytes_read);
  496. *ppos = bytes_read;
  497. if (!bytes_read || (bytes_read && err_block_err(ret)))
  498. bytes_read = ret;
  499. return bytes_read;
  500. }
  501. static int __bsg_write(struct bsg_device *bd, const char __user *buf,
  502. size_t count, ssize_t *bytes_written)
  503. {
  504. struct bsg_command *bc;
  505. struct request *rq;
  506. int ret, nr_commands;
  507. if (count % sizeof(struct sg_io_v4))
  508. return -EINVAL;
  509. nr_commands = count / sizeof(struct sg_io_v4);
  510. rq = NULL;
  511. bc = NULL;
  512. ret = 0;
  513. while (nr_commands) {
  514. struct request_queue *q = bd->queue;
  515. bc = bsg_alloc_command(bd);
  516. if (IS_ERR(bc)) {
  517. ret = PTR_ERR(bc);
  518. bc = NULL;
  519. break;
  520. }
  521. if (copy_from_user(&bc->hdr, buf, sizeof(bc->hdr))) {
  522. ret = -EFAULT;
  523. break;
  524. }
  525. /*
  526. * get a request, fill in the blanks, and add to request queue
  527. */
  528. rq = bsg_map_hdr(bd, &bc->hdr);
  529. if (IS_ERR(rq)) {
  530. ret = PTR_ERR(rq);
  531. rq = NULL;
  532. break;
  533. }
  534. bsg_add_command(bd, q, bc, rq);
  535. bc = NULL;
  536. rq = NULL;
  537. nr_commands--;
  538. buf += sizeof(struct sg_io_v4);
  539. *bytes_written += sizeof(struct sg_io_v4);
  540. }
  541. if (bc)
  542. bsg_free_command(bc);
  543. return ret;
  544. }
  545. static ssize_t
  546. bsg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  547. {
  548. struct bsg_device *bd = file->private_data;
  549. ssize_t bytes_written;
  550. int ret;
  551. dprintk("%s: write %Zd bytes\n", bd->name, count);
  552. bsg_set_block(bd, file);
  553. bsg_set_write_perm(bd, file);
  554. bytes_written = 0;
  555. ret = __bsg_write(bd, buf, count, &bytes_written);
  556. *ppos = bytes_written;
  557. /*
  558. * return bytes written on non-fatal errors
  559. */
  560. if (!bytes_written || (bytes_written && err_block_err(ret)))
  561. bytes_written = ret;
  562. dprintk("%s: returning %Zd\n", bd->name, bytes_written);
  563. return bytes_written;
  564. }
  565. static struct bsg_device *bsg_alloc_device(void)
  566. {
  567. struct bsg_device *bd;
  568. bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
  569. if (unlikely(!bd))
  570. return NULL;
  571. spin_lock_init(&bd->lock);
  572. bd->max_queue = BSG_DEFAULT_CMDS;
  573. INIT_LIST_HEAD(&bd->busy_list);
  574. INIT_LIST_HEAD(&bd->done_list);
  575. INIT_HLIST_NODE(&bd->dev_list);
  576. init_waitqueue_head(&bd->wq_free);
  577. init_waitqueue_head(&bd->wq_done);
  578. return bd;
  579. }
  580. static int bsg_put_device(struct bsg_device *bd)
  581. {
  582. int ret = 0;
  583. mutex_lock(&bsg_mutex);
  584. if (!atomic_dec_and_test(&bd->ref_count))
  585. goto out;
  586. dprintk("%s: tearing down\n", bd->name);
  587. /*
  588. * close can always block
  589. */
  590. set_bit(BSG_F_BLOCK, &bd->flags);
  591. /*
  592. * correct error detection baddies here again. it's the responsibility
  593. * of the app to properly reap commands before close() if it wants
  594. * fool-proof error detection
  595. */
  596. ret = bsg_complete_all_commands(bd);
  597. blk_put_queue(bd->queue);
  598. hlist_del(&bd->dev_list);
  599. kfree(bd);
  600. out:
  601. mutex_unlock(&bsg_mutex);
  602. return ret;
  603. }
  604. static struct bsg_device *bsg_add_device(struct inode *inode,
  605. struct request_queue *rq,
  606. struct file *file)
  607. {
  608. struct bsg_device *bd;
  609. #ifdef BSG_DEBUG
  610. unsigned char buf[32];
  611. #endif
  612. bd = bsg_alloc_device();
  613. if (!bd)
  614. return ERR_PTR(-ENOMEM);
  615. bd->queue = rq;
  616. kobject_get(&rq->kobj);
  617. bsg_set_block(bd, file);
  618. atomic_set(&bd->ref_count, 1);
  619. bd->minor = iminor(inode);
  620. mutex_lock(&bsg_mutex);
  621. hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(bd->minor));
  622. strncpy(bd->name, rq->bsg_dev.class_dev->class_id, sizeof(bd->name) - 1);
  623. dprintk("bound to <%s>, max queue %d\n",
  624. format_dev_t(buf, inode->i_rdev), bd->max_queue);
  625. mutex_unlock(&bsg_mutex);
  626. return bd;
  627. }
  628. static struct bsg_device *__bsg_get_device(int minor)
  629. {
  630. struct bsg_device *bd = NULL;
  631. struct hlist_node *entry;
  632. mutex_lock(&bsg_mutex);
  633. hlist_for_each(entry, bsg_dev_idx_hash(minor)) {
  634. bd = hlist_entry(entry, struct bsg_device, dev_list);
  635. if (bd->minor == minor) {
  636. atomic_inc(&bd->ref_count);
  637. break;
  638. }
  639. bd = NULL;
  640. }
  641. mutex_unlock(&bsg_mutex);
  642. return bd;
  643. }
  644. static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
  645. {
  646. struct bsg_device *bd;
  647. struct bsg_class_device *bcd;
  648. bd = __bsg_get_device(iminor(inode));
  649. if (bd)
  650. return bd;
  651. /*
  652. * find the class device
  653. */
  654. mutex_lock(&bsg_mutex);
  655. bcd = idr_find(&bsg_minor_idr, iminor(inode));
  656. mutex_unlock(&bsg_mutex);
  657. if (!bcd)
  658. return ERR_PTR(-ENODEV);
  659. return bsg_add_device(inode, bcd->queue, file);
  660. }
  661. static int bsg_open(struct inode *inode, struct file *file)
  662. {
  663. struct bsg_device *bd = bsg_get_device(inode, file);
  664. if (IS_ERR(bd))
  665. return PTR_ERR(bd);
  666. file->private_data = bd;
  667. return 0;
  668. }
  669. static int bsg_release(struct inode *inode, struct file *file)
  670. {
  671. struct bsg_device *bd = file->private_data;
  672. file->private_data = NULL;
  673. return bsg_put_device(bd);
  674. }
  675. static unsigned int bsg_poll(struct file *file, poll_table *wait)
  676. {
  677. struct bsg_device *bd = file->private_data;
  678. unsigned int mask = 0;
  679. poll_wait(file, &bd->wq_done, wait);
  680. poll_wait(file, &bd->wq_free, wait);
  681. spin_lock_irq(&bd->lock);
  682. if (!list_empty(&bd->done_list))
  683. mask |= POLLIN | POLLRDNORM;
  684. if (bd->queued_cmds >= bd->max_queue)
  685. mask |= POLLOUT;
  686. spin_unlock_irq(&bd->lock);
  687. return mask;
  688. }
  689. static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  690. {
  691. struct bsg_device *bd = file->private_data;
  692. int __user *uarg = (int __user *) arg;
  693. int ret;
  694. switch (cmd) {
  695. /*
  696. * our own ioctls
  697. */
  698. case SG_GET_COMMAND_Q:
  699. return put_user(bd->max_queue, uarg);
  700. case SG_SET_COMMAND_Q: {
  701. int queue;
  702. if (get_user(queue, uarg))
  703. return -EFAULT;
  704. if (queue < 1)
  705. return -EINVAL;
  706. spin_lock_irq(&bd->lock);
  707. bd->max_queue = queue;
  708. spin_unlock_irq(&bd->lock);
  709. return 0;
  710. }
  711. /*
  712. * SCSI/sg ioctls
  713. */
  714. case SG_GET_VERSION_NUM:
  715. case SCSI_IOCTL_GET_IDLUN:
  716. case SCSI_IOCTL_GET_BUS_NUMBER:
  717. case SG_SET_TIMEOUT:
  718. case SG_GET_TIMEOUT:
  719. case SG_GET_RESERVED_SIZE:
  720. case SG_SET_RESERVED_SIZE:
  721. case SG_EMULATED_HOST:
  722. case SCSI_IOCTL_SEND_COMMAND: {
  723. void __user *uarg = (void __user *) arg;
  724. return scsi_cmd_ioctl(file, bd->queue, NULL, cmd, uarg);
  725. }
  726. case SG_IO: {
  727. struct request *rq;
  728. struct bio *bio, *bidi_bio = NULL;
  729. struct sg_io_v4 hdr;
  730. if (copy_from_user(&hdr, uarg, sizeof(hdr)))
  731. return -EFAULT;
  732. rq = bsg_map_hdr(bd, &hdr);
  733. if (IS_ERR(rq))
  734. return PTR_ERR(rq);
  735. bio = rq->bio;
  736. if (rq->next_rq)
  737. bidi_bio = rq->next_rq->bio;
  738. blk_execute_rq(bd->queue, NULL, rq, 0);
  739. ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio);
  740. if (copy_to_user(uarg, &hdr, sizeof(hdr)))
  741. return -EFAULT;
  742. return ret;
  743. }
  744. /*
  745. * block device ioctls
  746. */
  747. default:
  748. #if 0
  749. return ioctl_by_bdev(bd->bdev, cmd, arg);
  750. #else
  751. return -ENOTTY;
  752. #endif
  753. }
  754. }
  755. static const struct file_operations bsg_fops = {
  756. .read = bsg_read,
  757. .write = bsg_write,
  758. .poll = bsg_poll,
  759. .open = bsg_open,
  760. .release = bsg_release,
  761. .unlocked_ioctl = bsg_ioctl,
  762. .owner = THIS_MODULE,
  763. };
  764. void bsg_unregister_queue(struct request_queue *q)
  765. {
  766. struct bsg_class_device *bcd = &q->bsg_dev;
  767. if (!bcd->class_dev)
  768. return;
  769. mutex_lock(&bsg_mutex);
  770. idr_remove(&bsg_minor_idr, bcd->minor);
  771. sysfs_remove_link(&q->kobj, "bsg");
  772. class_device_unregister(bcd->class_dev);
  773. put_device(bcd->dev);
  774. bcd->class_dev = NULL;
  775. bcd->dev = NULL;
  776. mutex_unlock(&bsg_mutex);
  777. }
  778. EXPORT_SYMBOL_GPL(bsg_unregister_queue);
  779. int bsg_register_queue(struct request_queue *q, struct device *gdev,
  780. const char *name)
  781. {
  782. struct bsg_class_device *bcd;
  783. dev_t dev;
  784. int ret, minor;
  785. struct class_device *class_dev = NULL;
  786. const char *devname;
  787. if (name)
  788. devname = name;
  789. else
  790. devname = gdev->bus_id;
  791. /*
  792. * we need a proper transport to send commands, not a stacked device
  793. */
  794. if (!q->request_fn)
  795. return 0;
  796. bcd = &q->bsg_dev;
  797. memset(bcd, 0, sizeof(*bcd));
  798. mutex_lock(&bsg_mutex);
  799. ret = idr_pre_get(&bsg_minor_idr, GFP_KERNEL);
  800. if (!ret) {
  801. ret = -ENOMEM;
  802. goto unlock;
  803. }
  804. ret = idr_get_new(&bsg_minor_idr, bcd, &minor);
  805. if (ret < 0)
  806. goto unlock;
  807. if (minor >= BSG_MAX_DEVS) {
  808. printk(KERN_ERR "bsg: too many bsg devices\n");
  809. ret = -EINVAL;
  810. goto remove_idr;
  811. }
  812. bcd->minor = minor;
  813. bcd->queue = q;
  814. bcd->dev = get_device(gdev);
  815. dev = MKDEV(bsg_major, bcd->minor);
  816. class_dev = class_device_create(bsg_class, NULL, dev, gdev, "%s",
  817. devname);
  818. if (IS_ERR(class_dev)) {
  819. ret = PTR_ERR(class_dev);
  820. goto put_dev;
  821. }
  822. bcd->class_dev = class_dev;
  823. if (q->kobj.sd) {
  824. ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
  825. if (ret)
  826. goto unregister_class_dev;
  827. }
  828. mutex_unlock(&bsg_mutex);
  829. return 0;
  830. unregister_class_dev:
  831. class_device_unregister(class_dev);
  832. put_dev:
  833. put_device(gdev);
  834. remove_idr:
  835. idr_remove(&bsg_minor_idr, minor);
  836. unlock:
  837. mutex_unlock(&bsg_mutex);
  838. return ret;
  839. }
  840. EXPORT_SYMBOL_GPL(bsg_register_queue);
  841. static struct cdev bsg_cdev;
  842. static int __init bsg_init(void)
  843. {
  844. int ret, i;
  845. dev_t devid;
  846. bsg_cmd_cachep = kmem_cache_create("bsg_cmd",
  847. sizeof(struct bsg_command), 0, 0, NULL);
  848. if (!bsg_cmd_cachep) {
  849. printk(KERN_ERR "bsg: failed creating slab cache\n");
  850. return -ENOMEM;
  851. }
  852. for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
  853. INIT_HLIST_HEAD(&bsg_device_list[i]);
  854. bsg_class = class_create(THIS_MODULE, "bsg");
  855. if (IS_ERR(bsg_class)) {
  856. ret = PTR_ERR(bsg_class);
  857. goto destroy_kmemcache;
  858. }
  859. ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
  860. if (ret)
  861. goto destroy_bsg_class;
  862. bsg_major = MAJOR(devid);
  863. cdev_init(&bsg_cdev, &bsg_fops);
  864. ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
  865. if (ret)
  866. goto unregister_chrdev;
  867. printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
  868. " loaded (major %d)\n", bsg_major);
  869. return 0;
  870. unregister_chrdev:
  871. unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
  872. destroy_bsg_class:
  873. class_destroy(bsg_class);
  874. destroy_kmemcache:
  875. kmem_cache_destroy(bsg_cmd_cachep);
  876. return ret;
  877. }
  878. MODULE_AUTHOR("Jens Axboe");
  879. MODULE_DESCRIPTION(BSG_DESCRIPTION);
  880. MODULE_LICENSE("GPL");
  881. device_initcall(bsg_init);